$(document).ready(function (){
	
	/**********************MENU PRINCIPAL******************************************************/
	$("ul.menu > li").each(function(){
		uls=$(this).find("ul");
		if (uls.length>0){
			$(this).addClass("submenu");
			$(this).hover(function() {
				clearInterval(this.menu_timer);	
				$(this).addClass("hover");
				$(this).find(">ul").show();
			},function() {
				var var_div=this;
				var_div.menu_timer=setTimeout(function (){
					$(var_div).removeClass("hover");
					$(var_div).find(">ul").hide();
				},500);
				
			});
			$(this).focusin(function (){
				$(this).trigger("mouseover");
			}).focusout(function (){
				$(this).trigger("mouseout");
			});
			
			$(this).find("ul > li").focusin(function (){
				$(this).addClass("focus");
			}).focusout(function (){
				$(this).removeClass("focus");
			});;
			
		}
	});
	/**********************Videos mais vistos******************************************************/
	$("#videos a.link").click(function (){
		opened=$("#videos").hasClass("open");
		if (opened==false){
			$("#videos").addClass("open").removeClass("close");
			$("#topvideos").animate({height:'129', opacity: 1},500, function (){
				$(this).css("opacity",'');
			});
		}else{
			//
			$("#topvideos").animate({height:'4', opacity: 0},500, function (){
				$("#videos").addClass("close").removeClass("open");
			});
		}
		return false;
	});
	
	/**********************SUB MENU******************************************************/
	$("#header_in .sub_menu .zona, #header_in .sub_menu .campeonato, #header_in .sub_menu .more").hover(function () {
		clearInterval(this.menu_timer);	
		$(this).addClass("open");
	}, function () {
		var var_div=this;
		var_div.menu_timer=setTimeout(function (){
			$(var_div).removeClass("open");
		},500);
	});
	
	
	$("#header_in .sub_menu .zona ul li a").click(function (){
		var cod=$(this).attr("href").substring(1);
		var type='pais';
		$.getJSON(url+'ajax/menu.php?type='+type+'&id='+cod ,function (data){
			menu_load_pais(data.pais);
			menu_load_epocas(data.epoca, data.epocas);
			menu_load_equipas( data.equipas);
		});
		return false;
	});
	$("#header_in .sub_menu .campeonato ul li a").live("click", function (){
		
		var cod=$(this).attr("rev");
		var type='epoca';
		
		$.getJSON(url+'ajax/menu.php?type='+type+'&id='+cod ,function (data){
			menu_load_epocas(data.epoca);
			menu_load_equipas( data.equipas);
		});
		return false;
	});
	function menu_load_pais(select){
		var html='<span class="name">'+select.nome+'</span>';
		if (select.imagem!=''){
			html+='<img class="flag" src="'+url+'/imagens/paises/32/'+select.imagem+'" width="32" height="32" alt="" />';
		}
		
		$('#header_in .sub_menu .zona .label').html(html);
	}
	function menu_load_epocas(select, data){
		var html='<span class="name">'+select.nome+'</span>';
		if (select.imagem!=''){
			html+='<img class="flag" src="'+url+'/imagens/epocas/32/'+select.imagem+'" width="32" height="32" alt="" />';
		}
		
		$('#header_in .sub_menu .campeonato .label').html(html);

		if (typeof(data)=='object'){
			html='';
			
			
			for(i=0;i<data.length;i++){
				html+='<li><a href="'+data[i].url+'" rev="'+data[i].id+'">'+
					'<span>'+data[i].nome+'</span>';
				if (data[i].imagem){
					html+='<img src="'+url+'/imagens/epocas/16/'+data[i].imagem+'" width="16" height="16" alt="" />';
				}
				html+='</a></li>';
			}
			html+='<li class="last"></li>';
			$('#header_in .sub_menu .campeonato ul').html(html);
		}
	}
	
	function menu_load_equipas(data){
		if (typeof(data)=='object'){
			html='';
			
			var total=data.length;
			var more=0;
			
			var max=16;
			if (total>max){
				more=total;
				total=max;
			}
			
			var html='';
			for(i=0;i<total;i++){
				html+='<li><a href="'+data[i].url+'">';
				if (data[i].imagem){
					html+='<img src="'+url+'/imagens/equipas/32/'+data[i].imagem+'" width="32" height="32" alt="" />';
				}else{
					html+='<img src="'+url+'/imagens/equipas/32.png" width="32" height="32" alt="" />';
				}
				html+='<span><cite>'+data[i].nome+'</cite><small></small></span>';
				
				html+='</a></li>';
			}
			html+='<li class="last"></li>';
			$('#header_in .sub_menu .equipas ul').html(html);
			
			if (more>0){
				var html='';
				for(i=total;i<more;i++){
					html+='<li><a href="'+data[i].url+'">';
					html+='<span>'+data[i].nome+'</span>';
					if (data[i].imagem){
						html+='<img src="'+url+'/imagens/equipas/16/'+data[i].imagem+'" width="16" height="16" alt="" />';
					}else{
						html+='<img src="'+url+'/imagens/equipas/16.png" width="16" height="16" alt="" />';
					}
					
					
					html+='</a></li>';
				}
				html+='<li class="last"></li>';
				$('#header_in .sub_menu .more ul').html(html);

				
			}else{
				$('#header_in .sub_menu .more ul').html('');
			}
			if ($("#header_in .sub_menu .more ul li").length<=1){
				$("#header_in .sub_menu .more").hide();
			}else{
				$("#header_in .sub_menu .more").show();
			}
			
			//<?php echo image_url($equipa["imagem"], "equipas", "16",  true, $equipa["nome"], true)?></a></li>
		}
	}
//	
//	var id_pais=$("#header_in .sub_menu .zona .label").attr("id");
//	var id_epoca=$("#header_in .sub_menu .campeonato .label").attr("id");
//	//alert(id_pais + ' - ' + id_epoca);
//	
//	//Paises
//	$("#sitemap_in > ul > li > label").each(function (){
//		$("#header_in .sub_menu .zona ul").append('<li><a href="#">'+$(this).html()+'</a></li>');
//	});
//	
//	//Epocas
//	$("#sitemap_in > ul > li."+ id_pais +" > ul > li > label").each(function (){
//		$("#header_in .sub_menu .campeonato ul").append('<li>'+$(this).html()+'</li>');
//	});
//	
//	//Equipas
//	$("#sitemap_in > ul > li."+ id_pais +" > ul > li."+ id_epoca +" >  ul > li").each(function (index){
//		if (index<=17){
//			$("#header_in .sub_menu .equipas ul").append('<li>'+$(this).html()+'</li>');
//		}else{
//			$("#header_in .sub_menu .more ul").append('<li>'+$(this).html()+'</li>');
//		}
//	});
	

	
	/**********************LOGIN TOP******************************************************/
	$("ul.btn li.entrar > a, ul.btn li.conta > a").click(function (){
		var li=$(this).parent();
		if (li.hasClass("open")){
			li.removeClass("open");
		}else{
			li.addClass("open");
		}
		return false;
	});
	
	/**********************Caixa homepage Ultimos jogos******************************************************/
	$(".last_games .dia > a.titulo").live("click", function (){
		if ($(this).hasClass("close")==true){
			$(this).removeClass("close");
			$(this).next().removeClass("close");
		}else{
			$(this).addClass("close");
			$(this).next().addClass("close");
		}
		return false;
	});
	
	$(".last_games .dia > a.load").click(function (){
		var data=last_games_date.getFullYear()+'-'+(last_games_date.getMonth()+0)+'-'+last_games_date.getDate();
		if (!last_games_load){
			last_games_load=true;
			$.ajax({
				url: url+'ajax/games.php?dia='+data,
				success: function(data) {
//					$('.last_games .dia > a.titulo').each(function(){
//						$(this).addClass("close");
//						$(this).next().addClass("close");
//					});
				    $('.last_games .dia > a.load').before(data);
				    last_games_load=false;
				}
			});
		}else{
			
		}
		return false;
	});
	
	/**********************Partilha******************************************************/
	
	$(".share_icons li a").each(function (){
		var share_url='';
		var share_scroll=true;
		var share_height=300;
		var share_width=600;
		var share_popup=true;
		
		var share_service=this.className;
		
		switch (share_service) {
			case "facebook":
				share_url='http://www.facebook.com/sharer.php?u={u}&amp;t={t}';
				share_scroll=false;
				break;
			case "delicious":
				share_url='http://del.icio.us/post?url={u}&amp;title={t}';
				share_popup=false;
				break;
			case "digg":
				share_url='http://digg.com/submit?phase=2&amp;url={u}&amp;title={t}';
				break;
			case "blogger":
				share_url='http://www.blogger.com/blog_this.pyra?t&u={u}&n={t}&pli=1';
				share_scroll=false;
				break;
			case "technorati":
				share_url='http://www.technorati.com/faves?add={u}';
				break;
			case "twitter":
				share_url='http://twitter.com/home?status={t}%20{u}';
				share_scroll=false;
				break;
			case "google-buzz":
				share_url='http://www.google.com/buzz/post?url={u}';
				share_scroll=false;
				break;

			default:
				break;
		}
		var page_url = window.location.href;
		var page_title = document.title || $('h1:first').text();
		var page_desc = $('meta[name=description]').attr('content') || '';
		
		share_url=share_url.replace(/\{u\}/, page_url).replace(/\{t\}/, page_title).replace(/\{t\}/, page_desc);
		$(this).attr("href", share_url).attr("target", "_blank").click(function (){
			_gaq.push(['_trackEvent', 'partilha', share_service, page_url]);
			if (share_popup){
				x=window.open ($(this).attr("href"),"share_window",(share_scroll?"scrollbars=1,":'')+"status=0,toolbar=0,location=0,menubar=0,resizable=1,height="+share_height+",width="+share_width+"");
				return false;
			}
			});
		
	});
	
	
	/**********************Abrir e fechar formulário de Comentários******************************************************/
	$(".comment_form .comment_open a").click(function (){
		var d=$(this).parent().parent();
		if (d.hasClass("open")){
			d.find("ul").slideUp();
			d.removeClass("open");
		}else{
			d.addClass("open");
			d.find("ul").slideDown();
		}
		return false;
	});
	
	$(".comments .comments_list .user .replay").live('click', function(){
		if (!$(".comment_form").hasClass("open")){
			$(".comment_form .comment_open a").trigger("click");
		}
		var coment=$(this).parent().parent();
		var user=coment.find(".fright .fn").text();
		var id=$(this).attr("href").substring(1);
		$(".comment_form form li.responder").slideDown();
		$(".comment_form form li.responder span cite").html(user);
		$(".comment_form form input[name='resposta']").val(id);
		if ($(".comment_form form input[name='nome']").length==1)
			$(".comment_form form input[name='nome']").focus();
		else
			$(".comment_form form textarea[name='texto']").focus();
			
		
		return false;
	});
	$(".comment_form form li.responder a.close_resposta").click(function (){
		$(".comment_form form li.responder").slideUp();
		$(".comment_form form input[name='resposta']").val(0);
		return false;
	});
	
	$(".comments .head ul li a").live("click",function (){
		h=$(this).attr("href").substring(1);
		$('.comments').load('../ajax/comentarios.php?'+h);
		return false;
	});
	
	
	
	/**********************caixa INFO para mostrar mais info******************************************************/
	$(".info .info_content a.more_info").click(function (){
		var open=$(this).parent().parent(".info_content");
		if (open.hasClass("open")){
			open.removeClass("open");
		}else{
			open.addClass("open");
		}
		return false;
	});
	
	
	$(".submit_form").click(function (){
		$(this).closest("form").submit();
	});
	
	
	/**********************Votação******************************************************/
	
	$(".stars").each(function (){
		stars(this);
	});
	
	
	
	
	
	/**********************Lista de Videos******************************************************/
	$(".player_list ul li a").click(function(){
		if (!$(this).parent().hasClass("selected")){
			var id=$(this).attr("href").substring(1);
			var video=videos[id];
			
			//    0       1        2         3     4     5   [6]
			//[id_video,titulo,video_embed,texto,stars,nome, url]
			//$("textarea[name='comment']").val((video["embed"]));
			$("#player").html((video["embed"]));
			$(".game_video .video_info .titulo").html(video["titulo"]);
			$(".game_video .video_info .user span").html(video["user"]);
			$(".game_video .video_info .descricao").html(video["texto"]);
	//		$(".game_video .partilha .stars").addClass('stars'+video["star"]);
			
			
			$(".game_video .partilha .stars input[name='cod']").val(video["id"]);
			$(".game_video .video_info .video_incorrecto").attr("href",'video_incorrecto.php?id='+video[0]);
			
			stars(".game_video .partilha .stars");
			$(".player_list ul li").removeClass("selected");
			$(this).parent().addClass("selected");
		}
	});
	$(".player_list ul li:first-child a").trigger('click');
	
	
	
	//***********************PERFIL *************************************************
	
	$("select.pais, select.epoca").change(function (){
		var id=$("option:selected", this).val();
		if ($(this).hasClass("pais")){
			$('select.epoca').load('ajax/options.php?type=epoca&cod='+id, function (){
				$(this).prepend("<option value='0'>-- Liga --</option>");
				$(this).val(0);
				$('select.equipa').html("<option value='0'>-- Clube --</option>");
			});
		}else if($(this).hasClass("epoca")){
			$('select.equipa').load('ajax/options.php?type=equipa&cod='+id, function (){
				$(this).prepend("<option value='0'>-- Clube --</option>");
				$(this).val(0);
			});
		}
		
	});
	
	
	$("input, textarea").focusin(function (){
		var t=$(this);
		if (t.val()==t.attr("title")){
			t.val('');
		}
	}).focusout(function (){
		var t=$(this);
		if (t.val()==''){
			t.val(t.attr("title"));
		}
	}).each(function (){
		var t=$(this);
		if (t.val()==''){
			t.val(t.attr("title"));
		}
	});
	
	/**********************Opções das caixas******************************************************/
	$(".gbox").each(function (){
		var gbox=$(this);

		gbox.find(".gbox_head .gbox_option a.close").click(function (){
			if (confirm("Está preste a fechar a caixa '" + gbox.find(".gbox_head .title").html()+ "'.\n\nPretende fechar esta caixa?")){
				gbox.remove();
			}
			return false;
		});
		gbox.find(".gbox_head .gbox_option a.min").click(function (){
			if ($(this).hasClass("max")){
				gbox.find(".gbox_content").show();
				gbox.find(".gbox_XX").css("visibility","visible");
				gbox.find(".gbox_footer").show();
				$(this).addClass("min").removeClass("max");	
			}else{
				gbox.find(".gbox_content").hide();
				gbox.find(".gbox_XX").css("visibility","hidden");
				gbox.find(".gbox_footer").hide();
				$(this).addClass("max").removeClass("min");
			}
			return false;
		});
		gbox.find(".gbox_head .gbox_option a.option").click(function (){
			if ($(this).hasClass("option")){
				gbox.find(".gbox_content").hide();
				gbox.find(".gbox_XX").css("height", "auto");
				$(this).addClass("option_close").removeClass("option");
			}else{
				gbox.find(".gbox_content").show();
				gbox.find(".gbox_XX").css("height", "3px");
				$(this).addClass("option").removeClass("option_close");
			}
			return false;
		});
	});
	
	$(".light").click(function(){
		return light_golos(this);
	});
	
});

/**********************GBox - Lightbox dos golos******************************************************/
function light_golos(link){
	var $this=$(link);
	var light=$(".light_golos");
	if (light.length==0){
		$("body").append('<div class="light_golos">' +
				'<div class="light_opacity"></div>' +
				'<div class="light_window">' +
					'<div class="light_head">' +
						'<span>'+$this.attr("title")+'</span>' +
						'<a href="#">Fechar</a>' +
					'</div>' +
					'<div class="light_content"></div>' +
					'<div class="light_footer"></div>' +
				'</div>' +
			'</div>');
		light=$(".light_golos");
	}else{
		$(".light_golos .light_window .light_head span").html($this.attr("title"));
	}
	
	var dist_top=20;
	var height=$(window).height() - (dist_top*3);
	
	
	//content
	if ($this.hasClass("iframe")){
		light.find(".light_window .light_content").html("<iframe src='"+$this.attr("href")+"' frameborder='0' width='100%' height='99%'></iframe>");
	}else if ($this.hasClass("youtube")){
		light.find(".light_window .light_content").html("<iframe src='"+url+'video_embed.php?type=youtube&id='+encodeURIComponent($this.attr("rev"))+'&titulo='+encodeURIComponent($this.attr("title"))+'&url='+encodeURIComponent($this.attr("href"))+'&flash='+encodeURIComponent($this.attr("rel"))+"' frameborder='0' width='100%' height='99%'></iframe>");
		height=333;
	}else if ($this.hasClass("sapo")){
		light.find(".light_window .light_content").html("<iframe src='"+url+'video_embed.php?type=sapo&id='+encodeURIComponent($this.attr("rev"))+'&titulo='+encodeURIComponent($this.attr("title"))+'&url='+encodeURIComponent($this.attr("href"))+'&flash='+encodeURIComponent($this.attr("rel"))+"' frameborder='0' width='100%' height='99%'></iframe>");
		height=333;
	}else if ($this.attr("rel") != ''){
		light.find(".light_window .light_content").html($("#"+$this.attr("rel")).html());
	}else{
		light.find(".light_window .light_content").html($("#"+$this.attr("rel")).html());
	}
	
	
	light.find(".light_opacity").css("height", $(document).height());
	light.find(".light_opacity").css("width", $(document).width());
	light.find(".light_window .light_content").css("height", height);
	
	
	light.find(".light_window").css("top", dist_top + $(document).scrollTop());
	light.find(".light_window").css("top", dist_top + $(document).scrollTop());
	$(document).scroll(function (){
		light.find(".light_window").css("top",  dist_top + $(document).scrollTop());
	});
	
	//close
	light.find(".light_window .light_head a, .light_opacity").click(function(){
		light.fadeOut();
		light.find(".light_window .light_content").html("");
	});
	

	
	light.fadeIn();
	return false;
}

/**********************Votos******************************************************/
function stars(dom){
	
		var s=$(dom);
		var form=s.find(".form_stars");
		
		var type=s.find("input[name='type']").val();
		var cod=s.find("input[name='cod']").val();
		var star=s.find("input[name='star']").val();
		
		form.removeClass("star0 star1 star2 star3 star4 star5");
		form.addClass("star"+star);
		
		form.find("a").hover(function (){
			form.removeClass("star0 star1 star2 star3 star4 star5");
			form.addClass("star"+$(this).text());
		}, function (){
			form.removeClass("star0 star1 star2 star3 star4 star5");
			form.addClass("star"+star);
		});
		
		form.find("a").unbind("click");
		form.find("a").bind("click", function (){
			$.getJSON("../ajax/votos.php?type="+type+'&id='+cod+'&star='+$(this).html() ,function (data){
				if (data["error"]!=''){
					alert(data["error"]);					
				}else{
					
					s.find(".label").html(data["ok"]);
					setTimeout(function(){ s.find(".label").html(data["votos"]+" votos na média de "+data["media"]); }, 1000);
					s.find("input[name='star']").val(data["star"]);
					star=s.find("input[name='star']").val();
					form.addClass("star"+star);
					
//					alert(data["star"]);
//					alert(data["class"]);
				}
				
			});
			return false;
		});
}


//light_golos(0);

