// Check user agent
var agt = navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_ie = ((agt.indexOf('msie') != -1) && (agt.indexOf('opera') == -1));
var is_ie6 = (is_ie && (is_major == 4) && (agt.indexOf('msie 6.')!= -1) );
var is_ff = agt.indexOf('firefox');

// Background element
var backgroundDiv = null;
var backgroundAdded = false;

// Array of select controls
var arrSelectControls = new Array();

var popUpShowing = false;

var globPopUpID = null;
var globScreenLocation = null;
var globPopWidth = null;

// Show the pop up window
var showWindow = function (popUpID, src, popWidth, popHeight, screenLocation) {
	// Set global properties to be used when screen is re-sized / scrolled
	globPopUpID = popUpID;
	globScreenLocation = screenLocation;

	// Get users scroll position
	var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

    var popUp = document.getElementById(popUpID);
    if (!popUp) {
        popUp = document.createElement('div');
        var body = document.getElementsByTagName("body").item(0);
        popUp.id = popUpID;
        body.appendChild(popUp);
    }
    
	if (!backgroundAdded) {
	    // Set up background
	    backgroundDiv = document.createElement('div');

	    // Style background div element
	    backgroundDiv.style.position = 'absolute';
	    backgroundDiv.style.top = 0;
	    backgroundDiv.style.left = 0;
	    backgroundDiv.style.backgroundColor = '#FFFFFF';
	    backgroundDiv.style.filter = 'alpha(opacity=75)';
	    backgroundDiv.style.opacity = 0.75;
	    backgroundDiv.style.KhtmlOpacity = 0.75;
	    backgroundDiv.style.MozOpacity = 0.75;

	    // Add element to DOM
	    document.getElementById(popUpID).parentNode.appendChild(backgroundDiv);

	    backgroundAdded = true;
	}

	// Determine height and width of background div element
	var clientWidth = getClientWidth();
	var clientHeight = getClientHeight();

	// Update background element size
	backgroundDiv.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth)+'px';
	backgroundDiv.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight)+'px';

	backgroundDiv.style.display = 'block';

	// Style pop up control
	document.getElementById(popUpID).className = 'popUp';
	document.getElementById(popUpID).style.width = popWidth + 'px';
	document.getElementById(popUpID).style.height = popHeight + 'px';
	document.getElementById(popUpID).style.zIndex = '999';
	document.getElementById(popUpID).style.position = 'absolute';
	document.getElementById(popUpID).style.visibility = 'hidden';
	document.getElementById(popUpID).style.display = 'block';

	// Get exact height of the pop up
	popHeight = document.getElementById(popUpID).offsetHeight;
	popWidth = document.getElementById(popUpID).offsetWidth;

    var leftPos, topPos;

	if ( is_ie ) {
		leftPos = (document.documentElement.clientWidth / 2);
		topPos = (document.documentElement.clientHeight / 2);
	}
	else {
		leftPos = (Math.min(window.innerWidth, document.documentElement.clientWidth) / 2);
		topPos = (Math.min(window.innerHeight, document.documentElement.clientHeight) / 2);
	}

	// Set the correct location of the pop-up depending on the user preference and scroll location
	if ( screenLocation == 'topLeft' ) {
		leftPos = (20 + scrollLeft);
		topPos = (20 + scrollTop);
	}
	else if ( screenLocation == 'topCenter' ) {
		leftPos = (leftPos - (popWidth / 2) + scrollLeft);
		topPos = (20 + scrollTop);
	}
	else if ( screenLocation == 'bottomCenter' ) {
		leftPos = (leftPos - (popWidth / 2) + scrollLeft);
		topPos = ((document.documentElement.clientHeight) - popHeight + scrollTop);
	}
	else {
		// Default is center screen
		leftPos = (leftPos - (popWidth / 2) + scrollLeft);
		topPos = (topPos - (popHeight / 2) + scrollTop);
	}

	// Set pop up position
	document.getElementById(popUpID).style.top = topPos+'px';
	document.getElementById(popUpID).style.left = leftPos+'px';
	document.getElementById(popUpID).style.visibility = 'visible';

	// Hide all select controls that are outside the pop up - IE6 only
	if (is_ie6)
	{
		// Hide all selects
		arrSelectControls = document.getElementsByTagName('SELECT');

        var x;
		for (x = 0; x < arrSelectControls.length; x++) {
			arrSelectControls[x].style.visibility = 'hidden';
		}

		// Show selects within the pop up element
		var arrPopUpSelectControls = new Array();
		arrPopUpSelectControls = document.getElementById(popUpID).getElementsByTagName('SELECT');

		for (x = 0; x < arrPopUpSelectControls.length; x++) {
			arrPopUpSelectControls[x].style.visibility = 'visible';
		}
	}
    popUp.innerHTML = '<div class="popUp-head"><a href="#" onClick="hideWindow(\''+popUpID+'\');"><img src="'+ROOT_URL+'images/icons/clear.gif" title="close" alt="close"/></a></div><iframe width="'+popWidth+'px" height="'+popHeight+'px" src="'+src+'"></iframe>';

	popUpShowing = true;

    return false;
};

var showFullScreen = function(title, href) { showWindow(title, href, 990, 680); };

// Hide the pop up window and reset background
var hideWindow = function(popUpID) {
    // Hide the pop up element
    document.getElementById(popUpID).style.display = 'none';

    // Hide background div element
    if (backgroundDiv)
        backgroundDiv.style.display = 'none';

	// Show all select controls - IE 6 only
	if (is_ie6)
		for ( x = 0; x < arrSelectControls.length; x++ ) {
			arrSelectControls[x].style.visibility = 'visible';
		}

	// Reset variables
	popUpShowing = false;
	globPopUpID = null;
	globScreenLocation = null;
	globPopWidth = null;
};

// Update pop up position when user scrolls or re-sizes the window
var updatePopUp = function()
{
	if (popUpShowing)
	{
	    var popHeight = document.getElementById(globPopUpID).offsetHeight;
	    var globPopWidth = document.getElementById(globPopUpID).offsetWidth;

		// Get users scroll position
		var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
		var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

        var leftPos, topPos;

		if ( is_ie ) {
			leftPos = (document.documentElement.clientWidth / 2);
			topPos = (document.documentElement.clientHeight / 2);
		}
		else {
			leftPos = (Math.min(window.innerWidth, document.documentElement.clientWidth) / 2);
			topPos = (Math.min(window.innerHeight, document.documentElement.clientHeight) / 2);
		}
		// Set the correct location of the pop-up depending on the user preference and scroll location
		if ( globScreenLocation == 'topLeft' ) {
			leftPos = (20 + scrollLeft);
			topPos = (20 + scrollTop);
		}
		else if ( globScreenLocation == 'topCenter' ) {
			leftPos = (leftPos - (globPopWidth / 2) + scrollLeft);
			topPos = (20 + scrollTop);
		}
		else if ( globScreenLocation == 'bottomCenter' ) {
			leftPos = (leftPos - (globPopWidth / 2) + scrollLeft);
			topPos = ((document.documentElement.clientHeight) - popHeight + scrollTop);
		}
		else {
			// Default is center screen
			leftPos = (leftPos - (globPopWidth / 2) + scrollLeft);
			topPos = (topPos - (popHeight / 2) + scrollTop);
		}

		// Determine height and width of background div element
		var clientWidth = getClientWidth();
		var clientHeight = getClientHeight();

		// Update background element size
		backgroundDiv.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth)+'px';
        backgroundDiv.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight)+'px';

		// Update pop up location
		if (popHeight < parseInt(clientHeight))
		    document.getElementById(globPopUpID).style.top = topPos+'px';

		if (globPopWidth <= parseInt(clientWidth))
		    document.getElementById(globPopUpID).style.left = leftPos+'px';
	}
};

var getClientWidth = function()
{
	if (is_ie)
		return document.documentElement.clientWidth;
	else
		return Math.min(window.innerWidth, document.documentElement.clientWidth);
};

var getClientHeight = function()
{
	if (is_ie)
		return document.documentElement.clientHeight;
	else
		return Math.min(window.innerHeight, document.documentElement.clientHeight);
};

// Adjust pop up & background as user scrolls
window.onresize = updatePopUp;
window.onscroll = updatePopUp;

var myTest = function() { alert(document.body.innerHtml); };

var OpenFileBrowser = function( url, width, height )
{
	// oEditor must be defined.

	var iLeft = 20 ;
	var iTop  = 20 ;

	var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes,scrollbars=yes" ;
	sOptions += ",width=" + width ;
	sOptions += ",height=" + height ;
	sOptions += ",left=" + iLeft ;
	sOptions += ",top=" + iTop ;

	// The "PreserveSessionOnFileBrowser" because the above code could be
	// blocked by popup blockers.
	if ( navigator.appName == "Microsoft Internet Explorer" )
	{
		// The following change has been made otherwise IE will open the file
		// browser on a different server session (on some cases):
		// http://support.microsoft.com/default.aspx?scid=kb;en-us;831678
		// by Simone Chiaretta.
		var oWindow = window.open( url, 'BrowseWindow', sOptions ) ;
		var problem = "Your popup blocker has prevented this functionality from working.  Please allow popups for this site!";
		if ( oWindow )
		{
			// Detect Yahoo popup blocker.
			try
			{
				var sTest = oWindow.name ; // Yahoo returns "something", but we can't access it, so detect that and avoid strange errors for the user.
				oWindow.opener = window ;
			}
			catch(e)
			{
				alert( problem ) ;
			}
		}
		else
			alert( problem ) ;
    }
    else
		window.open( url, 'BrowseWindow', sOptions ) ;
};

function getSid() {
    return 0;
}


