/**
 * 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.23
 * @author Michal Luberda
 */

/**
 * initLoad
 */
function initLoad ()
{
	// get language
	st_lang = ($ ("#ul_language .li_selected").text ().substr (0, 8) == "Italiano")? "it" : "en";
	
	// create first link in logo position
	// create main menu from hidden element get voices from 1 to 4 adding images,
	// create add menu from last three voices
	var in_li = $ ("#div_menu .ul_channel li").length;
	$ ("#div_header h1").append ("<a href='http://www.interlogica.it/" + (st_lang == "it" ? "it/" : "") + "index.htm'><div id='div_header_logo'></div></a>");
	var ar_images = {
		normal: [
			window._path + "image/" + st_lang + "/menu.progettazione.n.png",
			window._path + "image/" + st_lang + "/menu.prodotti.n.png",
			window._path + "image/" + st_lang + "/menu.cartografia.n.png",
			window._path + "image/" + st_lang + "/menu.produzione.n.png"
		],
		over: [
			window._path + "image/" + st_lang + "/menu.progettazione.o.png",
			window._path + "image/" + st_lang + "/menu.prodotti.o.png",
			window._path + "image/" + st_lang + "/menu.cartografia.o.png",
			window._path + "image/" + st_lang + "/menu.produzione.o.png"
		]
	};
	$ ("#div_main_menu").append ("<ul></ul>");
	for (var i = 1; i < 5; i++) {
		ob_li = $ ("#div_menu .ul_channel li:eq(" + i + ")").clone (true);
		ob_li.css ({
			left: (i - 1) * 150 + "px",
			opacity: "0"
		});
		if (
		$ ("#div_submenu li.menu_channel .div_id").text () == ob_li.children (".div_id").text () ||
		ob_li.attr ("class") == "li_channel_selected"
		) {
			bo = true;
		}
		else {
			bo = false;
		}
		ob_li.children (".div_name").children ("a").html (
			"<img src='" + (bo ? ar_images.over[i - 1] : ar_images.normal[i - 1]) + "' alt='' />" +
			"<span>" +
				ar_images.over[i - 1] +
				"|" +
				(bo ? ar_images.over[i - 1] : ar_images.normal[i - 1]) +
			"</span>"
		);
		ob_li.children (".div_name").children ("a").hover (
			function () {
				var ar = $ (this).children ("span").text ().split ("|");
				$ (this).children ("img").attr ("src", ar[0]);
			},
			function () {
				var ar = $ (this).children ("span").text ().split ("|");
				$ (this).children ("img").attr ("src", ar[1]);
			}
		);
		$ ("#div_main_menu ul").append (ob_li);
	}
	in_counter = 0;
	$ ("#div_main_menu ul li").animate ({
			opacity: "1"
		},
		"fast",
		function () {
			// set home effect
			if ($ (".li_video").length) {
				setTimeout (
					"highlightEffect ('" + $ (this).children (".div_name").children ("a").children ("span").text () + "', " + in_counter + ", true)",
					in_counter * 50
				);
				in_counter++;
			}
		}
	);
	$ ("#div_add_menu").append ("<ul></ul>");
	for (var i = in_li - 4; i < in_li; i++) {
		$ ("#div_add_menu ul").append ($ ("#div_menu .ul_channel li:eq(" + i + ")").clone (true));
	}
	
	// IE6 fix set max height of the news for home page
	if ($ (".li_video").length) {
		in_height = $ ("#div_news").height ();
		$ (".news:eq(0)").height (in_height);
		$ (".news:eq(1)").height (in_height);
		$ (".news:eq(2)").height (in_height);
	}
	
	// iframe with contacts
	if ($ ("#div_contact").length) {
		$ ("#div_contact").append ("<iframe allowtransparency='true' id='iframe_contact' name='iframe_contact' style='border: none; background: transparent;' border='no' frameborder='0' width='940' height='500' src='http://dynamic.interlogica.it/contatti2009?idlingua=" + (st_lang == "it"? "1" : "2") + "'></iframe>");
		$ ("#iframe_contact").load (iframeResize);
	}
	
	// set parent link color also as selected
	$ ("li.menu_selected_1").parents ("li").children ("a").css ({color: "#5d5d5d"});
	$ ("li.menu_selected_2").parents ("li").children ("a").css ({color: "#5d5d5d"});
	$ ("li.menu_selected_3").parents ("li").children ("a").css ({color: "#5d5d5d"});
	
	// flash effect only for index.htm
	if ($ ("#div_tv").length) {
		$ ("#div_main_menu ul li").each (function () {
			ar = $ (this).children (".div_name").children ("a").children ("span").text ().split ("|");
		});
	}
	
	// launch random video
	in_number =  $ (".a_video").length / 2;
	$ (".a_video:eq(" + (Math.floor (Math.random () * in_number) * 2) + ")").click ();
	
	// twitter
	if ( $( "#twitter_div" ).length ) {
		$("body").append("<script type=\"text/javascript\" src=\"http://twitter.com/javascripts/blogger.js\"></script>");
		$("body").append("<script type=\"text/javascript\" src=\"http://twitter.com/statuses/user_timeline/interlogica.json?callback=twitterCallback2&amp;count=4\"></script>");
	
		id_interval = window.setInterval (
			function () {
				if ($ ("#twitter_update_list").text () != "*") {
					in_height = Math.max (Math.max ($ (".news:eq(0)").height (), $ (".news:eq(1)").height ()), $ ("#twitter_div").height ());
					$ (".news:eq(0)").height (in_height);
					$ (".news:eq(1)").height (in_height);
					$ (".news:eq(2)").height (in_height);
					
					window.clearInterval (id_interval);
				}
			},
			100
		);
	}
	
	// Pressroom pagination
	if ( $( "div.pressroom" ).length ) {
		$( "div.pressroom" ).append( "<div id='pageButtons'></div>" );
		
		var rp = 4;
		var li = $( "div.pressroom li.menu_channel ul li" ).length;
		var pg = Math.ceil ( li / rp );
		var ct = 1;
		
		window._page = 0;
		
		$( "div.pressroom li.menu_channel ul li" ).each(
			function () {
				if ( $( this ).hasClass( "menu_selected_0" ) ) {
					window._page = Math.ceil ( ct / rp ) - 1;
				}
				ct++;
			}
		);
		
		showPage( rp , li , pg );
		
		$( "div.pressroom" ).show();
	}
	
	// gallery
	if ($ ("#div_gallery").length) {
		videoconfig.screen_width  = 680;
		videoconfig.screen_height = 510;
		videoconfig.video_stretch = "fit";
		videoconfig.skin          = "interlogica_4x3.swf";
		
		$ ("#div_gallery a").lightBox ();
		$ ("#div_tv_background").css ({opacity: 0.2});
		$ ("#a_gallery").click (function () {
			$ ("#div_gallery").show ();
			$ ("#div_tv_background").show ();
			$ ("#div_player").hide ();
			$ ("#div_tv").css ({background: "transparent"});
			// remove player
			if ($ ("#div_player").length) {
				swfobject.removeSWF( "div_player" );
			}
		});
		$ ("#a_video").click (function () {
			// add player
			if (! $ ("#div_player").length) {
				$ ("#div_tv").prepend ("<div id='div_player'></div>");
			}
			$ ("#div_video_submenu .li_video:eq(0) a").click ();
			$ ("#div_gallery").hide ();
			$ ("#div_tv_background").hide ();
			$ ("#div_tv").css ({background: "#000"});
		});
		$ ("#a_backstage").click (function () {
			// add player
			if (! $ ("#div_player").length) {
				$ ("#div_tv").prepend ("<div id='div_player'></div>");
			}
			$ ("#div_video_submenu .li_video:eq(1) a").click ();
			$ ("#div_gallery").hide ();
			$ ("#div_tv_background").hide ();
			$ ("#div_tv").css ({background: "#000"});
		});
		
		$ ("#div_video_submenu .li_video:eq(0) a").click ();
	}
}

function showPage ( rp , li , pg ) {
	var clang = $( "current_language" , window._wtv.xml ).text();
	
	var btext = {
		  it_IT: { prev: 'Precedente' , next: 'Successivo' }
		, en_UK: { prev: 'Previous' , next: 'Next' }
	};
	
	$( "div.pressroom li.menu_channel ul li" ).hide();
	for ( var j = ( window._page * rp ); j < ( window._page * rp ) + rp ; j++ ) {
		$( "div.pressroom li.menu_channel ul li:eq(" + j + ")" ).show();
	}
	
	$( "#prevPage" ).remove();
	if ( window._page > 0 ) {
		$( "#pageButtons" ).append( "<div id='prevPage'><span>" + ( btext[ clang ]['prev'] || btext['en_UK']['prev'] ) + "</span></div>" );
		$( "#prevPage" ).click(
			function () {
				window._page -= 1;
				showPage ( rp , li , pg );
			}
		);
	}
	
	$( "#nextPage" ).remove();
	if ( window._page < ( pg - 1 ) ) {
		$( "#pageButtons" ).append( "<div id='nextPage'><span>" + ( btext[ clang ]['next'] || btext['en_UK']['next'] ) + "</span></div>" );
		$( "#nextPage" ).click(
			function () {
				window._page += 1;
				showPage ( rp , li , pg );
			}
		);
	}
}

function highlightEffect (st, in_ele, bo_hover)
{
	var ar = st.split ("|");
	$ ("#div_main_menu ul li:eq(" + in_ele + ")").children (".div_name").children ("a").children ("img").attr ("src", (bo_hover? ar[0] : ar[1]));
	if (bo_hover) {
		$ ("#div_main_menu ul li:eq(" + in_ele + ")").children (".div_name").children ("a").children ("img").css ({
			opacity: "0.9"
		});
		setTimeout (
			"highlightEffect ('" + st + "', " + in_ele + ", false)",
			in_ele * 50 + 50
		);
	}
	else {
		$ ("#div_main_menu ul li:eq(" + in_ele + ")").children (".div_name").children ("a").children ("img").css ({
			opacity: "1"
		});
	}
}

/**
 * iframeResize
 */
function iframeResize () {
	var helpFrame = $ ("#iframe_contact");
	var innerDoc = (helpFrame.get (0).contentDocument)? helpFrame.get (0).contentDocument : helpFrame.get (0).contentWindow.document;
	helpFrame.height (innerDoc.body.scrollHeight + 35);
}

/**
 * clickVideo
 *
 * @param object ob_li clicked li element
 * @param string st_type VIDEO or LIVE
 */
function clickVideo (ob_li, st_type)
{
}

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

/**
 * 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)
{
}
