$(window).resize(function() {
	if($.browser.msie && $.browser.version=="6.0") {
	calcola();
	}
})


 
$(document).ready(function(){
    
	$(function(){
	 $('input[type=radio].rate_clip').rating({
	     required: true,
	     starWidth: 22,
	     callback: function(value, name){
	         $values = value.split(/_/);
			 $div_rating = $(this).parent().parent();
	         $.ajax({
     			type: "POST",
     			url: "/streams/rate",
     			data: "id=" + $values[1] + "&rate=" + $values[2] + "&streamtype=" + $values[0],
     			success: function(rate) {	
     			    if (rate == "0") {
						$div_rating.find(".vote_area").hide();
         				$div_rating.find(".message_area").html("Grazie per aver votato!").fadeIn();
         		    } else if (rate == "-1") {
         				$div_rating.find(".vote_area").hide();
         				$div_rating.find(".message_area").html("Hai esaurito i tuoi<br/>voti per oggi.<br/>Torna domani<br/>per votare ancora!").fadeIn();
         		    } else if (rate == "-2") {
         				$div_rating.find(".vote_area").hide();
         				$div_rating.find(".message_area").html("Effettua il <a href=\"/user_session/new\">login</a> per votare").fadeIn();
         		    }
     			},

     			error: function() {
     				$div_rating.find(".vote_area").hide();
     				$div_rating.find(".message_area").html("Votazione non disponibile").fadeIn();
     			}
     		});
	     }
	 });
	});
 
	$('.vote_clip').click(function() { 
	    $values = $(this).attr("id").split(/_/);
	    $voted_clip = $(this).attr("id")
	    $div_rating = $(this).parent().parent();
	    $.ajax({
 			type: "POST",
 			url: "/streams/rate",
 			data: "id=" + $values[1] + "&rate=5&streamtype=" + $values[0],
 			success: function(rate) {
 			    if (rate == "0") {
     				$div_rating.find(".vote_area").hide();
     				$div_rating.find(".message_area").html("Grazie per aver votato!").fadeIn();
     				setTimeout("$div_rating.find(\".message_area\").hide(); $div_rating.find(\".vote_area\").fadeIn();",2000)
     		    } else if (rate == "-1") {
     				$div_rating.find(".vote_area").hide();
     				$div_rating.find(".message_area").html("Hai esaurito i tuoi<br/>voti per oggi.<br/>Torna domani<br/>per votare ancora!").fadeIn();
     		    } else if (rate == "-2") {
     				$div_rating.find(".vote_area").hide();
     				$div_rating.find(".message_area").html("Effettua il <a href=\"/streams_votables/new\">login</a> per votare").fadeIn();
     		    }
 			},
 			error: function() {
 				$div_rating.find(".vote_area").hide();
 				$div_rating.find(".message_area").html("Votazione non disponibile").fadeIn();
 			}
 		});
    });
 
	$('.vote_mybox').click(function() { 

	    $values = $(this).attr("id").split(/_/);
	    $voted_clip = $(this).attr("id")
	    $div_rating = $(this).parent().parent();
	    $.ajax({
 			type: "POST",
 			url: "/streams/rate",
 			data: "id=" + $values[1] + "&rate=5&streamtype=" + $values[0],
 			success: function(rate) {
 			    if (rate == "0") {
     				$div_rating.find(".vote_area").hide();
     				$div_rating.find(".message_area").html("Grazie per aver votato! Torna domani").fadeIn();
     				setTimeout("$div_rating.find(\".message_area\").hide(); $div_rating.find(\".vote_area\").fadeIn();",2000)
     		    } else if (rate == "-1") {
     				$div_rating.find(".vote_area").hide();
     				$div_rating.find(".message_area").html("Hai esaurito i tuoi<br/>voti per oggi.<br/>Torna domani<br/>per votare ancora!").fadeIn();
     		    } else if (rate == "-2") {
     				$div_rating.find(".vote_area").hide();
     				$div_rating.find(".message_area").html("Effettua il <a href=\"/myboxes_votables/new\">login</a> per votare").fadeIn();
     		    }
 			},
 			error: function() {
 				$div_rating.find(".vote_area").hide();
 				$div_rating.find(".message_area").html("Hai esaurito i tuoi<br/>voti per oggi.<br/>Torna domani<br/>per votare ancora!").fadeIn();
 			}
 		});
    });
 
	$('#flash').flash({
    	src: '/swf/video.swf',
		width: '100%',
		height: '100%',
		wmode: 'transparent',
		allowFullScreen: true
	});
 
	$('#player').flash({
    	src: '/swf/TeamworldTV.swf',
		width: '720px',
		height: '512px',
		wmode: 'transparent',
		allowFullScreen: true
	});
 
	$(".banner_swf").each(function() {
		src = "/banner_swf/" + $(this).attr("title") + ".swf";
		$(this).flash({
			src: src,
			width: '100%',
			height: '100%',
			wmode: 'transparent'			
		})
	});
 
	$(".fieldWithErrors").each(function(i) {
		$(this).removeClass("fieldWithErrors").parent("li").addClass("alert");
	});
 
	$(".checkWithErrors").each(function(i) {
		$(this).parent().parent("div").addClass("alert");
	});
 
	$('a.lightbox').lightBox();
 
	var myPic = new Image(32,32);
	myPic.src = "/images/ajax-loader.gif";
 
	$("#fascia").pngfix();
	$("#logo img").pngfix();
	$("#flogo img").pngfix();
	$("#nav > ul > li > a").pngfix();
	$("#sfumato").pngfix({sizingMethod: "scale"});
	$(".ph img").pngfix();
	$("#tooltip").pngfix();
	$(".title").pngfix();
	$(".imag").pngfix();
	$("#slogan").pngfix();
	$("#onair a").pngfix();
	$("#searchby .sfondo").pngfix();
	$("#mybox_image img").pngfix();
	$(".box h3").pngfix();
	$(".arrow").pngfix();
	$("#series_banner .buttons a").pngfix();
 
	if($.browser.msie && $.browser.version=="6.0") {
		calcola();
	}
 
	var altezzahighlight = $("#highlight").height();
//	if (altezzahighlight>124) $("#flashcontent").height(610-124+altezzahighlight);
 
	resizedivs();
 
	$("#banner_buttons > .buttons > ul > li > a").click(function() {
	 	var offset = $("."+$(this).parent().attr("title")+"").offset();
		// var offset_top = offset.top - 260;
		// 		if($.browser.msie && $.browser.version=="6.0") {
			offset_top = offset.top;
		//}
 
		var speed = offset_top / 1200 * 1000;
		$("html, body").animate({ scrollTop: offset_top }, speed);
	});
 
	$(".programmi_t").click(function() {
	 	var offset = $("."+$(this).parent().attr("title")+"").offset();
		// var offset_top = offset.top - 260;
		// 		if($.browser.msie && $.browser.version=="6.0") {
			offset_top = offset.top;
		//}
 
		var speed = offset_top / 1200 * 1000;
		$("html, body").animate({ scrollTop: offset_top }, speed);
	});
 
	$("#reporter_error").each(function() {
	 	var offset = $(".reporter").offset();
		// var offset_top = offset.top - 260;
		// 		if($.browser.msie && $.browser.version=="6.0") {
			offset_top = offset.top;
		//}
 
		var speed = offset_top / 1200 * 1000;
		$("html, body").animate({ scrollTop: offset_top }, speed);
	});
 
//	alert(offset_wj_top);
 
	var m = 61;
	var newsize = 0;
	var i = 0;
 
	$(".hl").each(function() {
		if (i==2) $(this).width(318);
		i++;
	});
 
	$("#nav > ul > li").each(function() {
		$(this).css("margin-top",m+"px");
		m = m - 12;
//		$(this).html("<a href=\""+$(this).find("a").attr("href")+"\"><img src=\""+$(this).attr("title")+"\" /></a>");
//		$(this).attr("title","");
//		$(this).find("a").find("img").pngfix();
	});
 
	$(".social a").each(function() {
		$(this).css("background","url(../images/social/"+$(this).text()+".gif) no-repeat");
		$(this).text(" ");
	});
 
	$("#searchby .form input").focus(function() { if ($(this).val()=="Search by") $(this).val(""); });
	$("#searchby .form input").blur(function() { if ($(this).val()=="") $(this).val("Search by"); });
 
	$(".message_board li:last").css("border","none");
 
	// $("#main > .c > .q > a > .tip").each(function() {
	// 	var offset = $(this).offset();
	// 	
	// 	var p_top = offset.top - 100;
	// 	var p_left = offset.left + 10;
	// 	
	// 	var idtooltip = $(this).attr("id").substring(7);
	// 	var image_title_switch = "";
	// 	
	// 	$(this).hover(function() {
	// 		image_title_switch = $(this).attr("src");
	// 		$(this).attr("src", $(this).attr("title"));
	// 		$(this).attr("title", "");			
	// 	  }, function() {
	// 		$(this).attr("title", $(this).attr("src"));
	// 		$(this).attr("src", image_title_switch);
	// 	  });
	// 	
	// 	$(this).mouseover(function() {
	// 
	// 	  
	// 
	// 	setTimeout(function() {
	// 		$("#tooltip").css({"top": p_top+"px", "left": p_left+"px"});
	// 
	// 		$.ajax({
	// 			$type: "GET",
	// 			url: "/series/" + idtooltip,
	// 			beforeSend: function() {
	// 				$("#tooltip").html("<div class=\"loading\"></div>");
	// 			},
	// 			success: function(content) {
	// 				$("#tooltip").html(content);
	// 				$("#tooltip").show();
	// 			}				
	// 		});	
	// 	}, 500);
	// 	});
	// 	
	// 	$(this).mouseout(function() {
	// 		$(this).show();
	// 	});
	// 	
	// });
 
	// $("#tooltip").mouseleave(function() {
	// 	$("#tooltip").hide();
	// });
 
// PROCEDURA PER CANCELLARE UN FILE CARICATO CON UPLOAD DAL SITO
 
	$(".x_del").click(function() {
		var campo = "file_upload";
		if (conferma_cancellazione()) {
			$.ajax({
				type: "POST",
				url: "urlfilexcancellarefilecaricato",
				data: "campo="+campo,
				success: function(msg) {
					$("#bl_"+campo+"text").hide();
					$("#bl_"+campo).show();
					$("#"+campo+" .text").text("");
				}
			});
		}
	});
 
	$("#content").mouseover(function(e) {
		var h = docommand(e,"src");
		var j = docommand(e,"id");
		if ((h==null) && (j!="tooltip") ) $("#tooltip").hide();
//		if ( (h=="wrapper") || (h=="main") ) $("#tooltip").hide();
	});
 
	$(".remember_activation").click(function() {
		$.ajax({
			type: "POST",
			url: "/resendactivation",
			data: "email="+$("#user_session_email").val(),
			success: function() {
				$(".flash_error").fadeOut();
			}
		});
	});
 
	$(".box ul").each(function() { $(this).addClass("clearfix") });
 
	$(".has_rollover").each(function() {
		var rollover = $(this).find(".rollover:first");
		$(this).data("rollover", { over: rollover.attr("title"), out: rollover.attr("src"), target: rollover });
		$(this).hover(
			function() { $(this).data("rollover").target.attr("src", $(this).data("rollover").over) },
			function() { $(this).data("rollover").target.attr("src", $(this).data("rollover").out) }
		);	
		rollover.attr("title", "");
	});
 
	$(".box li:has(.tip)").each(function() {
		var altezzabox = $(this).height();
		var altezzatip = $(this).find("p").height();
		var margine = altezzabox + altezzatip + 10;
		$(this).find(".tip").css("top","-"+margine+"px");
		$(this).find(".arrow").css("top","-"+margine+"px");
	});
 
	$(".box li:has(.tip)").each(function() {
		$(this).data("tip", {timer: null }).hover(
			function() { 
				var o = $(this); 
				o.data("tip").timer = setTimeout(function() { o.find(".tip").fadeIn("medium",function() {o.find(".arrow").show()}) }, 1000); 
			},
			function() { 
				clearTimeout($(this).data("tip").timer);
				$(this).find(".arrow").hide();
				$(this).find(".tip").fadeOut();
			}
		);
	});
 
	$(".box li:has(.tipweek)").each(function() {
		var altezzabox = $(this).height();
		var altezzatip = $(this).find("p").height();
		var margine = altezzabox + altezzatip - 150;
		$(this).find(".tipweek").css("top","-"+margine+"px");
	});
 
	$(".box li:has(.tipweek)").each(function() {
		$(this).data("tipweek", {timer: null }).hover(
			function() { 
				var o = $(this); 
				o.data("tipweek").timer = setTimeout(function() {
				o.css({"position":"relative","z-index":"100"}).siblings().css("position","static");
				o.find(".tipweek").fadeIn("medium",function() {o.find(".arrow").show()})
				}, 1000); 
			},
			function() { 
				$(this).css("z-index","98");
				clearTimeout($(this).data("tipweek").timer);
				$(this).find(".arrow").hide();
				$(this).find(".tipweek").fadeOut();
			}
		);
	});
 
	$(".go_to_top").live("click",function() {
	 	var offset = $(this).offset();
		// var offset_top = offset.top - 260;
		// 		if($.browser.msie && $.browser.version=="6.0") {
			offset_top = offset.top;
		//}
 
		var speed = offset_top / 1200 * 1000;
		$("html, body").animate({ scrollTop: 0 }, speed);
	});
 
	$(".ttop").after('<div style="clear:both; height: 80px;"><p class="go_to_top"><a href="#">Top</a></p></div>');
 
});
 
function resizedivs() {
	$(".resize").each(function() {
		newsize = Math.floor($(this).height()/80);
		resto = Math.floor($(this).height()%80);
		if (resto>0) {
			newsize++;
			newsize = 80*newsize;
			$(this).height(newsize);
		}
	});
}
 
function conferma_cancellazione() {
	return confirm("Vuoi cancellare questo file?");
}
 
function docommand(evnt,tp)
{ 
   if (evnt.srcElement) 
   {
	if (tp=="src") {
      if (evnt.srcElement.getAttribute('src')) return evnt.srcElement.getAttribute('src'); 
	} else {
      if (evnt.srcElement.id) return evnt.srcElement.id; 		
	}
   } 
   else if (evnt.target) 
   {
	if (tp=="src") {
	   if (evnt.target.getAttribute('src')) return evnt.target.getAttribute('src');
	} else {
	   if (evnt.target.id) return evnt.target.id;		
	}
   } 
}
 
 
function calcola() {
	if($.browser.msie){
		larghezza = document.body.clientWidth;
		} else {
		larghezza = window.innerWidth - 12;
	}
 
//	var altezzafascia = Math.round(larghezza / 3.5);
 
	if (larghezza>960) {
		var margine_left = (3200 - larghezza) / 2;
		$("#fascia").css("margin-left","-"+margine_left+"px");
//		$("#fascia").css("margin-left","-"+margine_left+"px").css("height",altezzafascia+"px");
//		$("#header").css("height",altezzafascia+"px");
	}
}
 
