var init={
	start:function()
	{
		
		if(window.name != "navegacao"){
			$.cookie("openplayer", 0, { path: '/'});
		} else {
			$("title" ,window.parent.document).html($("title").html());
		}
		
		timeReload=0;
		init.corner();
		init.positionPlayer();
		init.configActionPlayer();
		init.scroller("#videos");
		init.scroller("#fotos");
		init.rotativo("#destaques");
		init.abas("#letraContent");
		init.musicaInfo();
		init.configProgramacao();
		init.selectColor();
		init.enquete();
		init.insertPlayer();
	},
	
	insertPlayer:function()
	{
		if(window.name != "navegacao"){
			var nav = navigator.appName;
			
			if(nav.indexOf("Microsoft") != -1){
				$("#player").html('<object id="playerEmbed" width="155" height="45" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" type="application/x-oleobject" style="height:45px;">'+
									'<param name="url" value="http://www.blackfirestation.com.br/external-player/black_fire_station.asx" />'+
									'<param name="autostart" value="true" />'+
									'<param name="uiMode" value="mini" />'+
									'<param name="volume" value="70" />'+
									'<param name="showControls" value="true">'+
									'<param name="windowlessvideo" value="false">'+
									'<embed type="application/x-oleobject" src="http://www.blackfirestation.com.br/external-player/black_fire_station.asx" width="155" height="45" uimode="mini" autostart="1" showcontrols="1" showstatusbar="0"></embed>'+
								'</object>');
				init.openPlayer();
			} else {
				var plugins = navigator.plugins;
				
				for(i = 0; i < plugins.length; i ++){
					if(plugins[i]["name"].indexOf("Windows Media Player") != -1){
						$("#player").html('<object id="playerEmbed" width="155" height="45" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" type="application/x-mplayer2">'+
											'<param name="url" value="http://www.blackfirestation.com.br/external-player/black_fire_station.asx" />'+
											'<param name="autostart" value="true" />'+
											'<param name="uiMode" value="mini" />'+
											'<param name="volume" value="70" />'+
											'<embed type="application/x-mplayer2" src="http://www.blackfirestation.com.br/external-player/black_fire_station.asx" width="155" height="45" ShowControls="1" uimode="mini" autostart="1"></embed>'+
										'</object>');
						init.openPlayer();
						return true;
					} else if(plugins[i]["name"].indexOf("RealPlayer") != -1){
						$("#player").html('<object  id="playerEmbed" classid="CLSID:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" height="45" width="150">'+
											'<param name="controls" value="ControlPanel">'+
											'<param name="src" value="http://174.36.75.194:8080" />'+
											'<param name="autostart" value="true" />'+
											'<param name="volume" value="70" />'+
											'<embed controls="ControlPanel" src="http://174.36.75.194:8080" autostart="true" volume="70" width="150" height="45" type="audio/x-pn-realaudio-plugin"></embed>'+
										'</object>');
						init.openPlayer();
						return true;
					} else if(plugins[i]["name"].indexOf("QuickTime") != -1) {
						$("#player").html('<object id="playerEmbed" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="150" height="60" codebase="http://www.apple.com/qtactivex/qtplugin.cab">'+
											'<param name="src" value="http://www.blackfirestation.com.br/external-player/black_fire_station.qtl">'+
											'<param name="autoplay" value="true">'+
											'<param name="controller" value="true">'+
											'<param name="loop" value="false">'+
											'<embed src="http://www.blackfirestation.com.br/external-player/black_fire_station.qtl" width="150" height="60" autoplay="true" controller="true" loop="false" pluginspage="http://www.apple.com/quicktime/download/"></embed>'+
										'</object>');
						init.openPlayer();
						return true;
					} else {
						$("#player").html('<object id="playerEmbed" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="70" height="55">'+
											'<param name="allowScriptAccess" value="sameDomain" />'+
											'<param name="allowFullScreen" value="false" />'+
											'<param name="movie" value="/swf/player.swf" />'+
											'<param name="quality" value="high" />'+
											'<param name="wmode"	 value="transparent" />'+
											'<embed src="/swf/player.swf" quality="high" wmode="transparent" bgcolor="#ffffff" width="70" height="55" name="player" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer_br" />'+
										'</object>');
						init.openPlayer();
					}
				}	
			}
		}
	},
	
	selectColor:function()
	{
		if($.cookie("bfscolor")!=null){
			$("#color").attr("href","/includes/css/bfs-cores.php?cor="+$.cookie("bfscolor"));
		}
		
		$("#cores li").each(function(){
			$(this).click(function(){
				var cor=$(this).attr("id");
				$.get("/includes/css/bfs_styles_"+$(this).attr("id")+".css",
				function(){
					$.cookie("bfscolor",cor,{path:'/',expires:365});
					$("#color").attr("href","/includes/css/bfs-cores.php?cor="+cor);
				});
			});
		});
	},
	
	corner:function()
	{
		$(".round").corner("tl bl br");
		if(navigator.appName!="Microsoft Internet Explorer"&&navigator.appName!="Opera")
		{
			$("input").corner();
		}
	},
	
	openPlayer:function()
	{
		if($.cookie("openplayer")!=1){
			$("a").each(function(){
				$(this).click(function(e){
					var urlLink = $(this).attr("href");
					if($(this).attr("target")!="_blank" && $(this).attr("class") != "noPlayer" && urlLink.indexOf("#") == -1){
						e.preventDefault();
						$("#estrutura").remove();
						
						iframe = $("<iframe id=\"navegacao\" name=\"navegacao\" width=\"100%\" height=\"800\"  scrolling=\"no\" src=\""+$(this).attr("href")+"\"></iframe>").appendTo("body");
						$(iframe).load(function(){

							var contentFrame = document.getElementById("navegacao").contentWindow;

							$("#navegacao").css({
								width : "100%",
								border : "none",
								height : contentFrame.document.body.offsetHeight + 100 + "px"
							});
						});

						$.cookie("openplayer", 1, { path: '/'});
					}
				});
			});
		}
	},
	
	positionPlayer:function()
	{
		if(window.name  != "navegacao"){
			height=0;
			width=0;
			
			if(!window.innerHeight){
				height = document.documentElement.clientHeight;
				width = document.documentElement.clientWidth;
			} else {
				height = window.innerHeight;
				width = window.innerWidth;
			}
			$("#playerBar").css("left",0);
			$("#playerBar").css("top",height+"px");
			$("#playerBarBg").css("top",height+"px");
			
			if($.cookie("minmax")!=1){
				$("#playerBar").animate({top:height-80+"px"},800);
				$("#playerBarBg").animate({top:height-80+"px"},800);
				$("#minmax").html("Minimizar");
			}else{
				$("#minmax").html("Maximizar");
			}
			window.onresize = function(){
				init.positionPlayer();
			}
		} else {
			$("#playerBar").remove();
			$("#playerBarBg").remove();
		}
	},
	
	configActionPlayer:function()
	{
		$("#minmax").corner("tl tr");
		$("#minmax").click(function(){
			if($.cookie("minmax")==1){
				$.cookie("minmax",0);
				$("#playerBar").animate({top:height-80+"px"},800);
				$("#playerBarBg").animate({top:height-80+"px"},800);
				$("#minmax").html("Minimizar");
			} else {
				$.cookie("minmax",1,{path:'/'});
				$("#playerBar").animate({top:height+"px"},800);
				$("#playerBarBg").animate({top:height+"px"},800);
				$("#minmax").html("Maximizar");
			}
		});
		
		$("#comunicarErro").click(function(e)
		{
			e.preventDefault();
			init.openWindow("/comunicar-erro.php",600,400);
		});
		
		$("#envieAmigo").click(function(e){
			e.preventDefault();
			init.openWindow("/compartilhe.php",600,400);
		});
		
		$("#pedidos").click(function(e){
			e.preventDefault();
			init.openWindow("/pedidos.php",600,400);
		});
		
		$("#historico").click(function(e){
			e.preventDefault();
			init.openWindow("/player/historico.php",600,400);
		});
	},
	
	rotativo:function(object)
	{
		if($(object).attr("id")){
			var counter=0;
			var widthUl=0;
			var tempo=0;
			var itemAtual=1;
			$(".conteudo #rotativo ul",object).css("position","relative");
			$(".conteudo #rotativo ul",object).css("left","0");
			$(".conteudo #rotativo",object).attr("rel",itemAtual);
			$(".conteudo #rotativo ul li",object).each(function(){
				counter++;
				$(".legendBg",this).corner("bl br");
				$(".image",this).corner("bl br");
				var img=$(".image img",this).attr("src");
				$(".image",this).css("background","url("+img+") no-repeat");
				$(".image img",this).remove();
			});
			
			widthUl=counter*520;
			$(".conteudo #rotativo ul",object).css("width",widthUl);
			$(".conteudo .scrollLeft",object).mouseover(function(){
				itemAtual= $(".conteudo #rotativo",object).attr("rel");
				
				if(itemAtual>1){
					$(this).css("opacity",1.0);
					$(this).css("filter","alpha(opacity = 100)");
				}
			});
			
			$(".conteudo .scrollLeft",object).mouseout(function(){
				$(this).css("opacity",0.40);
				$(this).css("filter","alpha(opacity = 40)");
			});
			
			$(".conteudo .scrollLeft",object).click(function(){
				itemAtual=$(".conteudo #rotativo",object).attr("rel");
				if(itemAtual>1){
					itemAtual--;
					position=(itemAtual-1)*520;
					$(".conteudo #rotativo ul",object).animate({left:-position},500);
					$("span.navigate",object).html(itemAtual+" de "+counter);
					$(".conteudo #rotativo",object).attr("rel",itemAtual);
					clearInterval(tempo);
					tempo=setInterval(function(){init.rotativoAuto(object,counter)},2500);
				}
			});
			
			$(".conteudo .scrollRight",object).mouseover(function(){
				itemAtual=$(".conteudo #rotativo",object).attr("rel");
				if(itemAtual<counter){
					$(this).css("opacity",1.0);
					$(this).css("filter","alpha(opacity = 100)");
				}
			});
			
			$(".conteudo .scrollRight",object).mouseout(function(){
				$(this).css("opacity",0.40);
				$(this).css("filter","alpha(opacity = 40)");
			});
			
			$(".conteudo .scrollRight",object).click(function(){
				itemAtual=$(".conteudo #rotativo",object).attr("rel");
				if(itemAtual<counter){
					position=itemAtual*520;
					$(".conteudo #rotativo ul",object).animate({left:-position},500);
					itemAtual++;$("span.navigate",object).html(itemAtual+" de "+counter);
					$(".conteudo #rotativo",object).attr("rel",itemAtual);
					clearInterval(tempo);
					tempo=setInterval(function(){init.rotativoAuto(object,counter)},5500);
				}
			});
			
			$("span.navigate",object).html(itemAtual+" de "+counter);
			tempo=setInterval(function(){init.rotativoAuto(object,counter)},2500);
		}
	},
	
	rotativoAuto:function(object,counter)
	{
		itemAtual=$(".conteudo #rotativo",object).attr("rel");
		if(itemAtual<counter){
			position=itemAtual*520;
			$(".conteudo #rotativo ul",object).animate({left:-position},500);
			itemAtual++;
			$("span.navigate",object).html(itemAtual+" de "+counter);
			$(".conteudo #rotativo",object).attr("rel",itemAtual);
		} else {
			position=0;
			$(".conteudo #rotativo ul",object).animate({left:-position},500);
			itemAtual++;
			$("span.navigate",object).html(1+" de "+counter);
			$(".conteudo #rotativo",object).attr("rel",1);
		}
	},
	
	scroller:function(object)
	{
		if($(object).attr("id")){
			tempo=0;
			widthUl=0;
			counter=0;
			widthLi=$(".scroller ul li",object).css("width");
			widthLi=parseInt(widthLi.substr(0,widthLi.length-2))+26;
			$(".scroller ul li",object).each(function(){
				counter++;
			});
			
			widthUl=counter*widthLi;
			$(".scroller ul",object).css("width",widthUl+"px");
			$(".scroller ul",object).css("left",0);
			$(".scroller ul",object).css("position","relative");
			$(".scrollLeft",object).mouseover(function(){
				$(this).css("opacity",1.0);
				$(this).css("filter","alpha(opacity = 100)");
				tempo=setInterval(function(){init.scroll(object,"left",widthUl)},1);
			});
			
			$(".scrollLeft",object).mouseout(function(){
				$(this).css("opacity",.70);
				$(this).css("filter","alpha(opacity = 70)");
				clearInterval(tempo);
			});
			
			$(".scrollRight",object).mouseover(function(){
				$(this).css("opacity",1.0);
				$(this).css("filter","alpha(opacity = 100)");
				tempo=setInterval(function(){init.scroll(object,"rigth",widthUl)},1);
			});
			
			$(".scrollRight",object).mouseout(function(){
				$(this).css("opacity",.70);
				$(this).css("filter","alpha(opacity = 70)");
				clearInterval(tempo);
			});
		}
	},
	
	scroll:function(object,direction,sizeScroll)
	{
		var position=$(".scroller ul",object).css("left");
		var areaScroll=$(".scroller",object).css("width");
		position=parseInt(position.substr(0,position.length-2));
		areaScroll=parseInt(areaScroll.substr(0,areaScroll.length-2));
		if(direction=="left"){
			if(position<0){
				$(".scroller ul",object).css("left",position+6+"px");
			}
		}else{
			if(position>-(widthUl-areaScroll)){
				$(".scroller ul",object).css("left",position-6+"px");
			}
		}
	},
	
	openWindow:function(url,w,h)
	{
		$("<div id=\"bloqueio\"></div>").appendTo("body");
		$("#bloqueio").css("width",document.body.offsetWidth+"px");
		$("#bloqueio").css("height",document.body.offsetHeight+"px");
		if(!window.innerHeight){
			height=document.documentElement.clientHeight;
			width=document.documentElement.clientWidth;
		} else {
			height=window.innerHeight;width=window.innerWidth;
		}
		$("<div id=\"janela\"></div>").appendTo("body");
		$("#janela").css("width",w+"px");
		$("#janela").css("height",h+"px");
		$("#janela").css("left",(width/2)-(w/2)+"px");
		$("#janela").css("top",((height-70)/2)-(h/2)+"px");
		$("#janela").corner("tl bl br");
		$.post(url,
		function(data){
			$("#janela").html(data);
		});
	},
	
	closeWindow:function()
	{
		$("#bloqueio").fadeOut("slow",function(){$("#bloqueio").remove();});
		$("#janela").fadeOut("slow",function(){$("#janela").remove();});
	},
	
	abas:function(object)
	{
		if($(object).attr("id")){
			$("ul li",object).each(function(){
				$(this).corner("tl tr");
			});
			
			$("ul li a",object).each(function(){
				$(this).click(function(e){
					e.preventDefault();
					$("ul li",object).each(function(){
						$(this).attr("class","");
					});
					
					$(this).parent().attr("class","ativo");
					$("div",object).each(function(){
						$(this).css("display","none");
					});
					$($(this).attr("href")).css("display","block");
				});
			});
		}
	},
	
	musicaInfo:function()
	{
		clearInterval(timeReload);
		$.get("/player/musica-info.php",
		function(data){
			clearInterval(timeReload);
			$("#dadosMusica").html(data);
			timeReload=setInterval(init.musicaInfo,20000);
			
			if($("#navegacao").attr("id")){
				$("#dadosMusica a").attr("target", "navegacao");
			}
		});
		clearInterval(timeReload);
	},
	
	configProgramacao:function()
	{
		object="#paginacaoSemanal";
		if($(object).attr("id")){
			$("ul li",object).each(function(){
				$(this).corner("tl tr");
			});
			
			$("ul li a",object).each(function(){
				$(this).click(function(e){
					e.preventDefault();
					$(".lista li").css("display","none");
					$("."+$(this).attr("href")).css("display","block");
				});
			});
		}
	},
	
	enquete:function()
	{
		if($("#enqueteForm").attr("id")){
			if($.cookie($("#enquete").val())==1){
				$("#enquete input:radio, #votar").css({display:"none"});
			}
			$("#enqueteForm input:radio").each(function(){
				$(this).click(function(){
					$("#opcao").val($(this).attr("id"));
				});
			});
			
			$("#votar").click(function(e){
				e.preventDefault();
				if($("#opcao").val()==""){
					$("#estrutura").bar({color:'#990000',background_color:'#FFFFFF',removebutton:true,message:'Voce tem que selecionar uma opçao para concluir seu voto!',time:4000});
				} else {
					$("#enqueteForm").submit();
				}
			});
		}
	}
};	
$(document).ready(function(){init.start();});