var prefixDevil = "";
if(location.href.indexOf("/live/dl/") != -1) {
	prefixDevil = "/live";
}

if(typeof(DEBUGRAITV)=='undefined'){
	DEBUGRAITV=false;
};

$(function()
{
	if (typeof(window.skipInitElencoTematiche) != 'undefined' &&
			window.skipInitElencoTematiche == true) {
		return;
	}
	$("UL.elencoTematiche LI>A").click(function() {
		$("UL.elencoTematiche LI").attr("id","");
		var setUrl = $(this).attr("id");
		if(__TAB == "W") {
			//alert(staticUrl.replace(/#action#/,setUrl));
			url = staticUrl.replace(/#action#/,setUrl);
			$(this).parent().attr("id","selected");
			//setNielsen(prefixDevil+"/dl/RaiTV/programmi/liste/"+urlSet+"-"+__TAB__URL+"-0.html");
		} else {
			if (DEBUGRAITV) {alert("cont 0");}
			if($(this).next(".sottoTematica").length!=0) {
				if (DEBUGRAITV) {alert("cont 1");}
				$("UL.sottoTematica").hide();
				$(this).next().show();
				if($(this).parent().attr("class") == "") {
					if (DEBUGRAITV) {alert("cont 1.1");}
					$(this).parent().attr("id","unselectedDown");
					$(this).next().children("LI:visible:first").attr("id","selected");
					setUrl = $(this).next().children("LI:visible:first").children().attr("id").toString();
				} else {
					if (DEBUGRAITV) {alert("cont 1.2");}
					$(this).parent().attr("id","selected");
					$(this).parents(".sottoTematica").parent().attr("id","unselectedDown");
				}
			} else {
				if (DEBUGRAITV) {alert("cont 2");}
				if($(this).parents(".sottoTematica").length == 0) {
					if (DEBUGRAITV) {alert("cont 3");}
					$("UL.sottoTematica").hide();
					$(this).parent().attr("id","selected");
				} else {
					if (DEBUGRAITV) {alert("cont 4");}
					$(this).parents(".sottoTematica").parent().attr("id","unselectedDown");
					$(this).parent().attr("id","selected");
				}
			}
			var url = prefixDevil+"/dl/RaiTV/programmi/liste/"+setUrl+".html";
			if(pageVisited == "page") {
				url = prefixDevil+"/dl/RaiTV/programmi/liste/"+setUrl+"-"+__TAB+"-0.html";
			}
		}

		$.ajax({
		  url: url,
		  cache: false,
		  success: function(html){
			if (html.indexOf('<title>')<0) {
				$("#mediaList").html(html);
				setView();
				$("#mediaList").fadeIn("slow", function () {
					setReflection(".reflect");
					pagination();
					rewriteHREF();
					applyPngFix();
				});
				setNielsen(url);
			} else {
				$.ajax({
				  url: PATH_NO_CONTENT,
				  cache: false,
				  success: function(html){
					$("#mediaList").html(html);
					setView();
					$("#mediaList").fadeIn("slow", function () {
						setReflection(".reflect");
					});
					setNielsen(url);
				  }
				});
			}
		  },
		  error: function(html){
			$.ajax({
			  url: PATH_NO_CONTENT,
			  cache: false,
			  success: function(html){
				$("#mediaList").html(html);
				setView();
				$("#mediaList").fadeIn("slow", function () {
					setReflection(".reflect");
				});
				setNielsen(url);
			  }
			});
		  }
		});
	  return false;
	});
});

/* auto refresh */
if (typeof(refreshByJS) == 'undefined') {
	var refreshByJS = true;
}
function tryAutoRefresh() {
	var canRefresh = true;
	$('object,embed').each(function() {

		if (/Lettore_Rai_VOD\.xap/.test($('param[name=source]', this).attr('value'))// vecchio player SL
			||
				((/\/dl\/RaiTV\/flash\/podcast\.swf/.test($('param[name=src]', this).attr('value'))
				|| /\/dl\/RaiTV\/flash\/podcast\.swf/.test($(this).attr('src'))) && 'playerRSS' == $(this).attr('name')) // podcast
			|| (/audio\/x-pn-realaudio-plugin/.test($(this).attr('type')) && 'live' == $(this).attr('name'))
			) {
			canRefresh = false;
		}
		// podcast
		// real audio
		var res = 0;
		try {
			var res = this.Content.UniPlayerController.GetPlayerState();
		} catch (e) {
			res = 0; // l'object non e' il player unico :P
		}
		if (res != 0) {
			canRefresh = false;
		}
	});
	// if($('.ajaxwidget .editor').length > 0){
	if (/\/community\//.test(''+document.location)) {
		canRefresh = false;
	}
	try {
		console.log("tryAutoRefresh::canRefresh = "+canRefresh);
	} catch (e) {}

	if (refreshByJS && canRefresh) {
		var hash = '#'+(''+document.location.hash).replace(/^#/, '');
		if (hash == '#') {
			hash = '';
		}
		var search = '?'+(''+document.location.search).replace(/^\?/, '');
		if (search == '?') {
			search = '';
		}
		if (/\?./.test(search)) {
			if (!/refresh_ce/.test(search)) {
				search += '&refresh_ce';
			}
		} else {
			search += '?refresh_ce';
		}
		var href = document.location.protocol+'//'+document.location.hostname+(document.location.port!=''?':'+document.location.port:'')+document.location.pathname+search+hash;
		document.location.replace(href);
	}
}
function initAutoRefresh() {
	window.setInterval(tryAutoRefresh, 300000); // prova l'autorefresh ogni 5 minuti
}
$(initAutoRefresh);

