// +------------------------------------------------------------------+
// | JavaScript version 1.0                                           |
// +------------------------------------------------------------------+
// | general.js – Some standard functions used in the CMS cleint      |
// |              side. Script is loaded in template by parser.       |
// +------------------------------------------------------------------+
// | Copyright (c) 2008 MultiMove                                     |
// +------------------------------------------------------------------+
// | Authors: S.F.Beck <sander@multimove.nl>     					  |
// +------------------------------------------------------------------+

//Pop-up vars
var popupname; 
var popupid;
var click = false;
	
//Slider vars
var step = 5; 
var height = 85;
var mm_header = '';
var _root = '';


/**
 * The load function
 * 
 * @return void
 */
function doLoad()
{
	//Read cookie for header
	if( readCookie('mm_header') == 'in')
	{
		document.getElementById('header').style.height = '15px';
		document.getElementById('show_hide_header').className = "hide_header";
		height = 15;
	}

	//Auto select first form element if available
	if (document.forms.length > 0) {
		var field = document.forms[0];
		
		//For the loginfields
		if(field.name == 'login'){
			if(document.forms['login'].elements['username'].value == ""){
					document.forms['login'].elements['username'].focus();
			}
			else{
 				document.forms['login'].elements['password'].focus();
			}
		}
		else{
			for (i = 0; i < field.length; i++) {
				if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
					document.forms[0].elements[i].focus();
				break;
				}
			 }
		}
   	}
}


/**
 * Sliding the header
 * 
 * @return void
 */
function slide_header(){	
	if( readCookie('mm_header') == 'in' || mm_header == 'in')
	{
		slide_out();
		eraseCookie('mm_header');
		document.getElementById('show_hide_header').className = "show_header";
		mm_header = ''; //IE6
	}
	else
	{	
		slide_in();
		createCookie('mm_header','in',1);
		document.getElementById('show_hide_header').className = "hide_header";
		mm_header = 'in'; //IE6
	}
}
	
	
/**
 * Slide the header in
 * 
 * @return void
 */
function slide_in(){ 	
	height -= step; 
	document.getElementById('header').style.height = height + 'px'; 
	if ( height > 15 ) setTimeout('slide_in()', 30); 
}


/**
 * Slide the header out
 * 
 * @return void
 */
function slide_out(){ 
	height += step; 
	document.getElementById('header').style.height = height + 'px'; 
	if ( height < 85 ) setTimeout('slide_out()', 30); 
}



/**
 * Check if already was detected
 * 
 * @return bool
 */
function needToDetect()
{
    try
    {
        var allcookies = document.cookie;    
        return (allcookies.indexOf("popup") == -1);
    }
    catch (exception)
    {
        return true;
    }
}


/**
 * Detect pop-up blocker
 * 
 * @return bool
 */
function detect_blocker()
{
	var e = false;
	var pw1 = null;
	var pw2 = null;
	try
	{
	    if (!needToDetect())
	    {
	        return false;
	    }
		
		do
		{
			var d = new Date();
			var wName = "ptest";
			var testUrl = _root + "help.html";
			pw1 = window.open(testUrl,wName,"width=0,height=0,left=5000,top=5000",true);
			if (null == pw1 || true == pw1.closed)
			{
				e = true;
				break;
			}
			
			pw2 = window.open(testUrl,wName,"width=0,height=0");
			if (null == pw2 || true == pw2.closed)
			{
				e = true;
				break;
			}
			if (IsYBI())
			{
				if (pw1 != pw2)
				{
					e = true;
					break;
				}
			}			
			
			if (!pw1.closed && !pw2.closed)
			{
			pw1.close();
			pw2.close();
			}
			pw1 = pw2 = null;
		}
		while(false);
	}
	catch(ex)
	{
		e = false;
	}
	if (null != pw1)
	{
		try { if (!pw1.closed) pw1.close(); } catch(ex){}
	}
	if (null != pw2)
	{
		try { if (!pw2.closed) pw2.close(); } catch(ex){}
	}
	return e;
}


/**
 * Hide element by id
 * 
 * @param object element
 * @return void
 */
function hide_element( element )
{
	document.getElementById(element).style.display = 'none'; 	
}


/**
 * Copy text from a field to clipboard
 * 
 * @param object field
 * @return void
 */
function copy_to_clipboard( field, feedback )
{
	var data = document.getElementById(field).value;
	window.clipboardData.setData('Text', data);
	alert(feedback);
}

/**
 * Open imagebrowser
 * 
 * @return void
 */
function selectImage(session_id)
{
	popup('connectors/imagebrowser/imagebrowser.php?sid='+session_id, 900, 600);
}


/**
 * Pop-up callback
 * 
 * @return void
 */
function popupCallback(id, name)
{
	eval(popupname).value = name;
	eval(popupid).value   = id;
}


/**
 * Make pop-up windows
 * 
 * @return void
 */
function popup( url, width, height )
{
	var iLeft = (screen.width - width) / 2 ;
	var iTop  = (screen.height - height) / 2 ;

	var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes,scrollbars=yes," ;
	sOptions += ",width=" + width ;
	sOptions += ",height=" + height ;
	sOptions += ",left=" + iLeft ;
	sOptions += ",top=" + iTop ;

	window.open( url, "FCKBrowseWindow", sOptions ) ;
}


/**
 * Open file from connector and place
 * 
 * @return void
 */
function OpenFile( fileUrl, fileDescription, fileID )
{
	window.top.opener.SetUrl( fileUrl, fileDescription, fileID ) ;
	window.top.close() ;
	window.top.opener.focus() ;
}


var PositionX = 100;
var PositionY = 100;
var defaultWidth  = 100;
var defaultHeight = 100;

// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows
var AutoClose = true;

if (parseInt(navigator.appVersion.charAt(0))>=4){
	var isNN=(navigator.appName=="Netscape")?1:0;
	var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;
}
var optNN='scrollbars=no, status=yes, width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no, status=yes, width=150,height=100,left='+PositionX+',top='+PositionY;

function popImage(imageURL,imageTitle){
	if (isNN){imgWin=window.open('about:blank','',optNN);}
	if (isIE){imgWin=window.open('about:blank','',optIE);}
	with (imgWin.document){
		writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
		writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
		writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
		writeln('function reSizeToImage(){');
		writeln('if (isIE){');
		writeln('window.resizeTo(100,100);');
		writeln('width=10+document.images[0].width;');
		writeln('height=59+document.images[0].height;');
		
		writeln('window.resizeTo(width,height);}');
		writeln('if (isNN){');
		writeln('window.innerWidth=document.images["George"].width;');
		writeln('window.innerHeight=document.images["George"].height;}}');
		writeln('function doTitle(){document.title="'+imageTitle+'";}');
		writeln('</sc'+'ript>');
		if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle(); self.focus()">')
		else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
		writeln('<img name="George" src='+imageURL+' onClick="window.close();" style="display: block; cursor: pointer;"></body></html>');
		close();
	}
}
/**
 *
 */
function strpos(haystack, needle, offset)
{
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}

/**
 *
 */
function loadVideo(url, width, height, backcolor, frontcolor, lightcolor)
{
	//Initiate video player
	var so = new SWFObject(BASE_URL+"flash/videoplayer.swf", "videoplayer", width, height, "9");
	so.addParam('allowfullscreen', 'true');
	so.addParam('allowscriptaccess', 'always');
	so.addParam('wmode', 'transparent');
	so.addVariable('flashvars', '&amp;file='+url+'&amp;autostart=true&amp;repeat=true&amp;backcolor='+backcolor+'&amp;frontcolor='+frontcolor+'&amp;lightcolor='+lightcolor);
	so.write("video_player");
}

/**
 *
 */
function initShadowbox(backcolor, frontcolor, lightcolor, text_cancel, text_previous, text_next, text_close, text_of)
{
	try { Shadowbox.init(backcolor+';'+frontcolor+';'+lightcolor+';'+text_cancel+';'+text_previous+';'+text_next+';'+text_close+';'+text_of); }
	catch(e) { alert(e.toString()); }
}
