/*
 * Thickbox 3.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 *
 * Updated by Ladislav Safarik 20090415
*/

imgLoader = new Image();// preload image
imgLoader.src = "stc/tpl/crp/img/loader.gif";

//function called when the user clicks on a video link
function openVideoLayer( videoPath, musicPath, width, height, close, errorMsg, title, startType )
{
	var params = new Array();
	params[ 'videoPath' ] = videoPath;
	params[ 'musicPath' ] = musicPath;
	params[ 'width' ] = width;
	params[ 'height' ] = height;
	params[ 'close' ] = close;
	params[ 'errorMsg' ] = errorMsg ? errorMsg : '';
	params[ 'title' ] = title ? title : '';
	params[ 'startType' ] = startType ? startType : 1;
	
	filename = videoPath.substring( videoPath.lastIndexOf( '/' ) + 1 );
	tb_show( params );
	try
	{
		fc_click( filename , 'media' );
	}
	catch(e)
	{
	}
	return false;
}

// Feedback submit
function submit( countFormFeedback, valueSubmit)
{
	var input = document.createElement('input');
		input.setAttribute( "type", "hidden" );
		input.setAttribute( "name", "answ" );
		input.setAttribute( "value", valueSubmit );

	form = 'feedback' + countFormFeedback; 
	document.forms[form].appendChild( input );
	document.forms[form].submit();
}

function tb_show( params )
{
	try
	{
		if (typeof document.body.style.maxHeight === "undefined")
		{//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null)
			{//iframe to hide select elements in ie6
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}
		else
		{//all others
			if(document.getElementById("TB_overlay") === null)
			{
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}

		if(tb_detectMacXFF())
		{
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}
		else
		{
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		}

		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#TB_load').show();//show loader

		var suffix = params[ 'videoPath' ].substring( params[ 'videoPath' ].length - 3, params[ 'videoPath' ].length );

		//new global constants
		TB_WIDTH = params[ 'width' ] + 30;
		TB_HEIGHT = params[ 'height' ] + 45;

		if( suffix == 'flv' )
		{
			TB_HEIGHT += 25;
		}

		var ajaxContentW = params[ 'width' ];
		var ajaxContentH = params[ 'height' ] + 30;

		//the window content
		$("#TB_window").append("<div id='TB_top'><span class='sh2'>" + params[ 'title' ] + "</span><span id='TB_close'><a class='del' href='#' id='TB_closeWindowButton'>" + params[ 'close' ] + "</a></span></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'><p>"+params['errorMsg']+"</p></div>");

		$("#TB_closeWindowButton").click(tb_remove);

		if( suffix == 'swf')
		{
			//load the requested video tutorial
/*
			var so = new SWFObject( params['videoPath'], "videoid", params['width'], params['height'], "9", "#ffffff");
			so.addVariable("musicpath", params['musicPath'] );
			so.addVariable("autoplay" , "1" );
*/
			var so = new SWFObject("http://www.grisoft.cz/filedir/faq/swf_toolbar.swf", "videoid", params['width'], params['height'], "9", "#ffffff");
			so.addVariable("showToolbar" , "true" );
			so.addVariable("topToolbar" , "false" );
			so.addVariable("showFullScreen" , "false" );
			so.addVariable("notScaleInFullScreen" , "false" );
			so.addVariable("autoHide" , "false" );
			so.addVariable("showAutoHide" , "false" );
			so.addVariable("showTimer" , "true" );
			so.addVariable("showTimeline" , "true" );
			so.addVariable("showNextButton" , "true" );
			so.addVariable("showPrevButton" , "true" );
			so.addVariable("showFBLogo" , "false" );
			so.addVariable("showVolumeBar" , "true" );
			so.addVariable("fps" , "12" );
			so.addVariable("fileName" , params['videoPath'] );
			so.addVariable("previewMode" , "0" );
			so.addVariable("str1" , "Created%20with%20BB%20FlashBack" );
			so.addVariable("str2" , "Visit%20www.bbflashback.com" );
			so.addVariable("str3" , "Hide" );
			so.addVariable("showPausedOverlay" , "true" );
			so.addVariable("pauseByClickingOnMovie" , "true" );
			so.addVariable("startingPlaybackMode" , "2" );
			so.addVariable("preloadPercent" , "0" );

			so.write("TB_ajaxContent");
		}
		else if( suffix == 'flv')
		{
			//load flv video
			var startFiledir = params[ 'videoPath' ].search('filedir');
			var filedirUrl = params[ 'videoPath' ].substring(0,startFiledir); //part of url before filedir
			var playerUrl = filedirUrl.concat('linkscanner/img/player.swf');
			var playerHeight = params['height'] + 25 ;

			var so = new SWFObject(playerUrl, "videoid", params['width'] , playerHeight, "9", "#ffffff");
			so.addParam("allowFullScreen", "true");
			so.addVariable("videopath" ,params['videoPath'] );
			so.addVariable("width" , params['width']);
			so.addVariable("height" , params['height'] );
			so.addVariable("autoplay" , params[ 'startType' ] );
			so.write("TB_ajaxContent");
		}

		$("#TB_window").unload(function()
		{
			$("#TB_window").append( $("#TB_ajaxContent").children() ); // move elements back when you're finished
		});

		tb_position();
		$("#TB_load").remove();
		$("#TB_window").css({display:"block"});

		document.onkeyup = function(e)
		{
			if (e == null)
			{ // ie
				keycode = event.keyCode;
			}
			else
			{ // mozilla
				keycode = e.which;
			}
			if(keycode == 27)
			{ // close
				tb_remove();
			}
		};
	} catch(e)
	{
		alert("show failed");//nothing here
	}
	return false;
}

//helper functions below
function tb_remove()
{
	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	if( jQuery.browser.opera )
	{
		$("#TB_overlay").css({display: "none"}); // safely hide the background in Opera
	}
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_ajaxContent").remove();
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined")
	{//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function tb_position()
{
	$("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7) // take away IE6
			|| window.navigator.appVersion.search( 'MSIE 7.0' ) ) //exception for IE 7.0
	{
		$("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
	}
}

function tb_detectMacXFF()
{
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox') != -1)
	{
		return true;
	}
}
