var Kuwaity = Kuwaity || {blockHide: false, domain: null, cookiePath: '/'};

Kuwaity.init = function ()
{
	Kuwaity.block('sidebar-hide');
};

Kuwaity.block = function (el)
{
	el = $(el);
	if (!el)
	{
		return false;
	}
	var icon = 'hide';
	if (Kuwaity.getCookie('sidebarHide'))
	{
		Effect.DropOut('block', 'appear');
		Element.setStyle('main', {'margin-right': '0px'});
		icon = 'show';
	}
	el.innerHTML = '<img src="'+ Kuwaity.domain +'/templates/images/blue/sidebar-' + icon + '.png" id="sidebar-img" border="0" />';
	Event.observe(el, 'click', Kuwaity.toggleBlock);
};

Kuwaity.toggleBlock = function()
{
	if (!Kuwaity.blockHide)
	{
		Kuwaity.blockHide = true;
		Effect.DropOut('block', 'appear');
		Element.setStyle('main', {'margin-right': '0px'});
		Element.setStyle('main', {'width': '100%'});
		$('sidebar-img').src = Kuwaity.domain +'/templates/images/blue/sidebar-show.png';
		Kuwaity.setCookie('sidebarHide', true);
	}
	else
	{
		Kuwaity.blockHide = false;
		Effect.toggle('block');
		Element.setStyle('main', {'margin-right': '200px'});
		Element.setStyle('main', {'width': '90%'});
		
		$('sidebar-img').src = Kuwaity.domain +'/templates/images/blue/sidebar-hide.png';
		Kuwaity.unsetCookie('sidebarHide');
	}
};

Kuwaity.setCookie = function (name, value, expires)
{
	if(!expires)
	{
		expires = "; expires=Wed, 1 Jan 2020 00:00:00 GMT;"
	}
	else
	{
		expire = new Date();
		expire.setTime(expire.getTime()+(expires*1000));
		expires = "; expires="+expire.toGMTString();
	}

	if(Kuwaity.domain)
	{
		domain = "; domain="+Kuwaity.domain;
	}
	else
	{
		domain = "";
	}

	if(Kuwaity.cookiePath)
	{
		path = Kuwaity.cookiePath;
	}
	else
	{
		path = "";
	}

	document.cookie = name+"="+escape(value)+"; path="+path+domain+expires;
};

Kuwaity.getCookie = function(name)
{
	cookies = document.cookie;
	name = name+"=";
	cookiePos = cookies.indexOf(name);
	if(cookiePos != -1)
	{
		cookieStart = cookiePos+name.length;
		cookieEnd = cookies.indexOf(";", cookieStart);
		if(cookieEnd == -1)
		{
			cookieEnd = cookies.length;
		}
		return unescape(cookies.substring(cookieStart, cookieEnd));
	}
	return false;
};

Kuwaity.unsetCookie = function(name)
{
	Kuwaity.setCookie(name, 0, -1);
};