var objMap;

var LS_Map_Results = {
	$Layer: null,
	Page: 1,
	Pages: 0,
	PageHeight: 0,
	Mode: "latestreviews"
};

$j(function(){
	if(GBrowserIsCompatible()){
		try{
			objMap = new GMap2(document.getElementById("ls_map"));
			objMap.addControl(new GLargeMapControl());
			objMap.addControl(new GOverviewMapControl());
			objMap.setCenter(GMAPS_POINT_TORONTO(), GMAPS_DEFAULT_ZOOM);
		}
		catch(e){}
	}
	
	$j("#searchbar_listing").change(function(){
		if(this.value != ""){
			window.location = "/" + this.value;
		}
	});
	
	$j("#searchbar_button").hover(function(){
		$button = $j(this).find("img").get(0);
		$button.src = $button.src.replace(".png", "-hover.png");
	}, function(){
		$button = $j(this).find("img").get(0);
		$button.src = $button.src.replace("-hover.png", ".png");
	}).click(function(){
		LS_Search();
		return false;
	});
	
	$j("#searchbar_form").submit(function(){
		LS_Search();
		return false;
	});
	
	$j("#ls_map_results ul#ls_results_tabs li.tab").click(LS_Map_ChangeTab);
	
	$ls_map_filter = $j("#listings_section .map .filter");
	
	LS_Map_Results.$Results = $j("#ls_map_results .results");
	LS_Map_Results.$Paging = $j("#ls_map_results .paging");
	LS_Map_Results.$PageNav = LS_Map_Results.$Paging.find(".pages");
	
	LS_Map_Results.PageHeight = LS_Map_Results.$Results.height();
	
	LS_Map_Results.$Paging.find(".previous a").click(function(){
		LS_Map_ChangePage(-1);
		return false;
	});
	
	LS_Map_Results.$Paging.find(".next a").click(function(){
		LS_Map_ChangePage(1);
		return false;
	});
	
	$j("#listings_section .map .filter form select").change(function(){
		LS_Map_ChangeTab($j("#tab_searchresults"));
	});
	
	setTimeout(function(){
		LS_Map_ChangeTab($j("#tab_latestreviews"));
	}, 1000);
});

function Columnize(aList, iColumnSize){
	var aColumns = [];
	if(iColumnSize > 0){
		for(i=0; i<aList.length; i+=iColumnSize){
			aColumns.push(aList.slice(i, (i + iColumnSize)));
		}
	}
	return aColumns;
}

function LS_Search(){
	if(!objMap) return;
	$searchbar_keywords = $j("#searchbar_keywords");	
	$search_results = $j("#search_results");
	$search_results_list = $search_results.find("ul");
	
	if($searchbar_keywords.get(0).value == ""){
		$search_results.fadeOut();
		return;
	}
	
	$search_results_list.empty();
	$search_results_list.append($j("<li class=\"noresults\">Loading...</li>"));
	
	$search_results.css({
		left: (($searchbar_keywords.offset().left + Math.floor($searchbar_keywords.outerWidth() / 2)) - Math.floor($search_results.outerWidth() / 2)),
		top: ($searchbar_keywords.offset().top + $searchbar_keywords.outerHeight() + 8)
	}).fadeIn();
	
	$j.post(location.pathname, $j("#searchbar_form").serialize(), function(sResponse){
		try{
			eval("aListings = " + sResponse);
			$search_results_list.empty();
			
			if(aListings.length != 0){
				for(i=0; i<aListings.length; i++){
					$search_results_list.append($j("<li><a href=\"" + aListings[i].URL + "\">" + aListings[i].Name + "</a></li>"));
				}
				
				$search_results.find("ul > li[class!=noresults]").unbind().hover(function(){
					$j(this).addClass("hover");
				}, function(){
					$j(this).removeClass("hover");
				}).click(function(){
					window.location = $j(this).find("a").get(0).href;
				});
				
				$j("html, body").animate({scrollTop: $j("#listings_section .searchbar").offset().top});
			}
			else{
				$search_results_list.append($j("<li class=\"noresults\">Sorry, no matching results were found.</li>"));
			}
		}
		catch(e){
			$search_results.fadeOut();
			alert("Sorry, a technical problem ocurred and your search couldn't be completed, please try again in a few moments.");
		}
	});
}

function LS_Search_Clear(){
	$j("#search_results").fadeOut();
	$searchbar_keywords = $j("#searchbar_keywords");
	$searchbar_keywords.get(0).value = "";
	$searchbar_keywords.get(0).focus();
}

function LS_Map_ChangeTab($tab){
	if(!$tab.get){
		$tab = $j(this);
	}
	$tab.parent().find("li.active").removeClass("active");
	$tab.addClass("active");
	
	var sMode = $tab.find("a").get(0).href.split("#")[1];
	if(sMode == "latestreviews"){
		LS_Map_LatestReviews();
	}
	else if(sMode == "searchresults"){
		LS_Map_Search();
	}
	
	return false;
}

function LS_Map_LatestReviews(){
	LS_Map_Results.$PageNav.fadeOut();
	LS_Map_Results.$Results.html("<span class=\"loading\">Loading...</span>");
	$j.post(location.pathname, {
		cmd: "map-search",
		mode: "latestreviews",
		type: $ls_map_filter.find("form").get(0).type.value
	}, function(r){
		try{
			LS_Map_LoadResults(eval(r));	
		}
		catch(e){	
		}
	});	
}
 
function LS_Map_Search(){
	LS_Map_Results.$PageNav.fadeOut();
	LS_Map_Results.$Results.html("<span class=\"loading\">Loading...</span>");
	$j.post(location.pathname, $ls_map_filter.find("form").serialize(), function(r){
		try{
			LS_Map_LoadResults(eval(r));
		}
		catch(e){
			alert("Sorry, a technical error ocurred and the search couldn't be made, please try again in a few moments.");
		}
	});
}

function LS_Map_LoadResults(aListings){
	var aPoints = [];
	var aResults = [];
	
	objMap.clearOverlays();
	
	if(aListings.length != 0){
		for(i=0; i<aListings.length; i++){
			aResults.push("<li><a href=\"" + aListings[i].URL + "\">" + aListings[i].Name + "</li>");
			objPoint = new GLatLng(aListings[i].Latitude, aListings[i].Longitude);
			objMarker = new GMarker(objPoint);
			objMarker.info = aListings[i];
			objMap.addOverlay(objMarker);
			GEvent.addListener(objMarker, "click", LS_Map_Marker_Click);
			aPoints.push(objPoint);
		}
		
		var sHTML = "";
		var aGroups = Columnize(aResults, 3);
		sHTML += "<div class=\"container\">";
		for(i=0; i<aGroups.length; i++){
			sHTML += "<ul>";
			for(j=0; j<aGroups[i].length; j++){
				sHTML += aGroups[i][j];		
			}
			sHTML += "</ul>";
		}
		sHTML += "</div>";
	}
	else{
		sHTML = "<span>Sorry, no results that matched your search criteria were found.";	
	}
	
	LS_Map_Results.$Results.html(sHTML);
	
	LS_Map_Results.Pages = Math.ceil(LS_Map_Results.$Results.find(".container").height() / LS_Map_Results.PageHeight);
	LS_Map_Results.$PageNav.empty();
	
	for(i=1; i<=LS_Map_Results.Pages; i++){
		LS_Map_Results.$PageNav.append("<a href=\"#" + i + "\"><img id=\"ls_mapresults_page_" + i + "\" src=\"/images/pages/arts/page-dot.gif\" alt=\"Page " + i + "\" /></a>");
	}
	
	LS_Map_Results.$PageNav.fadeIn();
	
	LS_Map_Results.$PageNav.find("a").unbind().click(function(){
		var iPage = parseInt(this.href.split("#")[1]);
		LS_Map_SetPage(iPage);
		return false;
	});
	
	LS_Map_SetPage(1);
	
	if(aPoints.length != 0){
		$ls_map_filter.find("span.loading").html("");
		GMaps_FocusMap(objMap, aPoints);
	}
	else{
		$ls_map_filter.find("span.loading").html("Sorry, no matching results were found.");
		objMap.setCenter(GMAPS_POINT_TORONTO(), GMAPS_DEFAULT_ZOOM);
	}
}

function LS_Map_ChangePage(iStep){
	iPage = LS_Map_Results.Page + iStep;
	if(iPage < 1) iPage = 1;
	if(iPage > LS_Map_Results.Pages) iPage = LS_Map_Results.Pages;
	if(iPage != LS_Map_Results.Page){
		LS_Map_SetPage(iPage);
	}
}

function LS_Map_SetPage(iPage){
	LS_Map_Results.Page = iPage;
	
	LS_Map_Results.$PageNav.find("a img").each(function(){
		var sImage = this.src;
		sImage = sImage.replace("dot-active", "dot");
		this.src = sImage;
	});
	
	$j("#ls_mapresults_page_" + LS_Map_Results.Page).each(function(){
		var sImage = this.src;
		sImage = sImage.replace("dot", "dot-active");
		this.src = sImage;
		return false;
	});
	
	var iOffsetY = -((iPage - 1) * LS_Map_Results.PageHeight);
	LS_Map_Results.$Results.find(".container").animate({
		top: iOffsetY
	});
}

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 + "</div>";
	this.openInfoWindowHtml(sHTML);
}
