/**
 * @author Maurizio Mangione
 * @verison 1.0
 */

//get URL parameters
function queryString(parameter) {
	var loc = location.search.substring(1, location.search.length);
	var param_value = false;

	var params = loc.split("&");
	for (i=0; i<params.length;i++) {
		param_name = params[i].substring(0,params[i].indexOf('='));
		if (param_name == parameter) {
			param_value = params[i].substring(params[i].indexOf('=')+1)
		}
	}
	if (param_value) {
		return param_value;
	} else {
		return false;
	}
}

//initialize the variable needed to check if the user changed the current playlist
var pList;

//VALE inserisco variabile per gestione pagine squadra
var selTeam;

//VALE inserisco variabile per gestione click all'onload della pagina (non deve chiamare changePlaylist)
var init = true;


//get the video and channel id from flash
function clickVideo(videoid,channelid,widgetId) {
	//if a team icon is clicked, the channeilid is = to the team name
	if (selTeam != undefined) {
		channelid = selTeam;
	}
	//if the playlist is a stub to link to the premium player
	if(videoid.indexOf("pay")>=0) {
		window.location.href = "http://www.gazzetta.it/contentDeliver/golA/loginGtv.jsp";
	}
	//if the requested video isn't from the current channel, retrieve the URL and go to the channel
	else if((channelid == currentChannel && widgetId == "pm") || (currentChannel == "Home" && pList != "changed" && widgetId == "pm")) {
		$.scrollTo(".tv-player");
		player.playVideo(videoid);
	} else {
		var goTo = $("#gtv-navigation li a[rev="+ channelid +"]").attr("href");
		if(goTo == undefined) {
			var goTo = $(".gtv-subnavigation li a[rev="+ channelid +"]").attr("href");
			if (goTo == undefined) {
				var goTo = "/archivio/archivio/";
			}
		}
		//the video id is passed to the new page
		window.location.href = goTo + "?videoid=" + videoid;
	}
}

//adv functions
function refreshAdv(banner_id, oas_random, oas_url) {
	/*
	var suffixTemp = oas_url.split("@")[1];
	var suffix = suffixTemp.split("!")[0];
	$("#tv-adv iframe").attr("src","http://oas.rcsadv.it/RealMedia/ads/adstream_sx.ads/gazzetta.it/tv/" + currentChannel + "/tv/" + oas_random + "@" + suffix + "!" + banner_id);
	*/
	if(oas_url.indexOf("${RCS_NEWSPAPER}")>0)
	{
		oas_url = oas_url.replace("${RCS_NEWSPAPER}","gazzetta");
	}
	if(oas_url.indexOf("${PLAYER_CONFLABEL}")>0)
	{
		oas_url = oas_url.replace("${PLAYER_CONFLABEL}","tv");
	}
	if(oas_url.indexOf("${RCS_CHANNEL}")>0)
	{
		oas_url = oas_url.replace("${RCS_CHANNEL}",currentChannel);
	}
	if(oas_url.indexOf("${OAS_RANDOM}")>0)
	{
		oas_url = oas_url.replace("${OAS_RANDOM}",oas_random);
	}
	var prefix = oas_url.split("!")[0];
	$("#tv-adv iframe").attr("src", prefix +  "!" + banner_id);
}

function searchAdv(){
	if(currentChannel == "search") {
		randSeven = Math.floor(1000000+(Math.random()*(9999999-1000000)));
		$("#tv-adv iframe").attr("src","http://oas.rcsadv.it/RealMedia/ads/adstream_sx.ads/gazzetta.it/tv/Home/tv/" + randSeven + "@Frame1");
	}
}

//change playlist
function changePlaylist(node) {
	//if search is the current channel, go to the clicked menu URL (the search page doesn't contains any playlist)
	if (currentChannel == "search") {
		var menuURL = $(node).attr("href");
		window.location.href = menuURL;
	} else {
		//get the file containing the playlist ID
		var idUrl = node.attr("name");
		pList = "changed";
		//change playlist
		$.ajax({
			url: "/ssi/2008/boxes/gazzettatv/uuid-" + idUrl,
			success: function(data){
				menu.cambiaPlaylist(data);
			},
			error: function(){
				console.log("ajax error");
			}
		});
	}
}

$(document).ready(function(){

	//menu behaviours
	$("#gtv-navigation li a").click(function(){
		//set active state
		$("#gtv-navigation li.active").removeClass("active");
		$(this).parent("li").addClass("active");

		if($(this).attr("rel") != "external") {
			//show/hide the setSub
			var sub = $(this).attr("class");
			if(sub != "") {
				$(".gtv-subnavigation").hide();
				$(".gtv-subnavigation#"+sub).show();
				if (currentChannel != "search") {
					//click on the deafult submenu link (active)
					$(".gtv-subnavigation#" + sub + " li.active a").click();
				}
			} else {
				//get the "context" parameter
				var context = $(this).text();

				//hide the setSub
				$(".gtv-subnavigation").hide();
				//change the playlist with the requested uuid
				changePlaylist($(this));
			}
			return false;
		} else {
			//if the link is an external one, go to the defaul href location
			var loc = $(this).attr("href");
			window.location.href= loc;
		}
	});

	$(".gtv-subnavigation>li>a").click(function(event){
		if ($(this).attr("rel") != "external") {
			event.preventDefault();
			//get the "context" parameter
			var context = $(this).text();

			//get the normalized name
			var normalized = $(this).attr("name");

			//get the id from the parent <ul>
			var activeId = $(this).parents("ul").attr("id");

			//set active state
			$("#" + activeId + " li.active").removeClass("active");
			$(this).parent("li").addClass("active");

			//change the playlist with the requested uuid
			//VALE inserisco if
			if (init == false) {
				changePlaylist($(this));
			}
		} else {
			//if the link is an external one, go to the default href location
			loc = $(this).attr("href");
			window.location.href= loc;
		}
	});

	//initialize the tooltip for the team images
	$(".team li a").tooltip({
		extraClass: "tt",
		left: 0,
		top: 20
	});

	$(".team li a").click(function(event){
		event.preventDefault();
		$(this).parents("ul").find("li.active").removeClass("active");
		selTeam = $(this).attr("rev");
		//change the playlist with the requested uuid
		changePlaylist($(this));
	});

	//initialize the menu with the current active tab
	if (typeof setMain != "undefined") {
		if (setMain != "") {
			$("#gtv-navigation li a").filter(function(){
				return $(this).text() == setMain;
			}).parent("li").addClass("active");
		}
		if (setSub != "") {
			$("#gtv-navigation li a").filter(function(){
				return $(this).text() == setMain;
			}).click();

			$(".gtv-subnavigation li a").filter(function(){
				return $(this).text() == setSub;
			}).parents("ul").find("li.active").removeClass("active");

			if((setMain == "GOL A" || setMain == "GOL B") && setSub != "ULTIMA GIORNATA") {
				$(".team").parents("ul").find("li.active").removeClass("active");
			} else {
				$(".gtv-subnavigation li a").filter(function(){
					return $(this).text() == setSub;
				}).parent("li").addClass("active");
			}
		}
		init = false;
	}

	//search function
	$("#search-input").focus(function(){
		if($(this).val() == "Cerca in Gazzetta Tv") {
			$(this).val("");
		}
	});

	$("#hsearch-submit").click(function(){
		$("#header-search").submit();
	});

	$("#header-search").submit(function(){
		var q = $("#search_val").val();
		window.location.href = "/search.shtml?q=" + q;
		return false;
	});

	$("#tv-search form").submit(function(){
		var q = $("#search-input").val();
		window.location.href = "/search.shtml?q=" + q;
		return false;
	});

	if(currentChannel == "search") {
		//inizialize the search functions
		searchByUrl();
		//remove the active class from the default sub-channel
		$(".gtv-subnavigation li").removeClass("active");
	}

	//GazzaSpace box
	$("#gspace-login").live("click", function(){
		$("#login-gspace").submit();
	});

	$(".gspace-user, .gspace-password").live("click", function(){
		$(this).val("");
	});

	//adv function call
	searchAdv();

}); //end of doc ready