/*
UI functions for CLB web sites
by Christophe Le Bot - 2007-05
http://www.christophelebot.fr/
*/

/*=== Config ==*/
var themePath = '/wp-content/themes/clb-v2/';
/*var themePath = 'clb-v2/';*/

/*=== Cookies ===*/
/* Set cookie */
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 {
		expires = "";
	}
	document.cookie = name+"="+value+expires+";path=/";
}
/* Read cookie */
function readCookie(name) {
	var needle = name + "=";
	var cookieArray = document.cookie.split(';');
	for(var i=0;i <cookieArray.length;i++) {
		var pair = cookieArray[i];
		while (pair.charAt(0)==' ') {
			pair = pair.substring(1, pair.length);
		}
		if (pair.indexOf(needle) == 0) {
			return pair.substring(needle.length, pair.length);
		}
	}
	return null;
}

/*=== Effets ==*/
/* Toggle */
function toggleOptions(e){
	Event.stop(e);
	new Effect.toggle('options-content','appear');
	setCookie('toogle-options', readCookie('toogle-options') == 'invert' ? '' : 'invert', 365);
	var toggleImage = readCookie('toogle-options') == 'invert' ? 'url(' + themePath + 'images/options-down-arrow.png)' : 'url(' + themePath + 'images/options-up-arrow.png)';
	Element.setStyle('toogle-options', {backgroundImage:toggleImage});
}
function toggleCategories(e){
	Event.stop(e);
	new Effect.toggle('tools-content-categories','blind');
	setCookie('categories-title', readCookie('categories-title') == 'invert' ? '' : 'invert', 365);
	var toggleImage = readCookie('categories-title') == 'invert' ? 'url(' + themePath + 'images/tools-down-arrow.png)' : 'url(' + themePath + 'images/tools-up-arrow.png)';
	Element.setStyle('categories-title', {backgroundImage:toggleImage});
}
function toggleTags(e){
	Event.stop(e);
	new Effect.toggle('tools-content-tags','blind');
	setCookie('tags-title', readCookie('tags-title') == 'invert' ? '' : 'invert', 365);
	var toggleImage = readCookie('tags-title') == 'invert' ? 'url(' + themePath + 'images/tools-down-arrow.png)' : 'url(' + themePath + 'images/tools-up-arrow.png)';
	Element.setStyle('tags-title', {backgroundImage:toggleImage});
}
function toggleServices(e){
	Event.stop(e);
	new Effect.toggle('tools-content-services','blind');
	setCookie('services-title', readCookie('services-title') == 'invert' ? '' : 'invert', 365);
	var toggleImage = readCookie('services-title') == 'invert' ? 'url(' + themePath + 'images/tools-down-arrow.png)' : 'url(' + themePath + 'images/tools-up-arrow.png)';
	Element.setStyle('services-title', {backgroundImage:toggleImage});
}
function setToggle() {
	if($('options-content')) {
		if(readCookie('toogle-options') == 'invert') {
			Effect.toggle('options-content','appear');
			Element.setStyle('toogle-options', {backgroundImage:'url(' + themePath + 'images/options-down-arrow.png)'});
		}
		Event.observe('toogle-options', 'click', toggleOptions);
	}
	if(readCookie('categories-title') == 'invert') {
		Effect.toggle('tools-content-categories','blind');
		Element.setStyle('categories-title', {backgroundImage:'url(' + themePath + 'images/tools-down-arrow.png)'});
	}
	Event.observe('categories-title', 'click', toggleCategories);
	
	if(readCookie('tags-title') == 'invert') {
		Effect.toggle('tools-content-tags','blind');
		Element.setStyle('tags-title', {backgroundImage:'url(' + themePath + 'images/tools-down-arrow.png)'});
	}
	Event.observe('tags-title', 'click', toggleTags);
	
	if(readCookie('services-title') == 'invert') {
		Effect.toggle('tools-content-services','blind');
		Element.setStyle('services-title', {backgroundImage:'url(' + themePath + 'images/tools-down-arrow.png)'});
	}
	Event.observe('services-title', 'click', toggleServices);
}

/*=== Init ===*/
Event.observe(window, 'load', setToggle);