var timeLine = 0;

var moveabas = function(elemento) {
		if(elemento.attr("id") == "aba1") { var esquerda = "2"; }
		else { var esquerda = Number(elemento.prev().width()) + Number(elemento.prev().css("left").replace("px","")) +5; }

		elemento.animate({
		left: esquerda,
		bottom: 0
		}, 500, 'linear',
		function(){				
			if($(this).next().attr("id")) {
				moveabas($(this).next());
			} else { 
				$("#banner"+bannerAtivo+" embed,#banner"+bannerAtivo+" img").ready(function(){
					playFlash('mce'+bannerAtivo);
					if(playerAtivo == 1) { playerAtivo = 0; setTimeout("$('#playStop').click()",(bannerTime[1] *1000)); }
					return false;
				});
			}
		});
}

var loadBanners = function(){

	$("#vitrine .banner").each(function(){
		var thisid = $(this).attr("id").replace("banner","");

		if(bannerType[thisid] == "img"){
			$("#vitrine #banner"+thisid).css({
				backgroundImage: "url("+bannerSrc[thisid]+")"
			});
			$("#vitrine #banner"+thisid).attr("href",bannerHref[thisid]);
		} else {
			rwdFlash('mce'+thisid);
			stopFlash('mce'+thisid);
		}
	});
	
}

function playVitrine() {
	
	clearInterval(timeLine);

	if(bannerAtivo == $("#vitrine .banner").length) {
		var ativar = 1;
	} else {
		var ativar = Number(bannerAtivo) + 1;
	}

	timeLine = setInterval(function(){
		playVitrine(bannerAtivo);
	}, bannerTime[ativar] * 1000);

	$("#vitrine #aba"+ativar).click();
	
}

function stopVitrine() {
	clearInterval(timeLine);
}

$(function(){

	loadBanners();

	$("#vitrine #playStop").live("click", function(){
		if($(this).attr("class") == "off") {
			$(this).attr("class","");
			playVitrine();
		} else {
			$(this).addClass("off");
			stopVitrine();
		}
	});

	$("#vitrine li").live("mouseover",function(){
		$(this).addClass("on");
	});
	
	$("#vitrine li").live("mouseout",function(){
		if($(this).attr("id").replace("aba","") != bannerAtivo){
			$(this).removeClass("on");
		}
	});

	$("#vitrine li").live("click",function(){		
		if($(this).attr("id").replace("aba","") != bannerAtivo){
			if(bannerType[bannerAtivo] == "swf"){ stopFlash('mce'+bannerAtivo); }
			bannerAtivo = $(this).attr("id").replace("aba","");
			if($(this).attr("data-type") == "swf") {
				rwdFlash('mce'+bannerAtivo);
				playFlash('mce'+bannerAtivo);
			}
			$("#vitrine li").each(function(){ $(this).removeClass("on"); });
			$(this).addClass("on");
			$("#vitrine .banner").each(function(){
				if($(this).css("opacity") > 0) { $(this).animate({opacity: 0},1000); $(this).css({ zIndex: 1 }); }
			});
			$("#vitrine #banner"+bannerAtivo).animate({opacity: 1},1000);
			$("#vitrine #banner"+bannerAtivo).css({ zIndex: 2 });
		} else return false;
	});

	moveabas($("#aba1"));

});

/* Manipulação de MovieClips do Flash */
var getFlash = function (movieName) {
	if (window.document[movieName]) {
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1) {
		if (document.embeds && document.embeds[movieName])
		return document.embeds[movieName]; 
	}
	else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	{
	return document.getElementById(movieName);
	}
}

var stopFlash = function(movieName) {
	var flashMovie = getFlash(movieName);
	flashMovie.StopPlay();
}

var playFlash = function (movieName) {
	var flashMovie = getFlash(movieName);
	flashMovie.Play();
}

var rwdFlash = function(movieName) {
	var flashMovie = getFlash(movieName);
	flashMovie.Rewind();
}

function writeFlash(elemento,flash,width,height)
{
	elemento.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+width+'" height="'+height+'" />';
	elemento.innerHTML += '<param name="movie" value="'+flash+'" />';
	elemento.innerHTML += '<param name="quality" value="high" />';
	elemento.innerHTML += '<param name="menu" value="0" />';
	elemento.innerHTML += '<param name="wmode" value="transparent" />';
	elemento.innerHTML += '<embed src="'+flash+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" menu="0" wmode="transparent"></embed>';
	elemento.innerHTML +='</object>';
}

