var catalogFrame = null;
var stillsFrame = null;
var catalogButton = null;
var stillsButton = null;
var wrongScreenMessage = 'wrong screen resoluion';


function initCatalogs() 
{
	try
	{
		catalogFrame = document.getElementById("FrameBottom");
		stillsFrame = document.getElementById("FrameStills");
		catalogButton = document.getElementById("btnGallery");
		stillsButton = document.getElementById("btnStills");
		highlightButtons();
	}
	catch(e)
	{
		util_reportError('initCatalogs', e);
	}
}

function showNamedCatalog(name) {
	if(name=='stills') {
		showCatalog(stillsFrame, stillsButton);
	} else {
		showCatalog(catalogFrame, catalogButton);
		if( FrameBottom )
			FrameBottom.init( );
	}
	doResize( true );
}

function hideNamedCatalog(name) {
	if(name=='stills') {
		hideCatalog(stillsFrame, stillsButton);
	} else {
		hideCatalog(catalogFrame, catalogButton);
	}
	doResize();
}

function hideCatalog(catalog, btn) {
	try {
		if( catalog ) {
			deleteCookie("bottom-frame-show");
			catalog.style.display = "none";
		}
		if( btn ) {
			btn.className = "";
		}
	}
	catch (e) {
		util_reportError('hideCatalog', e);
	}
}

function showCatalog(catalog, btn) {
	var 
		screenWidth = screen.width,
		screenHeight = screen.height;
	
	if( screenWidth > 800 && screenHeight >  600 )
	{
		try {
			if( catalog ) {
				setNonExpiresCookie("bottom-frame-show", "yes");
				catalog.style.display = "block";
			}
			if (btn) {
				btn.className = "current";
			}
		}
		catch (e) {
			util_reportError('showCatalog', e);
		}
	}
}

function swapGallery() {
	if (catalogFrame) {
		try {
			if (catalogFrame.style.display != "none") {
				hideCatalog(catalogFrame, catalogButton);
			} else {
				hideCatalog(stillsFrame, stillsButton);
				showCatalog(catalogFrame, catalogButton);
				if( FrameBottom )
					FrameBottom.init( );
				setNonExpiresCookie("current-catalog", "gallery");
			}
			doResize(true);
		}
		catch (e) {
			util_reportError('swapGallery', e);
		}
	}
}

function swapStills() {
	if (stillsFrame) {
		try {
			if (stillsFrame.style.display != "none") {
				hideCatalog(stillsFrame, stillsButton);
			} else {
				hideCatalog(catalogFrame, catalogButton);
				showCatalog(stillsFrame, stillsButton);
				setNonExpiresCookie("current-catalog", "stills");
			}
			doResize(true);
		}
		catch (e) {
			util_reportError('swapStills', e);
		}
	}
}

function checkCatalogs( )
{
	var catalogCookie = getCookie("current-catalog");
	
	if( catalogCookie != "gallery" )
	{
		checkStillsCookie( );
	}
	else
	{
		checkCatCookie( );
	}
}

function checkCatCookie() {
	var checkCookie = getCookie("bottom-frame-show");
	if( checkCookie == "yes" ) {
		if( catalogFrame ) {
			showCatalog(catalogFrame, catalogButton);
			if( FrameBottom )
				FrameBottom.init( );
		}
	}
}

function checkStillsCookie() {
	var checkCookie = getCookie("bottom-frame-show");
	if( checkCookie == "yes" ) {
		if( catalogFrame ) {
			showCatalog(stillsFrame, stillsButton);
		}
	}
}

function setCatCookie() {
	if( catalogFrame ) {
		if( catalogFrame.style.display != "none" ) {
			setNonExpiresCookie("bottom-frame-show", "yes");
		} else {
			deleteCookie("bottom-frame-show");
		}
	}
}

function util_reportError(funcName, ex) {
	alert('Exception:' + ex.message + '\with number:' + ex.number + '\in function:' + funcName);
}

function highlightButtons() {
	if(catalogFrame) {
		if( catalogFrame.style.display != "none" ) {
			catalogButton.className = "current";
		} else {
			catalogButton.className = "";
		}
	}
	if(stillsFrame) {
		if( stillsFrame.style.display != "none" ) {
			stillsButton.className = "current";
		} else {
			stillsButton.className = "";
		}
	}
}
function closeWindow( )
{
	if( window.confirm( "Are you sure you want to close the player window?" ) )
	{
		window.close( );
	}
}

function doResize( errorReport )
{
	try
	{
		//console.log($);
		if( catalogFrame || stillsFrame )
		{
			var 
				screenWidth = screen.width,
				screenHeight = screen.height,
				borderWidth,borderHeight;
				if(window.outerWidth) {
					borderWidth = window.outerWidth - window.innerWidth;
					borderHeight = window.outerHeight - window.innerHeight;
				} else {
					borderWidth = 20;
					borderHeight = 85;
				}
				

			if( screenWidth > 800 && screenHeight > 600 )
			{
				if( catalogFrame && catalogFrame.style.display != "none" || stillsFrame && stillsFrame.style.display != "none" )
				{
					var 
						windowScreenTop = ( window.screen.top ? window.screen.top : window.screenTop );

					if( windowScreenTop + windowBigHeight > screenHeight )
					{
						var windowTop = screenHeight - windowBigHeight - windowScreenTop;

						window.moveBy( 0, windowTop );
					}
					window.resizeTo( windowWidth + borderWidth, windowBigHeight + borderHeight );

					//					window.resizeTo( (windowWidth*2-$(window).width()), (windowBigHeight*2-$(window).height()) );
/*
//					window.resizeTo( windowWidth, windowBigHeight );
					console.log('windowWidth:' + windowWidth);
					console.log('windowBigHeight:' + windowBigHeight);
					console.log('$(window).width():' + $(window).width());
					console.log('$(document).width():' + $(document).width());
					console.log('window.outerWidth:' + window.outerWidth);
*/

					}
				else
				{
					window.resizeTo( windowWidth + borderWidth, windowSmallHeight + borderHeight );
//					window.resizeTo( (windowWidth*2-$(window).width()), (windowSmallHeight*2-$(window).height()) );
/*
					console.log('windowWidth:' + windowWidth);
					console.log('windowSmallHeight:' + windowSmallHeight);
					console.log('$(window).width():' + $(window).width());
					console.log('$(document).width():' + $(document).width());
					console.log('window.outerWidth:' + window.outerWidth);
*/
				}
			}
			else if( errorReport )
			{
				alert( wrongScreenMessage );
			}
		}
		else
		{

			window.resizeTo( windowWidth + borderWidth, windowSmallHeight + borderHeight );
//			window.resizeTo( (windowWidth*2-$(window).width()), (windowSmallHeight*2-$(window).height()) );
		
		/*
//		window.resizeTo( windowWidth, windowSmallHeight );

			console.log('windowWidth:' + windowWidth);
			console.log('windowSmallHeight:' + windowSmallHeight);
			console.log('$(window).width():' + $(window).width());
			console.log('$(document).width():' + $(document).width());
			console.log('window.outerWidth:' + window.outerWidth);
 */
		}
	}
	catch( e )
	{
		//alert(e);
		//console.log('doResize:' + e);
	}
}

function openInParent(url)
{
	try
	{
		if( !top.opener || top.opener.closed )
		{
			window.open( url, '___mpParent' );
		}
		else
		{
			top.opener.location = url;
		}
	}
	catch( e )
	{
		//alert( 'error:' + e.message );
	}
}
/* Validates form for Select Bitrate page */
function validateSelectBitrateForm(form) {
  if (form.bitrate.value == "") {
	alert("Please select your connection speed.")
	return false;
  }
  else {
	// record bitrate here
	var mySel = document.sbitrate.bitrate.selectedIndex;
	var myBitrate = document.sbitrate.bitrate.options[mySel].value;
	
	setCookie ("curbitrate", myBitrate, expDate())
	return true;
  }  
}


function saveTestSpeedInfo(kbits, mtype) {
	try {
		var bitrate;
		var
			sLeafClass = null;
		
		try
		{
			sLeafClass = currentLeafClass;
		}
		catch( e )
		{
		}

		if(kbits < 56 ) bitrate = false;
		else if( kbits < 100 ) bitrate = "A";
		else if( kbits < 300 ) bitrate = "B";
		else if( kbits < 400 ) bitrate = "F";
		else if( kbits < 700 ) bitrate = "C";
		else bitrate = "D";

		if( sLeafClass == "VODDOWN" && bitrate < "B" )
		{
			bitrate = false;
		}

		intKBits = Math.floor( kbits );
		myHideObject("runningtest");
		myShowObject("testresults");
		var speed1 = document.getElementById("speedspan");
		var speed2 = document.getElementById("speedspan2");
		if( speed1 ) speed1.innerHTML = intKBits;
		if( speed2 ) speed2.innerHTML = intKBits;

		if( !bitrate || bitrate == "A" && sLeafClass == "VODDOWN" )
		{
			if( sLeafClass != "VODDOWN" )
				setCookie("curbitrate", "A", expDate());

			myShowObject("speedenought");
			myShowObject("speedbelowmessage");
		}
		else
		{
			var
				oLink = document.getElementById( "continueLink" );

			setCookie("curbitrate", bitrate, expDate());
			
			if( sLeafClass == "VODDOWN" && bitrate > "A" )
			{
				setCookie("curbitrateD", bitrate, expDate( ) );
			}

			if( oLink )
			{
				document.location.href = oLink.href;
			}
			else
			{
				myShowObject("speedenought");
			}
		}

	}
	catch (e) {
		alert('error:' + e.message);
	}

}

function saveTestSpeedInfoShort(kbits) {
	try {
		
		var bitrate;
		intKBits = Math.floor( kbits );
		myHideObject("runningtest");
		myShowObject("testresults");
		var speed1 = document.getElementById("speedspan");
		var speed2 = document.getElementById("speedspan2");
		if( speed1 ) speed1.innerHTML = intKBits;
		if( speed2 ) speed2.innerHTML = intKBits;
		setCookie("curspeed", intKBits, expDate());
		myShowObject("speedenought");
	}
	catch (e) {
		alert('error:' + e.message);
	}

}

function myHideObject(id) {
	try {
		var obj = document.getElementById(id);
		if( obj ) {
			obj.style.display = "none";
		}
	}
	catch (e) {
		alert(e.message);
	}
}
function myShowObject(id) {
	try {
		var obj = document.getElementById(id);
		if( obj ) {
			obj.style.display = "block";
//			obj.style.visibility = "visible";
		}
	}
	catch (e) {
		alert(e.message);
	}
}
/* Cookies functions set */
function expDate() {
	oExpDate = new Date;
	oExpDate.setMonth(oExpDate.getMonth()+6);
	return oExpDate;	
}

function setCookie (name, value, expires) {
  document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/";
}

function getCookie(name) {
	var search;
	search = name + "="
	offset = document.cookie.indexOf(search) 
	if (offset != -1) {
		offset += search.length ;
		end = document.cookie.indexOf(";", offset) ;
		if (end == -1)
			end = document.cookie.length;
		return unescape(document.cookie.substring(offset, end));
	}
	else
		return "";
}

function setNonExpiresCookie(name, value) {
	document.cookie = name + "=" + escape(value) +  "; path=/";
}

function deleteCookie(name) {
	var expdate = new Date();   
	expdate.setMonth(expdate.getMonth() - 1);
	setCookie(name, "", expdate);
}

try
{
	WI_attachEvent( 'onload', ( isOpera ? document : window ), MPInit );
}
catch( e )
{
	alert( e.message );
}


function MPInit( )
{
	var
		arLink = document.getElementsByTagName( 'a' );
	
	setWindowStatus( );

	if( arLink.length )
	{
		for( var i = 0; i < arLink.length; i ++ )
		{
			WI_attachEvent( 'onmouseover', arLink[ i ], setWindowStatus );
			WI_attachEvent( 'onmouseout', arLink[ i ], setWindowStatus );
		}
	}
}

function setWindowStatus( )
{
	var
		oEvent = ( window.event ? window.event : WIEventHolder );
	
	window.status = document.title ? document.title : 'Media Player';

	if( oEvent )
	{
		oEvent.returnValue = true;
	}
}
