$(function(){	
	
	//reveal content - homepage
	if(!readCookie('homepage_intro')){
		$('#home_content').css({'left':'-900px'});
		$('#home_content').delay(3000).animate({
			'left':'0'
		},1000
		);
		createCookie('homepage_intro','1',365);
	}
	
	
	//showcase slider
	$('#showcase_controls a').click(function(){
		var $animSpeed = 1000;
		var $link = $(this);
		var $href = $link.attr('href');
		if($link.hasClass('disabled')) return false;
		disableControls();
		if ($link.hasClass('next')) {
			$('#content_slider').css({'left':'0','right':'auto'}).animate({'left':'-900px'},$animSpeed,function(){
				$(this).empty();
				loadShowcase($href,'next',$animSpeed);	
			});
		} else {
			$('#content_slider').css({'left':'auto','right':'0'}).animate({'right':'-900px'},$animSpeed,function(){
				$(this).empty();
				loadShowcase($href,'prev',$animSpeed);	
			});
		}
		return false;
	});
	
	//range page slideshow
	$('.img_range').hide();
	$('#img_range1').show();
	range_slideshow();
	function range_slideshow(){
		var delay = 3000;
		var fade = 1400;
		$('#img_range2').delay(delay).fadeIn(fade,function(){
			$('#img_range3').delay(delay).fadeIn(fade,function(){
				$('#img_range2').hide();
				$('#img_range3').delay(8000).fadeOut(fade,function(){
					range_slideshow();
				})
			});
		});
	}
	
	//form help popups
	$('.form_help').click(function(){
		showTooltip($(this));
		return false;
	});
	$('#tt_container').live('click',function(){
		$(this).hide();
	});
	
	function showTooltip(btnObj){
		var offset = btnObj.offset();
		inputDiv = btnObj.parents('div.input');
		tipName = inputDiv.find('input, select, textarea').attr('id');
		label = inputDiv.find('p.label').length ? inputDiv.find('p').text() : inputDiv.find('label').text();
		tipText = '<p><strong>' + label + '</strong></p>';
		tipText += $("div[id^=help_" + tipName + "]").html();		
		$('#tt_content').html(tipText);
		tipHeight = $('#tt_container').height();
		$('#tt_container').css({'top': offset.top -(tipHeight/2),'left': offset.left -140}).fadeIn(200);
	}
	
	/*********** Validation **********/
	//   ^\d+(?:\.\d{1,2})?$
	jQuery.validator.addMethod("decimal", function(value, element) { 
		return /^\d+\.\d{1}$/.test(value); 
	}, "Must be to 1 decimal place");


	
	$("#quote_form").validate({
		errorPlacement: function(error, element) {
			error.appendTo( element.parents("div.input") );
		},
		rules: {
			application : {required :true},
			obscurity : {required :true},
			clarity : {required :true},
			perimeter_detail_left : {required :true},
			perimeter_detail_right : {required :true}
		}
	});
    
    $("#brochure_form").validate();


	
	
});

function loadShowcase($href, $button, $animSpeed){
	$.get($href, {}, function(data) {
	    var $response = $('<div />').html(data.replace(/<script(.|\s)*?\/script>/g, ""));
	    var $content = $response.find('#content')
	    var $trans = $response.find('#content_trans');
		if ($button == 'next') {
			$('#content_slider').css({'left':'auto','right':'-900px'}).append($content).append($trans).delay(500).animate({'right':'0'},$animSpeed,function(){
				updateControls($href);
			});
		} else {
			$('#content_slider').css({'left':'-900px','right':'auto'}).append($content).append($trans).delay(500).animate({'left':'0'},$animSpeed,function(){
				updateControls($href);
			});
		}
		;	    
	},'html');
}

function disableControls(){
	$('#showcase_controls a').addClass('disabled');
}

function updateControls($href){
	var $pageNo = $href.split('-');
	var $pageNo = parseInt($pageNo.pop());
	$('#showcase_controls a').removeClass('disabled');
	if($pageNo >= 2 && $pageNo <= 4){		
		$('#showcase_controls .next').attr('href',$href.replace($pageNo,$pageNo+1));
		if($pageNo == 2){
			$('#showcase_controls .prev').attr('href','inspiration');
		} else {
			$('#showcase_controls .prev').attr('href',$href.replace($pageNo,$pageNo-1));
		}		
	}
	else if($pageNo == 5){
		$('#showcase_controls .next').addClass('disabled');
		$('#showcase_controls .prev').attr('href',$href.replace($pageNo,$pageNo-1));
	}
	else {
		$('#showcase_controls .prev').addClass('disabled');
		$('#showcase_controls .next').attr('href','inspiration-part-2');
	}
}

//cookies
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}



