var nCoef  = 1000;
var nTpl   = -1;
var nTpls  = 0;

$(document).ready(function() {
	var aTpl = $(".tpl");

	nTpls = aTpl.length - 1;

	aTpl.css("position", "absolute").each(function(key, obj) {
		$(this).css("display", "none");
		$(this).find(".zone .item").css("display", "none").attr("act", "0");
	});

	showTpl();

});

function showTpl() {
	var tpl, exp, tl;

	tpl = $(".tpl").filter(":eq("+nTpl+")");
	tpl.fadeOut(1000, function() {
		$(this).find(".zone .item").css("display", "none").attr("act", "0");
	});

	nTpl = (++nTpl>nTpls ? 0 : nTpl);

	tpl = $(".tpl").filter(":eq("+nTpl+")");
	tpl.fadeIn(1000).find(".item1").css("display", "block").attr("act", "1");

	exp = tpl.find("input:eq(0)").attr("value");
	tl  = tpl.find("input:eq(1)").attr("value");

	if(tl!="*") processTL(tl);

	setTimeout(function() { showTpl(); }, exp*nCoef);
}

function processTL(tl) {
	var acts = tl.split(":");
	for(i=0; i<acts.length; i++) {
		ainfo = acts[i].split(" ");
		setTimeout("changeItem('"+ainfo[1]+"', '"+ainfo[2]+"')", ainfo[0]*nCoef);
	}
}

function changeItem(zone, effect) {
	var zone, i2hide, i2show;
	tpl    = $(".tpl").filter(":eq("+nTpl+")").find("."+zone);
	i2hide = tpl.find(".item").filter("[@act=1]");
	i2show = i2hide.next();
	switch(effect) {
		case "fx1":
			i2hide.fadeOut("slow", function() {
				$(this).css("display", "none").attr("act", "0");
				i2show.fadeIn("slow").attr("act", "1");
			});
			break;
		case "fx2":
			i2hide.slideToggle("slow", function() {
				$(this).css("display", "none").attr("act", "0");
				i2show.slideToggle("slow").attr("act", "1");
			});
			break;
		case "fx3":
			i2hide.slideToggle("slow", function() {
				$(this).css("display", "none").attr("act", "0");
			});
			i2show.slideToggle("slow").attr("act", "1");
			break;
	}
}