var init={

	start:function()
	{
		if(window.name != "navegacao"){
			$.cookie("openplayer", 0, { path: '/'});
		} else {
			$("title" ,window.parent.document).html($("title").html());
		}
		
		init.openPlayer();
		init.statusPlayer = false;
		timeReload=0;
		init.corner();
		init.positionPlayer();
		init.configActionPlayer();
		init.scroller("#videos");
		init.rotativo("#destaques");
		init.musicaInfo();
		init.configProgramacao();
		init.enquete();
		init.playStop();
		init.album();
		init.widgetBandas();
	},
	
	playStop : function(){
		
		$("#controlPlayer").click(function(){
			if(!init.statusPlayer){
				
				$("#iframePlayer").attr("src", "/player/play.html");
				$(this).removeClass("stop").addClass("play");
				init.statusPlayer = true;
				
			} else {
				
				$("#iframePlayer").attr("src", "/player/stop.html");
				$(this).removeClass("play").addClass("stop");
				init.statusPlayer = false;
				
			}
		});
	},
	
	album : function()
	{
		if($("#listaFotos").size() > 0){
			$("#listaFotos ul li a").click(function(e){
				e.preventDefault();
				
				$("#ampliar .content img").attr("src", $(this).attr("href"));
				$("#ampliar .content img").attr("alt", $(this).attr("title"));
				$("#ampliar .content p").text($(this).attr("title"));
			});
		}
	},
	
	corner:function()
	{
		$(".round").corner("tl bl br");
		$(".roundAll").corner();
		
		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();
						$("#sitemap").remove();

						iframe = $("<iframe id=\"navegacao\" name=\"navegacao\" width=\"100%\" height=\"1000\"  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 #playerBarBg").css({
				position : "fixed",
				bottom 	: 0,
				left	: 0
			});
			if($.cookie("minmax")!=1){
				$("#minmax").html("Minimizar");
			}else{
				$("#minmax").html("Maximizar");
			}

		} else {
			$("#playerBar").remove();
			$("#playerBarBg").remove();
		}
	},
	
	configActionPlayer:function()
	{
		$("#minmax").corner("tl tr");
		$("#minmax").click(function(){
			if($.cookie("minmax")==1){
				$.cookie("minmax",0);
				$("#playerBar").animate({bottom:0},500);
				$("#playerBarBg").animate({bottom:0},500);
				$("#minmax").html("Minimizar");
			} else {
				$.cookie("minmax",1,{path:'/'});
				$("#playerBar").animate({bottom : -80},500);
				$("#playerBarBg").animate({bottom: -80},500);
				$("#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)},6000);
		}
	},
	
	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();});
	},
	
	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();
				}
			});
		}
	},
	
	widgetBandas : function()
	{
		if($(".home").size() > 0){
			
			$.ajax({
				url:"/includes/bandas-list.php", 
				type: "GET",
				dataType:'jsonp'
			});

		}
	},
	
	populaWidgetBandas : function(data)
	{
		/*Total de paginas no widget de bandas*/
		init.dataLength = data.length;
		init.tpWgBandas = Math.ceil(data.length / 18);
		
		$("#bandas ul").css({
			width : init.tpWgBandas * 260
		});
		
		init.pgWgBandas = 0;
		
		$("#bandas .scrollRight").click(function(){
			
			if(init.pgWgBandas < init.tpWgBandas - 1){
				init.pgWgBandas ++;
			
				$("#bandas ul").animate({
					left : -(258 * init.pgWgBandas)				
				}, 500);
			}
		});

		$("#bandas .scrollLeft").click(function(){
			
			if(init.pgWgBandas > 0){
				init.pgWgBandas --;
			
				$("#bandas ul").animate({
					left : -(258 * init.pgWgBandas)				
				}, 500);
			}
		});
		
		for(i = 0; i < data.length; i ++){
			var img = data[i].file_reso;
			
			if(img){
				img = img.split(".");
				img = img[0] + "_80x80."+img[1];
			} else {
				img = "";
			}
			
			$("<li><a href=\"/bandas/"+data[i].url+"\" title=\""+data[i].titl_cont+"\"><img src=\"/resources/"+img+"\" alt=\""+data[i].titl_cont+"\" /></a></li>").appendTo("#bandas ul");
		};
		
	},
	
	AlertPlugin : function()
	{
		$("#estrutura").bar({
			color 			 : '#009900',
			background_color : '#FFFFFF',
			removebutton     : false,
			message			 : 'Você não possui nenhum plugin para ouvir a rádio, recomendamos que você baixe pelo menos o Flash Player para curtir o melhor do Rock and Roll!',
			time			 : 10000
		});	
	}
};	
$(function(){ init.start(); });
