/**	Other functions.
*	
*	@author Dinu Florin.
*/

/**	Miscalaneous scripts for the menu.
*	
*	@author Dinu Florin.
*/
var img = new Image();
img.src = LINK_BASE+"images/layout/content_box.png";

var img = new Image();
img.src = LINK_BASE+"images/layout/btn_over.jpg";

function hideAllTips()
{
	document.getElementById('MTAcasa').style.display = 'none';
	document.getElementById('MTPetreceri').style.display = 'none';
	document.getElementById('MTContact').style.display = 'none';
	document.getElementById('MTGalerie').style.display = 'none';
	document.getElementById('MTComentarii').style.display = 'none';
	document.getElementById('MTTest').style.display = 'none';
}
function showTip(id)
{
	hideAllTips();
	document.getElementById(id).style.display = 'block';
}
/** Insert PNG image.
*
*	@param src string, the image url.
*	@return void
*/
function insertPng(src)
{
	if(navigator.userAgent.indexOf('IE') != -1)
	{
		document.write('<img style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+LINK_BASE+src+'\');" src="'+LINK_BASE+'blank.gif" alt=" ">');
	}
	else
	{
		document.write('<img src="'+LINK_BASE+src+'" alt=" ">');
	}
}
/** Movable object class. Adds the necessary behaviour to a html element to make it draggable */
function MovableObject()
{
	var ww = getWindowWidth();
	var wh = getWindowHeight();
	
	this.y = 0;
	this.x = 0;
	this.width = 0;
	this.height = 0;
	var element = null;
	this.initialize = function (_element)
	{
		element = _element;
		
		this.x = getElementLeft(element);
		this.y = getElementTop(element);
		this.width = getElementWidth(element);
		this.height = getElementHeight(element);
		
		element.classInstance = this;
		element.onmousemove = onMouseMove;
		element.onmouseup = onMouseUp;
		element.onmousedown = onMouseDown;
	}
	/* MouseUp handler. This is private */
	var onMouseUp = function(e)
	{
		if(!e) e = window.event;

		this.canDrag = false;
		return false;
	}
	/* MouseDown handler. This is private */
	var onMouseDown = function(e)
	{
		ww = getWindowWidth();
		wh = getWindowHeight();
		this.classInstance.width = getElementWidth(element);
		this.classInstance.height = getElementHeight(element);
		if(!e) e = window.event;

		this.canDrag = true;
		return false;
	}
	/* MouseMove handler. This is private */
	var onMouseMove = function(e)
	{
		if(this.canDrag)
		{
			if(!e) e = window.event;
			
			this.classInstance.setX(e.clientX - this.classInstance.width/2);
			this.classInstance.setY(e.clientY - this.classInstance.height/2);
		}
		return false;
	}
	/** Change the y position.
		@param _y integer, the new position.
		@return void.
	*/
	this.setY = function(_y)
	{
		this.y = _y;
		element.style.top = this.y + 'px';
	}
	/** Change the x position.
		@param _x integer, the new position.
		@return void.
	*/
	this.setX = function(_x)
	{
		this.x = _x;
		element.style.left = this.x + 'px';
	}
}
/** Get the x coordinate of an dom element 
*	
*	@param element HtmlObject
*	@return integer, the top position in pixels
*/
function getElementLeft(element)
{
	if(element.offsetLeft>0) return element.offsetLeft;
	else if (element.currentStyle) return parseInt(element.currentStyle['left']);
	else if (window.getComputedStyle)
		return parseInt(document.defaultView.getComputedStyle(element,null).getPropertyValue('left'));
	return null;
}
/** Get the y coordinate of an dom element 
*	
*	@param element HtmlObject
*	@return integer, the left position in pixels
*/
function getElementTop(element)
{
	if(element.offsetTop>0) return element.offsetTop;
	else if (element.currentStyle) return parseInt(element.currentStyle['top']);
	else if (window.getComputedStyle)
		return parseInt(document.defaultView.getComputedStyle(element,null).getPropertyValue('top'));
	return null;
}
/** Get the height of an dom element 
*	
*	@param element HtmlObject
*	@return integer, the height in pixels
*/
function getElementHeight(element)
{
	if(element.offsetHeight>0) return element.offsetHeight;
	else if (element.currentStyle) return parseInt(element.currentStyle['height']);
	else if (window.getComputedStyle)
		return parseInt(document.defaultView.getComputedStyle(element,null).getPropertyValue('height'));
	return null;
}
/** Get the width of an dom element 
*	
*	@param element HtmlObject
*	@return integer, the width in pixels
*/
function getElementWidth(element)
{
	if(element.offsetWidth>0) return element.offsetWidth;
	else if (element.currentStyle) return parseInt(element.currentStyle['width']);
	else if (window.getComputedStyle)
		return parseInt(document.defaultView.getComputedStyle(element,null).getPropertyValue('width'));
	return null;
}
/**	Set a cookie.
*	
*	@param name string, the cookie name.
*	@param value string, the cookie value.
*	@param days integer, the numbar of days until the cookie expires.
*	@return void.
*/
function setCookie(name, value, days) 
{
	if(days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
/**	Get a cookie.
*	
*	@param name string, the cookie name.
*	@return string, the cookie value.
*/
function getCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
/**	Remove a cookie.
*	
*	@param name string, the cookie name.
*	@return void.
*/
function removeCookie(name) 
{
	createCookie(name,"",-1);
}
/**	Get the window width.
*	
*	@return integer, the window width in pixels.
*/
function getWindowWidth()
{
	var width = 0;
  	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
   		width = window.innerWidth;
  	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
    	//IE 6+ in 'standards compliant mode'
    	width = document.documentElement.clientWidth;
  	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
    	//IE 4 compatible
    	width = document.body.clientWidth;
  	}
	return width;
}
/**	Get the window height.
*	
*	@return integer, the window height in pixels.
*/
function getWindowHeight()
{
	var height = 0;
  	if( typeof( window.innerWidth ) == 'number' ) 
	{
    	//Non-IE
   		height = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
	    //IE 6+ in 'standards compliant mode'
    	height = document.documentElement.clientHeight;
  	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
    	//IE 4 compatible
    	height = document.body.clientHeight;
  	}
	return height;
}
/**	Get elements by class name.
*	
*	@param tag string, tag name, it will search for these tags and there children only.
*	@param className, the class name to search for.
*	@return array, the elements it found.
*/
function getElementsByClass(tag, className) 
{
	var elements = new Array();
	var tags = document.getElementsByTagName(tag);
	for(var i=0; i<tags.length; i++) 
	{
		if(tags[i].className.match(new RegExp("(^|\\s)" + className + "(\\s|$)"))) elements.push(tags[i]);
	}
	return elements;
}
/*	Launch the admin panel. */
var misc_oldKeyHandler = null;
function goAdmin(e)
{
	if(!e && window.event) var e = window.event;
	if(misc_oldKeyHandler) misc_oldKeyHandler(e);
	if(e)
	{
		if(e.ctrlKey && e.shiftKey)
		{
			if(e.which) var c = e.which;
			else if(e.keyCode) var c = e.keyCode;
			else return false;
			if(String.fromCharCode(c) == 'A')
			{
				window.open(LINK_BASE+"admin");
			}
		}
	}
}
if(document.onkeypress) misc_oldKeyHandler = document.onkeypress;
document.onkeypress = goAdmin;

/**	AjaxQuery class.
*	
*	@author Dinu Florin.
*/
function AjaxQuery()
{
	var xmlHTTP = ajax_GetXmlHttpObject();
	if(!xmlHTTP) throw("Could not initialise the AJAX framework");
	
	var onOpenEvents = new Array();
	var onRecievingEvents = new Array();
	var onLoadedEvents = new Array();
	
	/**	Load a remote file.
	*	
	*	@param remote_file string, the remote file to load.
	*	@param async boolean, how to load the file, asynchronous or with blocking.
	*	@param method string, the http request method (get, post..).
	*	@param data string, aditional data to send in the request body (on post for example).
	*	@return void.
	*/
	this.load = function(remote_file, async, method, data)
	{
		if(async == undefined) async = true;
		if(data == undefined) data = null;
		if(method == undefined) method = "GET";
		xmlHTTP.open(method, remote_file, async);
		xmlHTTP.send(data);
	}
	/**	Perform a POST request.
	*	
	*	@param remote_file string, the remote file to load.
	*	@param data string, aditional data to send in the request body.
	*	@return void.
	*/
	this.post = function(remote_file, data)
	{
		if(data == undefined) data = null;
		xmlHTTP.open("POST", remote_file, true);
		xmlHTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHTTP.send(data);
	}
	
	/**	Add an event listener.
	*	
	*	@param type string, event type ("open", "recieving", "loaded").
	*	@param handler function, a function to be called when the event occurs.
	*	@return void.
	*/
	this.addEvent = function(type, handler)
	{
		switch(type)
		{
			case "open":
				onOpenEvents.push(handler);
				break;
			case "recieving":
				onRecievingEvents.push(handler);
				break;
			case "loaded":
				onLoadedEvents.push(handler);
				break;
		}
	}
	
	/* Private, used to trigger the event listeners */
	xmlHTTP.onreadystatechange = function() 
	{
		switch(xmlHTTP.readyState)
		{
			case 1:
				for(i in onOpenEvents) {onOpenEvents[i]();}
				break;
			case 3:
				for(i in onRecievingEvents) {onRecievingEvents[i]();}
				break;
			case 4:
				for(i in onLoadedEvents) {onLoadedEvents[i](xmlHTTP.responseText);}
				break;
		}
	}
}

/* Miscalaneous functions used by this class */
function ajax_checkAJAX()
{
	if(obj = ajax_GetXmlHttpObject()!=null)
	{
		delete obj;
		return true;
	}
	else return false;
}
function ajax_GetXmlHttpObject(handler)
{ 
	var XMLHttp=null;
	try
	{
		XMLHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
 		{
 			XMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
 		}
		catch(e)	
		{
			try
			{
				XMLHttp=new XMLHttpRequest();
			}
			catch(e) {}
		}
	}
	return XMLHttp;
}
