(function() {

	var window = this, undefined;
	var __DEBUG = window.__DEBUG || false;
	
		
	var host = window.location.protocol + "//" + window.location.hostname;

	var config = {
		mediaplayer		: "/js/mediaplayer.swf",
		mediaplayer_id	: "mp",
		width			: "100%",
		height			: "100%",
		embedcode		: escape('<object width="__WIDTH__" height="__HEIGHT__"><param name="movie" value="__HOST__/__MP__"><param name="allowfullscreen" value="true"><param name="flashvars" value="config=__HOST__/__CONFIG__&amp;file=__FILE__"><embed src="__HOST__/__MP__" type="application/x-shockwave-flash" flashvars="config=__HOST__/__CONFIG__&amp;file=__FILE__" allowfullscreen="true" width="__WIDTH__" height="__HEIGHT__"></embed></object>'),
		configfile		: host + '/webtvconfig.xml',
		player : {
			'file': '',
			
			'skin'		: "/js/skin.swf",
			'controlbar': 'over',
			'screencolor': '000000',
			'backcolor': '000000',
			'frontcolor': 'FFFFFF',
			'lightcolor': '0099FF',
			'icons': 'true',
			'logo.file': '',
			'logo.position': 'bottom-left',
			'logo.hide': 'false',
			
			'autostart': 'true',
			'bufferlength': 5,
			'item': 0,
			'playerready': 'player.init',
			'stretching': 'uniform'
		},
		videoinfo: function(info) {
			$("#videoinfo .title").html(info.title);
			$("#videoinfo .abstract").html(info.description);
		},
		set_current: function(selector, index) {
			$(selector).removeClass("current");
			$(selector + ":eq(" + index + ")").addClass("current")
		}
		
	}

	//funzioni del player
	var player = function() {
		var mediaplayer	= null;
		function init_listeners() {
			if (mediaplayer) {
				mediaplayer.addModelListener("STATE", "webtv.monitor_playerstate");
				mediaplayer.addControllerListener("ITEM", "webtv.monitor_playeritem");
			} else {
				setTimeout("init_listeners()", 1000);
			}
		};
		
		return {
			current: 0,
						
			load: function(movieobj) {
				mediaplayer.sendEvent('LOAD', movieobj);
			},

			stop: function() {
				mediaplayer.sendEvent('STOP');
			},
			
			play: function() {
				mediaplayer.sendEvent('PLAY', 'true');
			},
			
			pause: function() {
				mediaplayer.sendEvent('PLAY', 'false');
			},

			playpause: function() {
				mediaplayer.sendEvent('PLAY');
			},
	
			playitem: function(i) {
				mediaplayer.sendEvent('ITEM', i);
			},
	
			seek: function(pos) {
				mediaplayer.sendEvent('SEEK', pos);
			},
			
			getinfo: function(i) {
				return mediaplayer.getPlaylist()[i]
			},
			
			getconfig: function() {
				return mediaplayer.getConfig();
			},
		
			init: function(mp) {
				mediaplayer = document.getElementById(mp.id);
				init_listeners();
			}
		}
	}();
	
	//gestione delle playlist
	var playlist = function() {
		return {
			init: function() {
				$("#playlist-content").jScrollPane();
				$("#playlist-content>ul li").live("click", function(){
					i = $(this).index();
					info = player.getinfo(i);
					config.videoinfo(info);
					config.set_current("#playlist-content>ul li", i);
					player.playitem(i);
				});
			}
		}
	}();
	
	
	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 share = function() {
		function show_overlay(videoid, videotitle, videowidth, videoheight, embedcode) {
			linkcode = host + "/video/" + videoid;
			movie = host + "/xml/video/" + videoid;
			embedcode = unescape(config.embedcode).replace(/__HOST__/g, host).replace(/__CONFIG__/g, config.configfile).replace(/__FILE__/g, movie).replace(/__WIDTH__/g, videowidth).replace(/__HEIGHT__/g, videoheight).replace(/__MP__/g, config.mediaplayer)

			$("#videolink").val(linkcode);
			$("#videoembed").val(embedcode);

			sh = new sharelinks(linkcode, videotitle);
			$("#sharelinks a").each(function(){
				vitem = $(this)[0].id;
				$(this).attr("href", sh[vitem])
			})
		};
		return {
			init: function() {
				$("#condividi").click(function(){
					current_video = player.getinfo(player.current);
					videoid = current_video.videoid;
					videotitle = current_video.title;
					show_overlay(videoid, videotitle, config.width, config.height, config.embedcode);
					$(".overlay").show();
				});
				
				$("#embedarea .closeembed").click(function(){
					$(".overlay").hide();
				});
				
				$("#embedarea .embeddata").click(function() {
					this.focus();
					this.select();
				});
			}
		}
	}();
	
	//funzioni specifiche della webtv
	var webtv = function() {
		function init_config() {
			$.extend(true, config, window.settings);
		};
	
		function init_player() {
			/*
			var playerswf = $("#video").flash({
				swf: '/js/mediaplayer.swf',
				width: '100%',
				height: '100%',
				attrs: {
					id 		: "mp",
					name	: "mp"
				},
				params: {
					menu 				: "false",
					quality 			: "best",
					wmode 				: "transparent",
					allowfullscreen 	: "true",
					allowscriptaccess 	: "always",
					allownetworking 	: "all"
				},
				flashvars: config.player
			});
			*/
			
			var swf = config.mediaplayer;
			$('#' + config.mediaplayer_id).replaceWith('<div id="video"></div');
			
			var flashvars = config.player;
			var params = {
				menu 				: "false",
				quality 			: "best",
				wmode 				: "transparent",
				allowfullscreen 	: "true",
				allowscriptaccess 	: "always",
				allownetworking 	: "all"
			};

			var attributes = {
				id 					: config.mediaplayer_id,
				name				: config.mediaplayer_id
			};
			
			swfobject.embedSWF(swf, "video", config.width, config.height, "9.0.0", false, flashvars, params, attributes);
		};
		
		function init_playlist() {
			playlist.init();
		};
		
		function init_share() {
			$(".overlay").hide();
			share.init();
		}
		
		return {
			monitor_playerstate: function(obj) {
				current_state = obj.newstate; 
				previous_state = obj.oldstate; 
				//console.log("state: " + current_state);
			},
			
			monitor_playeritem: function(obj) {
				info = player.getinfo(obj.index);
				//console.log(info);
				config.videoinfo(info);
				config.set_current("#playlist-content>ul li", obj.index);
				player.current = obj.index;
			},
			
			init: function() {
				init_config();
				init_player();
				init_playlist();
				init_share();
			}
		}
	}();
	
	$(function() {
		webtv.init();
	})
	
	window.webtv = webtv;
	window.playlist = playlist;
	window.player = player;
	window.config = config;
	
})();
