
/**
 * class and global definitions
 */
var LinGlobal = Class.create();
var myLinGlobal;

LinGlobal.prototype =
{
	/**
	 * initialize()
	 * Constructor runs on completion of the DOM loading.
	 */
	initialize: function()
	{
		this.menuNeedsUpdating = false;
		
		this.linInnerWidth = 0;
		this.linInnerHeight = 0;
		this.linPageXOffset = 0;
		this.linPageYOffset = 0;
	},

	/**
	 * dynamically open syslog
	 * used in ajax context
	 */
	linSyslog: function(text)
	{
		if( typeof $('linDivSyslogText') != 'undefined' )
		{
			if( $('linDivSyslogText').innerHTML == '' )
			{
				$('linDivSyslogText').innerHTML += text;	
			}
			else
			{
				$('linDivSyslogText').innerHTML += '<br />' + text;
			}
			
			this.setScrollSize();
			$('linDivSyslog').style.top = this.linPageYOffset + 15;
			$('linDivSyslog').style.left = this.linPageXOffset + 15;
			Element.show('linDivSyslog');
		}
	},
	closeSyslog: function()
	{
		$('linDivSyslogText').innerHTML = '';
		Element.hide('linDivSyslog');
	},
	
	/**
	 * check ajax compatibilty
	 * will be on every page view until it was successfully
	 * then, the page will be reloaded
	 * 
	 * use this on every page and not only on home page, because if user is linked directly to an image
	 * 
	 * ajax may be disabled with ./?linCat=ajax&disable_ajax
	 * and may be re-enabled with ./?linCat=ajax&enable_ajax
	 * 
	 * http://wiki.script.aculo.us/scriptaculous/show/Ajax.Request
	 */
	checkAjaxCompatibility: function()
	{
		new Ajax.Request(LINPHA_LINK + '&linCat=ajax&use_js', {method:'get', asynchronous:true, onSuccess:myLinGlobal.checkAjaxCompatibilitySuccess} );
	},
	
	/**
	 * reload page, now with ajax activated
	 * will be called if the request in checkAjaxCompatibility() was successfully
	 */
	checkAjaxCompatibilitySuccess: function(t)
	{
		if( t.responseText == 'use_js saved' )
		{
			myLinGlobal.linSyslog('Ajax enabled');
			location.href = document.location;
		}
	},
	
	AjaxPost: function(parameters,successFunction)
	{
		var opt = {
			// Use POST
			method: 'post',
			// Send this lovely data
			postBody: parameters
			,
			// asynchron, of course
			asynchronous:true,
			
			// Handle successful response
			onSuccess: successFunction,
	
			// Handle 404
			on404: function(t) {
				alert('Error 404: location "' + t.statusText + '" was not found.');
			},
			// Handle other errors
			onFailure: function(t) {
				alert('Error ' + t.status + ' -- ' + t.statusText);
			}
		}
	
		new Ajax.Request(xmlUrl + '&linId=' + IdCurrent + '&xml', opt);
	},
	
	reloadMenu: function()
	{
		if( this.menuNeedsUpdating )
		{
			new Ajax.Updater('menu', LINPHA_LINK + '&linCat=ajax&reloadmenu&linId='+IdCurrent, {method: 'get', asynchronous:true, evalScripts:true});
			this.menuNeedsUpdating = false;
		}
	},
	
	/**
	 * setWindowSize()
	 * 
	 * sets two variables of the inner window width and height
	 * takes care of browser incompatibilities
	 */
	setWindowSize: function()
	{
		/**
		 * browser incompatibilities...
		 */
		if (window.innerHeight) // all except Explorer
		{
			this.linInnerWidth = self.innerWidth;
			this.linInnerHeight = self.innerHeight;
		}
		else if (document.body && document.body.offsetWidth)
		{
			this.linInnerWidth = document.body.offsetWidth;
			this.linInnerHeight = document.body.offsetHeight;
		}
		else
		{
			this.linInnerWidth = 500;
			this.linInnerHeight = 300;
		}	
	},

	/**
	 * setMainHeight
	 * 
	 * sets the height in thumb and image view of the "linDivMainOuter"
	 */
	setMainHeight: function()
	{
		this.setWindowSize();
		
		var newHeight = this.linInnerHeight - $('linDivMain').offsetTop - 40;	// 35 = 15 (bottom rounded corners) + 10 (margin-bottom) + 15 (??)
		if(newHeight < 100)
		{
			newHeight = 100;
		}
	
		$('linDivMain').style.height = newHeight;
	},
	
	/**
	 * setScrollSize
	 * 
	 * sets two variables with the scroll offset
	 * takes care of browser incompatibilites
	 */
	setScrollSize: function()
	{
		if (self.pageYOffset) // all except Explorer
		{
			this.linPageXOffset = self.pageXOffset;
			this.linPageYOffset = self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
			// Explorer 6 Strict
		{
			this.linPageXOffset = document.documentElement.scrollLeft;
			this.linPageYOffset = document.documentElement.scrollTop;
		}
		else if (document.body) // all other Explorers
		{
			this.linPageXOffset = document.body.scrollLeft;
			this.linPageYOffset = document.body.scrollTop;
		}
	},
	
	fOnError: function(txt_message, url, line) {
		var txt = "Error occured...\n\n";
		txt += "Message: " + txt_message + "\n";
		txt += "URL: " + url + "\n";
		txt += "Line: " + line;
		alert(txt);
	}
}

/**
 * initLinGlobal()
 * 
 * create the class object
 */
function initLinGlobal()
{
	myLinGlobal = new LinGlobal();

	Event.observe(window, 'error', myLinGlobal.fOnError, false);
}

//Event.observe(window, 'load', initLinGlobal, false);
initLinGlobal();
