/**
 * WebTV Basic Javascript
 *
 * @version 1.7
 * @author Max Kiusso, Michal Luberda, Andrea Doimo
 */

// config
var videoconfig = {
	id_player:    "div_player",    // player container's id
	id_recorder:  "div_recorder",  // simple video recorder
	id_slideshow: "div_slideshow"  // slideshow container's id
};

$ (document).ready (
	function ()
	{
		videoconfig.config = window._path.replace( /^\.\.\// , "" ) + "data.xml";     // path to webtv config file
		// load xml config file
		window._wtv = {};
		window._wtv.xmlLoaded = false;
		window._wtv.playlist = false;
		window._wtv.type = "";
		
		$.ajax ({
			type:     "GET",
			url:      videoconfig.config,
			dataType: "xml",
			success:  function (data) {
				window._wtv.xml       = data;
				window._wtv.xmlLoaded = true;
			},
			error:    function (XMLHttpRequest, textStatus, errorThrown) {
				videoconfig.config = window._path + "data.xml"; // path to webtv config file
				$.get (
					  videoconfig.config
					, ""
					, function (data) {
						window._wtv.xml       = data;
						window._wtv.xmlLoaded = true;
					}
					, "xml"
				);
			}
		});
		
		intval = window.setInterval (
			function () {
				if (window._wtv.xmlLoaded) {
					// get webtv properties
					$.each (
						  $( "webtv" , window._wtv.xml ).children()
						, function () {
							videoconfig[ $( this ).get(0).nodeName ] = $( this ).text();
						}
					);
					
					// get skin name
					var st_skin = videoconfig.skin.split ("\/");
					videoconfig.skin = (st_skin[ st_skin.length - 1 ]).replace (/\.jpg$/i, '\.swf');
					
					// set click action on video & live
					$ (".a_video, .a_live").click (
						function () {
							// get properties
							var ob_li = $ (this).parent ("div").parent ("li");
							
							var st_video	= ob_li.children (".div_videoid").text ();
							var st_bitrate	= ob_li.children (".div_bitrate").text ();
							var st_url		= ob_li.children (".div_url").text (); // live parameter
							var st_name		= ob_li.children (".div_name").text ();
							
							//var in_id_video = ob_li.children (".div_id").text ();
							//$ ("#div_addcomment .div_id").text (in_id_video);
							
							window._wtv.playlist = false;
							window._wtv.type = ($ (this).attr ("class") == "a_live")? "live" : "video";
							
							// open video
							openVideo ({
								  url: st_url || videoconfig.videoserver
								, file: st_video
								, speed: st_bitrate
								, live: $ (this).attr ("class") == "a_live"
								, skin: videoconfig.skin || ''
								, slidesinterface: $ (this).attr ("class") == "a_video"
								, title: st_name
							});
							// open slideshow
							openSlideshow ({
								  webtvid: videoconfig.id
								, setid:   ob_li.children (".div_imageset").text ()
								, path:    videoconfig.path
								, url:     window._path + "set/" + ob_li.children (".div_name").children ("a").text ().replace (/\s/g, "_").toLowerCase () + ".xml"
								, mode:    ($ (this).attr ("class") == "a_video")? "ondemand" : "live"
							});
							
							//showComment (ob_li);
							
							// call user function
							try {
								clickVideo (
									  ob_li
									, ($ (this).attr ("class") == "a_video")? "VIDEO" : "LIVE"
								);
							}
							catch (e) {}
						}
					);
					
					// set click action on category
					$ (".a_category").click (
						function () {
							//showComment ($ (this).parent ("div").parent ("li"));
							
							// call user function
							try {
								clickCategory ($ (this).parent ("div").parent ("li"));
							}
							catch (e) {}
						}
					);
					
					// set click action on playlist
					$ (".a_playlist").click (
						function () {
							var ob_li = $ (this).parent ("div").parent ("li");
							
							var st_timebased = ob_li.children (".div_timebased").text ();
							
							var in_id_playlist = ob_li.children (".div_id").text ();
							//$ ("#div_addcomment .div_id").text (in_id_playlist);
							
							window._wtv.playlist = true;
							window._wtv.type = "playlist";
							
							// open playlist
							openVideo ({
								  url: ""
								, file: window._path + "playlists/" + $ (this).text ().replace (/\s/g, "_").toLowerCase () + ".xml"
								, speed: ""
								, live: false
								, skin: videoconfig.skin || ''
								, timeb: st_timebased == "ok"
							});
							
							// hide all comments
							//$ (".div_comment_container").hide ();
							
							// call user function
							try {
								clickPlaylist (ob_li);
							}
							catch (e) {}
						}
					);
					
					// set click action on channel
					$ (".a_channel").click (
						function () {
							var ob_li = $ (this).parent ("div").parent ("li");
							
							//showComment (ob_li);
							
							// call user function
							try {
								clickChannel (ob_li);
							}
							catch (e) {}
						}
					);
					
					// set action on click comment
					/*
					$ (".a_comment").click (
						function (i) {
							// open video
							openVideo ({
								  url: videoconfig.videoserver
								, file: $ (this).parent ("p").parent ("div").children (".div_commentid:eq(0)").text ()
								, speed: ''
								, live: false
								, skin: videoconfig.skin || ''
							});
							
							// call user function
							try {
								clickComment ($ (this));
							}
							catch (e) {}
						}
					);
					*/
					// set add comment action
					/*
					$ (".a_addcomment").click (
						function () {
							in_id = $ (this).parent ("div").children (".div_id").text ();
							openAddComment (in_id);
							
							// call user function
							try {
								clickAddComment (in_id);
							}
							catch (e) {}
						}
					);
					*/
					
					// launch the first possible video if autostart is set to t
					if (videoconfig.video_autostart == "t") {
						var elemenu = ( $( ".ul_menu:eq(0)" ).length ? ".ul_menu:eq(0)" : "#div_submenu" );
						var ob_li = getFirstLiNode ( elemenu );
						
						try {
							switch (ob_li.attr ("id").substr (0, ob_li.attr ("id").lastIndexOf("_"))) {
								case "li_live":
								case "li_video": {
									
									// get properties
									var st_video	= ob_li.children (".div_videoid").text ();
									var st_birtrate	= ob_li.children (".div_bitrate").text ();
									var st_url		= ob_li.children (".div_url").text ();
									var st_name		= ob_li.children (".div_name").text ();
									
									// open video
									openVideo ({
										  url: st_url || videoconfig.videoserver
										, file: st_video
										, speed: st_birtrate
										, live: ob_li.attr ("class") == "li_live"
										, skin: videoconfig.skin || ''
										, slidesinterface: ob_li.attr ("class") == "li_video"
										, title: st_name
									});
									// open slideshow
									openSlideshow ({
										  webtvid: videoconfig.id
										, setid:   ob_li.children (".div_imageset").text ()
										, path:    videoconfig.path
										, url:     window._path + "set/" + ob_li.children (".div_name").children ("a").text ().replace (/\s/g, "_").toLowerCase () + ".xml"
										, mode:    (ob_li.attr ("class") == "li_video")? "ondemand" : "live"
									});
									
									// set init comment type
									window._wtv.playlist = false;
									window._wtv.type = (ob_li.attr ("class") == "li_live")? "live" : "video";
									
									break;
								}
								case "li_playlist": {
									// open playlist
									openVideo ({
										  url: ""
										, file: window._path + "playlists/" + ob_li.children (".div_name").children ("a").text ().replace (/\s/g, "_").toLowerCase () + ".xml"
										, speed: ""
										, live: false
										, skin: videoconfig.skin || ''
										, timeb: ob_li.children (".div_timebased").text () == "ok"
									});
									
									// set init comment type
									window._wtv.playlist = true;
									window._wtv.type = "playlist";
									
									break;
								}
							}
							
							// set init comment
							/*
							var in_id_video = ob_li.children (".div_id").text ();
							$ ("#div_addcomment .div_id").text (in_id_video);
							
							showComment (ob_li);
							*/
						}
						catch (e) {
						}
					}
					
					window.clearInterval (intval);
					
					// call init user function
					try {
						initLoad();
					}
					catch (e) {}
				}
			},
			100
		);
	}
);

/**
 * showComment
 *
 * @param integer video or playlist's id
 */
 /*
function showComment (ob_li)
{
	// hide all comments
	$ (".div_comment_container").hide ();
	
	st_class = ob_li.attr ("class");
	in_id = ob_li.children (".div_id").text ();
	
	$ (".div_comment_container").each (function () {
		if (
		$ (this).children (".div_id").text () == in_id &&
			(
				(
					st_class == "li_video" ||
					st_class == "li_live"
				) || (
					st_class == "li_category" &&
					$ (this).children (".div_playlist").text () == "t"
				) || (
					st_class == "li_playlist" &&
					$ (this).children (".div_playlist").text () == "t"
				)
			)
		) {
			$ (this).show ();
		}
	});
}
*/

/**
 * getFirstLiNode
 *
 * get the first found object element which match the type li_video, li_live or li_playlist
 *
 * @param string st_ul_node ul element to begin with
 * @return object
 */
function getFirstLiNode (st_ul_node)
{
	var in_first = false;
	
	$ (st_ul_node + " ul li").each (function (i) {
		if (
		$ (this).attr ("class") == "li_video" ||
		$ (this).attr ("class") == "li_live" ||
		$ (this).attr ("class") == "li_playlist") {
			if (in_first === false) in_first = i;
		}
	});
	
	return $ (st_ul_node + " ul li:eq(" + in_first + ")");
}

/**
 * openVideo
 *
 * launch player using swfobject
 *
 * @param object parameters
 */
function openVideo (ob)
{
	
	var swfplayer = "<embed src='./stplayer.swf' flashvars='";
	swfplayer += "mser=" + ob.url;
	swfplayer += "&amp;vid=" + ob.file;
	swfplayer += "&amp;live=" + ( ob.live ? true : false );
	swfplayer += "&amp;speed=" + ob.speed;
	swfplayer += "&amp;autoplay=" + ( videoconfig.video_autostart == "t" ? true : false );
	swfplayer += "&amp;skin=" + ( videoconfig.skin ? (window._path + ob.skin) : "" );
	swfplayer += "&amp;timebased=" + ( ob.timeb || false );
	swfplayer += "&amp;stretch=" + ( videoconfig.video_stretch || "no" );
	swfplayer += "&amp;debug=" + ( videoconfig.video_debug == "t" ? true : false );
	swfplayer += "&amp;loop=false";
	swfplayer += "&amp;slidesinterface=" + ob.slidesinterface;
	swfplayer += "&amp;title=" + ob.title;
	swfplayer += "' quality='Best' wmode='transparent' bgcolor='#333333'";
	swfplayer += " width='" + ( videoconfig.screen_width || 400 ) + "' height='" + ( videoconfig.screen_height || 300 ) + "' name='stplayer' id='stplayer' align='middle' allowScriptAccess='always' allowFullScreen='true' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer'>"
	
	$ ("#"+videoconfig.id_player).html( swfplayer );

}

/**
 * openAddComment
 *
 * @param integer video or playlist's id
 */
 /*
function openAddComment (in_id)
{
	$.modal (
		'<div style="overflow: hidden; padding: 10px" class="div_modal"><div class="div_close"><a href="#" class="a_close">Close</a></div><div id="' + videoconfig.id_recorder + '"></div></div>', {
		containerCss: {
			  paddingTop: "0px"
			, width: "340px"
			, left: "50%"
			, marginLeft: "-170px"
			, height: "450px"
		}
	});
	
	$ (".a_close").click (function () {
		$.modal.close();
	});
	
	var flashvars = {
		webtvid:  videoconfig.id,
		videoid:  in_id,
		language: videoconfig.default_language,
		//playlist: window._wtv.playlist,
		playlist: window._wtv.type,
		url:      videoconfig.path,
		debug:    "true"
	};
	
	var params = {
		wmode:             "transparent",
		allowScriptAccess: "always",
		allowfullscreen:   "true"
	};
	
	var attributes = {
		id:   videoconfig.id_recorder || "div_recorder",
		name: videoconfig.id_recorder || "div_recorder"
	};
	
	if ($ ("#" + videoconfig.id_recorder).length) {
		swfobject.embedSWF (
			window._path + "simplerecorder.swf",
			videoconfig.id_recorder,
			320,
			410,
			"9.0.0",
			"expressInstall.swf",
			flashvars,
			params,
			attributes
		);
	}
}
*/

/**
 * openSlideshow
 *
 * @param object parameters
 */
function openSlideshow (ob)
{
	var flashvars = {
		width:  videoconfig.slideshow_width || 400,
		height: videoconfig.slideshow_height || 300
	};
	for (var i in ob) {
		eval ("flashvars." + i + "='" + ob[i] + "'");
	}
	
	var params = {
		wmode:             "transparent",
		allowScriptAccess: "always",
		allowfullscreen:   "true"
	};
	
	var attributes = {
		id:   videoconfig.id_slideshow || "div_slideshow",
		name: videoconfig.id_slideshow || "div_slideshow"
	};
	
	if ($ ("#" + videoconfig.id_slideshow).length) {
		swfobject.embedSWF (
			window._path + "slideshow.swf",
			videoconfig.id_slideshow,
			videoconfig.slideshow_width || 400,
			videoconfig.slideshow_height || 300,
			"9.0.0",
			"expressInstall.swf",
			flashvars,
			params,
			attributes
		);
	}
}

/**
 * closeAddComment
 */
 /*
function closeAddComment ()
{
	$.modal.close ();
}
*/

/**
 * videoInfo
 *
 * this function is called by STPlayer
 *
 * @param object ob
 */
function videoInfo (ob)
{
	try {
		streamInfo (ob);
	}
	catch (e) {
	}
}

/**
 * timeUpdate
 */
function timeUpdate (in_time)
{
	var ob_flash = getFlashMovieObject (videoconfig.id_slideshow);
	ob_flash.SetVariable("/:playertime", parseInt (in_time));
}

/**
 * getFlashMovieObject
 *
 * @param st_name flash element id
 */
function getFlashMovieObject (st_name)
{
	if (window.document[st_name]) {
		return window.document[st_name];
	}
	if (navigator.appName.indexOf ("Microsoft Internet") == -1) {
		if (document.embeds && document.embeds[st_name]) {
			return document.embeds[st_name];
		}
	}
	else {
		return document.getElementById (st_name);
	}
}