var settings_webtv = {
	host					: "http://www.ilcannocchiale.tv",
	playlist				: plist,
	play_item				: playitem,
	main_swf				: "/js/player.swf",
	skin_swf				: "/js/skin.swf",
	player_color			: playercolor,
	player_logo				: playerlogo,
	player_id				: "player",
	palinsesto_id			: "palinsesto",
	embed_config			: embedconfig,
	embed_code		 		: escape('<object width="320" height="240"><param name="movie" value="__HOST__/js/player.swf"><param name="allowfullscreen" value="true"><param name="flashvars" value="config=__HOST__/__CONFIG__.xml&amp;file=__FILE__"><embed src="__HOST__/js/player.swf" type="application/x-shockwave-flash" flashvars="config=__HOST__/__CONFIG__.xml&amp;file=__FILE__" allowfullscreen="true" width="320" height="240"></embed></object>')
};

var webtv = function() {
	return {
		
		config: null,
		player: null,
		is_live: false,

		current_videoid: 0,
		current_file: 0,
		current_title: "",

		playlist: null,


		// LEGGE LA CONFIGURAZIONE E INIZIALIZZA TUTTO L'AMBARADAM
		init: function() {
			webtv.config = settings_webtv;
			if (webtv.config.playlist == "/xml/video/561") {
				webtv.is_live = true;
			}
			webtv.movie = gethostname() + webtv.config.playlist;
			webtv.init_ricerca();
			webtv.init_menu();
			webtv.init_sceltapalinsesto();
			webtv.init_palinsesto();
			webtv.init_embedarea();
			webtv.init_player_playlist(webtv.movie, false);
			
			$(".noaction").unbind("click");
			$(".sceltapalinsesto li.noaction").unbind("mouseenter");
		},
		
		init_ricerca: function() {
			$("#ricerca").focus(function(){
				if ($(this).val() == "Cerca") {
					$(this).val("");
				}
			});
			$("#ricerca").blur(function() {
				if ($(this).val().length == 0) {
					$(this).val("Cerca");
				}
			});
			$("#ricerca").click(function() {
				if ($(this).val().length > 0) {
					window.location = "/search/" + $("#ricerca").val();
				}
			});
			$("#ricerca").keypress(function (e) {
				if ((e.which == 13) && ($(this).val().length > 0)) {
					window.location = "/search/" + $("#ricerca").val();
				}
			});
		},


		init_menu: function() {
			$("#menu1 li:first").addClass("active");
			$("#menu li").click(function(){
				if (!$(this).hasClass("active")) {
					$("#menu li").removeClass("active");
					$(this).addClass("active");
					if ($(this).hasClass("tema1")) {
						$(".sceltapalinsesto").hide();
						$("#menu1").show();
					}
					if ($(this).hasClass("tema2")) {
						$(".sceltapalinsesto").hide();
						$("#menu2").show();
					}
					if ($(this).hasClass("tema3")) {
						$(".sceltapalinsesto").hide();
						$("#menu3").show();
					}
					if ($(this).hasClass("tema4")) {
						$(".sceltapalinsesto").hide();
						$("#menu4").show();
					}
				}
			});
		},
		
		set_palinsesto: function(obj) {
			$("#palinsesto h3").html($(obj).html())
			var theme_items  = ["#palinsesto", "#palinsesto-content li:first", "#videoinfo .title", "#embedvideo", "#embedarea", "#mediaplayer_wrap"];
			if ($(obj).parents("#menu1").length>0) {
				$.each(theme_items, function(i){
					$(theme_items[i]).removeClass("tema2").removeClass("tema3").addClass("tema1");
				});
			}
			if ($(obj).parents("#menu2").length>0) {
				$.each(theme_items, function(i){
					$(theme_items[i]).removeClass("tema1").removeClass("tema3").addClass("tema2");
				});
			}
			if ($(obj).parents("#menu3").length>0) {
				$.each(theme_items, function(i){
					$(theme_items[i]).removeClass("tema1").removeClass("tema2").addClass("tema3");
				});
			}
		},
		
		init_sceltapalinsesto: function() {
			if ($("#palinsesto.playlist").length>0) {
				webtv.playlist = $("#menu1 li:first")[0].id.split("__")[1];
			}
			$(".sceltapalinsesto li").mouseenter(function(){
				$(".sceltapalinsesto li").removeAttr("style").removeClass("active");
				$(this).animate({"width":"246px"}, "fast").addClass("active");
			});

			$(".sceltapalinsesto").mouseleave(function(){
				$(".sceltapalinsesto li").removeAttr("style").removeClass("active");
				$("#playlist__" + webtv.playlist).animate({"width":"246px"}, "fast").addClass("active");
			});
			
			$(".sceltapalinsesto li").click(function(){
				if (this.id != "playlist__" + webtv.playlist) {
					$(".sceltapalinsesto li").removeAttr("style").removeClass("active");
					webtv.reinit_palinsesto(this);
					$(this).animate({"width":"246px"}, 250).addClass("active");
					
					webtv.set_palinsesto(this);
				}
			});
			
			
		},
		
		init_palinsesto: function() {
			$("#palinsesto-content").jScrollPane();
			$("#palinsesto.playlist #palinsesto-content li").live("click", function(){
				vitem = this.id.slice(16);
				webtv.play_item(vitem);
				webtv.set_active(this, vitem);
			});
			$("#palinsesto.video #palinsesto-content li").live("click", function(){
				vid = this.id.slice(17);
				window.location = "/video/" + vid;
			});
		},
		
		reinit_palinsesto: function(obj) {
			function reset_scrollpane() {
				$("#palinsesto-content").jScrollPane();
			}
			$("#palinsesto-content ul").empty();
			var els = obj.id.split("__");
			webtv.playlist = els[1];
			plist = gethostname() + "/xml/videos_of_" + els[0] + "/" + els[1];
			if ($("#palinsesto").hasClass("playlist")) { 
				webtv.load(plist);
				$("#palinsesto-content ul").load("/panel/palinsesto_" + els[0] + "/" + els[1], '', reset_scrollpane);
			} else {
				$("#palinsesto-content ul").load("/panel/videos_of_playlist/" + els[1], '', reset_scrollpane);
			}
			
		}, 

		
		
		init_embedarea: function() {
			$("#embedvideo").click(function(){
				webtv.show_embed();
				$(".overlay").show();
			})
			$("#embedarea .closeembed").click(function(){
				$(".overlay").hide();
			})
			$("#embedarea .embeddata").click(function() {
				this.focus();
				this.select();
			});
		}, 

		// INZIALIZZA IL PLAYER PER UNA PLAYLIST
		init_player_playlist: function(pl, hd) {
			webtv.init_player(null, pl, hd);
			webtv.current_file = pl;
		},


		// INIZIALIZZA IL PLAYER 
		init_player: function(stream, movie, is_playlist) {

			//var swf = webtv.config.host + webtv.config.main_swf;
			var swf = gethostname() + webtv.config.main_swf;
			$('#' + webtv.config.player_id).replaceWith('<div id="mediaplayer"></div');

			var flashvars = {
				//'streamer' 			: stream,
				'file' 				: webtv.movie,
				'skin' 				: gethostname() + webtv.config.skin_swf,
				'logo'				: webtv.config.player_logo,
				'bufferlength' 		: '5',
				'volume' 			: '100',
				'backcolor' 		: '000000',
				'frontcolor' 		: 'ffffff',
				'lightcolor' 		: webtv.config.player_color,
				'screencolor' 		: '000000',
				'controlbar' 		: 'over',
				'autostart' 		: 'true',
				'item'				: webtv.config.play_item,
				'stretching' 		: 'uniform',
				'repeat'			: 'always'
			};

			var params = {
				menu 				: "false",
				quality 			: "best",
				wmode 				: "transparent",
				allowfullscreen 	: "true",
				allowscriptaccess 	: "always",
				allownetworking 	: "all"
			};

			var attributes = {
				id 					: webtv.config.player_id,
				name				: webtv.config.player_id
			};

			swfobject.embedSWF(swf, "mediaplayer", "420", "336", "9.0.0", false, flashvars, params, attributes);
			
			webtv.player = $("#"+ webtv.config.player_id);
		},
		
		show_embed: function(id) {
			host = gethostname();
			linkcode = host + "/video/" + webtv.current_videoid;
			movie = host + "/xml/video/" + webtv.current_videoid;
			embedcode = unescape(webtv.config.embed_code).replace(/__HOST__/g, host).replace(/__CONFIG__/g, webtv.config.embed_config).replace(/__FILE__/g, movie)
			$("#videolink").val(linkcode);
			$("#videoembed").val(embedcode);
			share = new sharelinks(linkcode, webtv.current_title);
			$("#sharelinks a").each(function(){
				vitem = $(this)[0].id;
				$(this).attr("href", share[vitem])
			})
		},
		
		set_active: function(obj, i) {
			i = i || 0;
			if (!$(obj).hasClass("active")) {
				$("#palinsesto-content ul li").removeClass("active");
				$(obj).addClass("active");
				$("#videoinfo .title").html($(".title", obj).html())
				abstract = $(".abstract", obj).html();
				if (webtv.player.getPlaylist()[i].format == "Focuseventi") {
					var mp4file = webtv.player.getPlaylist()[i].file.replace(".flv", ".mp4").replace("ws-dol/FdG/FocusEventi/", "/videos/fdg/focuseventi/")
					abstract = abstract + " [<a style='color:#FFF' href='" + mp4file + "'>Download video</a>]"; 
				}
				$("#videoinfo .abstract").html(abstract)
				
				/*
				if ($("#palinsesto.video #palinsesto-content li").length>0) {
					$(activevideo).addClass("active").unbind("click");
				}
				*/
			}
		}, 
		
		
		// Wrapping delle funzioni del player
		load: function(movieobj) {
			webtv.player.sendEvent('LOAD', movieobj);
		},
		
		stop: function() {
			webtv.player.sendEvent('STOP');
		},
		
		playpause: function() {
			webtv.player.sendEvent('PLAY');
		},
		
		play_item: function(i) {
			webtv.player.sendEvent('ITEM', i);
		},
		
		seek: function(pos) {
			webtv.player.sendEvent('SEEK', pos);
		}
	}
}();


var sharelinks = function(link, title) {
	return {
		twitter: "http://twitter.com/home?status=" + link,
		facebook: "http://www.facebook.com/share.php?u=" + link,
		friendfeed: "http://friendfeed.com/?url=" + link + "&title=" + escape(title),
		google: "http://www.google.com/bookmarks/mark?op=edit&bkmk="+ link + "&title=" + escape(title),
		current: "http://current.com/clipper.htm?url=" + link + "&title=" + escape(title),
		delicious: "http://del.icio.us/post?url=" + link + "&title=" + title,
		wikio: "http://www.wikio.it/vote?url=" + link,
		diggita: "http://www.diggita.it/submit.php?url=" + link,
		oknotizie: "http://oknotizie.alice.it/post.html.php?url=" + link + "&title=" + escape(title)
	}
}


$(function() {
	$.ajaxSetup ({ cache: false});
	webtv.init();
	if ($("#page.search").length>0) {
		$("#videolist .video").hover(function(){
			$(this).addClass("hover");
		}, function(){
			$(this).removeClass("hover");
		})
	}
});


function gethostname() {
	return window.location.protocol + "//" + window.location.hostname;
}


// FUNZIONI DEL PLAYER
function playerReady(mp) {
	webtv.player = document.getElementById(mp.id);
	addListeners();
};


function addListeners() {
	if (webtv.player) { 
		webtv.player.addModelListener("STATE", "stateListener");
		webtv.player.addControllerListener("ITEM", "itemListener");
		webtv.current = webtv.config.play_item;
		webtv.current_videoid = webtv.player.getPlaylist()[webtv.current].videoid;
		webtv.current_title = webtv.player.getPlaylist()[webtv.current].title;
		if ($("#palinsesto.playlist").length>0) {
			webtv.set_active($("#palinsesto_item_" + webtv.current)[0], webtv.current);
		} else {
			if ($(activevideo).hasClass("f__29")) {
				plist = $(activevideo).attr("class").replace("f__29", "").replace("active", "").replace(" ", "").slice(3);
				$("#menu li").removeClass("active");
				$("#menu li.tema3").addClass("active");
				$(".sceltapalinsesto li").removeClass("active");
				$("#playlist__" + plist).addClass("active");
				$("#menu1").hide();
				$("#menu3").show();
				webtv.playlist = plist
			} else {
				plist = $(activevideo).attr("class").replace("f__30", "").replace("active", "").replace(" ", "").slice(3);
				webtv.playlist = plist
			}
			webtv.set_active($(activevideo)[0], webtv.current);
		}
	} else {
		setTimeout("addListeners()", 1000);
	}
}

function stateListener(obj) {
	current_state = obj.newstate; 
	previous_state = obj.oldstate; 

	//IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	if ((current_state == "PAUSED") && (webtv.is_live)) {
		webtv.player.sendEvent('STOP')
	}
}

function itemListener(obj) {
	//alert(obj.index)
	webtv.current = obj.index;
	webtv.current_videoid = webtv.player.getPlaylist()[webtv.current].videoid;
	webtv.current_title = webtv.player.getPlaylist()[webtv.current].title;
	if ($("#palinsesto.playlist").length>0) {
		webtv.set_active($("#palinsesto_item_" + webtv.current)[0], webtv.current);
	} else {
		if ($(activevideo).hasClass("f__29")) {
			$("#palinsesto h3").empty();
			$("#palinsesto_content ul").empty();
			plist = $(activevideo).attr("class").replace("f__29", "").replace("active", "").replace(" ", "").slice(3);
			$("#menu li").removeClass("active");
			$("#menu li.tema3").addClass("active");
			$(".sceltapalinsesto li").removeClass("active");
			$("#playlist__" + plist).addClass("active");
			$("#menu1").hide();
			$("#menu3").show();
			webtv.set_palinsesto($("#playlist__" + plist)[0]);
			webtv.playlist = plist;
		} else {
			plist = $(activevideo).attr("class").replace("f__30", "").replace("active", "").replace(" ", "").slice(3);
			webtv.playlist = plist
		}
		webtv.set_active($(activevideo)[0], webtv.current);
	}
}





