$(function() {
	
	// Set background image
	// var randomImages = ['bg1','bg2'];
	// var rndNum = Math.ceil(Math.random() * randomImages.length);
	// $("body").css({
	// 	background : "url(/templates/tyrian/images/framework/background/bg_" + rndNum + ".jpg) -63px 0 no-repeat"
	// });

	// Load cufon
	Cufon.replace('.toplevel > a',{
	    hover: true
	});
	
	Cufon.replace('.feature_content h3');

	$('div.list_item').hover(
		function(){
			$('.listhover', this).show();
		},
		function(){
			$('.listhover', this).hide();
		}
	);
	

	/* 
	----------------------
	Footer animation functions
	----------------------
	*/
	
	// Global vars
	footerOpen = false;
	currentItem = "default";
	vipOpen = false;
	subscribeOpen = false;
	playing = true;

	var openFooter = function(){
		$('#footer_max').animate({ opacity : "show", bottom : "0" },{ easing: 'easeOutCirc', duration: 600 });
		$('#footer_max').addClass("open");
		Cufon.replace('.feature_content h3');
		Cufon.now();
	};

	var closeFooter = function(){
		$('#footer_max').animate({ bottom : "-176px", opacity : "hide" },{ easing: 'easeInCirc', duration: 500 } );
		$('#footer_max').removeClass("open");
	};
	
	var galleryScroll = function(playing){
		var playing;
		if(playing == true){
			currentItem = "gallery";
			$('ul#footer_items').animate({ left : "-2352px" },{ easing: 'linear', duration: 25000 } );
			$('ul#footer_items').queue( function(){
				if(playing == true){
					$(this).css("left", "0");
					$(this).dequeue();
					galleryScroll(true);
				};
			});
			
		} else {
			$('ul#footer_items').stop( function(){
				closeFooter();
				footerOpen = false;
				playing = false;
				currentItem = "contact"
			});
			
		};
		
	};
	
	var contactItem = function(){
		$('#footer_max').queue( function(){
			galleryScroll(false);
			playing = false;
			$('ul#footer_items').css( "left", "-4704px" );
			openFooter();
			footerOpen = true;
		});
		currentItem = "contact";
	};
	
	var showSubscribe = function(){
		$('#subscribe').fadeIn();
	};
	
	var hideSubscribe = function(){
		$('#subscribe').fadeOut();
	};
	
	var closeSubscribe = function(){
		if(subscribeOpen == true)
		hideSubscribe();
	};
	
	var showVIP = function(){
		$('#viplogin').fadeIn();
	};
	
	var hideVIP = function(){
		$('#viplogin').fadeOut();
	};
	
	/* 
	----------------------
	footer maximised functions
	----------------------
	*/

	$('a.contact_btn').click(
		function(){
			if(footerOpen == false){
				$('ul#footer_items').css("left", "-4702px");
				openFooter();
				currentItem = "contact";
				footerOpen = true;
			} else if(footerOpen == true && currentItem == "contact"){
				closeFooter();
				footerOpen = false;
			} else if(footerOpen == true && playing == true){
				closeFooter();
				contactItem();
				playing = false;
			}

			//contactItem();
			closeSubscribe();
			subscribeOpen = false;
			return false;
	});

	
	$('a.vip_btn').toggle(
		function(){
			showVIP();
			vipOpen = true;
			return false;
		},
		function(){
			hideVIP();
			vipOpen = false;
			return false;
		}
	);
	
	$('a.subscribe_btn').click( function(){
		if(subscribeOpen == false){
			showSubscribe();
			subscribeOpen = true;
		} else if(subscribeOpen == true){
			hideSubscribe();
			subscribeOpen = false;
		}
		return false;
	});
	
	if( $('body').hasClass("homepage") ){
		openFooter();
		footerOpen = true;
		galleryScroll(true);
		currentItem = $('a.specials_btn');
	};

	// Remove factsheet download if empty
	$('.download a').each(function (){
		if($(this).attr('href') == "") {
			$(this).remove();
		}
	});

	// Hide popups
	$('.hover_btn a').each(function (){
		hoverParent = $(this).parents('.list_item');
		if($(this).attr('href') == "" || $(this).attr('href') == "#" ) {
			hoverParent.remove();
		}
	});

	// Clear subscribe fields
	$('#name').focus( function(){
		var nameVal = "NAME";
		if( $('input#name').val() == nameVal ){
			$('input#name').val("");
		}
	});

	$('#email').focus( function(){
		var emailVal = "EMAIL";
		if( $('input#email').val() == emailVal ){
			$('input#email').val("");
		}
	});
	
	$('a.nav_back_btn').attr("href", "#");
	$('a.nav_back_btn').click(function(){
		(window).history.go(-1);
		return false;
	});
	
	/* 
	----------------------
	Hide Search Bar if less than 3 items on listing template
	----------------------
	*/
	
	if($('div.list_item').size() <= 3) {
	    $('div#search').hide();
	}
	
	$('.caption_bg').css('opacity','0.5');
	$('.page_overlay').css('opacity','0.6');
	
	$('body a').each(function (){
		var rmslink1 = "https://www.rms.com.au/rmsopublicbookings/(S(az4pek553uyjf2uwxa05ecns))/pblogin.aspx?propcode=tyart&amp;agentcode=1";
		var rmslink2 = "https://www.rms.com.au/rmsopublicbookings/(S(az4pek553uyjf2uwxa05ecns))/pblogin.aspx?propcode=tyart&agentcode=1";
		if($(this).attr('href') == rmslink1 || $(this).attr('href') == rmslink2 ) {
			$(this).attr("onclick", "pageTracker._trackEvent('Book Now','list item');");
		}
	});

});