/*
	PAGE CONTROLS/ASSETS
*/
var numResults = 5;
var currentList = 'lodging_list.php';


jQuery(function( $ ){
	
	var state = 0;
	/*
	TOGGLE PANELS
	*/
    $("#merchanttbtn").click(function () {
		if(state == 1) {
		 $('#merchanttbtn a').css({"backgroundPosition": "0px 0px"});
		 $('#maptbtn a').css({"backgroundPosition": "-370px 0px"});
		 $('#mapcontainer').css({"display": "none"});
	 	 $('#categoryback').css({"display": "block"});
		 $('#merchantcontent').css({"display": "block"});
		 $('#landingtbtns').css({"height": "71px"});
		 $('#merchantbottom').css({"display": "block"});
		 state = 0;
		}
    });
	$("#maptbtn").click(function () {
		if(state == 0) {
		 $('#merchanttbtn a').css({"backgroundPosition": "-370px 0px"});
		 $('#maptbtn a').css({"backgroundPosition": "0px 0px"});
		 $('#mapcontainer').css({"display": "block"});
		 $('#categoryback').css({"display": "none"});
		 $('#merchantcontent').css({"display": "none"});
		 $('#landingtbtns').css({"height": "465px"});
		 $('#merchantbottom').css({"display": "none"});
		 state = 1;
		 initialize();
		 //load();
		}
    });
	
		 $('#merchanttbtn a').css({"backgroundPosition": "0px 0px"});
		 $('#maptbtn a').css({"backgroundPosition": "-370px 0px"});
		 $('#categoryback').css({"display": "block"});
		 $('#merchantcontent').css({"display": "block"});
		 $('#landingtbtns').css({"height": "71px"});
		 $('#merchantbottom').css({"display": "block"});
		// initialize();
		
		
		$("#merchantmaplist").load("lodging_maplist.php");

		
		/*
		RESULTS OPTIONS
		*/
		$("select").change(function(){
		  //alert(this.value);
		  numResults = this.value;
		  $("#hiddenresult").load(currentList, null, function(){initPagination();});
		}); 
		
		$("#hiddenresult").load(currentList, null, function(){initPagination();});
		$('select').val('5');
		
		$(".hotel").click(function () {
			numResults = 5;
			currentList = 'hotel_list.php';
			$("#hiddenresult").load(currentList, null, function(){initPagination();});
			$('select').val('5');
    	});
		$(".bedbeakfast").click(function () {
			numResults = 5;
			currentList = 'bedbeakfast_list.php';
			$("#hiddenresult").load(currentList, null, function(){initPagination();});
			$('select').val('5');
    	});
		$(".camp").click(function () {
			numResults = 5;
			currentList = 'camp_list.php';
			$("#hiddenresult").load(currentList, null, function(){initPagination();});
			$('select').val('5');
    	});
		$(".viewall").click(function () {
			numResults = 5;
			currentList = 'lodging_list.php';
			$("#hiddenresult").load(currentList, null, function(){initPagination();});
			$('select').val('5');
    	});
	
	

		
});


function pageselectCallback(page_index, jq){
				page_index = page_index*numResults;
				var allcontent = new Array();
				$('#Searchresult').empty();
				for(i=0; i<numResults; i++) {
					var count = page_index+i;
					var new_content = $('#hiddenresult div.result:eq('+count+')').clone();
					$('#Searchresult').append(new_content);
					
				}

		 		$('#Searchresult').css({"display": "none"});
               // $('#Searchresult').empty().append(new_content1).append(new_content2).append(new_content3).append(new_content4).append(new_content5).append(new_content6);
				$('#Searchresult').fadeIn("fast");

                return false;
}
           
            /** 
             * Callback function for the AJAX content loader.
             */

function initPagination() {
                var num_entries = $('#hiddenresult div.result').length;
                // Create pagination element
				//alert(num_entries)
                $("#Pagination3").pagination(num_entries, {
                    num_edge_entries: 1,
                    num_display_entries: 3,
                    callback: pageselectCallback,
                    items_per_page:numResults
                });
}
			 



/*
GOOGLEMAPS
*/
function initialize() {
    if (GBrowserIsCompatible()) {
      // this variable will collect the html which will eventualkly be placed in the side_bar
      var side_bar_html = "";
    
      // arrays to hold copies of the markers used by the side_bar
      // because the function closure trick doesnt work there
      var gmarkers = [];
	  
	   var baseIcon = new GIcon(G_DEFAULT_ICON);
        baseIcon.shadow = "dot.png";
		baseIcon.iconSize = new GSize(35, 35);
        baseIcon.shadowSize = new GSize(35, 35);
        baseIcon.iconAnchor = new GPoint(0, 5);
        baseIcon.infoWindowAnchor = new GPoint(16, 16); 

      // A function to create the marker and set up the event window
      function createMarker(point,index,image,name) {
		var letter = String.fromCharCode("A".charCodeAt(0) + index);
        var letteredIcon = new GIcon(baseIcon);
        letteredIcon.image = "images/" + (index+1) + ".png";
		
		markerOptions = { icon:letteredIcon };
        var marker = new GMarker(point, markerOptions);
        GEvent.addListener(marker, "click", function() {
													 
         marker.openInfoWindowHtml('<table width="260" border="0" cellspacing="10"><tr><td width="114"><img src="images_upld/'+image+'" width="114" height="78" align="left" /></td><td width="140"  valign="top"><p class="bodytextbold">'+name+'</p></td></tr></table>' );
        });
        // save the info we need to use later for the side_bar
        gmarkers.push(marker);
        // add a line to the side_bar html
        side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length-1) + ')">' + name + '<\/a><br>';
        return marker;
      }


      // This function picks up the click and opens the corresponding info window
      function myclick(i) {
        GEvent.trigger(gmarkers[i], "click");
      }


      // create the map
      var map = new GMap2(document.getElementById("map_canvas"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng( 39.752601, -105.215979), 12);


      // Read the data from example.xml
      GDownloadUrl("lodging_markers.php", function(doc) {
        var xmlDoc = GXml.parse(doc);
        var markers = xmlDoc.documentElement.getElementsByTagName("marker");
        var names = xmlDoc.documentElement.getElementsByTagName("name");
        for (var i = 0; i < markers.length; i++) {
          // obtain the attribues of each marker
          var lat = parseFloat(markers[i].getAttribute("lat"));
          var lng = parseFloat(markers[i].getAttribute("lng"));
          var point = new GLatLng(lat,lng);
          var image = markers[i].getAttribute("image");
          var name = markers[i].getElementsByTagName("name")[0].firstChild.nodeValue;
          // create the marker
          var marker = createMarker(point,i,image,name);
          map.addOverlay(marker);
        }
        // put the assembled side_bar_html contents into the side_bar div
       // document.getElementById("side_bar").innerHTML = side_bar_html;
      });
    }

    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
	
}