// JavaScript Document

var miniWidth = 204;
var miniHeight = 156;
var overviewWidth = 960;
var overviewHeight = 200;
var midWidth = 390;
var midHeight = 320;
var oldWidth;
var oldHeight;
var totalWidth;
var totalHeight;
var offsetTop;
var offsetLeft;
var sitePlanResizedWidth;
var sitePlanResizedHeight;
var enlarged = false;
var devArray;

var LandingPage = function (params){
	
	$.ajax({
		type: "GET",
		url: encodeURI(params.xmlPath),
		dataType: "xml",
		success: function(xml) {
			$(xml).find('xml').each(function(){
				
				$('.overviewWrapper h2').empty();
				$('.overviewWrapper h2').append($(this).attr("name"));
											 
				var location = $(this).find('location');
				
				var miniMapURL = "http://maps.google.com/maps/api/staticmap?size=204x156&sensor=false&";
				miniMapURL += "zoom=" + location.attr("smallzoom");
				miniMapURL += "&";
				miniMapURL += "maptype=" + location.attr("type");
				miniMapURL += "&";
				miniMapURL += "markers=icon:http://dev.chriscurddesign.co.uk/commercial/p/images/westleighMarker.png|" + location.attr("longitude") + "," + location.attr("latitude");
				
				$('.location').append('<img src="' + miniMapURL + '" width="204" height="156" />');
				
				var availability = $(this).find('availability');				
				$('.availability').append('<img src="' + availability.attr("map") + '" />');
				$('.availability img').load(function(){cropImage(miniWidth, miniHeight, $(this));});
				
				
				var photographs = $(this).find('photographs');
				var photoURL = photographs.attr("photo");
				
				if(photoURL == "" || photoURL == null)
				{
					$.ajax({
						type: "GET",
						url: encodeURI(params.galleryxmlPath),
						dataType: "xml",
						success: function(xml) {
							$(xml).find('xml').each(function(){
								var firstItem = $(this).find("item:first");
								photoURL = firstItem.find('file').text();
								$('.photographs').append('<img src="' + photoURL + '" />');
								$('.photographs img').load(function(){cropImage(miniWidth, miniHeight, $(this));});			
								$('.overviewBanner').append('<img src="' + photoURL + '" />');
								$('.overviewBanner img').load(function(){cropImage(overviewWidth, overviewHeight, $(this));});	
							});
						}
					});
							
				}
				else
				{
					$('.photographs').append('<img src="' + photoURL + '" />');
					$('.photographs img').load(function(){cropImage(miniWidth, miniHeight, $(this));});
					
					$('.overviewBanner').append('<img src="' + photoURL + '" />');
					$('.overviewBanner img').load(function(){cropImage(overviewWidth, overviewHeight, $(this));});								

				}
/*				$(this).find('item').each(function(){
				
					var imagePath = encodeURI($(this).find('file').text());
					
				});*/
			});
		}
	});

}

var initAgents = function(params){
	
	$.ajax({
		type: "GET",
		url: encodeURI(params.xmlPath),
		dataType: "xml",
		success: function(xml) {
			$(xml).find('xml').each(function(){
				
				var brochureURL = $(this).attr("brochureURL");
				$('.brochureBtn a').attr('href', brochureURL);
				
				var agents = $(this).find('agents');
				
				var agents = $(this).find('agents');
				$(agents).find('agent').each(function(){	 
				
					var image = $(this).attr("image");
					var name = $(this).attr("name");
					var email = $(this).attr("email");
					
					var newHTML = "";
					newHTML += '<div class="agentBox">';
					newHTML += '<div class="agentLogo">';
					newHTML += '<img src="' + image + '" />';
					newHTML += '</div>';
					newHTML += '<div class="agentInfo">';
					newHTML += '<h3>' + name + '</h3>';
					newHTML += '<div class="viewBtn"><a href="mailto:' + email + '" target="_blank">Email</a></div>';
					newHTML += '</div>';
					newHTML += '</div>';
					
					$('.agentViewBox').append(newHTML);
				
				});
				
			});
		}
	});

}

var initLocationPage = function(params){
	
	$.ajax({
		type: "GET",
		url: encodeURI(params.xmlPath),
		dataType: "xml",
		success: function(xml) {
			$(xml).find('xml').each(function(){
				
				$('.overviewWrapper h2').empty();
				$('.overviewWrapper h2').append($(this).attr("name"));
											 
				var location = $(this).find('location');
				
				var long = location.attr("longitude");
				var lat = location.attr("latitude");
				var zoom = parseInt(location.attr("largezoom"));
				var type = location.attr("type");
				
				var mapsURL = "http://maps.google.com/?ie=UTF8&ll=" + long + "," + lat + "&z=" + zoom;				
				$('#viewOnGoogle a').attr('href', mapsURL);
				
				if (GBrowserIsCompatible()) {
					var map = new GMap2(document.getElementById("map_canvas"));
					map.setCenter(new GLatLng(long, lat), zoom);
					map.setUIToDefault();
					
					var westleighIcon = new GIcon(G_DEFAULT_ICON);
					westleighIcon.image = "http://dev.chriscurddesign.co.uk/commercial/p/images/westleighMarker.png";
					
					// Set up our GMarkerOptions object
					markerOptions = { icon:westleighIcon };

					
					var point = new GLatLng(long, lat);
					map.addOverlay(new GMarker(point, markerOptions));
					
				}
				
				
			});
		}
	});

}

var initPhotoPage = function(params){
	
	$.ajax({
		type: "GET",
		url: encodeURI(params.xmlPath),
		dataType: "xml",
		success: function(xml) {
			$(xml).find('xml').each(function(){
				
				$('.overviewWrapper h2').empty();
				$('.overviewWrapper h2').append($(this).attr("name"));
				
			});
		}
	});

}

var initDevelopmentsPage = function(params){
	
	var devXML;
	
	$.ajax({
		type: "GET",
		url: encodeURI(params.xmlPath),
		dataType: "xml",
		success: function(xml) {
			$(xml).find('xml').each(function(){
				var devXML = $(this);
				
				var i = 0;
				$(this).find('development').each(function(){
														  
					var overviewPath = $(this).attr("overviewPath");
					var devXMLPath = $(this).attr("xmlPath");
					
					var newHTML = "";
					newHTML += '<div class="viewBox devViewBox" id="viewBox' + i + '">';
					newHTML += '<div class="header">';
					newHTML += '<div class="viewBtn"><a href="' + overviewPath + '">View</a></div>';
					newHTML += '<h3></h3>';
					newHTML += '</div>';
					newHTML += '<div class="devContentArea photograph"></div><div class="devContentArea location"></div>';
					newHTML += '</div>';
					
					$('.developmentsBoxes').append(newHTML);
					
					var localI = i;
					$.ajax({
						type: "GET",
						url: encodeURI(devXMLPath),
						dataType: "xml",
						success: function(thisXML)
						{
							setupDevelopment(thisXML, localI);
						}
					});
					
					i++;
					//alert(i);
					
				});
				
			});
			
			$('.developmentsBoxes').append('<div style="clear:both;"></div>');
		}
	});
	
	

}

var initArchivePage = function(params){
	
	var devXML;
	
	$.ajax({
		type: "GET",
		url: encodeURI(params.xmlPath),
		dataType: "xml",
		success: function(xml) {
			$(xml).find('xml').each(function(){
				var devXML = $(this);
				
				var i = 0;
				$(this).find('development').each(function(){
														  
					var name = $(this).attr("name");
					var overviewPath = $(this).attr("overviewPath");
					var devXMLPath = $(this).attr("xmlPath");
					
					var newHTML = "";
					newHTML += '<div class="viewBox devViewBox" id="viewBox' + i + '">';
					newHTML += '<div class="header">';
					newHTML += '<div class="viewBtn"><a href="' + overviewPath + '">View</a></div>';
					newHTML += '<h3>' + name + '</h3>';
					newHTML += '</div>';
					newHTML += '<div class="archiveContentArea photograph"></div>';
					newHTML += '</div>';
					
					$('.developmentsBoxes').append(newHTML);
					
					var localI = i;
					setupArchiveDev(devXMLPath, localI);
					
					i++;
					//alert(i);
					
				});
				
			});
			
			$('.developmentsBoxes').append('<div style="clear:both;"></div>');
		}
	});
	
	

}

var initAvailabilityPage = function(params)
{
	
	$.ajax({
		type: "GET",
		url: encodeURI(params.xmlPath),
		dataType: "xml",
		success: function(xml) {
			$(xml).find('xml').each(function(){
											 
				$('.overviewWrapper h2').empty();
				$('.overviewWrapper h2').append($(this).attr("name"));
											 
				var availability = $(this).find('availability');
				
				var i = 0;
				
				var newDesc = availability.attr("description");
				
				if(newDesc != null && newDesc != "" && newDesc != " ")
				{
					$('.midContentArea').each(function(){
						if(!$(this).hasClass('siteplan'))
						{
							$(this).before('<p class="developmentDesc">'+newDesc+'</p>');
						}
					});
				}
				
				$(availability).find('unit').each(function(){
					
					var tableRow = "";
					
					var trClass = "odd";
					
					if(i % 2 != 0)
					{
						trClass = "even";
					}
					
					tableRow += '<tr class="' + trClass + '">';
					tableRow += '<td>' + $(this).attr("name") + '</td>';
					tableRow += '<td>' + $(this).attr("size") + '</td>';
					tableRow += '<td>' + $(this).attr("parking") + '</td>';
					
					if($(this).attr("status") == "")
					{
						//tableRow += '<td><img src="availability_tick.png" width="25" height="21" alt="Available" /></td>';
						tableRow += '<td>-</td>';
					}
					else
					{
						tableRow += '<td>' + $(this).attr("status") + '</td>';
					}
					
					
					tableRow += '</tr>';

					$('.availabilityTable tbody').append(tableRow);
					
					i++;
				});
				
				$('.availabilityTable tbody').append('<tr class="availabilityTableFooter"><td colspan="4"></td></tr>');
				
				var siteMap = availability.attr("map");
				
				$('.siteplan').append('<img src="' + siteMap + '" />');
				$('.siteplan img').load(function(){
					var fullWidth = $(this).width();
					var fullHeight = $(this).height();
					var btnHref = 'javascript:enlargeSiteMap('+ fullWidth +', '+ fullHeight +');';
					$('#sitePlanEnlarge a').attr('href', btnHref);
					$('#sitePlanEnlarge a').attr('target', '');
					cropImage(midWidth, midHeight, $(this));
				});
				
				
			});
		}
	});

}

var openAgents = function()
{
	var newLeftMargin = ($(window).width() - 484) / 2;
	var newTopMargin = 196;
	
	var overlayHeight = $(document.body).height();
	
	$('.agentViewBox').css({marginTop: newTopMargin, marginLeft: newLeftMargin});
	
	$('.agentOverlay').css({display: "none", height: overlayHeight});
	$('.agentOverlay').stop().fadeIn(250);
}

var closeAgents = function()
{
	$('.agentOverlay').stop().fadeOut(250);
}

var enlargeSiteMap = function(fullWidth, fullHeight)
{
	
	offsetTop = $("#siteplan").offset().top;
	offsetLeft = $("#siteplan").offset().left;
	
	var newHTML = '';
	
	newHTML += '<div class="enlargeOverlay">';
	
	newHTML += '<div class="midViewBox enlarged">';
	newHTML += $('#siteplan').html();
	newHTML += '</div>';
	newHTML += '</div>';
	
	$('body').append(newHTML);
	
	$('.enlarged').css({marginTop: offsetTop, marginLeft: offsetLeft});
	
	$('.enlarged .viewBtn a').empty();
	$('.enlarged .viewBtn a').html('Close');
	$('.enlarged .viewBtn a').attr('href', 'javascript:closeEnlarged();');
	
	oldWidth = $('.enlarged .midContentArea').width();
	oldHeight = $('.enlarged .midContentArea').height();
	sitePlanResizedWidth = $('.enlarged .midContentArea img').width();
	sitePlanResizedHeight = $('.enlarged .midContentArea img').height();
	
	var widthDifference = fullWidth - oldWidth;
	var heightDifference = fullHeight - oldHeight;
	
	totalWidth = $('.enlarged').width();
	totalHeight = $('.enlarged').height();
	
	var newWidth = totalWidth + widthDifference;
	var newHight = totalHeight + heightDifference;
	
	//alert('newWidth: ' + newWidth + ', newHight: ' + newHight);
	
	var newLeft = (offsetLeft - widthDifference);
	
	$('.enlargeOverlay').css({display: "none"});
	$('.enlargeOverlay').stop().fadeIn(250);
	
	$('.enlarged').stop().delay(150).animate(
			{width: newWidth, height: newHight, marginLeft: newLeft},
			{duration: 750,
			easing: 'easeInOutQuart'});
	
	$('.enlarged img').stop().delay(150).animate(
			{width: fullWidth, height: fullHeight},
			{duration: 750,
			easing: 'easeInOutQuart'});
	
	$('.enlarged .midContentArea').stop().delay(150).animate(
			{width: fullWidth, height: fullHeight},
			{duration: 750,
			easing: 'easeInOutQuart'});
	
	$('.enlargeOverlay').click(closeEnlarged);
	
	enlarged = true;
	
}

var closeEnlarged = function()
{
	if(enlarged)
	{
		$('.enlarged').stop().animate(
				{width: totalWidth, height: totalHeight, marginLeft: offsetLeft},
				{duration: 500,
				easing: 'easeInOutQuart'});
		
		$('.enlarged img').stop().animate(
				{width: sitePlanResizedWidth, height: sitePlanResizedHeight},
				{duration: 500,
				easing: 'easeInOutQuart'});
		
		$('.enlarged .midContentArea').stop().animate(
				{width: oldWidth, height: oldHeight},
				{duration: 500,
				easing: 'easeInOutQuart'});
		
		$('.enlargeOverlay').stop().delay(400).fadeOut(250, function(){
			$('.enlargeOverlay').remove();
		});
		
		enlarged = false;
	}
	
}

var setupDevelopment = function(thisXML, i)
{
	
	$(thisXML).find('xml').each(function(){
		
		$('#viewBox' + i + ' h3').empty();
		$('#viewBox' + i + ' h3').append($(this).attr("name"));
										 
		var location = $(this).find('location');
		
		
		var miniMapURL = "http://maps.google.com/maps/api/staticmap?size=207x159&sensor=false&";
		miniMapURL += "zoom=" + location.attr("smallzoom");
		miniMapURL += "&";
		miniMapURL += "maptype=" + location.attr("type");
		miniMapURL += "&";
		miniMapURL += "markers=icon:http://dev.chriscurddesign.co.uk/commercial/p/images/westleighMarker.png|" + location.attr("longitude") + "," + location.attr("latitude");
		
		$('#viewBox' + i + ' .location').append('<img src="' + miniMapURL + '" width="207" height="159" />');
	
	
		var photographs = $(this).find('photographs');
		var photoURL = photographs.attr("photo");
		
		
		if(photoURL == "" || photoURL == null)
		{
			$.ajax({
				type: "GET",
				url: encodeURI(photographs.attr('galleryXMLPath')),
				dataType: "xml",
				success: function(xml) {
					$(xml).find('xml').each(function(){
						var firstItem = $(this).find("item:first");
						photoURL = firstItem.find('file').text();
						
						$('#viewBox' + i + ' .photograph').append('<img src="' + photoURL + '" />');
						$('#viewBox' + i + ' .photograph img').load(function(){cropImage(207, 159, $(this));});
					});
				}
			});
					
		}
		else
		{
			$('#viewBox' + i + ' .photograph').append('<img src="' + photoURL + '" />');
			$('#viewBox' + i + ' .photograph img').load(function(){cropImage(207, 159, $(this));});
		}
		
		
		//alert(iLocal);
	});
	
}

var setupArchiveDev = function(thisXML, i)
{
	
	$.ajax({
		type: "GET",
		url: encodeURI(thisXML),
		dataType: "xml",
		success: function(xml) {
			$(xml).find('xml').each(function(){
				var firstItem = $(this).find("item:first");
				photoURL = firstItem.find('file').text();
				$('#viewBox' + i + ' .photograph').append('<img src="' + photoURL + '" />');
				$('#viewBox' + i + ' .photograph img').load(function(){cropImage(423, 159, $(this));});
				
			});
		}
	});
	
}

var cropImage = function(newWidth, newHeight, image) {
	
	var width = image.width();
	var height = image.height();
	
	var ratio1 = (newWidth + 1) / width;
	var ratio2 = (newHeight + 1) / height;
	
	var ratio = ratio2;
	
	if(width * ratio1 >= newWidth && height * ratio1 >= newHeight)
	{
		ratio = ratio1;
	}
	
	image.attr('width', (width * ratio));
	image.attr('height', (height * ratio));
}
