var LS_Featured_Pages = 0;
var LS_Featured_Page = 1;
var LS_Featured_Image;
var LS_Featured_Image_Width = 576;
var LS_Featured_Image_Spacing = 15;
var LS_Featured_Excerpt;
var LS_Featured_Excerpt_Width = 495;

var LS_Results_Container;
var LS_Results_Pages;
var LS_Results_Page_Width;
var LS_Results_Page = 1;
var LS_Results_SearchQuery = "";
var LS_Results_SortBy = "alphabetical";

var bHideDialog = false;
var sLatestReviews = "";
var sSearchResults = "";

var objMap, objCenterPoint;
var aMarkers = [];

var aListingIcons = [];
aListingIcons[1] = "listing-restaurant";
aListingIcons[2] = "listing-bar";
aListingIcons[3] = "listing-cafe";
aListingIcons[4] = "listing-design";
aListingIcons[5] = "listing-fashion";
aListingIcons[6] = "listing-grocery";
aListingIcons[7] = "listing-artgallery";
aListingIcons[8] = "listing-books";
aListingIcons[9] = "listing-bakedgoods";

$j(function(){
	LS_Featured_Pages = $j("#ls_featured div.image div.container div").length;
	LS_Featured_Image = $j("#ls_featured div.image div.container");
	LS_Featured_Image_Item_Width = LS_Featured_Image_Width + LS_Featured_Image_Spacing;
	LS_Featured_Image.width((LS_Featured_Image_Item_Width * LS_Featured_Pages));
	
	LS_Featured_Excerpt = $j("#ls_featured div.excerpt div.container");
	LS_Featured_Excerpt_Item_Width = LS_Featured_Excerpt_Width;
	LS_Featured_Excerpt.width((LS_Featured_Excerpt_Item_Width * LS_Featured_Pages));
	
	var objImage = new Image();
	objImage.src = "/images/listings-dialog.gif";
	
	$j("#sortby_whatsthis").hover(
		function(){
			if($j.browser.msie) $j("#Neighborhood")[0].style.visibility = "hidden";
			$j("#sortby_whatsthis_dialog").fadeIn("fast");
		},
		function(){
			bHideDialog = true;
			setTimeout(function(){
				if(bHideDialog){
					if($j.browser.msie) $j("#Neighborhood")[0].style.visibility = "visible";
					$j("#sortby_whatsthis_dialog").fadeOut("fast");
					bHideDialog = false;
				}
			}, 200);
		}
	);
	
	$j("#sortby_whatsthis_dialog").hover(
		function(){
			bHideDialog = false;
		},
		function(){
			$j(this).fadeOut("fast");
		}
	);
	
	$j("#ls_results div.content div.container").html("<p>Loading, please wait...</p>");
	$j.post(sAJAXServer, {
		cmd: "load-latest"
	}, function(sResponse){
		sLatestReviews = sResponse;
		LS_Results_Update(sResponse);
	});
	
	$j("#ls_tabs_results").css({opacity: 0.3});
	$j("#ls_filters .sortby div").css({opacity: 0.3});
	
	if(GBrowserIsCompatible()){
		objMap = new GMap2(document.getElementById("ls_map"));
		objMap.addControl(new GLargeMapControl());
		objMap.addControl(new GOverviewMapControl());
		objCenterPoint = new GLatLng(43.67016874852075, -79.38682079315186);
		objMap.setCenter(objCenterPoint, 13);
		//0objMap.addControl(new GMapKitMapControls3d());
	}	
});
	
function LS_Featured_Scroll(iStep){
	LS_Featured_Page += iStep;
	if(LS_Featured_Page < 1){
		LS_Featured_Page = 1;
		return;
	}
	else if(LS_Featured_Page > LS_Featured_Pages){
		LS_Featured_Page = 1;
	}
	LS_Featured_Image.animate({left: ((LS_Featured_Image_Item_Width) * (LS_Featured_Page - 1) * -1)});
	LS_Featured_Excerpt.animate({left: ((LS_Featured_Excerpt_Item_Width) * (LS_Featured_Page - 1) * -1)});
}

function LS_Results_Update(sResponse){
	try{
		var aPages = eval(sResponse);
	}
	catch(e){
		alert("Sorry, we encountered a technical problem when trying to load the search results, please try again in few moments.");
		return;
	}
	objMap.clearOverlays();
	var aPoints = [];
	aMarkers = [];
	var sHTML = "";
	if(aPages){
		for(i = 0; i<aPages.length; i++){
			sHTML += "<div class=\"page\">\n";
			for(j = 0; j<aPages[i].length; j++){
				sHTML += "<div class=\"column\">\n";
				for(k = 0; k<aPages[i][j].length; k++){
					var objListing = aPages[i][j][k];
					if(objListing.name != false){
					if(objListing.latitude == 0 || objListing.longitude == 0) continue;
						objPoint = new GLatLng(objListing.latitude, objListing.longitude);
						sHTML += "<div><a href=\"" + objListing.url + "\">" + objListing.name + "</a></div>\n";
						if(Math.floor(objListing.latitude) == Math.floor(objCenterPoint.lat()) && Math.floor(objListing.longitude) == Math.floor(objCenterPoint.lng())){
							objMarker = new GMarker(objPoint, {title:objListing.name});
							objMap.addOverlay(objMarker);
							objListing.page = i + 1;
							objMarker.info = objListing;
							aMarkers.push(objMarker);
							GEvent.addListener(objMarker, "click", LS_Map_Marker_Click);
							if(i == 0) aPoints.push(objPoint);
							else objMarker.hide();
						}
					}
					else{
						sHTML += "<div class=\"nohover\"></div>";
					}
				}
				sHTML += "\n</div>\n";
			}
			sHTML += "</div>\n";
		}
	}
	
	if(aPages.length == 0) sHTML = "<p>Sorry, no listings that matched your search criteria were found.";
	
	LS_FocusMap(aPoints);
	
	$j("#ls_results div.content div.container a").unbind();
	$j("#ls_results div.content div.container").fadeOut("fast", function(){
		$j("#ls_results div.content div.container").html(sHTML).fadeIn("fast");
		
		var objPages = $j("#ls_results div.content div.page");
		LS_Results_Pages = objPages.length;
		LS_Results_Container = $j("#ls_results div.content div.container");
		LS_Results_Page_Width =$j(objPages[0]).width();
		iWidth = (LS_Results_Page_Width * LS_Results_Pages);
		if(LS_Results_Pages == 0) iWidth = 500;
		LS_Results_Container.width(iWidth);
		LS_Results_Container.css({left: 0});
		LS_Results_Page = 1;
		
		$j("#ls_results .tabs span p").fadeOut("fast", function(){
			$j("#ls_results .tabs span p").empty();
			for(i=0; i<LS_Results_Pages; i++){
				$j("#ls_results .tabs span p").append('<img src="/images/listings-dot-inactive.gif" class="page" />');
			}
			$j("#ls_results .tabs span p").fadeIn("fast");
			$j("#ls_results .tabs img.page").each(
				function(i){
					if(i == 0) this.src = "/images/listings-dot-active.gif";
					$j(this).click(new Function("LS_Results_ChangePage(" + (i + 1) + ")"));
				}
			);
		});
		
		$j("#ls_results div.content div.container a").click(
			function(){
				window.open(this.href);
				return false;
			}
		);
	});
}

function LS_FocusMap(aPoints){
	if(aPoints.length == 0) return;
	var fSumX = 0, fSumY = 0;
	var iPoints = aPoints.length;
	for(i=0; i<iPoints; i++){
		fSumX += aPoints[i].lat();
		fSumY += aPoints[i].lng();
	}
	var fAverageX = fSumX / iPoints;
	var fAverageY = fSumY / iPoints;
	fSumX = 0;
	fSumY = 0;
	for(i=0; i<iPoints; i++){
		fSumX += Math.pow((aPoints[i].lat() - fAverageX), 2);
		fSumY += Math.pow((aPoints[i].lng() - fAverageY), 2);
	}
	var fDeviationX = Math.sqrt(fSumX / (iPoints - 1));
	var fDeviationY = Math.sqrt(fSumY / (iPoints - 1));
	var iDeviationRadius = Math.sqrt(Math.pow((fDeviationX * -1), 2) + Math.pow((fDeviationY * -1), 2)) * 10;
	if(iDeviationRadius > 1){
		iZoom = 11;
	}
	else if(iDeviationRadius > 0.5){
		iZoom = 12;
	}
	else if(iDeviationRadius > 0.3){
		iZoom = 13;
	}
	else if(iDeviationRadius > 0.2){
		iZoom = 14;
	}
	else{
		iZoom = 15;
	}
	if(iDeviationRadius > 0 && iDeviationRadius <= 5){
		if(objMap.getZoom() == iZoom){
			objMap.panTo(new GLatLng(fAverageX, fAverageY));
		}
		else{
			objMap.setCenter(new GLatLng(fAverageX, fAverageY), iZoom);
		}
	}
	else{
		objMap.setCenter(objCenterPoint, 12);
	}
}

function LS_Results_Search(){
	var Form = document.forms.frmSearchListings;
	$j("#ls_filters .sortby div").unbind();
	
	var bValidSearch = Form.Type ? (Form.Type.value != "" || Form.Neighborhood.value != "") : (Form.Neighborhood.value != "");
	
	if(!bValidSearch){
		$j("#ls_tabs_results").css({opacity: 0.3});
		$j("#ls_filters .sortby div").css({opacity: 0.3});
		if($j("#ls_tabs_results").hasClass("selected")) LS_Results_SelectTab("latest");
		return;
	}
	
	$j("#ls_filters .sortby div").click(LS_Results_ChangeSort);
	
	var sType = Form.Type ? Form.Type.value : "";
	var sQuery = "type=" + sType + "&neighborhood=" + Form.Neighborhood.value + "&sortby=" + LS_Results_SortBy;
	
	if(sQuery != LS_Results_SearchQuery){
		LS_Results_SearchQuery = sQuery;
		$j("#ls_results div.content div.container").html("<p>Loading, please wait...</p>");
		$j.post(sAJAXServer, {
			cmd: "search",
			type: sType,
			neighborhood: Form.Neighborhood.value,
			sortby: LS_Results_SortBy
		}, function(sResponse){
			sSearchResults = sResponse;
			$j("#ls_tabs_results").css({opacity: 1});
			$j("#ls_filters .sortby div").css({opacity: 1});
			LS_Results_SelectTab("results");
		});
	}
	else LS_Results_SelectTab("results");
}

function LS_Results_ChangeSort(){
	if($j("#ls_tabs_latest").hasClass("selected")) return;
	$j("#ls_filters .sortby div").removeClass("selected");
	$j(this).addClass("selected");
	LS_Results_SortBy = this.id.split("_")[1];
	LS_Results_Search();
}

function LS_Results_SelectTab(sTabID){
	$j("#ls_results .tabs div").removeClass("selected");
	$j("#ls_tabs_" + sTabID).addClass("selected");
	if(sTabID == "latest"){
		LS_Results_Update(sLatestReviews);
	}
	else if(sTabID == "results"){
		LS_Results_Update(sSearchResults);
	}
}

function LS_Results_ChangePage(iPage){
	if(iPage < 1 || iPage > LS_Results_Pages) return;
	LS_Results_Page = iPage;
	var aPoints = [];
	for(i=0; i<aMarkers.length; i++){
		var objMarker = aMarkers[i];
		if(objMarker.info.page == iPage){
			objMarker.show();
			aPoints.push(new GLatLng(objMarker.info.latitude, objMarker.info.longitude));
		}
		else{
			objMarker.hide();
		}
	}
	LS_FocusMap(aPoints);
	$j("#ls_results .tabs img.page").each(
		function(i){
			if(i == (iPage - 1)) this.src = "/images/listings-dot-active.gif";
			else this.src = "/images/listings-dot-inactive.gif";
		}
	);
	LS_Results_Container.animate({left: ((LS_Results_Page_Width) * (LS_Results_Page - 1) * -1)});
}

function LS_Results_PreviousPage(){
	LS_Results_ChangePage(LS_Results_Page - 1);
}

function LS_Results_NextPage(){
	LS_Results_ChangePage(LS_Results_Page + 1);
}

function LS_Map_Marker_Click(){
	var objListing = this.info;
	var sHTML = "<div class=\"ls_map_infowindow\"><p style=\"font-size: 15px; font-weight: bold; margin-bottom: 3px;\"><strong><a href=\"" + objListing.url + "\" target=\"_blank\">" + objListing.name + "</a></strong></p>" +
	objListing.address + "<br>" + objListing.phone + "<p><a href=\"" + objListing.url + "\" target=\"_blank\">View Review</a> | <a href=\"/listings/facebook-faves.php?entryid=" + objListing.entryID + "\" target=\"_blank\">Add to Faves</a></p></div>";
	this.openInfoWindowHtml(sHTML);
}