$(document).ready(function() {
	//When the page loads we need to cheack what type of search has been selected and make sure the correct criteria are displayed
	initialctype = $(".header .subNav li input:radio:checked").val();
	//Update the main property type hidden field
	$("#property_type").val(initialctype);
	//Hide Number of bedrooms field for Commercial, Farms & Land searches
	if(initialctype == "3" || initialctype == "5" || initialctype == "4"){			
		$("#bedLabel").hide();
		$("#beds").hide();
	}else{
		$("#bedLabel").show();
		$("#beds").show();
	}
	//Hide Buy/Let radio button options for  Farms & Land searches
	if(initialctype == "5" || initialctype == "4"){			
		$("#isrental0").hide();
		$("#isrental1").hide();
		$("#isrental0label").hide();
		$("#isrental1label").hide();
	}else{
		$("#isrental0").show();
		$("#isrental1").show();
		$("#isrental0label").show();
		$("#isrental1label").show();
	}
		
	//rotation speed and timer
	var speed = 7000;
	var run = setInterval('rotate()', speed);	
	
	//grab the width and calculate left value
	var item_width = $('.flshBanner li').outerWidth(); 
	var left_value = item_width * (-1); 
        
    //move the last item before first item, just in case user click prev button
	$('.flshBanner li:first').before($('.flshBanner li:last'));
	
	//set the default item to the correct position 
	$('.flshBanner ul').css({'left' : left_value});

    //if user clicked on prev button
	$('#prev').click(function() {

		//get the right position            
		var left_indent = parseInt($('.flshBanner ul').css('left')) + item_width;

		//slide the item            
		$('.flshBanner ul:not(:animated)').animate({'left' : left_indent}, 400,function(){    

            //move the last item and put it as first item            	
			$('.flshBanner li:first').before($('.flshBanner li:last'));           

			//set the default item to correct position
			$('.flshBanner ul').css({'left' : left_value});
		
		});

		//cancel the link behavior            
		return false;
            
	});

 
    //if user clicked on next button
	$('#next').click(function() {
		
		//get the right position
		var left_indent = parseInt($('.flshBanner ul').css('left')) - item_width;
		
		//slide the item
		$('.flshBanner ul:not(:animated)').animate({'left' : left_indent}, 400, function () {
            
            //move the first item and put it as last item
			$('.flshBanner li:last').after($('.flshBanner li:first'));                 	
			
			//set the default item to correct position
			$('.flshBanner ul').css({'left' : left_value});
		
		});
		         
		//cancel the link behavior
		return false;
		
	});        
	
	//if mouse hover, pause the auto rotation, otherwise rotate it
	$('.flshBanner').hover(
		
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('rotate()', speed);	
		}
	); 
	
	$(".ctoDtlsMl a.shwlss").click(function () {
		var div = "." + $(this).attr("rel") + " > .ceoDtls";
		if ($(div).hasClass("show")) {
			$(this).attr("title", "Show more");
			$(this).html("&gt; Show more");
			$(".ceoDtls").removeClass("show");
			$(div).removeClass("show");
		}
		else {
			$(".ctoDtlsMl a.shwlss").attr("title", "Show more");
			$(".ctoDtlsMl a.shwlss").html("&gt; Show more");
			$(this).attr("title", "Show less");
			$(this).html("&gt; Show less");
			$(".ceoDtls").removeClass("show");
			$(div).addClass("show");
		}
		return false;
    });
	
	$(".ourclients .tptitle a.more").click(function () {
		var div = "." + $(this).attr("rel") + " > .allclients";
		if ($(div).hasClass("show")) {
			$(this).attr("title", "Show More");
			$(this).html("Show More");
			$(this).removeClass("less");
			$(this).addClass("more");
			$(".allclients").removeClass("show");
			$(div).removeClass("show");
		}
		else {
			$(".tptitle a").attr("title", "Show More");
			$(".tptitle a").html("Show More");
			$(".tptitle a").removeClass("less");
			$(".tptitle a").addClass("more");
			
			$(this).removeClass("less");
			$(this).addClass("more");
			$(this).attr("title", "Show Less");
			$(this).html("Show Less");
			$(this).removeClass("more");
			$(this).addClass("less");
			$(".allclients").removeClass("show");
			$(div).addClass("show");
		}
		return false;
    });
	
	$(".contAg1 .tptitle a.more").click(function () {
		var div = "." + $(this).attr("rel") + " > .lisTingsMain";
		if ($(div).hasClass("show")) {
			$(this).attr("title", "Show More");
			$(this).html("Show More");
			$(this).removeClass("less");
			$(this).addClass("more");
			$(".lisTingsMain").removeClass("show");
			$(div).removeClass("show");
		}
		else {
			$(".tptitle a").attr("title", "Show More");
			$(".tptitle a").html("Show More");
			$(".tptitle a").removeClass("less");
			$(".tptitle a").addClass("more");
			
			$(this).attr("title", "Show Less");
			$(this).html("Show Less");
			$(this).removeClass("more");
			$(this).addClass("less");
			$(".lisTingsMain").removeClass("show");
			$(div).addClass("show");
		}
		return false;
    });
	
	$(".ouNoff a.chlnk").click(function () {
		var div = "." + $(this).attr("rel") + " > .addcol";
		if ($(div).hasClass("show")) {
			$(this).attr("title", "Show More");
			$(this).html("Show More");
			$(this).removeClass("shl");
			$(this).addClass("shm");
			$(".ouNoff .addcol").removeClass("show");
			$(div).removeClass("show");
		}
		else {
			$(".ouNoff a.chlnk").attr("title", "Show More");
			$(".ouNoff a.chlnk").html("Show More");
			$(".ouNoff a.chlnk").removeClass("shl");
			$(".ouNoff a.chlnk").addClass("shm");
			
			$(this).attr("title", "Show Less");
			$(this).html("Show Less");
			$(this).removeClass("shm");
			$(this).addClass("shl");
			$(".ouNoff .addcol").removeClass("show");
			$(div).addClass("show");
		}
		return false;
    });
	
	$(".tsDtlsMl a.shwlss").click(function () {
		var div = "." + $(this).attr("rel") + " > .dvtmDtls";
		if ($(div).hasClass("show")) {
			$(this).attr("title", "Show more");
			$(this).html("&gt; Show more");
			$(".dvtmDtls").removeClass("show");
			$(div).removeClass("show");
		}
		else {
			$(".tsDtlsMl a.shwlss").attr("title", "Show more");
			$(".tsDtlsMl a.shwlss").html("&gt; Show more");
			$(this).attr("title", "Show less");
			$(this).html("&gt; Show less");
			$(".dvtmDtls").removeClass("show");
			$(div).addClass("show");
		}
		return false;
    });
	
	var $news = $('#clientticker');
	$news.serialScroll({
		items:'li',
		duration:1000,		
		force:true,
		axis:'y',
		easing:'linear',
		interval:1,
		step:2,
		cycle:true
	});
	
	$("#fadvice").change(function() {
		val = $(this).val();
		if (val != 0) {
			window.location = $(this).val();
		}
	});
	
	$("#foffice").change(function() {
		val = $(this).val();
		if (val != 0) {
			window.location = $(this).val();
		}
	});
	
	$("#fpeople").change(function() {
		val = $(this).val();
		if (val != 0) {
			window.location = $(this).val();
		}
	});
	
	$(".header .subNav li label").click(function() {
		$(".header .subNav li").removeClass("current");
		$(this).parent().parent().addClass("current");
		$(".header .subNav li input").removeAttr("checked");
		$(this).next().attr("checked","checked");
		var ctype = $(".header .subNav li input:radio:checked").val();
		//Update the main property type hidden field
		$("#property_type").val(ctype);
		//Hide Number of bedrooms field for Commercial, Farms & Land searches
		if(ctype == "3" || ctype == "5" || ctype == "4"){			
			$("#bedLabel").hide();
			$("#beds").hide();
			$("#beds").val("");
		}else{
			$("#bedLabel").show();
			$("#beds").show();
		}
		//Hide Buy/Let radio button options for  Farms & Land searches
		if(ctype == "5" || ctype == "4"){
			$(".btcolRad").hide();
			$(".btcolRad input:radio:checked").removeAttr("checked");
		}else{
			$(".btcolRad").show();
			$("#isrental0").attr('checked', 'checked');
		}
		$.ajax({
			type: "GET",
			url: "assets/snippets/gettypes/gettypes.php",
			data: ({pid : ctype}),
			success: function(msg){
				$("#type").html(msg);
			}
		});
		$.ajax({
			type: "GET",
			url: "assets/snippets/getprice/getpricemin.php",
			data: ({p : 0}),
			success: function(msg){
				$("#pricemin").html(msg);
			}
			});
		$.ajax({
			type: "GET",
			url: "assets/snippets/getprice/getpricemax.php",
			data: ({p : 0}),
			success: function(msg){
				$("#pricemax").html(msg);
			}
		});
		return false;
	});
	
	$(".btcolRad input:radio").change(function() {
		var val = $(this).val();
		if (!$(this).hasClass("spr")) {
			$.ajax({
				type: "GET",
				url: "assets/snippets/getprice/getpricemin.php",
				data: ({p : val}),
				success: function(msg){
					$("#pricemin").html(msg);
				}
			});
			$.ajax({
				type: "GET",
				url: "assets/snippets/getprice/getpricemax.php",
				data: ({p : val}),
				success: function(msg){
					$("#pricemax").html(msg);
				}
			});
		}
		else {			
			$.ajax({
				type: "GET",
				url: "assets/snippets/getprice/getpricemin1.php",
				data: ({p : val}),
				success: function(msg){
					$("#pricemin").html(msg);
				}
			});
			$.ajax({
				type: "GET",
				url: "assets/snippets/getprice/getpricemax1.php",
				data: ({p : val}),
				success: function(msg){
					$("#pricemax").html(msg);
				}
			});
		}
	});
	
	$("#stype").change(function() {
		//Update the main property type hidden field
		var ctype = $(this).val();
		//Hide Number of bedrooms field for Commercial, Farms & Land searches
		if (ctype == "3" || ctype == "5" || ctype == "4"){			
			$(".btcolRad").hide();			
			$(".rwcolbeds").hide();
			$("#bd").val("");
		}
		else {
			$(".btcolRad").show();
			$(".rwcolbeds").show();
		}
		//Hide Buy/Let radio button options for  Farms & Land searches
		if (ctype == "5" || ctype == "4"){			
			$(".btcolRad").hide();
			$(".btcolRad input:radio:checked").removeAttr("checked");
		}
		else {
			$(".btcolRad").show();
			$("#buy").attr('checked', 'checked');
		}
		
		$.ajax({
			type: "GET",
			url: "assets/snippets/gettypes/gettypes.php",
			data: ({pid : ctype}),
			success: function(msg){
				$("#typ").html(msg);
			}
		});
		$.ajax({
			type: "GET",
			url: "assets/snippets/getprice/getpricemin1.php",
			data: ({p : 0}),
			success: function(msg){
				$("#pricemin").html(msg);
			}
			});
		$.ajax({
			type: "GET",
			url: "assets/snippets/getprice/getpricemax1.php",
			data: ({p : 0}),
			success: function(msg){
				$("#pricemax").html(msg);
			}
		});
		return false;
	});
	
	$(".clphGalMn .phGl p img").hover(function() {
		$("#mainpropimg").attr("src", $(this).attr("rel"));
		return false;
	});
	
	var fNum = 0; 
	var lNum = 4;
	
	var chsize = $(".clphGalMn .phGl p").children("img.smallimgs").size() - 1;
	var cl = "#smallimg";
	
	$(".scrright").click(function() {
		if (lNum != chsize) {
			fId = cl + fNum;
			$(fId).css("display","none");
			fNum ++;
			lNum ++;
			if (lNum == chsize) {
				$(".scrright").addClass("scrright1");
			}
			$(".scrleft").removeClass("scrleft1");
		}
		return false;
	});
	
	$(".scrleft").click(function() {		
		if (fNum != 0) {
			fNum --;
			lNum --;
			fId = cl + fNum;
			$(fId).css("display","inline");
			if (fNum == 0) {
				$(".scrleft").addClass("scrleft1");
			}
			$(".scrright").removeClass("scrright1");
		}
		return false;
	});
	
	
// Branding switch stuff

	$('body').append('<a href="#switch" class="switch">Switch styles</a>');
 
	if($.cookie("css")) {
		$("link.switch").attr("href",$.cookie("css"));
	}
	$("a.switch").click(function() { 
		$("link.switch").attr("href","assets/templates/rea/css/branding2012.css");
		$.cookie("css","assets/templates/rea/css/branding2012.css", {expires: 365, path: '/'});
		return false;
	});

});

//a simple function to click next link
//a timer will call this function, and the rotation will begin :)  
function rotate() {
	$('#next').click();
}

