/***Search Directory
*******Search Results Page - Google Maps
**********************/

var map;
var markersArray = [];
var infowindow = null;
var bounds = null;

function intializeMap(){

    //set default options for google maps
    infowindow = new google.maps.InfoWindow();
    bounds = new google.maps.LatLngBounds();
    var latlng = new google.maps.LatLng(-37.804353, 144.98746);
	
    var myOptions = {
      zoom: 16,
      navigationControl: true,
      navigationControlOptions: {
        style: google.maps.NavigationControlStyle.SMALL
      },
      mapTypeControl: false,
      scaleControl: false,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
	
    //create map
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

}

//Add pin to array
function createPins(location,title,contentString,markerImage){

	var marker = new google.maps.Marker({
    	    position: location,
    	    map: map,
	    title: title,
	    icon: markerImage
  	});
	
	//add each location to area bounds for fitting to map later
	bounds.extend(location);
	
	markersArray.push(marker);
	
	//add info window for marker
	google.maps.event.addListener(marker, 'click', function() {
  		infowindow.setContent(contentString);
		infowindow.open(map,marker);
	});
	
}

function createResultsMap(){

	//create container for Map
	$("#refine-search").prepend('<div id="map_canvas"></div>');
	
	//create map
	intializeMap();

	var numListings = 0;
        var recordNo = 0;
	
	//create all results as pins
	$(".result").each(function(i){
		recordNo = i+1;

		//add a search pin for each result
		$(".resultHeading",this).after('<div class="map-pin"><a href="#map_canvas" class="map_marker pin'+recordNo+'">'+recordNo+'</a></div>');
		
		var rawLatLng = $(".geocode",this).text().split(", ");		
		var resultLatLng = new google.maps.LatLng(parseFloat(rawLatLng[0]), parseFloat(rawLatLng[1]));
		
		var contentString ="<div style='font-size:0.9em;'><h3>"+$("h3",this).html() + "</h3><p>" + $(".summary",this).html()+ "</p><p>"+ $(".category_icons",this).html() +"</p></div>";
	  
		var markerImage = new google.maps.MarkerImage(location.protocol+'//'+location.hostname+'/__data/assets/image/0003/349608/red_icons_1_10_transparent.png',new google.maps.Size(36, 28),new google.maps.Point(0,0 +(i*28)));

                //var markerImage = new google.maps.MarkerImage(location.protocol+'//'+location.hostname+'/__data/assets/image/0009/53379/acfe_marker.png',new google.maps.Size(36, 28),new google.maps.Point(0,0),new google.maps.Point(0, 28));

		createPins(resultLatLng,$("h3",this).text(), contentString, markerImage);
		
		//trigger click on map pin when click on result pin
		$(".map-pin a", this).click(function(){	
			google.maps.event.trigger(markersArray[i], 'click');
		});

		numListings ++;

	});
	
        map.fitBounds(bounds);

	//Zoom out if there is only one result to be displayed.
	if(numListings<=1){
			var zoomChangeBoundsListener = google.maps.event.addListener(map, 'zoom_changed', function() {
				google.maps.event.removeListener(zoomChangeBoundsListener);

				if (this.getZoom() > 16) // Change max/min zoom here
				this.setZoom(16);

			});
	}

}

/***Search Directory
*******Search Results Page - Pagination
**********************/
function buildPagination(){

	var newPaginateHTMLArray = [];
	var r_page_count = 0;

	$(".pagination a, .pagination b").each(function(i){
		if($(this).text()== "NEXT"){
			$(this).addClass("next");
		}else if($(this).text()== "PREVIOUS"){
			$(this).addClass("prev");
		}

		newPaginateHTMLArray[i] = $(this).clone();

	});

	$(".pagination").html("");
	if (newPaginateHTMLArray) {
    	for (i in newPaginateHTMLArray) {
			$(".pagination").append($(newPaginateHTMLArray[i]));
		}

	}

	$(".pagination b").replaceWith("<a href='#current' class='current'>"+$(".pagination b").text() +"</a>");

}

/***Search Directory
*******Search Results Page - Display Learning Category Icons
**********************/
function displayLearningCategoryIcons(learning_categories, a_id){

	var split_learning_categories = learning_categories.split("; ");
	var lc_legend = "";
	var split_lc = "";
	var learning_title = "";

	for (i=0; i<split_learning_categories.length; i++){

		//alt for icons
		switch (split_learning_categories[i]){
			case "English":
				learning_title = "Icon for ESL English as a Second Language";
				break;
			case "Literacy and Numeracy":
				learning_title = "Icon for Literacy & Numeracy Services";
				break;
			case "Employment":
				learning_title = "Icon for Employment Services";
				break;
			case "Finish Year 12":
				learning_title = "Icon for VCE and VCAL Providers";
				break;
			case "Introductory Courses":
				learning_title = "Introductory Courses";
				break;
			case "Qualifications":
				learning_title = "Icon for Qualification Course Providers";
				break;
			case "Disability":
				learning_title = "Disability";
				break;
			case "Youth":
				learning_title = "Icon for Youth Education Services";
				break;
			case "Muilticultural":
				learning_title = "Icon for Multicultural Services";
				break;
			case "Indigenous":
				learning_title = "Icon for Indigenous Services";
				break;			
		}

		split_lc = split_learning_categories[i].toLowerCase().replace(/\s/g, "_");
		lc_legend = "<div class='"+split_lc+"'><img src='./?a=349607' width='25px' height='25px' alt='" + learning_title + "' title='" + learning_title + "' /></div>";
			$("#"+a_id).append(lc_legend);
	}
	
}

//get query string
function querySt(ji) {

	hu = window.location.search.substring(1);
	gy = hu.split("&");
	
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
	
	return "";

}
