Cufon.replace([".headerBanner .bannerLeft", ".headerBanner .bannerTitle", "#engguidelink", "#mainnavi li.active a", ".cufon_PTSansNarrow"], { fontFamily: "PT Sans Narrow" });
Cufon.replace(["#mainnavi li:not(.active) a"], { textShadow: "0 0 #007ccb", fontFamily: "PT Sans Narrow" });
Cufon.replace(["p.phone_no strong", "ul.footer-links a", "#footerlogo"], { fontFamily: "PT Sans" });

$(document).ready(function(){ 
	$(document).pngFix();
	
	// colorbox open-calls
	$("#engguidelink a").colorbox({ title: "Need service in English? Sure, let us help you!", close: "", transition: "none", speed: 0, width:"600", height:"95%", maxHeight: "680px", inline:true, href:"#engGuide .layerWrapper", opacity: 0.5});
	$(".layerClose").click(function() {
		$.colorbox.close();
	});
	
	//$(".layerHeader").corner();
	//$(".layerContent").corner();
	
	// update opened popup	
	setInterval (function() {
		if($('#colorbox img.popStatus')) { 
			var currentId = $('#colorbox .talentID').text(), 
			currentStatus = $('.talentstatus-' + currentId).attr("alt"); 
			
			$('#colorbox img.popStatus').attr('src', 'resources/com.elisa.nsm.taas.Talent/img/pop-talent-'+ currentStatus +'.png').attr('alt', currentStatus);
		}
	}, 5000);
	
	// banner switching
	if($(".bannerlist").length > 0) {
		setInterval (function() {
			var current, listLength = $(".bannerlist li").length;
			current = $(".bannerlist .active").attr("id");
			current = parseInt(current.substring(6));
			current = current + 1;
			
			if(current > listLength) {
				current = 1;
			}
				
			$(".bannerlist .active").removeClass("active");
			$("#banner"+current).addClass("active");
		}, 2000);
	}
	
	// check availability input, should contain only numerical characters
	if($(".availability_form").length > 0) {
		$(".availability_form").submit(function() {
			$("#searchResult").html("Saatavuus ja hinta: ---");
			
			$("#searchfield").filter(function() {
				$(this).addClass("input_error");
				$(".availability_error").show();
			    return this.value.match(/[0-9]{5}/);
			}).removeClass("input_error");
			
			if($("#searchfield").hasClass("input_error")) {
				$(".availability_error").show();
				return false;
			}
			else {
				$(".availability_error").hide();
				return true;
			}
		});
	}
	
	$("#orderlink").live("click", function() {
		$("#orderbtn").click();
	});
	
	// homeVisit order form, if order successfully sent, show popup
	if($(".homeVisitOrderForm").length > 0 && $("#orderConfirmationText:visible").length > 0) {
		$.colorbox({ open: true, title: "Kiitos tilauksestasi!", close: "", transition: "none", speed: 0, width:"600", height:"250px", inline:true, href:"#orderConfirmationText", opacity: 0.5});
		$(".layerClose").click(function() {
			$.colorbox.close();
		});
	}
	
	function datepickerInit() {
		var picker = "";
		
		$(".addDatePicker").datepicker({
			beforeShowDay: noSundays,
			minDate: 1,
			maxDate: 15
		});
		
		if($(".annualMaintenanceForm").length > 0) {
			picker = "annualMaintenance";
		} else if ($(".homeVisitOrderForm").length > 0) {
			picker = "homeVisit";
		}
		disableDays(picker);
				
		if($(".errorText:visible").length == 0) {
			$(".product_order_form").hide();
		}
		else {
			$("#orderbtn").attr("disabled", "disabled");
			$("#genError").show();
			location.href = location.href + "#genError";
		}
		
		if($("#orderConfirmationText:visible").length > 0) {
			$("#orderbtn").attr("disabled", "disabled");
		}
		
		$("#deliveryOptions").show();
		$("#orderbtn").show().click(function() {
			$(".product_order_form").slideDown();
			$("#orderbtn").attr("disabled", "disabled");
		});
	}
	if($(".addDatePicker").length > 0) {
		datepickerInit();
	}

	function noSundays(date) {
		// sundays closed
		if(date.getDay() == 0) {
			return [false, ''];
		}
		else return [true, ''];
	}
	
	function disableDays(picker) {
		var now = new Date(), nowHours, nowDay;		
		
		nowHours = parseInt(now.getHours());
		nowDay = parseInt(now.getDay());
		
		if (picker == "annualMaintenance") {
			// limit tomorrow if clock if over 16:00
			if(nowHours >= 16) {
				$(".addDatePicker").datepicker("option", "minDate", 2);
				$(".addDatePicker").datepicker("option", "maxDate", 16);
			}
		}
		
		if (picker == "homeVisit") {
			$(".addDatePicker").datepicker("option", "minDate", 2);
			$(".addDatePicker").datepicker("option", "maxDate", 16);
			
			// disable extra day if clock if over 19:00 on weekday
			if(nowHours >= 19 && nowDay > 0 && nowDay < 6) {
				$(".addDatePicker").datepicker("option", "minDate", 3);
				$(".addDatePicker").datepicker("option", "maxDate", 17);
			}
			
			// disable extra day if clock if over 16:00 on saturday
			if (nowHours >= 16 && nowDay == 6) {
				$(".addDatePicker").datepicker("option", "minDate", 3);
				$(".addDatePicker").datepicker("option", "maxDate", 17);
			}
		}
	}
}); 

