$(function(){

	// ***
	// Scrolling background
	// ***
		
	// height of background image in pixels
	var backgroundheight = 4000;
	
	// get the current minute/hour of the day
	var now = new Date();
	var hour = now.getHours();
	var minute = now.getMinutes();
	
	// work out how far through the day we are as a percentage - e.g. 6pm = 75%
	var hourpercent = hour / 24 * 100;
	var minutepercent = minute / 60 / 24 * 100;
	var percentofday = Math.round(hourpercent + minutepercent);
	
	// calculate which pixel row to start graphic from based on how far through the day we are
 	var offset = backgroundheight / 100 * percentofday;

	// graphic starts at approx 6am, so adjust offset by 1/4
	var offset = offset - (backgroundheight / 4);

	function scrollbackground() {
		// decrease the offset by 1, or if its less than 1 increase it by the background height minus 1
   		offset = (offset < 1) ? offset + (backgroundheight - 1) : offset - 1;
		// apply the background position
   		$('body').css("background-position", "50% " + offset + "px");
   		// call self to continue animation
   		setTimeout(function() {
			scrollbackground();
			}, 100
		);
   	}
    
	// Start the animation
	scrollbackground();
	
	
	// ***
	// Fully clickable divs
	// ***
	
	$("div.clickable").click(
	function()
	{
	    window.location = $("a", this).attr("href");
	    return false;
	});

	// ***
	// Leading image expander
	// ***

	$('.leading').hover(
		function() {
			if ($('img', this).height() > 300) {
				$(this).animate({
					height: $('img', this).height()
				}, 300, 'swing');
			}
		},
		function() {
			$(this).animate({
				height: "300px"
			}, 200);
		}	
	);	


	// ***
	// Contact form validation
	// ***
	
	var imgtick = 'images/icon-tick.png';
    var imgcross = 'images/icon-cross.png';
	var validname = false;
	var validemail = false;
	var validcomments = false;
	
    function delaytimer() { 
        var timer; 
        return function(func, time) { 
            clearTimeout(timer); 
            timer = setTimeout(func, time); 
        }; 
    } 	
	
	function validatecontact() {
		
		if ($('#contact-name').val().length) {
			$('img#contact-name-status').fadeIn();
		} else {
			$('img#contact-name-status').fadeOut();
		}
		if ($('#contact-name').val().length > 2) {
			$('img#contact-name-status').attr('src', imgtick);
			validname = true;
		} else {
			$('img#contact-name-status').attr('src', imgcross);
			validname = false;
		}
		
		if ($('#contact-email').val().length) {
			$('img#contact-email-status').fadeIn();
		} else {
			$('img#contact-email-status').fadeOut();
		}
		if ($('#contact-email').val().match(/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$/)) {
			$('img#contact-email-status').attr('src', imgtick);
			validemail = true;
		} else {
			$('img#contact-email-status').attr('src', imgcross);
			validemail = false;
		}

		if ($('#contact-comments').val().length) {
			$('img#contact-comments-status').fadeIn();
		} else {
			$('img#contact-comments-status').fadeOut();
		}
		if ($('#contact-comments').val().length > 10) {
			$('img#contact-comments-status').attr('src', imgtick);
			validcomments = true;
		} else {
			$('img#contact-comments-status').attr('src', imgcross);
			validcomments = false;
		}
		
		return (validname && validemail && validcomments);  
	
	}
    
	var validationdelay = delaytimer();
	
	$('#contact-name, #contact-email, #contact-comments').bind('keyup', function() {
		validationdelay(validatecontact, 500);
	});
	
	validatecontact();
	
	$('#contact-form').bind('submit', function() {
		var valid = validatecontact(); 
		if (!valid) {
			if (!validname) {
				alert('Please enter your name.');
				return false;
			}
			if (!validemail) {
				alert('Please enter a valid email address.');
				return false;
			}
			if (!validcomments) {
				alert('Please enter some comments.');
				return false;
			}
		}
	})
	
	// light_rounded / dark_rounded / light_square / dark_square
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'dark_rounded' 
	});
			
	$('#foliothumbs').cycle();
	
});

var blank = new Image();
blank.src = '/template/theme/youloveus/img/blank.gif';

$(document).ready(function() {
	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if (badBrowser) {
	 // get all pngs on page
	 $('img[src$=.png]').each(function() {
	   if (!this.complete) {
	     this.onload = function() { fixPng(this) };
	   } else {
	     fixPng(this);
	   }
	 });
	}
});

function fixPng(png) {
	// get src
	var src = png.src;
	// set width and height
	if (!png.style.width) { png.style.width = $(png).width(); }
	if (!png.style.height) { png.style.height = $(png).height(); }
	// replace by blank image
	png.onload = function() { };
	png.src = blank.src;
	// set filter (display original image)
	png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}
	