﻿var settings_webtv = {
	host					: "http://cast.dolwebcast.com",
	playlist				: plist,
	main_swf				: "/js/player.swf",
	skin_swf				: "/js/skin.swf",
	player_id				: "player",
	palinsesto_id			: "palinsesto",
	embed_code		 		: escape('<object width="320" height="240"><param name="movie" value="http://www.dolwebcast.com/js/player.swf"><param name="allowfullscreen" value="true"><param name="flashvars" value="config=http://www.dolwebcast.com/player__TYPE__.xml&amp;file=__FILE__"><embed src="http://www.dolwebcast.com/js/player.swf" type="application/x-shockwave-flash" flashvars="config=http://www.dolwebcast.com/player__TYPE__.xml&amp;file=__FILE__" allowfullscreen="true" width="320" height="240"></embed></object>')
};


var settings_webcast = {
	slide_id				: "slide",
	slidelist_id			: "slidelist"
};


var webtv = function() {
	return {
		
		config: null,
		player: null,
		is_live: false,
		
		movie: "",
		current: 0,
		
		playlist: null,


		// LEGGE LA CONFIGURAZIONE E INIZIALIZZA TUTTO L'AMBARADAM
		init: function() {
			webtv.config = settings_webtv;
			webtv.movie = webtv.config.host + webtv.config.playlist;
			webtv.init_player_playlist(webtv.movie, false);
			webtv.init_palinsesto();
		},


		// 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;
			$('#' + webtv.config.player_id).replaceWith('<div id="mediaplayer"></div');

			var flashvars = {
				'streamer' 			: stream,
				'file' 				: webtv.movie,
				'skin' 				: webtv.config.host + webtv.config.skin_swf,
				'bufferlength' 		: '5',
				'volume' 			: '100',
				'frontcolor' 		: 'ffffff',
				'lightcolor' 		: 'FF0000',
				'controlbar' 		: 'over',
				'autostart' 		: 'true',
				'stretching' 		: 'fill',
				'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", "340", "272", "9.0.0", false, flashvars, params, attributes);
		},
		
		init_palinsesto: function() {
			$("#" + webtv.config.palinsesto_id + " ul").jcarousel({
				vertical: true,
				scroll: 4,
				buttonNextHTML: '<div class="down btn"></div>',
				buttonPrevHTML: '<div class="up btn"></div>'
			});
			
			$("#" + webtv.config.palinsesto_id + " .btn").hover(
				function() {
					$(this).addClass("active");
				},
				function() {
					$(this).removeClass("active");
				}
			);

			$("#" + webtv.config.palinsesto_id + " li.palinsesto_item").click(function(){
				var item = $(this).attr("id").slice(16);
				webtv.play_item(item);
			});
			
			$("#" + webtv.config.palinsesto_id + " li.palinsesto_item:first").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 webcast = function() {
	return {

		id: 0,
		config: null,
		file: "",
		dir: "",
		items: [],
		current: -1,
		is_live: false,
		is_ready: false,
		
		init: function(id) {
			webcast.config = settings_webcast;
			webcast.id = id;
			webcast.file = "/webcast/" + webcast.id;
			$.getJSON(webcast.file, function(data) {
				webcast.is_live = data["webcast"]["is_live"];
				webcast.dir = data["webcast"]["dir"];
				webcast.items = data["webcast"]["items"];
				webcast.is_ready = true;
				if ((webcast.is_live)) { // || (webcast.items.length == 0)) {
					webcast.init_comments();
				} else {
					if (webcast.items.length > 0) {
						webcast.init_thumbs();
						webcast.show_slide(0);
						webcast.init_comments();
					} else {
						$("#thumbnailsarea").hide();
						$("#questionarea").hide();
					}
				}
			});
		},
		
		init_comments: function() {
			$("#thumbnailsarea *").remove();
			$("#thumbnailsarea").append('<ul id="thumbs"></ul>')
			$("#thumbnailsarea").hide();
			$("#questionarea").show();
			$("#questionarea input.submit").click(function(){
				$.get("/question", { question_subject: $("#question_subject").val(), question_text: $("#question_text").val() }, function(data){
					$("#question_subject").val("");
					$("#question_text").val("");
					$("#msg").html("La sua domanda è stata inviata")
				})
			})
		},
		
		init_thumbs: function() {
			function item_load_callback(carousel, state) {
				for (var i = carousel.first; i <= carousel.last; i++) {
					if (carousel.has(i)) {
						continue;
					}
					if (i > webcast.items.length) {
						break;
					}
					carousel.add(i, item_html(webcast.items[i-1], i-1));
				}
				
				$("#thumbs li img").click(function(){
					var vitem = $(this).attr("id").slice(1);
					//webcast.slide(parseFloat(webcast.items[vitem].time));
					webtv.seek(parseFloat(webcast.items[vitem].time))
				})
			}
			
			function item_html(item, i) {
				url = webcast.dir + "s" + item.file;
				return '<img id="s' + i + '" src="' + url + '" alt="' + item.title + '" /><br />' + item.title + '';
			};
			
			$("#questionarea").hide();
			$("#thumbnailsarea *").remove();
			$("#thumbnailsarea").append('<ul id="thumbs"></ul>')
			$("#thumbnailsarea").show();
			
			$("#thumbs").html("").jcarousel({
				scroll: 1,
				visible: (webcast.items.length>8) ? 8 : webcast.items.length,
				buttonNextHTML: '<div class="right btn"></div>',
				buttonPrevHTML: '<div class="left btn"></div>',
				size: webcast.items.length,
				itemLoadCallback: item_load_callback //{onBeforeAnimation: item_load_callback},
				//initCallback: item_load_callback 
			});
			
			$("#thumbnailsarea .btn").hover(
				function() {
					$(this).addClass("active");
				},
				function() {
					$(this).removeClass("active");
				}
			);
			
			$("#fullscreen a").lightBox({
				imageLoading: '/img/loading.gif',
				imageBtnClose: '/img/close.gif'
			});
			
		},

		show_slide: function(i) {
			if (webcast.items.length == 0) {
				$("#" + webcast.config.slide_id + " img").attr({
					src: webcast.dir + "cover.jpg",
					title: "",
					alt: ""
				});
				$("#lightbox-image").attr({
					src: "",
					title: "",
					alt: ""
				});
				$("#fullscreen").attr({
					href: "",
					title: ""
				});
			} else {
				
				$("#" + webcast.config.slide_id + " img").attr({
					src: webcast.dir + "m" + webcast.items[i].file,
					title: webcast.items[i].title,
					alt: ""
				});
				
				/*
				if (i > 0) {
					$("#" + webcast.config.slide_id).crossfade({
						src: webcast.dir + "m" + webcast.items[i].file,
						title: webcast.items[i].title
					});
				} else {
					$("#" + webcast.config.slide_id + " img").attr({
						src: webcast.dir + "m" + webcast.items[i].file,
						title: webcast.items[i].title,
						alt: ""
					});
				}*/
				if ($("#lightbox-image").length>0) {
					$("#lightbox-image").attr({
						src: webcast.dir + "l" + webcast.items[i].file,
						title: webcast.items[i].title,
						alt: ""
					});
				};
				
				/*
				href = webcast.dir + "l" + webcast.items[i].file;
				title = webcast.items[i].title;
				$("#fullscreen").html("<a href='"+ href + "' title='" + title + "'><span>full screen</span></a>")
				$("#fullscreen a").lightBox({
					imageLoading: '/img/loading.gif',
					imageBtnClose: '/img/close.gif'
				});
				*/
				$("#fullscreen a").attr({
					href: webcast.dir + "l" + webcast.items[i].file,
					title: webcast.items[i].title
				});
				$("#fullscreen a").lightBox({
					imageLoading: '/img/loading.gif',
					imageBtnClose: '/img/close.gif'
				});
				$("#thumbs li").removeClass("active");
				$($("#thumbs li")[i]).addClass("active")
			}
		}, 

		slide: function(pos, is_live) {
			/*
			function show_slide(i) {
				if (webcast.items.length == 0) {
					$("#" + webcast.config.slide_id + " img").attr({
						src: webcast.dir + "cover.jpg",
						title: "",
						alt: ""
					});
					$("#lightbox-image").attr({
						src: "",
						title: "",
						alt: ""
					});
					$("#fullscreen").attr({
						href: "",
						title: ""
					});
				} else {
					$("#" + webcast.config.slide_id + " img").attr({
						src: webcast.dir + "m" + webcast.items[i].file,
						title: webcast.items[i].title,
						alt: ""
					});
					if ($("#lightbox-image").length>0) {
						$("#lightbox-image").attr({
							src: webcast.dir + "l" + webcast.items[i].file,
							title: webcast.items[i].title,
							alt: ""
						});
					};
					$("#fullscreen").attr({
						href: webcast.dir + "l" + webcast.items[i].file,
						title: webcast.items[i].title
					});
					$("#thumbs li").removeClass("active");
					$($("#thumbs li")[i]).addClass("active")
				}
			};*/
		
			
			//if (webcast.items.length==0 || webcast.is_live) {
			if (is_live) {
				//if (pos%5==0) {
					//console.log(pos);
					$.getJSON(webcast.file, function(data) {
						webcast.is_live = data["webcast"]["is_live"];
						webcast.dir = data["webcast"]["dir"];
						webcast.items = data["webcast"]["items"];
						webcast.show_slide(0);
					});
				//}
			} else {
				i = 0;
				while (i < webcast.items.length && pos >= webcast.items[i].time) { i++ }
				if (i>0) { i-- };
				if (i != webcast.current) {
					//console.log(webcast.items[i].file);
					webcast.show_slide(i);
					webcast.current = i;
				}
			}
		}

	}
}();



$(function() {
	$.ajaxSetup ({ cache: false});
	webtv.init();
});

// FUNZIONI DEL PLAYER



function playerReady(mp) {
	webtv.player = document.getElementById(mp.id);
	if (!webcast.is_ready) {
		setTimeout(function() {
			webtv.playlist = webtv.player.getPlaylist();
			id = webtv.playlist[webtv.current].videoid;
			addListeners();
			webcast.init(id);
		}, 2000);
	}
	//addListeners();
};

function addListeners() {
	if (webtv.player) { 
		webtv.player.addModelListener("STATE", "stateListener");
		webtv.player.addModelListener("TIME", "timeListener");
		webtv.player.addControllerListener("ITEM", "itemListener");
		webtv.player.addControllerListener("SEEK", "seekListener");
	} else {
		setTimeout("addListeners()",1000);
	}
}

function stateListener(obj) {
	current_state = obj.newstate; 
	previous_state = obj.oldstate; 

	//IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	if ((current_state == "PAUSED") && (webcast.is_live)) {
		webtv.player.sendEvent('STOP')
	}
}

function timeListener(obj) {
	dur = obj.duration;
	pos = obj.position;
	if ((webcast.items.length==0) || (webcast.is_live)) {
		if (pos%5==0) { 
			webcast.slide(pos, true) 
			//console.log(pos, " true")
		}
	} else {
		if ((webcast.items.length > 1))  {// && (pos>0) && (pos%0.5==0)) {
			webcast.slide(pos, false)
			//console.log(pos, " false")
		}
	}
	//webcast.slide(pos);
}

function itemListener(obj) {
	webtv.current = obj.index;
	$("#" + webtv.config.palinsesto_id + " li.palinsesto_item").removeClass("active");
	$($("#" + webtv.config.palinsesto_id + " li.palinsesto_item")[webtv.current]).addClass("active");
	
	videoid = webtv.player.getPlaylist()[webtv.current].videoid;
	webcast.init(videoid)
	
	//setTimeout(function() {
	//	id = webtv.player.getPlaylist()[webtv.current].videoid;
	//	webcast.init(id)
	//},2000);
	//setTimeout("webcast.init_thumbs()", 1000);
}

function seekListener(obj) {
	pos = obj.position;
	//console.log(pos)
}





