/**
 * MicroTv Basic Javascript
 *
 * @version 1.0
 * @author Max Kiusso
 * @date 2009 02 05
 */

// 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
	, config:       "./data.xml"			// path to webtv config file
};

$( document ).ready (
	function () {
		// load xml config file
		window._wtv				= {};
		window._wtv.xmlLoaded	= false;
		window._wtv.playlist	= false;
		window._wtv.type		= "";
		
		$.post(
			  videoconfig.config
			, ""
			, function ( data ) {
				window._wtv.xml			= data;
				window._wtv.xmlLoaded	= true;
			}
			, "xml"
		);
		
		//jQuery('#div_submenu ul').jcarousel();
		
		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.playerskin.split( "\/" );
					videoconfig.playerskin	= ( st_skin[ st_skin.length - 1 ] ).replace( /\.jpg$/i , '\.swf' );
					
					window._wtv.playlist	= $( "#div_submenu ul:eq(0)" ).hasClass( "playlist" );
					
					// set click action on video & live
					$( ".a_video" ).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 in_id_video 	= ob_li.children( ".div_id" ).text(); // only for video comments
							var bo_preadvert    = (ob_li.children( ".div_pre_advert" ).length)? 1 : 0;
							var st_name			= ob_li.children( ".div_name" ).text();
							
							window._wtv.type	= $( this ).attr( "class" ) == "a_live" ? "live" : "video";
							
							$( "#div_submenu li" ).removeClass( "selected" );
							$( this ).parents( "li:eq(0)" ).addClass( "selected" );
							
							// open video
							openVideo ({
								  url: st_url || videoconfig.videoserver
								, file: ( window._wtv.playlist ? "playlists/playlist.xml" : st_video )
								, speed: st_bitrate
								, live: false
								, skin: videoconfig.playerskin || ''
								, slidesinterface: false
								//, firstvideo: ( window._wtv.playlist ? $( this ).parents( "li:eq(0)" ).attr( "alt" ) : "0" )
								, firstvideo: (window._wtv.playlist)? parseInt ($( this ).parents( "li:eq(0)" ).attr( "alt" )) - bo_preadvert : "0"
								, title: st_name
							});
						}
					);
					
					if ( window._wtv.playlist ) {
						// open playlist
						openVideo ({
							  url: 		""
							, file: 	"playlists/playlist.xml"
							, speed: 	""
							, live: 	false
							, skin: 	videoconfig.playerskin || ''
							, timeb:	$( "#div_submenu ul:eq(0)" ).hasClass( "timebased" )
						});
						
						// set init comment type
						window._wtv.type		= "playlist";
					} else {
						var ob_li = $( "#div_submenu ul:eq(0)" ).children( "li:eq(0)" );
						
						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_name		= ob_li.children( ".div_name" ).text();
									var st_url		= ob_li.children( ".div_url" ).text();
									
									// open video
									openVideo ({
										  url: 				st_url || videoconfig.videoserver
										, file: 			st_video
										, speed: 			st_birtrate
										, live: 			ob_li.attr( "class" ) == "li_live"
										, skin: 			videoconfig.playerskin || ''
										, slidesinterface:	ob_li.attr( "class" ) == "li_video"
										, title:			st_name
									});
									
									// set init comment type
									window._wtv.playlist 	= false;
									window._wtv.type		= ob_li.attr( "class" ) == "li_live" ? "live" : "video";
									break;
							}
						}
						catch (e) {}
					}
					
					window.clearInterval( intval );
					
					// call init user function
					try {
						initLoad();
					}
					catch (e) {}
				}
			}
			, 100
		);
	}
);

/**
 * openVideo
 *
 * launch player using swfobject
 *
 * @param object parameters
 */
function openVideo (ob)
{
	var flashvars = {
		  mser:      		ob.url
		, vid:       		ob.file
		, live:      		false
		, speed:     		ob.speed
		, autoplay:  		true
		, skin:      		ob.skin || ""
		, timebased: 		ob.timeb || false
		, stretch:   		videoconfig.video_stretch || "no"
		, debug:     		videoconfig.video_debug == "t" ? true : false
		, loop:      		false
		, slidesinterface:	ob.slidesinterface
		, firstvideo:		ob.firstvideo || "0"
		, title:			ob.title || ""
	};
	
	var params = {
		  wmode:             "transparent"
		, allowScriptAccess: "always"
		, allowfullscreen:   "true"
	};
	
	var attributes = {
		  id:   videoconfig.id_player || "div_player"
		, name: videoconfig.id_player || "div_player"
	};
	
	swfobject.embedSWF (
		  "stplayer.swf"
		, videoconfig.id_player || "div_player"
		, videoconfig.screen_width || 400
		, videoconfig.screen_height || 300
		, "9.0.0"
		, "expressInstall.swf"
		, flashvars
		, params
		, attributes
	);
}

function videoInfo ( ob ) {}
function timeUpdate ( in_time ) {}

/**
 * initLoad
 */
function initLoad () {
	/*
	if ( window._wtv.playlist && $( "#div_submenu ul:eq(0)" ).hasClass( "timebased" ) ) {
		$( "#div_submenu ul" ).hide();
	} else {
		$( "#div_submenu ul" ).show();
	}
	
	$( ".a_video" ).hover (
		function () {
			$ ("#div_footer").html ($ (this).parent ("div").parent ("li").children (".div_description").text ());
		},
		function () {
			$ ("#div_footer").html ("");
		}
	);
	*/
}

