/*=============================================================
Type:				Fresh Invest main JS file
Created by:			Strawberrysoup
Contact website:	http://strawberrysoup.co.uk/
Copyright: 			(c) 2008 Strawberrysoup	
---------------------------------------------------------------
Creation Date:		04-08-2008
=============================================================*/

var FI = function(){
	
	var initHomepage = function()
	{
		var openAccordianItem = null;
		
		$("#why_fresh_invest p").hide();
		$("#why_fresh_invest h3").click(function(){
			
			if ( openAccordianItem ) openAccordianItem.next("p").hide("medium");
			$(this).next("p:hidden").show("medium");
			openAccordianItem = $(this);
		}).parent("li").hover(function(){
			$(this).addClass("hover");
		}, function(){
			$(this).removeClass("hover");
		})
	}
	
	var initPropertyList = function()
	{
		if ( $("#available").size() )
		{
			$("#sold").hide();
			$("#property_status_switcher a[href=#available]").addClass("current");			
		}
		else
		{
			$("#available").hide();
			$("#property_status_switcher a[href=#sold]").addClass("current");
		}

		$("#property_status_switcher a").click(function(){
			if ( ! $(this).hasClass("current") )
			{
				$("#property_status_switcher a").removeClass("current");
				$(this).addClass("current");
				
				var statename = $(this).attr("href").replace("#","");

				$(".property_list").hide();
				$("#"+statename).show();
			}
		});
		
	}
	
	var initNavMenu = function()
	{
		
		$('#main_nav li#nav_international').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		})
		
		/*
		$("#main_nav li#nav_uk, #main_nav li#nav_uk li, #main_nav li#nav_international, #main_nav li#nav_international li, #main_nav li#nav_commercial,  #main_nav li#nav_alternative, #main_nav li#nav_alternative li, #main_nav li#nav_commercial").hover(function(){
			
			var par = $(this).parent("ul");
			
			par.children("li").removeClass("hover");
			$(this).addClass("hover");
			
			if ( par.parent("li").size() )
			{
				// second level menu item, hide the quote 
				$("#preview blockquote").hide();
			}
			else
			{
				// top level menu item
				if ( !$(this).find("ul li.hover").size() && ! $(this).parent("ul").hasClass("prop_summary_details")  )
				{
					// show the generic text description for this item
					$("#preview blockquote").show();
					$("blockquote.quotes").hide();
					$("blockquote#"+$(this).attr("id")+"_quote").show();
				}
				else
				{
					$("#preview blockquote").hide();
				}
			}
			
		},function(){
			
			// if ( $.browser.msie || $.browser.safari ) $("li li.hover").removeClass("hover");
			
		});
		$("#preview blockquote").hide();
		
		$("#main_nav li#nav_home, #main_nav li#nav_contact, #main_nav li#nav_register").hover(function(){
				$("#preview blockquote").show();
				$("#main_nav li").removeClass("hover");	
				$("blockquote.quotes").hide();
				$("blockquote#generic_quote").show();
			
		},function(){});
		// $("#main_nav #nav_uk").addClass("hover");
		
		// $("#main_nav #nav_uk li:first-child").addClass("hover");
			$("#preview blockquote").show();
			$("#main_nav li").removeClass("hover");	
			$("blockquote.quotes").hide();
			$("blockquote#generic_quote").show();
		*/
	}
	
	var scrolling;
	
	var initProperyImageScroller = function()
	{
		
		if ( $(".morepics").size() <= 3 )
		{
			$("#pics_scroller").hide();
		}
		else
		{		
			$("#scrollup").mousedown(function(){
			
				scrolling = setInterval(function(){
					if ( (-1 * parseInt($("#more_images").css("top"))) < parseInt($("#more_images_wrapper").height()) - 62 )
					{
						$("#more_images").css( "top", parseInt($("#more_images").css("top")) - 2 );	
					}
					else
					{
						clearInterval(scrolling)	
					}
				}, 20);				
			
			}).mouseup(function(){
				clearInterval(scrolling)
			});
		

			$("#scrolldown").mousedown(function(){
						
				scrolling = setInterval(function(){
					if ( parseInt($("#more_images").css("top")) < 0 )
					{
						$("#more_images").css( "top", parseInt($("#more_images").css("top")) + 2 );
					}
					else
					{
						clearInterval(scrolling)
					}
				}, 20);

			}).mouseup(function(){
				clearInterval(scrolling)
			});			
		}

	}
	
	var fixIE = function()
	{
		$("#request_callback").val("");
		$("form").submit(function(){
			$("#request_callback").val("Request a callback");
			return true;
		});
		$("input[type=text]").addClass("text");
	}
	
	var initLoginForm = function()
	{
		$body = $("#header_wrapper,#content_wrapper,#footer");
		var $loginform = $("#login_form");
		var $loginform_button = $("#login_tab a");

		if ( !$("form.open").size() )
		{

			$loginform.css({ "height" : "0" });
			if ( $.browser.msie && $.browser.version == 6 ) $loginform.hide();
			login_open = false;
			
		}

		$loginform_button.click(function(){
			if ( login_open )
			{
				if ( $.browser.msie && $.browser.version == 6 ) $loginform.slideUp();
				else $loginform.animate({ "height" : "0" });
				login_open = false;
			}
			else
			{
				if ( $.browser.msie && $.browser.version == 6 ) $loginform.slideDown();
				else $loginform.animate({ "height" : "50px" });
				login_open = close;
			}
			return false;
		});

		$body.click(function(e){
			if ( login_open )
			{
				if ( $.browser.msie && $.browser.version == 6 ) $loginform.slideUp();
				else $loginform.animate({ "height" : "0" });
				login_open = false;
			}
		});
		
	};

	return {
		
		init : function()
		{
			initHomepage();
			initPropertyList();
			initNavMenu();
			
			initLoginForm();
			
			initProperyImageScroller();
			
			if ( $("#sub_nav li.current").size() > 1 )
			{
				$("#sub_nav li.current").eq(0).removeClass("current");
			}
			
			if ( $.browser.msie )
			{
				fixIE();
			}
			
			$("#login_username").DefaultValue("enter email address");
			$("#login_password").DefaultValue("enter password");
			
			$(".tanc").click(function(){
				var win = window.open( $(this).attr("href"), "tandc", "width=900, height=500, scrollbars=1" )
				return false;
			});
			
			$('a[title=external]').click(function(){
		
				window.open(this.href);
				
				return false;
			});
			
		}
		
	}
	
}();

$(function(){ 
	FI.init();
	
	// Home-page rotating banners
	(function() {
		
		var $rotator = $('#rotating_banners'),
			$images  = $rotator.find('img');
			
		// If there isn't a rotator or is only one
		// image don't do anything
		if( !$rotator.length || $images.length < 2 ) {
			return;
		}
		
		var showing = 0;
		$rotator.find('img:gt(0)').hide();
		
		var showNext = function() {
			// Hide currently showing one
			$images.eq(showing).fadeOut(1500);
			
			showing = ($images.length == (showing + 1)) ? 0 : showing + 1;;
			$images.eq(showing).fadeIn(1500, function() {
				setTimeout(showNext, 5000);
			});
		};
		
		setTimeout(showNext, 5000);
		
	})();
	
	(function() {
		var phrases = [
				'Access to off market property',
				'Buyer guides for all countries',
				'Latest Investments emailed direct',
				'Dedicated Investment Specialists'
			],
			showing = 0,
			interval = 5000,
			$elem = $('#new_signup_banner h4'),
			showNext = function() {
				$elem.fadeOut(750, function() {
					showing++;
					showing = showing % phrases.length;
					
					$elem.text(phrases[showing]).fadeIn(750, function() {
						setTimeout(showNext, interval);
					});
				});
			};
		
		setTimeout(showNext, interval);
	})();
	
	var page1 = $('.content_left .home-slide-pagination:first'),
		page2 = $('.content_left .home-slide-pagination:last');
	
	$('.content_left .home-slide-pagination').click(function() {
		if( this == page1[0] ) {
			page1.removeClass('nonactive');
			page2.addClass('nonactive');
		} else {
			page2.removeClass('nonactive');
			page1.addClass('nonactive');
		}
		
		$('#slide-side-home-banners').animate({
			scrollTop: (this == page1[0]) ? 0 : 310
		});
		
		return false;
	});
	
});

$(window).load(function() {
	$('.property-tabs > ul a').click(function() {
		$('.property-tabs > ul a.active').removeClass('active');
		$(this).addClass('active');
		$('.property-tabs > *:not(ul)').hide();
		
		
		
		$(this.href.match(/#.*/)[0].replace('_tab', '')).show();
		
		//return false;
	});
	
	if( window.location.hash && window.location.hash.match('_tab') ) {
		$( '.property-tabs a[href$="' + window.location.hash + '"]' ).click();
	} else {
		$('.property-tabs > ul a:first').click();
	}

	$('#main_nav ul').each(function() {
		$(this).width( $(this).prev().outerWidth() - 1 );
	});
});

