var settings_webtv = {
	host					: "http://www.dolmedia.tv",
	playlist				: plist,
	play_item				: playitem,
	main_swf				: "/js/player_46.swf",
	skin_swf				: "/js/skin.swf",
	player_width			: playerwidth,
	player_height			: playerheight,
	player_color			: playercolor,
	player_logo				: playerlogo,
	player_id				: "player",
	palinsesto_id			: "palinsesto",
	embed_config			: embedconfig,
	embed_code		 		: escape('<object width="640" height="360"><param name="movie" value="http://youtelethon.telethon.it/js/player_46.swf"><param name="allowfullscreen" value="true"><param name="flashvars" value="config=http://youtelethon.telethon.it/__CONFIG__.xml&amp;file=__FILE__"><embed src="http://youtelethon.telethon.it/js/player_46.swf" type="application/x-shockwave-flash" flashvars="config=http://youtelethon.telethon.it/__CONFIG__.xml&amp;file=__FILE__" allowfullscreen="true" width="640" height="360"></embed></object>')
};



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)
	}
}

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;
			webtv.movie = gethostname() + webtv.config.playlist;
			webtv.init_widget();
			webtv.init_selectplaylist();
			webtv.init_playlist();
			webtv.init_embedarea();
			webtv.init_player_playlist(webtv.movie, false);
			
			$(".noaction").unbind("click");
			$(".sceltapalinsesto li.noaction").unbind("mouseenter");
		},
		
		init_widget: function() {
			//jTelethonMaratonaLive("totAmountLive09Txt", '2009');
			jTelethonMaratonaLive("totAmountLive09Img", '2009', 'img');
		},
		
		init_selectplaylist: function() {
			$("#selectplaylist ul li").click(function(){
				if (!$(this).hasClass("active")) {
					$("#selectplaylist ul li").removeClass("active");
					$(this).addClass("active");
					plist = this.id.replace("selectplaylist_", "");
					$("#playlist ul").addClass("hide");
					$("#playlist_" + plist).removeClass("hide");
					plistfile = gethostname() + "/xml/videos_of_playlist/" + plist;
					webtv.load(plistfile);
					//$("#playlist-content").jScrollPane();
					webtv.current = 0;
					webtv.play_item(webtv.current);
					webtv.set_activeitem(webtv.current);
				}
			})
		},
		
		init_playlist: function() {
			//$("#playlist-content").jScrollPane();
			$("#playlist-content li").live("click", function(){
				if (!$(this).hasClass("novideo")) {
					vitem = this.id.replace("playlistitem_","");
					webtv.play_item(vitem);
					webtv.set_activeitem(vitem);
				}
			});
		},

		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 = gethostname() + webtv.config.main_swf;
			$('#' + webtv.config.player_id).replaceWith('<div id="mediaplayer"></div');

		var flashvars = {
			'file' 				: webtv.movie, //'http://telethon.dolwebcast.dev.dol.it/xmltmp/1613.xml',
			'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' 		: 'fill',
			'repeat'			: 'always',
			'icons'				: false,
			'provider'			: 'rtmp',
			//'streamer'			: 'rtmpt://flashserver.dol.it/dolmedia',
			'livestream.devkey' : 'zcDy30nhs2dmGwKYr0GU_loP88C1dw4p5Xn9zm7Jnv6wmVQBerVUblEyO-KedTskvCYjNYrPT4_gUMNjZaxf5w'

			//'livestream.devkey' : '2Kua7lQIoPHF8UM-jVXyrmdZCSUmFxC9fWxIITUlWVCtWsMtJ7Mp9Az0nKGwbPn9kZF64gUN9cbsHqdaf7KOOmIcFKw7NyyU-dGzMoT_We4'				
		};


//			var flashvars = {
//				'file' 				: '/unicef/clip/videomsg_spadafora_20anni.flv',
//				'streamer'          : 'rtmpt://flashserver.dol.it/dolmedia',
//				'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',
//				'provider'          : 'rtmp'				
//			};


			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", webtv.config.player_width, webtv.config.player_height, "9.0.0", false, flashvars, params, attributes);
			
			webtv.player = $("#"+ webtv.config.player_id);
		},
		
		set_activeitem: function(i) {
			$("#playlist-content li").removeClass("active");
			$("#playlist-content li#playlistitem_"+i).addClass("active");
			$("#videoinfo .title").html(webtv.player.getPlaylist()[i].title);
			$("#videoinfo .abstract").html(webtv.player.getPlaylist()[i].description);
		},
		
		// 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);
		},


		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])
			})
		}


	}
}();


$(function() {
	$.ajaxSetup ({ cache: false});
	webtv.init();
});


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;
	} 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) {
	webtv.current = obj.index;
	webtv.current_videoid = webtv.player.getPlaylist()[webtv.current].videoid;
	webtv.current_title = webtv.player.getPlaylist()[webtv.current].title;
	webtv.set_activeitem(webtv.current);
}





