var homepageGallery;
var galleryTitleScale = 1;
var titleStep = 0;
var galleryAnimSpeed = 750;
var propertyTypeArray;
var propertyURLArray;
var propertyArray;
var propertiesLoaded = 0;

var initHomepage = function() {
	
	homepageGallery = new Gallery({wrapper: "homepageGallery", xmlPath: "storage/xml/slideshow-1.xml", layout: "nodesTitles", imagesClickable: false, loadedCallback: galleryLoaded, wrapTitleInPTags: true, preAnimationCallback: galleryTitleAnimate, animSpeed: galleryAnimSpeed, animTransition: "swing" });

}

var initProperties = function() {
	
	$.ajax({
		type: "GET",
		url: "storage/xml/properties.xml",
		dataType: "xml",
		success: function(xml) {
			$(xml).find('xml').each(function(){
				
				
				parsePropertyXML($(this));
				
			});
		},
		error: function(xhr, ajaxOptions, thrownError){
			//alert('Could not load XML "' + params.xmlPath + '", thrown error: ' + thrownError);
		}
	});
	
}

var parsePropertyXML = function($xml) {
	
	propertyTypeArray = new Array();
	propertyTypeArray.push('All');
	propertyURLArray = new Array();
	propertyArray = new Array();
		
	var i = 0;
	$xml.find('item').each(function(){
									
		var type = $(this).attr('type');
		var galleryPath = $(this).attr('galleryPath');
		
		var $newGalleryWrapper = $('<div class="propertyGallery on" id="propertyGallery'+i+'"></div>').appendTo('#propertyWrapper');
		
		propertyArray.push({$: $newGalleryWrapper, type: type});
		
		var newGallery = new Gallery({wrapperID: "#propertyGallery"+i, xmlPath: galleryPath, layout: "nodesTitlesCaptions", imagesClickable: true, loadedCallback: propertyGalleryLoaded, animSpeed: galleryAnimSpeed, animTransition: "swing", firstTitleOnly: true, firstCaptionOnly: true, slideInterval: 0, nodesAlign: "left", wrapTitleInPTags: true, wrapCaptionInPTags: true });

		
		var j = 0;
		var typeExists = false;
		for(j = 0; j < propertyTypeArray.length; j++)
		{
			if(propertyTypeArray[j] == type)
			{
				typeExists = true;
			}
		}
		if(!typeExists)
		{
			propertyTypeArray.push(type);
		}
		
		i++;
	
	});
	
	var j = 0;
	for(j = 0; j < propertyTypeArray.length; j++)
	{
		var newURL = makeURL(propertyTypeArray[j]);
		
		if(j == 0)
		{
			newURL = "";
		}
		
		propertyURLArray.push(newURL);
		
		$('#submenuLinks').append('<li id="type'+j+'"><a href="#'+newURL+'">'+propertyTypeArray[j]+'</a></li>');
	}
	
	//$('#submenuLinks li').click(propertyTypeClick);
	$('#submenuLinks li a').click(getPage);
	
	deepLinkInit();
	
	handleLink($.address.path(), true);
	
}

var getPage = function(event)
{
	event.preventDefault();
	$.address.value('/'+$(this).attr('href').split('#').join(''));
}

var deepLinkInit = function()
{
	$.address.crawlable(true)
	$.address.strict(false);
	
	$.address.init(function(event) {
		//console.log('init: "' + event.value + '"');
		deepLinkEvent(event, true);
	}).change(function(event) {
		//console.log('internalChange: "' + event.value + '"');
		deepLinkEvent(event, false);
	}).externalChange(function(event) {
		//console.log('externalChange: "' + event.value + '"');
		deepLinkEvent(event, false);
	});
	
}

var deepLinkEvent = function(E, external) {
	//console.log('deepLinkEvent fired');
	/*
	if(!xmlInitComplete)
	{
		firstPage = E.value;
		//console.log('firstPage set to: ' + E.value );
		linkInitComplete = true;
		//console.log('linkInitComplete = true');
	}
	else
	{
		*/
		//console.log('xmlInitComplete true, firing handleLink with value: ' + E.value );
		handleLink(E.value, external);
		
	/*
	}
	*/
}

var handleLink = function(linkValue, external) {
	
	linkValue = linkValue.split('/');
	linkValue = linkValue[linkValue.length-1];
	
	var found = true;
	
	if(linkValue == "#" || linkValue == "" || linkValue == "#all" || linkValue == "all")
	{
			found = false;
	}
	
	if(found)
	{
		found = false;
		
		var i = 1;
		for(i = 1; i < propertyURLArray.length; i++)
		{
			if(linkValue == propertyURLArray[i])
			{
				found = true;
				selectPropertyType(i);
				
				$.address.title(propertyTypeArray[i] + ' | Properties | Penhurst Properties Ltd');
				
			}
		}
		
	}
	
	if(!found)
	{
		selectPropertyType(0);
		$.address.title('Properties | Penhurst Properties Ltd');
	}
	
	/*
	if(linkValue == "/" ||linkValue == "#" || linkValue == "" || linkValue == "/home")
	{
		scrollToPage(0, external);
		
		if(verboseTitleArray[pageNum] != "")
		{
			$.address.title(verboseTitleArray[0] + ' | Toureen Mangan');
		}
		else
		{
			$.address.title(sectionName + ' | Toureen Mangan');
		}
	}
	*/
}

var makeURL = function(text) {
	
	var url = text.split(' ').join('-');
	url = url.split('&').join('and');
	url = url.toLowerCase();
	url = escape(url);
	return(url);
	
}

var propertyTypeClick = function() {
	
	selectPropertyType( $(this).attr('id').split('type').join('') );
															 
}

var selectPropertyType = function(num) {
	
	$('#submenuLinks li').removeClass('on');
	$('li#type'+num).addClass('on');
	
	$('html, body').animate({
		scrollTop: 0
	}, 500, 'swing');
	
	if(num == 0)
	{//all
		$('.propertyGallery').addClass('on').show();
	}
	else
	{
		var type = propertyTypeArray[num];
		var j = 0;
		for(j = 0; j < propertyArray.length; j++)
		{
			if(propertyArray[j].type == type)
			{
				propertyArray[j].$.addClass('on').show();
			}
			else
			{
				propertyArray[j].$.removeClass('on').hide();
			}
		}
		
	}
	
}

var propertyGalleryLoaded = function($this) {
	$($this + ' .captions').prependTo($this + ' .navBar');
	$($this + ' .titles').prependTo($this + ' .navBar');
	
	$($this + ' .captionWrapper').jScrollPane()
	
	$($this + ' .navBar').prepend('<div class="propertyInfoRight"><div class="galleryContactBtn"><a href="page.htm?cpi_id=549">Contact</a></div><div id="toolbox'+propertiesLoaded+'" class="addThisToolbox"><A class=addthis_button_facebook></A><A class=addthis_button_twitter></A><A class=addthis_button_linkedin></A><A class=addthis_button_email></A></div></div>').append('<div style="clear:both;"></div>');
	
	addthis.toolbox('#toolbox'+propertiesLoaded);
	
	propertiesLoaded++;
	
	if(!$($this).hasClass('on'))
	{
		$($this).hide();
	}
	
}

var galleryLoaded = function() {
	$('.titles').wrap('<div class="titlesWrapper"></div>');
	
	$('.titles').width(1092 * $('.titleWrapper').length * galleryTitleScale);
	
	$('.titleWrapper').css('marginLeft', 60);
	
	titleStep = 1092 * galleryTitleScale;
	
	$('.titleWrapper').each(function(){
		
		$(this).prepend('<div class="galleryTitleRight"></div><div class="galleryTitleLeft"></div>').append('<dev style="clear:both;"></div>');
		
		$(this).css('marginRight', titleStep - $(this).width() - 60);
		
	});
	
	
	// 60
}

var galleryTitleAnimate = function(num) {
	
	$('.titles').stop().animate({marginLeft: -(titleStep*num)}, galleryAnimSpeed, 'swing');
	
}

var initManagement = function() {
	
	$('.hotspot').hover(hotspotOver, hotspotOut);
	$('.managementBtn').hover(managementBtnOver, managementBtnOut);
	
	$('#darkOverlay').css({'opacity': '0'});
	$('.person').css({'opacity': '0'});
	$('.managementBtn').css({'opacity': '0'});
	
	
}

var hotspotOver = function() {
	var id = $(this).attr('id');
	var idNum = id.split('hotspot');
	idNum = idNum[1];
	fadePersonIn(idNum);
}

var hotspotOut = function() {
	var id = $(this).attr('id');
	var idNum = id.split('hotspot');
	idNum = idNum[1];
	fadePersonOut(idNum);
}

var managementBtnOver = function() {
	var id = $(this).attr('id');
	var idNum = id.split('managementBtn');
	idNum = idNum[1];
	fadePersonIn(idNum);
}

var managementBtnOut = function() {
	var id = $(this).attr('id');
	var idNum = id.split('managementBtn');
	idNum = idNum[1];
	fadePersonOut(idNum);
}

var fadePersonIn = function(idNum) {
	
	$('#darkOverlay').stop().css({'display': 'block'}).animate(
			{opacity: 0.7},
			300,
			'easeInOutSine');
	
	$('#person'+idNum).stop().css({'display': 'block'}).animate(
			{opacity: 1},
			300,
			'easeInOutSine');
	
	$('#managementBtn'+idNum).stop().css({'display': 'block'}).animate(
			{opacity: 1},
			300,
			'easeInOutSine');
}

var fadePersonOut = function(idNum) {
	
	$('#darkOverlay').stop().animate(
			{opacity: 0},
			300,
			'easeInOutSine');
	
	$('#person'+idNum).stop().animate(
			{opacity: 0},
			300,
			'easeInOutSine');
	
	$('#managementBtn'+idNum).stop().animate(
			{opacity: 0},
			300,
			'easeInOutSine',
			function() {
				$(this).css({'display': 'none'});
			}
	);
	
}

