/**
 * Moving Clouds script
 *
 * Copyright (c) 2010 Bart Kosse (www.bartkosse.nl)
 *
 * Version 0.1
 */

jQuery(document).ready(function($) {

		// Get BASE URL where visitor comes from
		referrerUrl = document.referrer.split( '/' );
		referrerHost = referrerUrl[2];
		// Get SITE URL for this site
		thisUrl = document.location.href.split( '/' );
		thisHost = thisUrl[2];
		
		// Check if BASE URL is same as SITE URL
		if (referrerHost == thisHost ) {
		   var background_pos = readCookie('background');
		   var midground_pos = readCookie('midground');
		   var foreground_pos = readCookie('foreground');
		}

	   var w = $(window).width();
	   
	   if (background_pos == null) {
	  	$('#background').css({backgroundPosition: w + 'px -30px'});
	   } else {
		$('#background').css({backgroundPosition: background_pos});
	   }
	  if (midground_pos == null) {
	    $('#midground').css({backgroundPosition: w + 350 + 'px 120px'});
	   } else {
	    $('#midground').css({backgroundPosition: midground_pos});
	   }
	  if (foreground_pos == null) {
		$('#foreground').css({backgroundPosition: w + 800 + 'px 280px'});
	   } else {
		$('#foreground').css({backgroundPosition: foreground_pos});
	   }

		$('#background').animate({
			backgroundPosition:"(-10000px -30px)"
		}, 1600000, 'linear');

		$('#midground').animate({
			backgroundPosition:"(-10000px 120px)"
		}, 1200000, 'linear');

		$('#foreground').animate({
			backgroundPosition:"(-10000px 280px)"
		}, 900000, 'linear');


    $('a').click(function() {
		var background_pos = $('#background').css('backgroundPosition') ;
		var midground_pos = $('#midground').css('backgroundPosition') ;
		var foreground_pos = $('#foreground').css('backgroundPosition') ;
		createCookie('background',background_pos,5);
		createCookie('midground',midground_pos,5);
		createCookie('foreground',foreground_pos,5);

    });


	});

// COOKIE FUNCTIONs
function createCookie(name,value,minutes) {
	if (minutes) {
		var date = new Date();
		date.setTime(date.getTime()+(minutes*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);
}

