var spDuration = 7000,
    spAnimateTime = 1500,
    spInAnimation = false,
    spTO = null;
$(function(){
	//Homepage slider
	if($.prototype.nivoSlider){
		$('#mi').nivoSlider({
			pauseTime : 6000,
			animSpeed : 700
		});
	}


	//$('#sp_slides').width($('#sp_slides .sp_slide').length * 930);
	//spTO = setTimeout(spAnimate, spDuration)
	//$('#next_slide').click(spAnimate);

	//Ideals text slider
	/*
	setInterval(function(){
		$('#ideals li:visible').each(function(){
			$(this).hide(300, function(){
				var lis = $('li', $(this).parent()),
				    i,
				    ni;
				for(i = lis.length - 1; i >= 0; i--){
					if(lis[i] == this){
						ni = (i + 1) % lis.length;
						break;
					}
				}
				$(lis[ni]).show(300);
			})
		})
	}, 5000);
	*/

	//Ideals - cookies
	var current_set = parseInt(readCookie('spm_set')) || 0,
	    total_sets = $('#ideals .ideal:first li').length;
	createCookie('spm_set', (current_set + 1) % total_sets);
	$('#ideals li').hide();
	$('#ideals li.set' + current_set).show();
})
function spAnimate(){
	if(spInAnimation) return;
	spInAnimation = true;
	if(spTO) clearTimeout(spTO);
	$('#sp_slides').animate({marginLeft : '-930px'}, spAnimateTime, '', function(){
		var first = $('.sp_slide', this).get(0);
		$(first).detach();
		$(this).append(first);
		$(this).css({marginLeft : 0});
		spTO = setTimeout(spAnimate, spDuration);
		spInAnimation = false;
	});
}

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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
