/*
 * 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 Pavel Vancurik and Rostislav Jadavan
*/

// Function is called when user clicks on a video link
function openVideo( videoPath, width, height, close, errorMsg, FormFeedback, countFormFeedback, feedbackAction, faqNum, faqDoc, description, sYes, sNo, title, startType )
{
	var params = new Array();
	params[ 'videoPath' ] = videoPath;
	params[ 'width' ] = width;
	params[ 'height' ] = height;
	params[ 'close' ] = close;
	params[ 'errorMsg' ] = errorMsg ? errorMsg : '';
	params[ 'title' ] = title ? title : '';
	params[ 'startType' ] = startType ? startType : 1;
	params[ 'FormFeedback' ] = FormFeedback;
	params[ 'countFormFeedback' ] = countFormFeedback;
	params[ 'feedbackAction' ] = feedbackAction;
	params[ 'faqNum' ] = faqNum;
	params[ 'faqDoc' ] = faqDoc;
	params[ 'description' ] = description;
	params[ 'sYes' ] = sYes;
	params[ 'sNo' ] = sNo;	

	tb_show( params );

	return false;
}

// Render overlay with video and FAQ feedback
function tb_show( params )
{
	try
	{
		// If IE 6
		if (typeof document.body.style.maxHeight === "undefined")
		{
			$("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);
			}
		}
		// Other browsers
		else
		{
			if( document.getElementById("TB_overlay") === null )
			{
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}

		// Detect firefox on macosx
		if(tb_detectMacXFF())
		{
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}
		else
		{
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		}

		TB_WIDTH = params[ 'width' ];
		TB_HEIGHT = params[ 'height' ];
		var ajaxContentW = params[ 'width' ];
		var ajaxContentH = params[ 'height' ];

		// The window content
		$("#TB_window").append("<div id='TB_top'><span class='sh2 mln'>" + params[ 'title' ] + "</span><span id='TB_close' class='mbottom'><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);

		// Feedback
		if( params[ 'FormFeedback' ] )
		{
			feedback = "<div class='tright'><form method='POST' class='mbn' action='" + params[ 'feedbackAction' ] + "'>";
			feedback+= "<strong class='mright'>" + params['description'] + "</strong>";
			feedback+= "<input type='hidden' name='faq_num' value='" + params['faqNum'] + "' />";
			feedback+= "<input type='hidden' name='faq_doc' value='" + params['faqDoc'] + "' />";
			feedback+= "<input type='hidden' name='faq_nid' value='' />";
			feedback+= "<input type='submit' name='answ' value='" + params['sYes'] + "' class='btn_yes' />";
			feedback+= " | <input type='submit' name='answ' value='" + params['sNo'] + "' class='btn_no' />";
			feedback+= "</form></div>";
			$("#TB_window").append(feedback);
		}

		// Embed video
		var suffix = params[ 'videoPath' ].substring( params[ 'videoPath' ].length - 3, params[ 'videoPath' ].length );
		if( suffix == 'swf')
		{
			var flashvars = {};
			flashvars.sWidth = params[ 'width' ];
			flashvars.sHeight = params[ 'height' ];
			flashvars.frameRead = 12;
			var flashparams = {};
			var attributes = {};
			attributes.id = "TB_ajaxContent";
			attributes.name = "TB_ajaxContent";

			swfobject.embedSWF(params[ 'videoPath' ], "TB_ajaxContent", TB_WIDTH, TB_HEIGHT , "9.0.0","http://download.avg.com/filedir/faq/tutorial/expressInstall.swf", flashvars, flashparams, attributes);
		}

		$("#TB_window").unload(function()
		{
			$("#TB_window").append( $("#TB_ajaxContent").children() );
		});

		// set position of inner window
		tb_position();

		document.onkeyup = function(e)
		{
			if (e == null)
			{ // ie
				keycode = event.keyCode;
			}
			else
			{ // mozilla
				keycode = e.which;
			}
			if(keycode == 27)
			{ // close
				tb_remove();
			}
		};
		
	} catch(e)
	{
		//nothing here
	}
	return false;
}

//helper functions below
function tb_remove()
{
	swfobject.removeSWF( "TB_ajaxContent" );
	
	$("#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 IE 6
	if (typeof document.body.style.maxHeight == "undefined")
	{
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	
	return false;
}

function tb_position()
{
	$("#TB_window").css({display:"block"});
	$("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});

	if ( !(jQuery.browser.msie && jQuery.browser.version < 7) )
	{
		var windowCorrection = 50;
		$("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2) + windowCorrection, 10) + 'px'});
	}
}

function tb_detectMacXFF()
{
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox') != -1)
	{
		return true;
	}
	else
		return false;
}

