

if(document.location.hash != ""){
	document.location = document.location.hash.substr(1);
}


// JavaScript Document
$(document).ready(function(){
	
	
	setTimeout('setPositions("ready"); init_map();', 100);
	//setPositions("ready");
		
	/* Display content */
	$("a.load_page").click(function(){
		
		var page_path = $(this).attr("href");
		$("#page-content").html("<div style='text-align:center; margin-top:50px;'><img src='images/loading.gif' border=0 /></div>");
		$("#page-content").load(page_path + "?ajax=1");
		
		//$("#fullpages").animate( { left:0 }, { queue:false, duration:500 }, "swing" )	
		$("#page-col").animate( { left:0 }, { queue:false, duration:500 }, "swing" )	
		
		document.location.hash = page_path;
		
		return false;
	});
	
	/* Hide content */
	$("#page-tab").click(function(){
		
		//$("#fullpages").animate( {left:-820 }, { queue:false, duration:500 }, "swing" )	
		$("#page-col").animate( { left:-757 }, { queue:false, duration:500 }, "swing" )	
		
		return false;
	});
	
	
	//Details trigger
	$("#details-tab").click(function(){
		
		if($("#details-col").position().left == 375){
			$("#details-col").animate( { left:6 }, { queue:false, duration:500 }, "swing" );
			$("#contact-col").animate( { left:6 }, { queue:false, duration:500 }, "swing" );
			$("#details-tab > span.arrow-to-left").removeClass('arrow-to-left').addClass('arrow-to-right');
			$("#contact-tab > span.arrow-to-left").removeClass('arrow-to-left').addClass('arrow-to-right');
		}else{
			$("#details-col").animate( { left:375 }, { queue:false, duration:500 }, "swing" );
			$("#contact-col").animate( { left:396 }, { queue:false, duration:500 }, "swing" );
			$("#details-tab > span.arrow-to-right").removeClass('arrow-to-right').addClass('arrow-to-left');
		}
		
	});	
	//Contact trigger
	$("#contact-tab").click(function(){
		
		if($("#details-tab > span.arrow-to-left").length>0){
		
			if($("#contact-col").position().left == 396){
				$("#contact-col").animate( { left:396+365 }, { queue:false, duration:500 }, "swing" );
				$("#contact-tab > span.arrow-to-right").removeClass('arrow-to-right').addClass('arrow-to-left');
			}else{
				$("#contact-col").animate( { left:396 }, { queue:false, duration:500 }, "swing" );
				$("#contact-tab > span.arrow-to-left").removeClass('arrow-to-left').addClass('arrow-to-right');
			}
		}
		
	});	
	
	
	$(window).resize(function() {
							  
		setPositions("resize");
		
	});//END window.resize event handler
	
});// END document.ready event handler


function setPositions(e){
	//Map size
	var mapheight = $(window).height() - $("#header").outerHeight(true) - $("#footer").outerHeight(true)-4;
	var mapwidth = $("#content").width() - $("#left-col").outerWidth(true) - (e!="resize" || $("#content").width()>=screen.width ? 20 : 0) - 20;
	$("#content").css("height",mapheight+"px");
	$("#maps").css("height","100%");
	$("#maps").css("width",mapwidth+"px");
	
	//Map Position Diagnostic
	//$("#left-col").html("Map width:"+ mapwidth+"px "+mapheight+"px;<br> Screen width is: "+screen.width+"px;<br> Content width is: "+$("#content").width()+"px");
	
	//Left column height
	var leftheight = mapheight - ($("#left-col").outerHeight() - $("#left-col").height());
	$("#left-col").css("height",leftheight+"px");
	
	//Details colmun height
	var detailsheight = mapheight - ($("#details-content").outerHeight() - $("#details-content").height());
	$("#details-col").css("height",mapheight+"px");
	$("#details-content").css("height",detailsheight+"px");
	$("#details-tab").css("height",mapheight+"px");
	
	//Contact column height
	var contactheight = mapheight - ($("#contact-content").outerHeight() - $("#contact-content").height());
	$("#contact-col").css("height",mapheight+"px");
	$("#contact-content").css("height",contactheight+"px");
	$("#contact-tab").css("height",mapheight+"px");
	
	//Tabs height
	$("#tabs1").css("height",mapheight-1+"px");
	$("#tabs2").css("height",mapheight-1+"px");
	$("#tabs").css("height",mapheight-1+"px");
	
	//Page content height
	var pageheight = mapheight - ($("#page-content").outerHeight() - $("#page-content").height());
	$("#page-col").css("height",mapheight+"px");
	$("#page-content").css("height",pageheight+"px");
	$("#page-tab").css("height",mapheight+"px");
	//$("#page-content").css("height",mapheight-40+"px");
	
}


/* MAP CONTROL */

function init_map(){
	var map = new GMap2(document.getElementById('maps')); 
	var montrealcoord = new GLatLng(47.141034649999995, -70.19603204999999); 
	map.setCenter(montrealcoord, 7); 
	var bounds = new GLatLngBounds();
	
	var markers = [];
	downloadUrl("proprietes?format=xml", function(data) {
		
		var properties = data.documentElement.getElementsByTagName("property");
		for (var i = 0; i < properties.length-1; i++) {
			
			var property = properties[i];
			var marker_info = property.getElementsByTagName("marker")[0];
			var latlng = new GLatLng(parseFloat(marker_info.getAttribute("lat")),parseFloat(marker_info.getAttribute("lng"))); 
			
			//Icon
			var flyIcon = new GIcon();
			flyIcon.image = "http://www.flyimmobilier.biz/skin/flyimmobilier/public/images/marker_small.png";
			flyIcon.iconSize = new GSize(15, 24);//52, 47
			flyIcon.iconAnchor = new GPoint(8, 25);//13, 43
			flyIcon.infoWindowAnchor = new GPoint(2, 1);
			var marker = new GMarker(latlng,flyIcon);
			map.addOverlay(marker);
			
			markers[i] = marker; 
			//createMarker(marker, latlng);
			
			bounds.extend(latlng);
		}
		
		
		
		map.setZoom(map.getBoundsZoomLevel(bounds));
		map.setCenter(bounds.getCenter());
		
		//alert(map.getZoom());
		//var map_center = map.getCenter();
		//alert(map_center.lat());
		//alert(map_center.lng());
		
		map.addControl(new GLargeMapControl3D(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10)) );
		map.addControl(new GMapTypeControl());
		
		GEvent.addListener(map, 'zoomend', function() {
			reloadProperties(map.getBounds(), 0);
		});
		
		GEvent.addListener(map, 'dragend', function() {
			reloadProperties(map.getBounds(), 0);
		});
		
		$(markers).each(function(i,marker){ 
			GEvent.addListener(marker,"click", function(){ 
				
				//Get marker position to pixel
				var marker_px = map.fromLatLngToDivPixel(marker.getPoint());
				//Ajust to the incoming info box
				marker_px.y -= 105;
				
				marker_px.x += 154-($("#details-col").position().left == 375 ? 182 : 0)-($("#contact-col").position().left == 761 ? 190 : 0);
				//Convert back to latlng
				var marker_latlng = map.fromDivPixelToLatLng(marker_px);
				// And recenter
				map.panTo(marker_latlng);
				
				displayPropertyBox(map,marker,properties[i]); 
			}); 
		});
		
		
	});
	
}


function displayPropertyBox(map,marker,property){
	
	var div_box = $(document.createElement('div'));
	var property_balloon_model = $("#property_balloon_model").html();
	
	property_balloon_model = property_balloon_model.replace("[property_id]",	property.getElementsByTagName("name")[0].childNodes[0].nodeValue); 
	property_balloon_model = property_balloon_model.replace("[img]", 			property.getElementsByTagName("img")[0].childNodes[0].nodeValue); 
	property_balloon_model = property_balloon_model.replace("[price]", 			property.getElementsByTagName("price")[0].childNodes[0].nodeValue); 
	property_balloon_model = property_balloon_model.replace("[address]", 		property.getElementsByTagName("address")[0].childNodes[0].nodeValue); 
	property_balloon_model = property_balloon_model.replace("[address2]", 		property.getElementsByTagName("address2")[0].childNodes[0].nodeValue); 
	property_balloon_model = property_balloon_model.replace("[id]", 			property.getElementsByTagName("id")[0].childNodes[0].nodeValue); 
	
	
	$(div_box).html(property_balloon_model);
	$(div_box).addClass('map_property_box');
	
	$(div_box).appendTo(map.getPane(G_MAP_FLOAT_SHADOW_PANE));
	var markerOffset = map.fromLatLngToDivPixel(marker.getPoint());
	$(div_box).css({ top:markerOffset.y-$(div_box).height()-40, left:markerOffset.x+29 });
	$(div_box).hide();
	$(div_box).fadeIn();
	
	$(div_box).find(".close_balloon").click(function(){
		$(div_box).fadeOut(500,function () {
			$(this).remove();
		});
	});
	
	GEvent.addListener(map, "zoomend", function(oldzoom,newzoom) {
		var markerOffset = map.fromLatLngToDivPixel(marker.getPoint());
		$(div_box).css({ top:markerOffset.y-$(div_box).height()-40, left:markerOffset.x+29 });
		
	}); 
}
//http://www.flyimmobilier.biz/propriete?prp_id=1&ajax=1

function loadPropertyDetails( prp_id, animate ){
	
	$("#details-content").html("<div style='text-align:center; margin-top:50px;'><img src='images/loading.gif' border=0 /></div>");
	if(animate==1){
		$("#details-col").animate( { left:375 }, { queue:false, duration:500 }, "swing" );
		$("#contact-col").animate( { left:396 }, { queue:false, duration:500 }, "swing" );
		$("#details-tab > span.arrow-to-right").removeClass('arrow-to-right').addClass('arrow-to-left');
	}
	$("#details-content").load("propriete?prp_id="+prp_id+"&ajax=1",  function(data) {
		Shadowbox.clearCache(); Shadowbox.init({ skipSetup: true }); Shadowbox.setup(); 
	});
	
}

function loadContactAgent( agt_id, animate ){
	
	$("#contact-content").html("<div style='text-align:center; margin-top:50px;'><img src='images/loading.gif' border=0 /></div>");
	if(animate==1){
		$("#contact-col").animate( { left:396+365 }, { queue:false, duration:500 }, "swing" );
		$("#contact-tab > span.arrow-to-right").removeClass('arrow-to-right').addClass('arrow-to-left');
	}
	$("#contact-content").load("contact-agent?agt_id="+agt_id+"&ajax=1");
	
}


function picturesPrev(){
	
	var current_left = parseInt($("#property-pictures-thumbs").css("left").replace(/px/g,''));
	var new_left = parseInt(current_left+57);
	//check if we are to the first one and if already in movement
	if(new_left <= 0 && current_left%57==0){
		$("#property-pictures-thumbs").animate( { left:new_left }, { queue:false, duration:300 }, "swing" );
	}
}

function picturesNext(nb_max){
	
	var current_left = parseInt($("#property-pictures-thumbs").css("left").replace(/px/g,''));
	var new_left = parseInt(current_left-57);
	var max_left = -57*(nb_max-5);
	//check if we are to the last one and if already in movement
	if(new_left >= max_left && current_left%57==0){
		$("#property-pictures-thumbs").animate( { left:new_left }, { queue:false, duration:300 }, "swing" );
	}
}

function loadPicture( e, url ){
	
	$("#ppi_img").attr("src",url);
	
	$("#ppi_a").attr("href",url.replace(/_347x185/g,''));
	
	$("#ppi_label").html($(e).attr("title"));
	
	
	Shadowbox.clearCache(); Shadowbox.init({ skipSetup: true }); Shadowbox.setup(); 
	
}

function activateProperty(e,prp_id){
	
	$("#left-col .fiches").removeClass('active');
	$(e).addClass('active');
	
	loadPropertyDetails( prp_id, 0); 
	
}

function reloadProperties(bounds, agt_id){
	
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	
	var min_lat = southWest.lat();
	var min_lng = southWest.lng();
	var max_lat = northEast.lat();
	var max_lng = northEast.lng();
	
	$("#left-col").html("<div style='text-align:center; margin-top:50px;'><img src='images/loading.gif' border=0 /></div>");
	$("#left-col").load("proprietes?agt_id="+agt_id+"&ajax=1&min_lat="+min_lat+"&min_lng="+min_lng+"&max_lat="+max_lat+"&max_lng="+max_lng);
	
	
}

