/**
 * User functions' set
 *
 * initLoad ()
 * clickVideo (ob_li, st_type)
 * clickCategory (ob_li)
 * clickPlaylist (ob_li)
 * clickChannel (ob_li)
 * clickAddComment (ob_li)
 * streamInfo (ob)
 *
 * @version 1.3
 * @author Michal Luberda
 */

/**
 * initLoad
 */
function initLoad ()
{
	// show channels without last two LIVE & FOOTER
	in_channels = $ ("#div_menu .ul_channel .li_channel").length;
	$ ("#div_menu .ul_channel>li").each (function (i) {
		if (i < in_channels - 1) {
			$ (this).show ();
		}
	});
	
	// copy videos into carousel container
	if ($ ("#div_submenu .ul_menu:eq(0) .ul_category li:eq(0)").length)
		_copy_videos ($ ("#div_submenu .ul_menu:eq(0) .ul_category:eq(0)"));
	
	// set active category
	$ ("#div_submenu .ul_channel .li_category:eq(0) .div_name .a_category").addClass ("active");
	
	// set live
	$ (window._wtv.xml).children ("data").children ("node").children ("node").each (function (i) {
		// live
		if ($ (this).children ("nodetype").text () == "LIVE"
		&& $ (this).children ("element").children ("hide").text () == "f") {
			// set live link
			
			$ ("#div_onair").html (
				"<ul id='ul_live'>" +
					"<li class='li_live' id='li_live_" + $ (this).children ("id").text () + "'>" +
					"<div class='div_name'><a class='a_live' href='#' title='Bugs TV'><img alt='' src='gfx/onair_on.png'></a></div>" +
					"<div class='div_id'>" + $ (this).children ("element").children ("id").text () + "</div>" +
					"<div class='div_videoid'>" + $ (this).children ("element").children ("videoid").text () + "</div>" +
					"<div class='div_url'>" + $ (this).children ("element").children ("url").text () + "</div>" +
					"</li>" +
				"</ul>"
			);
			
			st_video   = $ (this).children ("element").children ("videoid").text ();
			st_bitrate = $ (this).children ("element").children ("bitrate").text ();
			st_url     = $ (this).children ("element").children ("url").text ();
			
			// set onclick action
			$ ("#div_onair").click (function () {
				openVideo ({
					  url: st_url
					, file: st_video
					, speed: st_bitrate
					, live: true
					, skin: videoconfig.skin || ''
					, slidesinterface: false
				});
			});
		}
	});
	
	// footer
	$ (window._wtv.xml).children ("data").children ("node").each (function (i) {
		if ($ (this).children ("name").text () == "FOOTER") {
			$ (this).children ("node").each (function (j) {
				var st = "<ul id='ul_videos_" + (j + 1) + "'>";
				$ (this).children ("node").each (function (k) {
					ob = $ (this).children ("element");
					var st_video       = ob.children ("videoid").text ();
					var st_name        = ob.children ("name").text ();
					var st_description = ob.children ("description").text ();
					var st_bitrate     = ob.children ("bitrate").text ();
					var in_id_video    = ob.children ("id").text ();
					st += "<li class='li_video' id='li_video_" + $ (this).children ("id").text () + "'>" +
						"<div class='div_name'><a class='a_video' href='#' title='" + st_description + "'>" + st_name + "</a></div>" +
						"<div class='div_id'>" + in_id_video + "</div>" +
						"<div class='div_videoid'>" + st_video + "</div>" +
						"<div class='div_bitrate'>" + st_bitrate + "</div>" +
					"</li>";
				});
				st += "</ul>";
				$ ("#div_videos_" + (j + 1)).html (st);
				$ ("#div_videos_" + (j + 1)).children ("ul").children ("li").click (function (i) {
					var st_video	= $ (this).children (".div_videoid").text ();
					var st_bitrate	= $ (this).children (".div_bitrate").text ();
					
					// open video
					openVideo ({
						  url: videoconfig.videoserver
						, file: st_video
						, speed: st_bitrate
						, loop: false
						, live: false
						, skin: videoconfig.skin || ''
						, slidesinterface: false
					});
				});
			});
		}
	});

	//$('.box #div_videos_1 , .box #div_videos_2 , .box #div_videos_3').jScrollPane({ dragMaxHeight:14 , dragMinHeight: 14 , scrollbarWidth:21 });
	$('.box #div_videos_2 , .box #div_videos_3').jScrollPane({ dragMaxHeight:14 , dragMinHeight: 14 , scrollbarWidth:21 });

	
}

/**
 * clickVideo
 *
 * @param object ob_li clicked li element
 * @param string st_type VIDEO or LIVE
 */
function clickVideo (ob_li, st_type)
{
	
	ob_li.parent ("ul").children ("li").each (function (i) {
		ob = $ (this).children (".div_name").children ("a").removeClass ("active")
	});
	$(ob_li).children("div").children("a").addClass("active");
		
}

/**
 * clickCategory
 *
 * @param object ob_li clicked li element
 */
function clickCategory (ob_li)
{
	// remove class and set id
	ob_li.parent ("ul").children ("li").each (function (i) {
		ob = $ (this).children (".div_name").children (".a_category");
		if (ob.hasClass ("active")) {
			ob.removeClass ("active");
			bugstv.in_id = i;
		}
	});
	
	// set class
	ob_li.children (".div_name").children (".a_category").addClass ("active");
	
	// restore objects
	ob = ob_li.parent ("ul").children ("li:eq(" + bugstv.in_id + ")");
	ob.append ("<ul class='ul_category'></ul>")
	$ ("#div_carousel ul").children ("li").each (function (i) {
		ob.children (".ul_category").append ($ (this));
	});
	
	// copy videos
	if (ob_li.children (".ul_category").children ("li:eq(0)").length)
		_copy_videos (ob_li.children (".ul_category"));
		
	$ ("#div_carousel a.active").removeClass ("active");
		
}

/**
 * clickPlaylist
 *
 * @param object ob_li clicked li element
 */
function clickPlaylist (ob_li)
{
}

/**
 * clickChannel
 *
 * @param object ob_li clicked li element
 */
function clickChannel (ob_li)
{
}

/**
 * clickAddComment
 *
 * @param integer video or playlist id
 */
function clickAddComment (in_id)
{
}

/**
 * streamInfo
 *
 * @param object ob
 */
function streamInfo (ob)
{
}

/**
 * _copy_videos
 */
function _copy_videos (ob_ul)
{
	$ ("#div_carousel").html (ob_ul);
	// set attributs
	$ ("#div_carousel ul").attr ("class", "jcarousel-skin-tango");
	$ ("#div_carousel ul").attr ("id", "ul_carousel");
	// copy image into a
	$ ("#div_carousel ul li").each (function (i) {
		if (! $ (this).children (".div_name").children ("a").children ("img").length)
			$ (this).children (".div_name").children ("a").prepend ($ (this).children (".div_image").children (".a_video").html () + "<br/>");
	});
	
	// init carousel
	$ ("#div_carousel .div_image").hide ();
	$ ("#ul_carousel").jcarousel ({});
	
	$("#div_carousel li:eq(0)").children("div").children("a").addClass("active");
		
}

bugstv = {
	in_id: 0
};
