var alertTimerId = 0;
var featureNum = 2;
var rotationSpeed = 5000;

$(document).ready(function(){
	$(".image-rotate img").hide();
	
	var totalFeatureNum = $(".image-rotate img").size();
	featureNum = Math.floor(Math.random()*(totalFeatureNum+1));
	
	if(featureNum == 0)
		featureNum = 1;
	
	$(".image-rotate img:eq("+(featureNum - 1)+")").show();

	featureNum = featureNum + 1;
	if(featureNum > totalFeatureNum)
		featureNum = 1;
	
	var imageRotate = $(".image-rotate").size();
	if(imageRotate > 0) {
		alertTimerId = setTimeout("nextImage.show(featureNum)", rotationSpeed);
	}
	
	var autoplay = 'true';
	var is_autoplay = readCookie('apatel_player');
	if(is_autoplay == 'paused')
		autoplay = "false";
	
	$("#sounddl").jmp3({
		showfilename: "false",
		backcolor: "000000",
		forecolor: "ff0000",
		width: 25,
		showdownload: "false",
		repeat: "true",
		volume: "50",
		autoplay: autoplay
	});

	opt = '';
	$("ul.list li").each( function() {
		opt += '<option value="'+$(this).html()+'">'+$(this).html()+'</option>';
	});
	$("#event-listing").html(opt);
});

nextImage = {

	show : function(number) {
		var totalFeatureNum = $(".image-rotate img").size();

		$(".image-rotate img").fadeOut(1500);
		$(".image-rotate img:eq("+(number - 1)+")").fadeIn(1500);

		if(featureNum < totalFeatureNum)
			featureNum++;
		else
			featureNum = 1;

		alertTimerId = setTimeout("nextImage.show(featureNum)", rotationSpeed);
	}
}

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);
}