﻿var oldie = false;

var settings_webtv = {
	host					: "http://",
	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="480" height="270"><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="480" height="270"></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;
			webtv.movie = gethostname() + webtv.config.playlist;
			webtv.init_palinsesto();
			webtv.init_embedarea();
			webtv.init_player_playlist(webtv.movie, false);
			
			$(".noaction").unbind("click");
		},
		
		init_palinsesto: function() {
			$('#palinsestolist').css("overflow", "auto").jScrollPane({animateTo:true, animateInterval:50, animateStep:5, scrollbarWidth : 0, scrollbarMargin : 0});

			var current_arrow_button;
			var current_arrow_direction;
			var current_arrow_interval;
			function while_arrow_button_down() {
				$.jScrollPane.active[0].scrollBy(50*current_arrow_direction);
			};
			function on_arrow_mouse_up(event) {
				$('html').unbind('mouseup', on_arrow_mouse_up);
				current_arrow_button.removeClass('active_arrow_button');
				clearInterval(current_arrow_interval);
			};
			function on_arrow_mouse_down() {
				$('html').bind('mouseup', on_arrow_mouse_up);
				while_arrow_button_down();
				current_arrow_interval = setInterval(while_arrow_button_down, 200);
			};
	
			$("#down").mousedown(function() {
				current_arrow_button = $(this);
				current_arrow_direction = 1;
				on_arrow_mouse_down();
				this.blur();
				return false;
			});
	
			$("#up").mousedown(function() {
				current_arrow_button = $(this);
				current_arrow_direction = -1;
				on_arrow_mouse_down();
				this.blur();
				return false;
			});

			$("#palinsesto .palinsesto_item a").live("click", function(){
				webtv.set_active($(this).parent()[0]);
				vitem = $(this).parent()[0].id.slice(16);
				webtv.play_item(vitem);
			});
			
			$("#palinsesto .palinsesto_item_pl a").live("click", function(){
				webtv.set_active($(this).parent()[0]);
				vitem = $(this).parent()[0].id.slice(16);
				webtv.play_item(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 = 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' 		: 'normal',
				'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", "603", "338", "9.0.0", false, flashvars, params, attributes);
			
			webtv.player = $("#"+ webtv.config.player_id);
		},
		
		init_images: function() {
			if ($("#palinsesto .palinsesto_item_pl").length > 0) {
				$("#palinsesto .palinsesto_item_pl a").css("background", "url(/img/nottesenzafrontiere/playlist_giu.png) no-repeat 0 0;");
				$("#palinsesto .palinsesto_item_pl a:first").css("background", "url(/img/nottesenzafrontiere/playlist_su.png) no-repeat 0 0;");
				
				$("#palinsesto .palinsesto_item_pl").live("click", function(){
					webtv.set_active($(this)[0]);
					vitem = $(this)[0].id.slice(16);
					webtv.play_item(vitem);
				});
				$(document).pngFix();
				
				
				$("#palinsesto .palinsesto_item_pl").hover(function(){
					$(this).find("a").css("background", "url(/img/nottesenzafrontiere/playlist_su.png) no-repeat 0 0");
					$("#palinsesto").pngFix();
				}, function() {
					if (!$(this).hasClass("active")) {
						$(this).find("a").css("background", "url(/img/nottesenzafrontiere/playlist_giu.png) no-repeat 0 0");
						$("#palinsesto").pngFix();
					}
				})
				
				
			} else {
			
				$("#palinsesto .palinsesto_item a").css("background", "transparent");
				$("#palinsesto .palinsesto_item a:hover").css("background", "transparent");
				$("#palinsesto .mastandrea").html("<img width='209' height='112' src='/img/nottesenzafrontiere/mastrandrea_su.png' />");
				$("#palinsesto .bergamasco").html("<img width='209' height='111' src='/img/nottesenzafrontiere/bergamasco.png' />");
				$("#palinsesto .vergassola").html("<img width='209' height='113' src='/img/nottesenzafrontiere/vergassola.png' />");
				$("#palinsesto .rossi").html("<img width='209' height='118' src='/img/nottesenzafrontiere/rossi.png' />");
				$("#palinsesto .mixo").html("<img width='211' height='118' src='/img/nottesenzafrontiere/mixo.png' />");
				$("#palinsesto .senatore").html("<img width='211' height='118' src='/img/nottesenzafrontiere/senatore.png' />");
				
				$(document).pngFix();
				
				$("#palinsesto .mastandrea").hover(function(){
					$(this).html("<img width='209' height='112' src='/img/nottesenzafrontiere/mastrandrea_su.png' />");
					$(this).pngFix();
				}, function(){
					if (!$(this).parent().hasClass("active")) {
						$(this).html("<img width='209' height='112' src='/img/nottesenzafrontiere/mastrandrea.png' />");
						$(this).pngFix();
					}

				})
				
				$("#palinsesto .bergamasco").hover(function(){
					$(this).html("<img width='209' height='111' src='/img/nottesenzafrontiere/bergamasco_su.png' />");
					$(this).pngFix();
				}, function() {
					if (!$(this).parent().hasClass("active")) {
						$(this).html("<img width='209' height='111' src='/img/nottesenzafrontiere/bergamasco.png' />");
						$(this).pngFix();
					}
				})
				
				$("#palinsesto .vergassola").hover(function(){
					$(this).html("<img width='209' height='113' src='/img/nottesenzafrontiere/vergassola_su.png' />");
					$(this).pngFix();
				}, function() {
					if (!$(this).parent().hasClass("active")) {
						$(this).html("<img width='209' height='113' src='/img/nottesenzafrontiere/vergassola.png' />");
						$(this).pngFix();
					}
				})

				$("#palinsesto .rossi").hover(function(){
					$(this).html("<img width='209' height='118' src='/img/nottesenzafrontiere/rossi_su.png' />");
					$(this).pngFix();
				}, function() {
					if (!$(this).parent().hasClass("active")) {
						$(this).html("<img width='209' height='118' src='/img/nottesenzafrontiere/rossi.png' />");
						$(this).pngFix();
					}
				})
				
				$("#palinsesto .mixo").hover(function(){
					$(this).html("<img width='211' height='118' src='/img/nottesenzafrontiere/mixo_su.png' />");
					$(this).pngFix();
				}, function() {
					if (!$(this).parent().hasClass("active")) {
						$(this).html("<img width='211' height='118' src='/img/nottesenzafrontiere/mixo.png' />");
						$(this).pngFix();
					}
				})
				
				$("#palinsesto .senatore").hover(function(){
					$(this).html("<img width='211' height='118' src='/img/nottesenzafrontiere/senatore_su.png' />");
					$(this).pngFix();
				}, function() {
					if (!$(this).parent().hasClass("active")) {
						$(this).html("<img width='211' height='118' src='/img/nottesenzafrontiere/senatore.png' />");
						$(this).pngFix();
					}
				})
			}
			
		},
		
		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) {
			if (!$(obj).hasClass("active")) {
				if ($("#palinsesto .palinsesto_item_pl").length > 0) {
					$("#palinsesto .palinsesto_item_pl").removeClass("active");
					if (oldie) {
						$("#palinsesto .palinsesto_item_pl a").css("background", "url(/img/nottesenzafrontiere/playlist_giu.png) no-repeat 0 0");
						$(obj).find("a").css("background", "url(/img/nottesenzafrontiere/playlist_su.png) no-repeat 0 0");
						$("#palinsesto").pngFix();
					}
				} else {
					$("#palinsesto .palinsesto_item").removeClass("active");
					if (oldie) {
						$("#palinsesto .palinsesto_item img").each(function(){
							if ($(this).attr("src").indexOf("_su") > 0) {
								var imgurl = $(this).attr("src").replace("_su.png", ".png");
								$(this).attr("src", imgurl)
							} else {
								var imgurl = $(this).attr("src").replace(".png", "_su.png");
								$(this).attr("src", imgurl)
							}
							$(this).pngFix();
						})
					}
				}

				$(obj).addClass("active");
					
			}
		}, 
		
		
		// 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() {
	if (!$("#palinsesto .palinsesto_item_pl").length > 0) {
		images = ["mastrandrea.png","bergamasco_su.png", "vergassola_su.png", "rossi_su.png", "celestini_su.png", "mixo_su.png", "senatore_su.png"]
		for (i=0; i<6; i++) {
			var pic = new Image();
			pic.src = "/img/nottesenzafrontiere/" + images[i];  
		}
	}
	if (($.browser.msie) && ($.browser.version.substr(0,3) == "6.0" || $.browser.version.substr(0,3) == "5.5" ))  {
		oldie = true;
		webtv.init_images();
	}
	$.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;
		webtv.set_active($("#palinsesto_item_" + webtv.current)[0]);
	} 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_active($("#palinsesto_item_" + webtv.current)[0]);
}




