function php_urlencode (str) {
	str = escape(str);
	return str.replace(/[*+\/@]|%20/g,
	function (s) {
		switch (s) {
		case "*": s = "%2A"; break;
		case "+": s = "%2B"; break;
		case "/": s = "%2F"; break;
		case "@": s = "%40"; break;
		case "%20": s = "+"; break;
	}
	return s;
	}
	);
}

jQuery.fn.slideLeftHide = function( speed, callback ) { this.animate( { width: "hide", paddingLeft: "hide", paddingRight: "hide", marginLeft: "hide", marginRight: "hide" }, speed, callback ); } 
jQuery.fn.slideLeftShow = function( speed, callback ) { this.animate( { width: "show", paddingLeft: "show", paddingRight: "show", marginLeft: "show", marginRight: "show" }, speed, callback ); }

$(function(){
	$('.hhCalDiv').each(function(){
		$(this).find('a.hhCalAjax').fancybox();
	});
	
	initCrossFaders();
	initFancyBoxLinks();
	initGoogleMaps();
});

var slideshowTimers = new Array();

function initCrossFaders()
{
	$('.hhCrossFade').each(function(){
		var length = parseInt($(this).attr('data-hhcflength'));
		var delay = parseInt($(this).attr('data-hhcfdelay'));
		
		// don't apply crossfader if these values are invalid
		if (isNaN(length) || length < 250 || isNaN(delay) || delay < 1000)
			return;
			
		$(this).fader({
			length: length,
			delay: delay
		});
	});
}

function hhSetupEmailWidget(id, subj, url)
{
	if (!$('#'+id).hasClass('_custom'))
	{
		var html = '' +
		'<form style="margin:0;padding:0" method="post"><input type="hidden" name="url"/>' +
		'<table>' +
		'<tbody><tr><td>Your Name</td><td>Email to send to:</td></tr>' +
		'<tr><td><input type="text" class="textbox" name="name"></td>' +
		'<td><input type="text" class="textbox" name="recipient"></td>' +
		'</tr>' +
		'<tr><td colspan="2"><textarea class="comments" name="comments"></textarea></td></tr>' +
		'<tr><td colspan="2"><div id="'+id+'-rc'+'"></div>' +
		'<tr><td colspan="2">' +
		'<input type="button" class="button" value="Email this page" onclick="hhSendLink(\''+id+'\');return false;"> ' +
		'<input type="button" onclick="hhHideEmailWidget(\''+id+'\');return false;" class="button" value="Close">' +
		'</td></tr>' +
		'<tr class="status" style="display:none;"><td colspan="2">Sending, please wait...</td></tr>' + 
		'</tbody></table>' +
		'</form>';
		$('#'+id).hide().html(html);
		$('#'+id+' form').append('<input type="hidden" name="subject" />');
		$('#'+id+' [name=subject]').val(subj);
		
		if (typeof(url) == 'string')
			$('#'+id+' [name=url]').val(url);
		
		Recaptcha.create('6LcSF7oSAAAAALA0WrZ3gMhkF5XYma4YTKO2TZt4', id+'-rc', {
			theme: 'red'	
		});
	}
}

function hhShowEmailWidget(id)
{
	$('#'+id).fadeIn();
	Recaptcha.reload();
}

function hhHideEmailWidget(id)
{
	$('#'+id).fadeOut();
}

function hhSendLink(id)
{
	var f = $('#'+id+' form');
	var url = f.find('input[name=url]');
	
	if (url.val() == '')
		url.val(window.location.href);
	
	var data = f.serialize();
	data += '&modFunction=hhSendLink';
	
	$('#'+id+' .status').show();
	
	$.ajax({
		url: '/includes/inc.ajax_call.php',
		data: data,
		dataType: 'json',
		success: function(resp){
			$('#'+id+' .status').hide();
			if (resp.ok)
			{
				hhHideEmailWidget(id);
			}
			else
			{
				alert('The reCAPTHA check failed, please try again');
				Recaptcha.reload();
				return false;
			}
		}
	});
}

function initGoogleMaps()
{
	$('.hhMemberMap').each(function(){
		var latlng = new google.maps.LatLng(49.25, -123.1);
		var options = {
			zoom: 4,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(this, options);
		$(this).data('mapObj', map);
	});
}

// utility function to keep track of markers per map instance
google.maps.Map.prototype.addMarker = function(marker) {
	if (!this.hasOwnProperty('hhMarkers')) this.hhMarkers = [];
	this.hhMarkers.push(marker);
};

// clear all stored markers
google.maps.Map.prototype.clearMarkers = function() {
	if (this.hasOwnProperty('hhMarkers'))
	{
		while (this.hhMarkers.length > 0)
		{
			var marker = this.hhMarkers.pop();
			marker.setMap(null);
		}
	}
};

function hhUpdateMapStates(dropdown)
{
	var val = $(dropdown).val();
	$('.hhMapState').hide();
	
	if (val == 'CA')
	{
		$('.hhMapNorthAmerica').show();
		$('select[name=searchProvince]').show();
		$('select[name=searchState]').hide();
	}
	else if (val == 'US')
	{
		$('.hhMapNorthAmerica').show();
		$('select[name=searchProvince]').hide();
		$('select[name=searchState]').show();
	}
	else// if (val == 'Other')
	{
		$('.hhMapOtherCountry').show();
		//$('.hhMapState:has([name=searchOtherCountry])').show();
	}
}

function hhMapMemberSearch(button, type)
{
	var parent = $(button).parents('.hhMapSearch'); 
	var data = { 
		modFunction: 'mapMemberSearch',
		contentId: parent.find('[name=mapContentId]').val()
	};
	
	var field = '';
	
	if (type == 'radius')
	{
		var city = parent.find('[name=searchFieldCity]').val();
		var units = parent.find('[name=searchRadiusUnits]').val();
		var radius = parent.find('[name=searchRadius]').val();
		
		if (city == '')
		{
			alert('Please specify the city to search within');
			return false;
		}
		
		if (isNaN(radius) || radius == 0)
		{
			alert('Invalid search radius. The search radius must be numeric and greater than 0');
			return false;
		}
		
		data.type = 'radius';
		data.radius = radius;
		field = parent.find('[name=searchField]').val() + ' ' + city;
	
		// 1 mile = 1.609344 kilometers
		if (units == 'miles') data.radius *= 1.609344;
	}
	else if (type == 'city')
	{
		data.type = 'city';
		data.city = parent.find('[name=searchCity]').val();
		if (data.city == '')
		{
			alert('Please specify the city to search within');
			return false;
		}
		field = data.city;
	}
	else if (type == 'state')
	{
		var country = parent.find('[name=searchCountry]').val();
		data.type = 'state';
		if (country == 'CA')
		{
			data.country = 'CA';
			data.state = parent.find('[name=searchProvince]').val();
		}
		else if (country == 'US')
		{
			data.country = 'US';
			date.state = parent.find('[name=searchState]').val();
		}
		else if (country == 'Other')
		{
			data.country = parent.find('[name=searchOtherCountry]').val();
			data.state = parent.find('[name=searchOtherProvince]').val();
		}
		field = data.state + ' ' + data.country;
	}
	
	// Geocode given address
	var geocoder = new google.maps.Geocoder();
	geocoder.geocode({ address: field }, function(result,status){
		if (status != google.maps.GeocoderStatus.OK)
		{
			alert('Unable to complete search, please try again in a few moments.');
			return false;
		}
		
		// zoom in on geocoded address
		var pos = result[0].geometry.location;
		var map = $(parent).find('.hhMemberMap').data('mapObj');
		//map.setCenter(pos);
		
		// add marker to search address
		map.clearMarkers();
		/*var marker = new google.maps.Marker({
			map: map,
			position: pos,
			clickable: false
		});
		marker.setAnimation(google.maps.Animation.DROP);
		map.addMarker(marker);*/
		
		// get available members with geocoded addresses
		if (type == 'radius')
		{
			data.searchLat = pos.lat();
			data.searchLng = pos.lng();
		}
		
		$.ajax({
			type: 'GET',
			url: '/includes/inc.ajax_call.php',
			data: data,
			dataType: 'json',
			success: function(result){
				if (result.items.length > 0)
				{
					var bounds = new google.maps.LatLngBounds();
					parent.find('.hhMapResults').html(result.html);
					for(var i=0; i<result.items.length; i++)
					{
						var infoWindow = new google.maps.InfoWindow({
							content: $('#'+result.items[i].id).html()
						});
						
						var point = new google.maps.LatLng(result.items[i].geo_lat, result.items[i].geo_lng);
						var marker = new google.maps.Marker({
							map: map,
							position: point,
							clickable: true,
							infoWindow: infoWindow
						});
						marker.setAnimation(google.maps.Animation.DROP);
						
						google.maps.event.addListener(marker, 'click', function(){
							this.infoWindow.open(this.getMap(), this);
						});
						
						map.addMarker(marker);
						
						bounds.extend(point);
					}
					
					//map.fitBounds(bounds);
					map.setCenter(bounds.getCenter());
				}
			}
		});
	});
	
}
	

function hhGroupSlideShowInitControls() {
	$(".hhSlideshowControls").mouseover( function() {
		var src = $(this).attr("src");
		var newSrc = src.replace("inactive", "hover");
           $(this).attr("src", newSrc); 
	});
	$(".hhSlideshowControls").mouseout( function() {
		var src = $(this).attr("src");
		var newSrc = src.replace("hover", "inactive");
           $(this).attr("src", newSrc); 
	});
}

function hhGroupSlideShowOnLoad() {
	$(".hhSlideshowsAutorun").each(
		function() {
			var hhDataArr = $(this).val().split("x");
			hhGroupSlideShow(hhDataArr[0],hhDataArr[1],"play");
		}
	);
	hhGroupSlideShowInitControls();


}

function hhGroupSlideShow(contentId,assetId,cmd) {
	if ( cmd == "play" ) {
		$("#"+contentId+"x"+assetId+"_playbtn").hide();
		$("#"+contentId+"x"+assetId+"_stopbtn").show();

		if ( slideshowTimers[contentId] != "" ) {
			window.clearInterval(slideshowTimers[contentId]);
		}
		slideshowTimers[contentId] = setInterval( function() { hhGroupSlideShowSwitch(contentId,assetId,"next"); }, parseInt($("#"+contentId+"x"+assetId +"_scrollingTime").val()) ); 
	}else if ( cmd == "stop" ) {
		$("#"+contentId+"x"+assetId+"_stopbtn").hide();
		$("#"+contentId+"x"+assetId+"_playbtn").show();
		
		
		if ( slideshowTimers[contentId] != "" ) {
			window.clearInterval(slideshowTimers[contentId]);
			delete slideshowTimers[contentId];
		}
	}
}

function hhGroupSlideShowSwitch(contentId,assetId,action) {
	var curPage = $("#"+contentId+"x"+assetId +"_curPage").val();
	var numPages = $("#"+contentId+"x"+assetId +"_numPages").val();
	
	if ( action == "next" ) {
		var nextPage = parseInt(curPage)+1;
		if ( nextPage <= 1 || nextPage > numPages ) {
			nextPage = 1;
		}
	}else {
		var nextPage = parseInt(curPage)-1;
		if ( nextPage < 1 ) {
			nextPage = numPages;
		}
	}
	
	hhGroupPageFlipping(contentId,assetId,nextPage);
	initFancyBoxLinks();
}

function hhGroupPageFlipping(contentId,assetId,pageNum) {
	var divId = "hhGroup"+contentId+"x"+assetId;
	var randomnumber=Math.floor(Math.random()*10000)
	var urlstr = "&assetId="+ assetId +"&contentId="+contentId+"&pageNum="+pageNum+"&rn="+randomnumber;	
	

	if ( slideshowTimers[contentId] != undefined ) {
		urlstr += "&slideshowActive=1";
	}else {
		urlstr += "&slideshowActive=0";
	}
	
	var transition = $("#"+contentId+"x"+assetId +"_scrollTransition").val();
	
	
	$.ajax({
		type: "GET",
		url: "/includes/inc.ajax_call.php",
		data: "modFunction=hhGroupPageFlipping"+ urlstr,
		success: function(msg){
			if ( transition == "fade" ) {
				$("#"+divId).fadeTo(2000,0.01, function() {
					$("#"+divId).html(msg);
					hhGroupSlideShowInitControls();
					initFancyBoxLinks();
					$("#"+divId).fadeTo(2000,1.00, function() {
						$("#"+divId).show();
					});
				});
			}else if (transition == "slideVertical" ) {
				var eH = $("#"+divId).height();
				var startBot = $("#"+divId).css("bottom");
				var endBot = parseInt(eH)*2;

				$('#'+divId).animate({
			    	bottom: '-='+ endBot
				}, 2500, function() {
					$('#'+divId).animate({
				    	bottom: '+='+ (endBot+eH)
					}, 0, function() {
						$("#"+divId).hide();
						$("#"+divId).html(msg);
						hhGroupSlideShowInitControls();
						initFancyBoxLinks();
						$("#"+divId).show();
						$('#'+divId).animate({
				    		bottom: '-='+ (eH)
						}, 2500, function() {
						});

					});
				});
				
			}else if ( transition == "slideHorizontal" ) {
				var startLeft = $("#"+divId).css("left");
				var eW = $("#"+divId).width();
				
				$('#'+divId).animate({
			    	left: '-='+ eW
				}, 2500, function() {
					var curLeft = $("#"+divId).css("left");
					
					$("#"+divId).hide();
			    	// Animation complete.
					$("#"+divId).html(msg);
					hhGroupSlideShowInitControls();
					initFancyBoxLinks();
					if ( parseInt(curLeft)+parseInt(eW) != 0 ) {
						$('#'+divId).animate({
					    	left: '-200'
						},0, function() {
							$("#"+divId).css("left",eW);
							$("#"+divId).show();
							$('#'+divId).animate({
						    	left: '-='+ eW
							}, 2500, function() {
							});
						});
					}else {
						$("#"+divId).css("left",eW);
						$("#"+divId).show();
						$('#'+divId).animate({
					    	left: '-='+ eW
						}, 2500, function() {
						});
					}
					
					
				});
				
			}else {
				$("#"+divId).html(msg);
				hhGroupSlideShowInitControls();
				initFancyBoxLinks();
			}
			initFancyBoxLinks();
			// Activate Control Mouseovers again
			
		}
	});
}





function informationGalleryChangePanel(parentAsset,panelNum) {
	//$(".Gallery"+parentAsset+"_currentPanel").hide(); //("slide", { direction: "left" }, 1000);
	
	
	//gallery"+ parentAsset +"Panels
	var cnt = 0;
	var panelSelectedNum;
	var numPanels = 0;
	$(".gallery"+ parentAsset +"Panels").each(
		function() {
			if ( $(this).hasClass("gallery"+parentAsset+"_currentPanel") ) {
				panelSelectedNum = cnt;
			}
			cnt += 1;
			numPanels += 1;
		}
	);
	
	if ( panelSelectedNum < panelNum ) {
		// Slide From Right
		$(".gallery"+parentAsset+"_currentPanel").hide("slide", { direction: "down" }, 1000);
		$(".gallery"+parentAsset+"_currentPanel").removeClass("gallery"+parentAsset+"_currentPanel");
		//$("#Gallery"+parentAsset+"x"+panelSelectedNum).hide("slow");//, { direction: "right" }, 10);
		//$("#Gallery"+parentAsset+"x"+panelSelectedNum).hide("slide", { direction: "down" }, 1000);
		$("#Gallery"+parentAsset+"x"+panelNum).addClass("gallery"+parentAsset+"_currentPanel");
		$("#Gallery"+parentAsset+"x"+panelNum).show();		
	}else if ( panelSelectedNum > panelNum ) {
		$(".gallery"+parentAsset+"_currentPanel").hide("slide", { direction: "down" }, 1000);
		$(".gallery"+parentAsset+"_currentPanel").removeClass("gallery"+parentAsset+"_currentPanel");
		//$("#Gallery"+parentAsset+"x"+panelSelectedNum).hide("slow");//, { direction: "right" }, 10);
		
		//$("#Gallery"+parentAsset+"x"+panelSelectedNum).hide("slide", { direction: "down" }, 1000);

		
		$("#Gallery"+parentAsset+"x"+panelNum).addClass("gallery"+parentAsset+"_currentPanel");
		$("#Gallery"+parentAsset+"x"+panelNum).show();
		// 10 was selected, Jumping to 6
		// Slide From Left
	}else {
		// Do Nothing
	}

	//alert("was: "+ panelSelectedNum +", now "+ panelNum +" there are "+ numPanels +" panels");
	
	//$("#Gallery"+parentAsset+"x"+panelNum).addClass("Gallery"+parentAsset+"_currentPanel");
	//$(".Gallery"+parentAsset+"_currentPanel").show("slide", { direction: "right" }, 1000);
	//$("#Gallery"+parentAsset+"x"+panelNum).show();//"slide", { direction: "right" },1000);
}


function calendarDayView(assetId,dateOf,displayIn) {
	$(".hhCalendar"+ assetId +"_days").addClass('day').removeClass("dayCurrent");
	var selected = "hhCalendar"+ assetId +"_"+ dateOf.replace(/-/g,'');
	$("#"+selected).addClass('dayCurrent').removeClass('day');

	//alert('asset: '+ assetId +', date: '+ dateOf +', key: '+ selected +' , display: '+ displayIn);
	effect = "none";
	divId = "calendar"+ assetId +"_dayView";
	if ( displayIn == "div" ) {
		if ( effect == "fade" ) {
			$.ajax({
				type: "GET",
				url: "/includes/inc.ajax_call.php",
				data: "modFunction=displayCalendarDay&assetId="+ assetId +"&dateOf="+ dateOf,
				success: function(msg){
					$("#"+divId).fadeTo("slow",0.01, function() {
					
						$("#"+divId).html(msg);
						$("#"+divId).fadeTo("slow",1.00, function() {
							$("#"+divId).show();
						});
					});
				}
			});
		}else {
			$.ajax({
				type: "GET",
				url: "/includes/inc.ajax_call.php",
				data: "modFunction=displayCalendarDay&assetId="+ assetId +"&dateOf="+ dateOf,
				success: function(msg){
					$("#"+divId).html(msg);
					$("#"+divId).show();
					
				}
			});
		}
	}
}

function initFancyBoxLinks() {

	winW = $(window).width();
	winH = $(window).height();
	
	frameW = parseInt(winW*0.85);
	frameH = parseInt(winH*0.85);
		
	$("a.fancybox").fancybox( {
		'frameWidth': frameW,
		'frameHeight': frameH,
		'hideOnContentClick': false,
		'callbackOnShow': 
			function() {
				listingSlideShow();
				initFancyBoxLinks();
			}
		,
		'callbackOnClose':
			function() {
				if(typeof(intInterval) !="undefined") {
					intInterval=window.clearInterval(intInterval);
				}
			}
		}
	);
}

function slideshow_initialize() {
	setInterval ( "slideshow_loop()", 15000);
	initFancyBoxLinks();
}

function eventsChangeMonth(contentId,newDate) {
	var randomnumber=Math.floor(Math.random()*10000)
	urlstr = "&contentId="+ contentId +"&dateOf="+ newDate +"&refresh="+ randomnumber;

	divId = "hhEvents_"+contentId;
	obj = document.getElementById(divId);
	
	$.ajax({
		type: "GET",
		url: "/includes/inc.ajax_call.php?modFunction=displayEvents",
		data: urlstr,
		success: function(msg){
			//$('#'+ divId).html(msg);
			obj.innerHTML = msg;
			initFancyBoxLinks();
		}
	});
	
}

function listingSlideShow() {
	// Loop through thumbnails and build an array
	allThumbs = new Array();
	curIndex = $("#hhListSlideShowItem_Current").attr('value');
	
	$(".hhListSlideShowThumbs").each(
		function() {
			thumbIdArr = $(this).attr('id').split("hListSlideShowItem_");
			allThumbs.push(thumbIdArr[1]);
			//hhListSlideShowItem_". $each["assetId"] ."
		}
	);
	numEntries = allThumbs.length;
}

function listingSlideShowSet(indexNum,assetId) {
	$("#hhListSlideShowItem_Current").attr('value',indexNum);
	listingSlideShowDisplay(indexNum,assetId);
}

function listingSlideShowNext() {
	curIndex = $("#hhListSlideShowItem_Current").attr('value');
	if ( curIndex == (numEntries-1) ) { showIndex = 0;
	}else { showIndex = parseInt(curIndex)+1; }
	$("#hhListSlideShowItem_Current").attr("value",showIndex);
	listingSlideShowDisplay(showIndex,allThumbs[showIndex]);
}

function listingSlideShowPrev() {
	curIndex = $("#hhListSlideShowItem_Current").attr('value');
	if ( curIndex == 0 ) { showIndex = numEntries-1;
	}else { showIndex = parseInt(curIndex)-1; }
	$("#hhListSlideShowItem_Current").attr("value",showIndex);
	listingSlideShowDisplay(showIndex,allThumbs[showIndex]);
}

function listingSlideShowDisplay(indexNum,assetId) {
	//$(".hhListSlideShowThumbs").attr('style','padding-bottom:10px;border:1px solid #000000;');
	//$("#hhListSlideShowItem_"+assetId).attr('style','padding-bottom:10px;border:1px dashed red;');
	//$("#hhListSlideShowItem_"+assetId).focus();
	divId = $("#hhListSlideShowParent").attr("value");
	
	var randomnumber=Math.floor(Math.random()*10000)
	
	urlstr = "&modFunction=displaySlideShowItem&assetGroup="+ divId +"&assetId="+ assetId +"&rn="+ randomnumber;
	$.ajax({
		type: "GET",
		url: "/includes/inc.ajax_call.php",
		data: urlstr,
		success: function(msg){
			//$("#gallery"+ divId +"_large").html(msg);
			$("#SLIDESHOW_gallery"+ divId +"_large").fadeTo("slow",0.01, function() {
				$("#SLIDESHOW_gallery"+ divId +"_large").html(msg);
				$("#SLIDESHOW_gallery"+ divId +"_large").fadeTo("slow",1.00);
			});
		}
	});
}

function slideshow_loop() {
	$(".hhSlideShowItems").each(
	
		function() {
			//list_pageFlip(this.id,pageNum,perPage,orderBy,effect)
			var contentId = parseInt(this.value);
			var pageNum = parseInt(document.getElementById('hhSlideShow'+ contentId +'_pageNum').value);
			var orderBy = document.getElementById('hhSlideShow'+ contentId +'_orderBy').value;
			// Get the PageNum, OrderBy
			//alert('cId: '+ contentId +' , nextPage: '+ pageNum +', orderby: '+ orderBy);
//			list_pageFlip(this.id,pageNum,perPage,orderBy,effect)
			listing_pageFlip(contentId,pageNum,1,orderBy,'none');
		});
}

function photogallery_showMain(groupId,assetId,effect,displayIn) {
	displayIn = "div";
	effect = "";
	if ( displayIn == "div" ) {
		if ( effect == "fade" ) {
			$.ajax({
				type: "GET",
				url: "/includes/inc.ajax_call.php",
				data: "modFunction=displayPhotoGallery_large&assetGroup="+ groupId +"&assetId="+assetId,
				success: function(msg){
					$("#gallery"+ groupId +"_large").fadeTo("slow",0.01, function() {
						$("#gallery"+ groupId +"_large").html(msg);
						$("#gallery"+ groupId +"_large").fadeTo("slow",1.00);
					});
				}
			});
		}else {
			$.ajax({
				type: "GET",
				url: "/includes/inc.ajax_call.php",
				data: "modFunction=displayPhotoGallery_large&assetGroup="+ groupId +"&assetId="+assetId,
				success: function(msg){
					$("#gallery"+ groupId +"_large").html(msg);
					$("#gallery"+ groupId +"_large").show();
					
					initFancyBoxLinks();
				}
			});
		}
	}
}

function photogallery_pageFlip(assetId,pageNum,perPage,orderBy,effect) {
	//$("#gallery"+ contentId +"_thumbs");
	if ( effect == "fade" ) {
		$.ajax({
			type: "GET",
			url: "/includes/inc.ajax_call.php",
			data: "modFunction=photogallery_pageFlip&assetId="+ assetId +"&pageNum="+pageNum,
			success: function(msg){
				$("#gallery"+ assetId +"_thumbs").fadeTo("slow",0.01, function() {
					$("#gallery"+ assetId +"_thumbs").html(msg);
					$("#gallery"+ assetId +"_thumbs").fadeTo("slow",1.00);
				});
			}
		});
	}else {
		$.ajax({
			type: "GET",
			url: "/includes/inc.ajax_call.php",
			data: "modFunction=photogallery_pageFlip&assetId="+ assetId +"&pageNum="+pageNum,
			success: function(msg){
				$("#gallery"+ contentId +"_thumbs").html(msg);
				$("#gallery"+ contentId +"_thumbs").show();
			}
		});
	}
}

function list_pageFlip(assetId,pageNum,perPage,orderBy,effect) {
	//$("#gallery"+ contentId +"_thumbs");
	if ( effect == "fade" ) {
		$.ajax({
			type: "GET",
			url: "/includes/inc.ajax_call.php",
			data: "modFunction=displayList&assetId="+ assetId +"&pageNum="+pageNum,
			success: function(msg){
				$("#list"+ assetId).fadeTo("slow",0.01, function() {
					$("#list"+ assetId +"").html(msg);
					$("#list"+ assetId +"").fadeTo("slow",1.00);
				});
			}
		});
	}else {
		$.ajax({
			type: "GET",
			url: "/includes/inc.ajax_call.php",
			data: "modFunction=displayList&assetId="+ assetId +"&pageNum="+pageNum,
			success: function(msg){
				$("#list"+ assetId).html(msg);
				$("#list"+ assetId).show();
			}
		});
	}

}

function listing_pageFlip(assetId,pageNum,perPage,orderBy,effect) {

	var randomnumber=Math.floor(Math.random()*10000)
	effect = "fade";
	//$("#gallery"+ contentId +"_thumbs");
	if ( effect == "fade" ) {
		$.ajax({
			type: "GET",
			url: "/includes/inc.ajax_call.php",
			data: "modFunction=displayListing&assetId="+ assetId +"&pageNum="+pageNum +"&randNum="+ randomnumber,
			success: function(msg){
				$("#list"+ assetId).fadeTo("slow",0.01, function() {
					$("#list"+ assetId +"").html(msg);
					$("#list"+ assetId +"").fadeTo("slow",1.00);
					initFancyBoxLinks();
				});
			}
		});
	}else {
		$.ajax({
			type: "GET",
			url: "/includes/inc.ajax_call.php",
			data: "modFunction=displayListing&assetId="+ assetId +"&pageNum="+pageNum +"&randNum="+ randomnumber,
			success: function(msg){	
				$("#list"+ assetId).html(msg);
				$("#list"+ assetId).show();
				initFancyBoxLinks();
			}
		});
	}
}

function hhCaptchaReload(contentId) {
	var randomnumber=Math.floor(Math.random()*10000)
	$.ajax({
		type: "GET",
		url: "/includes/inc.ajax_call.php",
		data: "modFunction=reloadCaptcha&contentId="+ contentId +"&randNum="+ randomnumber,
		success: function(msg){
			$("#hhCaptcha_"+contentId).html(msg);
		}
	});
}

function hhReloadFeedback(contentId,pageNum) {
	var randomnumber=Math.floor(Math.random()*10000)
	divName = "hhFeedbackComments_"+ contentId;

	urlstr = "";
	
	$.ajax({
		type: "GET",
		url: "/includes/inc.ajax_call.php",
		data: "modFunction=feedbackReload&contentId="+ contentId +"&pageNum="+pageNum +"&randNum="+ randomnumber,
		success: function(msg){
			$("#"+divName).html(msg);
		}
	});
	
}

function hhLeaveFeedback(contentId) {
	var randomnumber=Math.floor(Math.random()*10000)
	className = "hhFeedbackForm_"+ contentId;
	
	$("#hhFeedbackBtn_"+contentId).disabled = true;
//	$('submit').value = "Processing.  Please Wait...";

	
	urlstr = "";
	
	$("."+ className).each(
		function() {
			if ( $(this).attr('name') == "postAs" ) {
				if ( $(this).attr('type') == "radio" ) {
					if ( $(this).attr('checked') == true ) {
						urlstr += "&postAs="+ $(this).attr('value');
						postAs = $(this).attr('value');
					}
				}else {
					urlstr += "&"+ $(this).attr('name') +"="+ $(this).attr('value');
					postAs = $(this).attr('value');
				}
				
			}else {
				urlstr += "&"+ $(this).attr('name') +"="+ php_urlencode($(this).attr('value'));
			}
		});
	

	if ( postAs == 'userAccount' ) {
		// Validate the User First
		$.ajax({
			type: "GET",
			url: "/includes/inc.ajax_call.php",
			data: "modFunction=feedbackValidateUser&randNum="+ randomnumber +"&"+ urlstr,
			success: function(msg){	
				validated = msg.indexOf('MEMBER_AUTH_SUCCESS');
				if ( validated != -1 ) {
					// Validated Successfully
					$.ajax({
						type: "GET",
						url: "/includes/inc.ajax_call.php",
						data: "modFunction=feedbackPost&randNum="+ randomnumber +"&"+ urlstr,
						success: function(msg){	
							//FEEDBACK_SUCCESSFUL"
							feedbackSuccess = msg.indexOf('FEEDBACK_SUCCESSFUL');
							if ( feedbackSuccess != -1 ) {
								alert("Thank you for sharing your comments. There may be a short delay before your comments are visible.");
								hhReloadFeedback(contentId,1);
								hhCaptchaReload(contentId);
								$("#hhFeedbackComment_"+contentId).attr('value','');
							}else {
								wrongCaptcha = msg.indexOf('WRONG-CAPTCHA');
								if ( wrongCaptcha != -1 ) {
									alert('You have entered an invalid captcha code. Please try again.');
									hhCaptchaReload(contentId);
								}else {
									alert('An error occured while adding your feedback. Please try again later.');
								}
							}
						}
					});
					
				}else {
					// Invalid User
					alert('You have specified an invalid username & password combination.');
					return false;
				}
			}
		});
		
	}else {
		// Do Anonymous
		
		$.ajax({
			type: "GET",
			url: "/includes/inc.ajax_call.php",
			data: "modFunction=feedbackPost&randNum="+ randomnumber +"&"+ urlstr,
			success: function(msg){	
				alert(msg);
				//FEEDBACK_SUCCESSFUL"
				feedbackSuccess = msg.indexOf('FEEDBACK_SUCCESSFUL');
				if ( feedbackSuccess != -1 ) {
					hhReloadFeedback(contentId,1);
					hhCaptchaReload(contentId);
					$("#hhFeedbackComment_"+contentId).attr('value','');					
				}else {
					alert('An error occured while adding your feedback. Please try again later.');
				}
			}
		});
	}

	// First We check if the user is logged in / do the login.
	
}

function hhClipContent(contentId)
{
	$.ajax({
		type: 'GET',
		url: '/includes/inc.ajax_call.php',
		data: 'modFunction=clipContent&id='+contentId,
		success: function(msg) {
			alert('Content saved!');
		}
	});
}

function hhSendClippings(f)
{
	var query = 'modFunction=sendClippings&'+$(f).serialize();
	
	$.ajax({
		type: 'GET',
		url: '/includes/inc.ajax_call.php',
		data: query,
		success: function(results){
			alert(results);
		}
	});
}


function hhUpdateCalendar(btn,assetId)
{
	var query = {
		modFunction: 'displayCalendarView',
		month: $('#hhCalMonth').val(),
		year: $('#hhCalYear').val(),
		calAssetId: assetId
	};
	
	$(btn).attr('disabled','disabled').val('Loading...');
	
	$.ajax({
		type: 'GET',
		url: '/includes/inc.ajax_call.php',
		data: query,
		success: function(results){
			$('#hhCalendar_'+assetId).html(results);
			hhFancyboxCalendar(assetId);
		}
	});
}

function hhFancyboxCalendar(id)
{
	$('#hhCalendar_'+id+' a.hhCalAjax').fancybox();
}

function hhEmailContentToFriend(linkNode,cid)
{
	var query = { modFunction: 'getEmailForm', contentId: cid };

	var offset = $(linkNode).offset();
	var width = $(linkNode).width();
	var height = $(linkNode).height();
	
	if ($('#hhEmailBox').length == 0)
		$('body').append('<div id="hhEmailBox" style="display:none;"/>');
	
	$('#hhEmailBox').css('left', (offset.left + width) - ($('#hhEmailBox').width()));
	$('#hhEmailBox').css('top', offset.top + height);
	$('#hhEmailBox').fadeIn('normal');
	$('#hhEmailBox').text('Loading form...');

	$.ajax({
		type: 'GET',
		url: 'includes/inc.ajax_call.php',
		data: query,
		success: function(result){
			$('#hhEmailBox').html(result);
		}
	});
}

function hhSendContent(form)
{
	var query = 'modFunction=emailContent&';
	query += $(form).serialize();
	
	$('#hhEmailBox').text('Sending, please wait...');
	$.ajax({
		type: 'GET',
		url: 'includes/inc.ajax_call.php',
		data: query,
		success: function(msg){
			$('#hhEmailBox').text(msg);
			$('#hhEmailBox').delay(1500).fadeOut('fast');
		}
	});
}

function hhSearchDirectory(element,offset)
{
	if (typeof(offset) == 'undefined') offset = 0;
	
	var table = $(element).parents('.hhDirectoryTable');
	var query = 'modFunction=searchDirectory&offset='+offset+'&';
	query += $(element).parents('form').serialize();
	
	if (offset == 0)
	{
		// remove previous results
		table.find('tr:gt(1)').remove();
	}
	
	$.ajax({
		type: 'GET',
		url: '/includes/inc.ajax_call.php',
		data: query,
		dataType: 'json',
		success: function(resp) {
			if (resp.totalResultCount == 0) return; // no results.
			
			// adding more rows if offset is > 0; get rid of more row link
			if (offset > 0)
				$(element).parents('tr').remove();
			
			// add rows based on JSON results
			for(var i=0; i<resp.results.length; i++)
			{
				var tr = $('<tr/>');
				var row = resp.results[i];
				for(var field in row)
				{
					var td = $('<td/>');
					td.text(row[field]);
					tr.append(td);
				}
				table.append(tr);
			}
			
			// display link to get more results
			if (resp.hasOwnProperty('offset'))
			{
				var colspan = resp.columnCount+1;
				table.append('<tr><td colspan="'+colspan+'"><a href="#" onclick="hhSearchDirectory(this,'+resp.offset+');return false;">more results</a></td></tr>');
			}
		}
	});
}

function hhAjaxRegForm() {
	var query = 'modFunction=feedbackAjaxSignup';
	query += '&email='+ php_urlencode($("#feedbackSignupEmail").val());
	query += '&displayName='+ php_urlencode($("#feedbackSignupDisplayName").val());
	query += '&firstName='+ php_urlencode($("#feedbackSignupFirstName").val());
	query += '&lastName='+ php_urlencode($("#feedbackSignupLastName").val());
	query += '&password='+ php_urlencode($("#feedbackSignupPassword").val());
	query += '&confirmPassword='+ php_urlencode($("#feedbackSignupConfirmPassword").val());
	
	$.ajax({
		type: 'GET',
		url: 'includes/inc.ajax_call.php',
		data: query,
		success: function(msg){
			var resultData = dejson(msg);
			if ( resultData.status == true ) {
				$("#ajaxFormDebug").html(resultData.message);
				$("#ajaxFormDebug").show();
				location.reload();
			}else {
				$("#ajaxFormDebug").html(resultData.message);
				$("#ajaxFormDebug").show();
			}
		}
	});
}

function hhAjaxLogin() {
	var query = 'modFunction=feedbackAjaxLogin';
	query += '&email='+ php_urlencode($("#feedbackLoginEmail").val());
	query += '&password='+ php_urlencode($("#feedbackLoginPassword").val());
	
	$.ajax({
		type: 'GET',
		url: 'includes/inc.ajax_call.php',
		data: query,
		success: function(msg){
			var resultData = dejson(msg);
			if ( resultData.status == true ) {
				$("#ajaxFormDebug").html(resultData.message);
				$("#ajaxFormDebug").show();
				location.reload();
			}else {
				$("#ajaxFormDebug").html(resultData.message);
				$("#ajaxFormDebug").show();
				$("#feedbackLoginPassword").focus();
			}
		}
	});
}

function hhAjaxResetPw() {
	var query = 'modFunction=feedbackAjaxPasswordRecovery';
	query += '&email='+ php_urlencode($("#feedbackRecoveryEmail").val());
	
	$.ajax({
		type: 'GET',
		url: 'includes/inc.ajax_call.php',
		data: query,
		success: function(msg){
			var resultData = dejson(msg);
			if ( resultData.status == true ) {
				$("#ajaxFormDebug").html(resultData.message);
				$("#ajaxFormDebug").show();

			}else {
				$("#ajaxFormDebug").html(resultData.message);
				$("#ajaxFormDebug").show();
				$("#feedbackLoginPassword").focus();
			}
		}
	});
}

function dejson(text) {
	var data = eval("(" + text + ")");
	return data;
}









