//Variables you can control outside this plugin
settings = $.extend({
	indexStart		: 0,
	timeOut			: 5000,
	transition		: 750,
	transitionClick	: 750
});


var isTimeout = false;
var oldpIndex;
var newpIndex;
var timer;
var videoOn;
var control;
var timerOn		= 0;
var loading		= false;
var element 	= this;
var item 		= $("#newsSlider li");
var count 		= item.length;
var pIndex 		= settings.indexStart;
if ($("ul#control").html() == "") {
	for (var i=0; i < count; i++) {
		$("ul#control").append("<li><a href='' alt='"+i+"'>"+(i+1)+"</a></li>");
	}
}
var control		= $("#control li");
if (item.length > 1) {
	isTimeout = true;
}
		

item.css("opacity", 0);
$("#text").hide();
$(item).find("h3").hide();
$(item).find("h2").hide();
$(item).find("p").hide();


$("ul#control li a").click(function (ev) {
	videoOn = false;
	clearTimer();
	ev.preventDefault();
	newpIndex = $(this).attr("alt");
	if (pIndex != newpIndex) {
		givenImg(settings.transitionClick);
	}
});


function transitVideo (transSpeed) {
	$(".media-target").html("");
	setActiveState();
	var text = "<h3>" + $(item[pIndex]).find("h3").html() + "</h3><h2>" + $(item[pIndex]).find("h2").html() + "</h2><p>" + $(item[pIndex]).find("p").html() + "</p>";
	link = $(item[pIndex]).find("img").attr("alt");
	src = $(item[pIndex]).find("a.video").attr("href");
	$(item[pIndex]).find("img").css({opacity: 0});
	newI = pIndex+1;
	if (pIndex > (count - 1)) {
		newI = 0;
	}
	$(item[pIndex]).find(".media-target").flash({
		swf: '/flash/film_press.swf',
		height: 334,
		width: 590,
		flashvars: {
			film: src,
			index: newI
		}
	});
	
	$("#newssliderReadmore").fadeOut(function () {
		$(this).html(link).fadeIn();
	});
	$("#text").fadeOut("fast", function () {
		$(this).html(text).fadeIn("slow");
	});
	$(item[oldpIndex]).animate({ "opacity": 0 }, transSpeed, "linear");
	$(item[pIndex]).animate({ "opacity": 1 }, transSpeed, "linear", function () {
		clearTimer();
		timeout();
	});
}

function givenImg (transSpeed) {
	oldpIndex = pIndex;
	pIndex = newpIndex;
	if ($(item[pIndex]).attr("alt") == "video") {
		transitVideo(transSpeed);
	} else {
		transit(transSpeed);
	}
}

function firstImg (transSpeed) {
	oldpIndex = count - 1;
	if ($(item[pIndex]).attr("alt") == "video") {
		transitVideo(transSpeed);
	} else {
		transit(transSpeed);
	}
}

function nextImg (transSpeed) {
	loading = true;
	pIndex++;
	oldpIndex = pIndex - 1;
	if (pIndex > (count - 1)) {
		pIndex = 0;
	}
	
	if ($(item[pIndex]).attr("alt") == "video") {
		transitVideo(transSpeed);
	} else {
		transit(transSpeed);
	}
}


function transit(transSpeed) {
	$(".media-target").html("");
	setActiveState();
	var text = "<h3>" + $(item[pIndex]).find("h3").html() + "</h3><h2>" + $(item[pIndex]).find("h2").html() + "</h2><p>" + $(item[pIndex]).find("p").html() + "</p>";
	link = $(item[pIndex]).find("img").attr("alt");
	//alert(link);
	$("#newssliderReadmore").fadeOut(function () {
		$(this).html(link).fadeIn();
	});
	$("#text").fadeOut("fast", function () {
		$(this).html(text).fadeIn("slow");
	});
	$(item[oldpIndex]).animate({ "opacity": 0 }, transSpeed, "linear");
	$(item[pIndex]).animate({ "opacity": 1 }, transSpeed, "linear", function () {
		clearTimer();
		timeout();
	});
}


function setActiveState () {
	item.css("z-index", 0);
	$(item[pIndex]).css("z-index", 2);
	control.removeClass("active");
	$(control[pIndex]).addClass("active");
}


function timeout () {
	if (isTimeout) {
		if (!timerOn) {
			resetCounter();
			timer = setTimeout(function () {
				nextImg(settings.transition);
			}, settings.timeOut);
		}
	}
}


function clearTimer () {
	clearTimeout(timer);
}

function resetCounter () {
	counter = settings.timeOut /1000;
	$("#target").html(counter);
}


$("#newsSliderWrap").hover(function () {
	resetCounter();
	timerOn = 1;
	clearTimer();
}, function () {
	timerOn = 0;
	clearTimer();
	if (!videoOn) {
		timeout();
	}
})

$(document).ready(function () {
	$("#text").fadeIn();
	$("#control").fadeIn();
	$("#newsSlider li").fadeIn();
	firstImg(settings.transition);
});

function startMovie () {
	videoOn = true;
}

function videoDone () {
	videoOn = false;
	timeout();
}
