//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 newI;
var videoOn = false;
var control;
var timerOn		= 0;
var loading		= false;
var element 	= this;
var item 		= $("#trackslider li");
var count 		= item.length;
var pIndex 		= settings.indexStart;
for (var i=0; i < count; i++) {
	$("ul#tracksliderControl").append("<li><a class='png' href='' alt='"+i+"'>"+(i+1)+"</a></li>");
}
var control		= $("#tracksliderControl li");
if (item.length > 1) {
	isTimeout = true;
}
if (item.length == 0) {
	$("#tracksliderWrap").html("").css({background: "none", height: "100px"});
}


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


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


function givenImg (transSpeed) {
	oldpIndex = pIndex;
	pIndex = newpIndex;
	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") {
		transit(transSpeed);
	} else {
		transitVideo(transSpeed);
	}
}


function transit(transSpeed) {
	setActiveState();
	videoOn = false;
	var text = "<h3>" + $(item[pIndex]).find("h3").html() + "</h3><h2>" + $(item[pIndex]).find("h2").html() + "</h2>";
	$("#tracksliderText").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 transitVideo(transSpeed) {
	setActiveState();
	var text = "<h3>" + $(item[pIndex]).find("h3").html() + "</h3><h2>" + $(item[pIndex]).find("h2").html() + "</h2>";
	src = $(item[pIndex]).find(".media-target").attr("alt");
	$(item[pIndex]).find("img").remove();
	newI = parseFloat(pIndex)+1;

	if (newI > (parseFloat(count) - 1)) {
		newI = 0;
	}

	$(item[pIndex]).find(".media-target").flash({
		swf: '/flash/film_element.swf',
		height: 384,
		width: 678,
		flashvars: {
			film: src,
			index: newI
		},
		wmode: "transparent"
	});
	$("#tracksliderText").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 () {
	//$(control[oldpIndex]).find("a").removeClass("active");
	for (var i=0; i < count; i++) {
		if (i != pIndex) {
			$(control[i]).css({"z-index": 1}).find("a").removeClass("active");
		}
	}	
	$(control[pIndex]).css({"z-index": 2}).find("a").addClass("active");
}


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

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

function clearTimer () {
	clearTimeout(timer);
}

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


$("#tracksliderWrap").hover(function () {
	if (videoOn == true) {
		$("#trackbalk").animate({
			bottom: 0
		});
		$("#tracksliderText").fadeIn();
		$("#tracksliderControl").fadeIn();
	}
	resetCounter();
	timerOn = 1;
	clearTimer();
}, function () {
	if (videoOn == true) {
		$("#tracksliderControl").fadeOut();
		$("#trackbalk").animate({
			bottom: "-52px"
		});
	}
	timerOn = 0;
	clearTimer();
	timeout();
})

$(document).ready(function () {
	$("ul#trackslider li").fadeIn();
	$("#tracksliderControl").fadeIn();
	firstImg(settings.transition);
});

function startMovie () {
	videoOn = true;
	$("#tracksliderText").fadeOut("slow");
	$("#tracksliderControl").fadeOut();
	$("#trackbalk").animate({
		bottom: "-52px"
	});
}

function videoDone () {
	videoOn = false;
	$("#trackbalk").animate({
		bottom: 0
	});
	$("#tracksliderText").fadeIn();
	$("#tracksliderControl").fadeIn();
	timeout();
}