matchHeight=function(){
	var height = 0;
	if(document.getElementById('sub_nav') != null){
		height = document.getElementById('sub_nav').offsetHeight;
	}
	height = height + 70;
	var div=document.getElementById('content');
	if(div.offsetHeight < height){
		div.style.height = height+'px';
	}
}

window.onload=function(){
	if(document.getElementById('content') && !document.getElementById('form_insulation')){
		matchHeight();
		
		// dealing with captcha
		if($('#captcha').html() != null){
			refreshCaptcha();
		}
	}
}

$(document).ready(function(){
	
	$('.btn_save').click(function(){
		$("input[name='dahlsensStoreSelect']").each(function() {
			if($(this).attr('checked') == true) {
				setCookie('dahlsensStoreSelect',$(this).val(),365);
				$('.status').fadeIn("slow", function(){
					setTimeout('$(".status").fadeOut("slow")', 500);
				});
			}
		});
	});
	
	$('#btn_go').click(function(){
		var postcode = $('#postcode').val();
		
		if(postcode != '') {
			$.ajax({
				type: "POST",
				url: "ajax/find_branch.php",
				data: 'postcode='+postcode,
				success:function(res){
					if(res != ''){
						$('#pre_branch').html(res);
						
						var storeid = $('#hidden_storeid').html();
						setCookie('dahlsensStoreSelect',storeid,365);
						
						$('#finder').fadeOut("normal", function(){
							$('#pre_branch').fadeIn("normal");
						});
					}
				}
			});
		}
		
		return false;
	});
	
	// solutions container
	var sc_height = $('#solutions_container').height()-18-15-12;
	$('#solutions_container div').css('height',sc_height+'px');
	
	// homepage rotate images
	var numOfImages = $('#home_image').children('a').size();
	
	$('#home_image').children('a').slice(0,1).fadeIn("slow");
	if(numOfImages > 1) {
		setTimeout('photoRotation(0,1,'+numOfImages+')', 8000);
	}
	
	// insulation form
	if($('#form_insulation').html() != undefined){
		
		$('#q1').show();
		
		$("#form_insulation #step1_wrap input[type='radio']").click(function(){
			var currentClass = $(this).attr("class");
			currentClass = currentClass.split(" ");
			
			if(currentClass[0] == 'continue'){
				$('.error_items p').hide();
				$('#'+currentClass[1]).show();
				
				if(($("input[name='q7']:checked").val() != undefined) || ($("input[name='q8']:checked").val() != undefined)){
					$('#btn_gotostep2_wrap').fadeIn('slow');
				}
				
				if(currentClass[2]){
					$('#'+currentClass[2]).hide();
				}
			}else if(currentClass[0] == 'continueNext'){
				$('.error_items p').hide();
				$('#btn_gotostep2_wrap').fadeIn('slow');
				
				if(currentClass[2]){
					$('#'+currentClass[2]).hide();
				}
			}else{
				$('#'+currentClass[1]).fadeIn('slow');
				$('#btn_gotostep2_wrap').hide();
			}
		});
		
		$('#btn_gotostep2').click(function(){
			$('#step1_wrap').hide();
			
			$('#step2_wrap').fadeIn('slow');
		});
		
		$('#btn_backtostep1').click(function(){
			$('#step2_wrap').hide();
			
			$('#step1_wrap').fadeIn('slow');
		});
		
		$('#btn_backtostep2').click(function(){
			$('#step3_wrap').hide();
			
			$('#step2_wrap').fadeIn('slow');
		});
		
		$('#btn_gotostep3').click(function(){
			if($("input[name='roof_size']").val() == ''){
				$('#step2_wrap .error_items p').fadeIn('slow');
				return false;
			}
			
			var count = 0;
			$("#form_insulation #step2_wrap input[type='radio']:checked").each(function(){
				count++;
			});
			
			if(count == 6){
				$('#step2_wrap .error_items p').hide();
				$('#step2_wrap').hide();
				
				$('#step3_wrap').fadeIn('slow');
			}else{
				$('#step2_wrap .error_items p').fadeIn('slow');
			}
		});
		
	}

});

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function showStoreFinder(){
	$('#pre_branch').fadeOut("normal", function(){
		$('#finder').fadeIn("normal");
	});
}

function refreshCaptcha(){
	var tmp = new Date();
	$('#captcha').attr('src','captcha.inc.php?'+tmp.getTime());
	//alert('here');
}

function photoRotation(start, end, numOfImages) {
	$('#home_image').children('a').slice(start,end,numOfImages).fadeOut("slow", function(){
		
		if(end == numOfImages) {
			start = 0;
			end = 1;
		} else {
			start += 1;
			end += 1;
		}
		
		$('#home_image').children('a').slice(start,end,numOfImages).fadeIn("slow");
		setTimeout('photoRotation('+start+','+end+','+numOfImages+')', 8000);
	});
}