/**
 * @author Daniel Pickering
 */
var $j = jQuery;
jQuery.noConflict();



$j(function() {
	
	//Do homepage swf load
	if ($j('.page-home').length > 0) {
		//swfobject.embedSWF(swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj, callbackFn)
		swfobject.embedSWF("carousel.swf", "carousel", "700", "590", "9.0.0", "", "",  {wmode:"transparent"});
	}	
	
	//set the pane to open on agents and branches page
	if ($j('.page-agents, .page-branches').length > 0 && window.location.hash != "") {
		//find if hash matches on Agent name (under .branch)
		$j('a[name="' + window.location.hash.substr(1) + '"]').eq(0).closest('.branch').prev('h3').addClass('branch-selected');
		
		//find if hash matches on Branch name
		$j('a[name="' + window.location.hash.substr(1) + '"]').eq(0).parent('h3').addClass('branch-selected');
	}

	// Accordion for agents and branches
	if ($j('.page-agents, .page-branches').length > 0) {
		$j(".expandy").accordion({ 
			header: "h3",
			active: ".branch-selected",
			fillspace: false,
			collapsible: true,
			autoHeight: false,
			navigation : false
		 });
	 }
	 
	 //Goto hash on agents and branches (code above opens the branch pane)
	 if ($j('.page-agents, .page-branches').length > 0 && window.location.hash != "") {
		window.location.hash = window.location.hash;
	}
	 
	 //Tidy up full search GET params sent, don't bother sending them if they are defaults (any)
	//TODO : Disabled for now as has worse visual effect of clearing selects (also broken?)
	 /*$j(".full-search form").submit(function(){
	 	$(this).find('select[value=any]').attr('selectedIndex', -1);
		return true;
	 });*/

	//Disable bedrooms and bathrooms for sections
	var categoryCode = $j('select[name=categorycode]')
	var disableItems = $j('select[name=bedrooms], select[name=bathrooms]');
	
	categoryCode.change(function(){
		if (this.value == 'Section' || this.value == 'Rural Section') {
			disableItems.attr('disabled', 'disabled').attr('value', 'any');
		} else {
			disableItems.removeAttr('disabled');
		}
	})
	
	//Ensure they're disabled on page load, if set	
	if (categoryCode.attr('value') == 'Section' || categoryCode.attr('value') == 'Rural Section') {
		disableItems.attr('disabled', 'disabled').attr('value', 'any');
	}
	
	//Swap to rentals page if All Rentals set as property type on search
	 $j(".full-search form").submit(function(){
	 	if ($j(this).find('select[value=rental]').length > 0) {
			$j(this).attr('action', 'rentals');
		}
		return true;
	 });	

	//Listing Page specific JS
	if ($j('.page-listing').length) {
		//Set up prettyPhoto popup for listings
		$j(".page-listing a[rel^='prettyPhoto']").prettyPhoto({
			showTitle : false,
			theme : "light_square"
		});
		
			//Email friend code
		$j(".page-listing .email-friend-open").click(function(){
			$j('.email-friend').show();
			return false;
		})
	
		$j(".page-listing .email-friend-close").click(function(){
			$j('.email-friend').hide();
			return false;
		})
	
		//Do AJAX load on email friend form submit
		$j(".page-listing .email-friend form").submit(function() {
			$j('.email-friend form').addClass('loading');
			
			$j(".page-listing .email-friend-status").load(
				this.action,
				$j(this).serializeArray(),
				function() {
					$j('.email-friend').hide();
					$j('.email-friend form').removeClass('loading');
					$j('.email-friend-status').show();
				}
			)
			
			return false;
		});
	}
	
	//Listing Search page specific JS
	if ($j('.page-search').length) {
		
		//Clear the default value on click
		$j(".page-search .email-search form .email").click(function() {
			if (this.value == "Email") this.value = "";
		})
		
		//Register email search AJAX
		$j(".email-search form").submit(function(){
			$j('.email-search form').addClass('loading');
			$j(".email-search .email-search-status").load(
			this.action, 
			$j(this).serializeArray(), 
			function(){
				$j('.email-search form').removeClass('loading');
				
				//Remove the button + fields if succesful, we don't want them adding it again
				if ($j(this).find(".success").length) {
					$j('.email-search form input, .email-search form select').remove();
				}
			})
			
			return false;
		});
	}
	
	//Listing Search Manage page specific JS
	if ($j('.page-manage-email').length) {
	
		//Unsubscribe AJAX
		$j(".unsubscribe a").click(function(){
			$j(this).addClass('loading');
			$j(".unsubscribe").load(this.href);			
			return false;
		});	
	}			
	
	
})

