$(document).ready(function (){
	$('#socialize_applications').find("img").each(function (){
		$(this).mouseover(function () {
			var id = $(this).attr("id");
			
			$(this).attr("src", "theme/slices/socialize_"+id+"_mo.png");
		});
		
		$(this).mouseout(function () {
			var id = $(this).attr("id");
			
			$(this).attr("src", "theme/slices/socialize_"+id+".png");
		});
	});
	
	$('#footer_right').find("a").each(function (){
		$(this).click(function () {
			var href = $(this).attr("href");
			
			window.open(href);
			
			return false;
		});
	});
	
	$('#footer_left').find("a").each(function (){
		$(this).click(function () {
			var href = $(this).attr("href");
			
			window.open(href);
			
			return false;
		});
	});
	
	$("#newWindow").click(function () {
		window.open(webroot, 'newWindow', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=1024,height=768');
	});
});


function initialize() {
    var latlng = new google.maps.LatLng(59.32045, 18.066902);
    var myOptions = {
      zoom: 13,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    theMap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    return theMap;
}

function getMarkerInfo (infowindow, rack_id)
{
	// Get all the racks
	$.ajax({
		type: "GET",
		url: webroot+"ajax/getRack/"+secretCode+"/"+rack_id,
		cache: false,
		dataType: "html",
		success: function(html) {
			$("#rack_"+rack_id).html();

			infowindow.setContent(html);
		}
	});
}

$(document).ready(function () {
	map = initialize();

	function attachSecretMessage(marker, rack_id) {
		var infowindow = new google.maps.InfoWindow({
			size: new google.maps.Size(150,150)
		});

		google.maps.event.addListener(marker, 'click', function(){
			var ret = '<div id="rack_'+rack_id+'" class="rackDiv"><div class="rackDivLoading"><img src="theme/images/loader-small.gif" border="0"/><span>Hämtar data</span></div></div>';
			infowindow.setContent(ret);
			infowindow.open(map,marker);
			
			getMarkerInfo(infowindow,rack_id);
		});
	}

	function attachOfflineMessage(marker, rack_id, title) {
		var infowindow = new google.maps.InfoWindow({
			size: new google.maps.Size(50,50)
		});

		google.maps.event.addListener(marker, 'click', function(){
			var ret = '<div id="rack_'+rack_id+'" class="rackDiv"><div class="rackInfo"><div class="rackInfo_title">'+title+'</div><div class="rackInfo_info">Detta cykelställ är för närvarande inte tillgänglig.</div></div>';
			infowindow.setContent(ret);
			infowindow.open(map,marker);
		});
	}

	function addPlacemark (lat,lng,title, rack_id, online) {
		if (online == 0)
		{
			var image = new google.maps.MarkerImage('http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png',
	    	      		new google.maps.Size(32, 32),
	    	      		new google.maps.Point(0,0),
	    	      		new google.maps.Point(0, 32));
		}
		else {
			var image = new google.maps.MarkerImage('http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png',
    	      			new google.maps.Size(32, 32),
    	      			new google.maps.Point(0,0),
    	      			new google.maps.Point(0, 32));
		}
		
	    var shadow = new google.maps.MarkerImage('http://maps.google.com/mapfiles/shadow50.png',
	    	      	 new google.maps.Size(37, 34),
	    	      	 new google.maps.Point(0,0),
	    	      	 new google.maps.Point(-4, 34));

		var location 	= new google.maps.LatLng(lat,lng);
	    var marker		= new google.maps.Marker({
	           				position: location, 
	           				map: map,
	           				icon: image,
	           		        shadow: shadow});
	    
	    marker.setTitle(title);

		if (online == 1)
		{
			attachSecretMessage(marker, rack_id);
		}
		else {
			attachOfflineMessage(marker, rack_id, title);
		}
	}
	
	// Get all the racks
	$.ajax({
		type: "GET",
		url: webroot+"ajax/getRacks/"+secretCode,
		cache: false,
		dataType: "xml",
		success: function(xml) {
			// Add the results
			$(xml).find("station").each(function () {
				rack_id = $(this).find("rack_id").text();
				title = $(this).find("description").text();
				longitute = parseFloat($(this).find("longitute").text());
				latitude = parseFloat($(this).find("latitude").text());
				
				addPlacemark(latitude, longitute, title, rack_id, $(this).find("online").text());
			});
		}
	});

	$("#addMark").click(function () {
		
		addPlacemark (59.32045, 18.066902);
	});

	// Set size
	setPageSize();
});

//Set the mid size
function setPageSize ()
{
	// Set the design
	var arrPageSizes = ___getPageSize();
	
	var pageHeight = arrPageSizes[3];
	var footerHeight = $('#footer').height();
	
	var pageWidth = arrPageSizes[2];
	var footerWidth = $('#footer_left').width() + $('#footer_right').width() + $('#footer_index').width(); 

	if (setsocializeSize)
		var socializeWidth = pageWidth - footerWidth;
	
	var mapHeight = pageHeight - footerHeight;
	
	$('#map_canvas').height(mapHeight);
	$('#socialize').width(socializeWidth);
}

$(window).bind('resize', function() {
	setPageSize();
});

