$(document).ready(function(){
    $("#pyt-places-tabs").tabs();

	// Check for hash value in URL  
	//var hash = window.location.hash.substr(1);  
	var href = $('ul.places-list li a').each(function(){  
		//var href = $(this).attr('href');  
	    //if(hash==href.substr(0,href.length-5)){  
	    //	var toLoad = hash+'.aspx #pyt-current-place';  
	     //   $('#pyt-current-place').load(toLoad)  
	   	//}   
	});
	
	var hash = window.location.hash.substr(1);  

   	var href = $('#ul.places-list li a').each(function(){  
   		var href = $(this).attr('href');  
   
   		if(hash==href.substr(0,href.length-5)){  
   			var toLoad = hash+'.aspx #pyt-current-place';  
   			$('#pyt-current-place').load(toLoad)  
   		}   
   	});  
	
	var location_parts = window.location.toString().split("/");

	var my_url_places = Array();
	var my_key = 0;

	var get_map = function(){
		$('ul.places-list li a').removeClass('current');
		$(my_url_places[my_key]).addClass('current');
		var toLoad = $(my_url_places[my_key]).attr('href')+' #pyt-current-place';  
		var mapDirToLoad = $(my_url_places[my_key]).attr('href')+' #pyt-places-map ul'; 
		var show_new_content = function showNewContent() {
	        $('#pyt-current-place').fadeIn('normal', load_map);  
	    }  
		var show_new_directions = function showNewDirections() {
	        $('#pyt-current-place ul').fadeIn('normal');  
	    } 
	    var load_my_content = function loadContent() {  
	        $('#pyt-current-place-holder').load(toLoad,'',show_new_content);
			$('#pyt-places-map ul').load(mapDirToLoad,'',show_new_directions);
	    }  
		
		$('#pyt-current-place').fadeOut('fast',load_my_content);
		my_key++;
		
		if(my_key >= my_url_places.length){
			my_key = 0;
		}
	}

	var my_map_rotator;
	if(location_parts[location_parts.length - 1] == "places-to-stay.aspx"){
		var my_anchors = document.getElementById("pyt-hotels").getElementsByTagName("A");
		for(var i = 0;i < my_anchors.length; i++){
			my_url_places[my_url_places.length] = my_anchors[i];
		}
		
		my_url_places.sort(function() {return 50 - Math.floor((100 * Math.random()) + 0.5)});
		get_map(my_url_places, my_key);
		my_map_rotator = setInterval(get_map, 5000);
	}
	else if(location_parts[location_parts.length - 1] == "things-to-do.aspx"){
		var my_anchors = document.getElementById("pyt-attractions").getElementsByTagName("A");
		for(var i = 0;i < my_anchors.length; i++){
			my_url_places[my_url_places.length] = my_anchors[i];
		}
		
		my_url_places.sort(function() {return 50 - Math.floor((100 * Math.random()) + 0.5)});
		get_map(my_url_places, my_key);
		my_map_rotator = setInterval(get_map, 5000);
	}
	
	$('*').click(function() {
		clearInterval(my_map_rotator);
	});	

	$('ul.places-list li a').click(function(){  
		
		clearMap();
		
		$('ul.places-list li a').removeClass('current');
		
		$(this).addClass('current');
	      
	    var toLoad = $(this).attr('href')+' #pyt-current-place';  
		
		var mapDirToLoad = $(this).attr('href')+' #pyt-places-map ul'; 

		var show_new_content = function showNewContent() {
	        $('#pyt-current-place').fadeIn('normal', load_map);  
	    }  
		
		var show_new_directions = function showNewDirections() {
	        $('#pyt-current-place ul').fadeIn('normal');  
	    } 
	    var load_my_content = function loadContent() {  
	        $('#pyt-current-place-holder').load(toLoad,'',show_new_content);
			$('#pyt-places-map ul').load(mapDirToLoad,'',show_new_directions);
	    }  
		
		//var linksrc = $(this).attr('href').substr(0,$(this).attr('href').length-5);
		//var tempstring = linksrc.split('/');
		
		//window.location.hash = tempstring[3]; 
		
		//alert(tempstring[3]);
	
	//	window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);  
		
		$('#pyt-current-place').fadeOut('fast',load_my_content);  
	    //$('#pyt-loader').remove();  
	    //$('#wrapper').append('<span id="load">LOADING...</span>');  
	    //$('#pyt-loader').fadeIn('normal'); 
	
		
		
		//window.location.hash = tempstring[4];   
	    //function hideLoader() {  
	    //    $('#pyt-loader').fadeOut('normal');  
	    //}  
	    return false;  
	      
	});
});

var load_map = function(){
	initGoogle();	
}

function initGoogle(){
	var my_map, my_coords, my_lat, my_long;
	if(GBrowserIsCompatible()){
		var my_container = document.getElementById("google_map");
		var my_children = my_container.childNodes;
		for(var i = my_children.length - 1; i >= 0; i--){
			my_children[i].parentNode.removeChild(my_children[i]);
		}
		var my_map = document.createElement("DIV");
		my_map.className = "google_map";
		my_container.appendChild(my_map);
		var my_container_nodes = my_container.getElementsByTagName("DIV");
		
		for(var i=0;i<my_container_nodes.length;i++){
			if(my_container_nodes[i].className == "google_map"){
				my_map = new GMap2(my_container_nodes[i]);
				my_lat = parseFloat(document.getElementById("pyt-latitude").innerHTML);
				my_long = parseFloat(document.getElementById("pyt-longitude").innerHTML);
				
				my_location = new GLatLng(my_lat, my_long);
				ga_aquarium = new GLatLng(33.76227100, -84.3939058);
				
				if(my_long < -84.3939058){
					var bounds = new GLatLngBounds(my_location, ga_aquarium);
				}
				else{
					var bounds = new GLatLngBounds(ga_aquarium, my_location);
				}
				
				zoom_level = my_map.getBoundsZoomLevel(bounds);
				
				ga_icon = new GIcon(G_DEFAULT_ICON);
				ga_icon.image = "/new/images/gmap/blue-dot_ga.png";
				ga_icon.shadow = "/new/images/gmap/shadow50.png";
				ga_icon.iconSize = new GSize(32, 32);
				ga_icon.shadowSize = new GSize(43, 32);
				
				my_map.setCenter(bounds.getCenter(), zoom_level);
				my_map.addOverlay(new GMarker(my_location));
				my_map.addOverlay(new GMarker(ga_aquarium, {icon:ga_icon}));
				
				ga_aq_coord = my_map.fromLatLngToDivPixel(ga_aquarium);
				if((ga_aq_coord.x < 30) || (ga_aq_coord.y < 100)){
					my_map.zoomOut();
				}
				else{
					loc_coord = my_map.fromLatLngToDivPixel(my_location);
					if((loc_coord.x < 30) || (loc_coord.y < 100)){
						my_map.zoomOut();
					}
				}
			}
		}
	}
}

function clearMap(){
	var google_map = document.getElementById("google_map");
	google_map.parentNode.removeChild(google_map);
	
	google_map = document.createElement("DIV");
	google_map.id = "google_map";
	google_map.className = "google_map";
	document.getElementById("google_map_holder").appendChild(google_map);
}