
/**
 * Set a cookie ith JavaScript in a page
 * @param string cName Cookie name
 * @param string value Cookie value
 * @param integer expiredays How many days is the Cookie valid
 * @return void
 */
function setCookie(cName, value, expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);

	document.cookie = cName + "=" + escape(value) + (
		(null == expiredays)
			? ""
			: ";expires=" + exdate.toGMTString()
		);
}

/**
 * Get the value of a valid cookie
 * @param string cName Cookie name
 * @return string
 */
function getCookie(cName)
{
	if (document.cookie.length > 0)
	{
		cStart = document.cookie.indexOf(cName + "=");
		if (cStart != -1)
		{
			cStart = cStart + cName.length + 1;
			cEnd = document.cookie.indexOf(";", cStart);
			if (cEnd == -1)
			{
				cEnd = document.cookie.length;
			}
			return unescape(document.cookie.substring(cStart,cEnd));
		}
	}
	return "";
}

InteractiveMessage = Class.create();
InteractiveMessage.prototype =
{

	/**
	 * 
	 * @param object Options
	 * @return InteractiveMessage
	 */
	initialize: function(Options)
	{
		this.TYPES = [ 'success', 'error', 'info' ];

		/**
		 * Default options for an InteractiveMessage
		 */
		this.Options = {
			message:	'---',
			type:		this.TYPES.MESSAGE_INFO,
			element:	'interactive-message',
			display:	'block',
			timeout:	4,
			onOpen:		null,
			onClose:	null,

			classPrefix: 'trafic-im-',
			timer:		null
		};

		Object.extend(this.Options, Options || {});

		if ('' == this.Options.message)	{ throw "No message provided."; }

		this.show();
	},
	
	/**
	 * Display the interactive message
	 * @return void
	 */
	show: function()
	{
		if (null != this.timer)
		{
			clearTimeout(this.timer);
		}
		
		this.element = $(this.Options.element);
		
		if (-1 == this.TYPES.indexOf(this.Options.type))
		{
			this.Options.type = this.TYPES.MESSAGE_INFO;
		}

		this.cssClass = (this.Options.classPrefix + this.Options.type);

		this.element.innerHTML = this.Options.message;
		this.element.className = '';
		this.element.addClassName(this.cssClass);

		this.element.style.display = (this.Options.display == 'inline') ? 'inline' : 'block';
	
		this.Options.timeout *= 1000;

		this.timer = window.setTimeout(this.hide.bindAsEventListener(this), this.Options.timeout);
		
		if (null != this.Options.onOpen)
		{
			(this.Options.onOpen)();
		}
	},
	
	/**
	 * Hide an interactive message
	 * This method is automatically called by the object
	 * @return void
	 */
	hide: function()
	{
		this.element.innerHTML = '';
		this.element.className = '';
		this.element.style.display = 'none';

		clearTimeout(this.timer);
		
		this.element = null;
		
		if (null != this.Options.onClose)
		{
			(this.Options.onClose)();
		}
	}

}

Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
if (Prototype.Browser.IE6)
{
	Event.observe(window, 'load', function(){
		bodyIE6 = document.getElementsByTagName('BODY');
		bodyIE6 = bodyIE6[0];
		warning = new Element('DIV', { 'class': 'ie6-warning', 'id': 'ie6-warning-div' }).update('<img src="http://cdn.trafic.ro/img/warning-ie6/warning-icon.png" title="WARNING" alt="WARNING:" /> Pentru a vizualiza corect siteul trafic.ro, te rugam sa iti actualizezi versiunea de Internet Explorer. ');
		updateIE6 = new Element('A', {'href': 'http://windows.microsoft.com/ro-ro/internet-explorer/downloads/ie', 'target': 'IE6-Update'}).update('Click aici');
		closeIE6Warning = new Element('A', {'onclick': '$("ie6-warning-div").hide();', 'class': 'ie6-warning-close'}).update('<img title="CLOSE" alt="CLOSE" src="http://cdn.trafic.ro/img/warning-ie6/xion.png" />');
		warning.appendChild(updateIE6);
		warning.appendChild(closeIE6Warning);

		bodyIE6.insertBefore(warning, bodyIE6.firstChild);
	});
}

