/**
 * jcarousel customisation for shs-group
 * SJW
 */

// the name if the cookie that is going to store the starting position of our carousel
var shs_cookie = 'shs_jcarousel_start';

// javascript cookie manipulation
// http://www.quirksmode.org/js/cookies.html
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;
}


// write the index of the first item displayed in the carousel to the cookie
function updateCookie(carousel, state){
	createCookie(shs_cookie, carousel.first);
}

jQuery(document).ready(function() {
	// start the carousel at it's previous index
	var start_pos = Number(readCookie(shs_cookie)) || 1;
    jQuery('#mycarousel').jcarousel({
        start: start_pos,
		itemLoadCallback: {
			onBeforeAnimation:	null,
			onAfterAnimation:	updateCookie	// maybe not the best callback to use but didn't want to modify jcarousel itself...
		}
    });
});
