/*!
 * byo - 2008
 * list v1
 */

var tools= {

	options : null,

	// init //////////////////////////////
	init : function(options) {

		if(options==undefined) {
			options= tools.options;
		}

		// hash the options
		options= new Hash(options);

		options.each(function(elements, option){

			eval("tools."+option+"('"+elements+"')");

		}) // /each

	},
	// /init //////////////////////////////

	// addMouseOver //////////////////////////////
	addMouseOver : function(items){

		items= $$(items);

		items.addEvent('mouseover', function(){
			this.addClass('hover');
		})

		items.addEvent('mouseout', function(){
			this.removeClass('hover');
		})

	},
	// /addMouseOver //////////////////////////////

	// addFocus //////////////////////////////
	addFocus : function(elements) {

		$$(elements).addEvent('focus',function(){

			this.addClass('focus');

			// add the onblur event
			this.addEvent('blur', function(){
				this.removeClass('focus');
				this.removeEvents('blur');
			})

			return true;

		});

	},
	// /addFocus //////////////////////////////

	// decorateCheckbox //////////////////////////////
	decorateCheckbox : function(items) {

		items= $$(items);

		items.each(function(item, index){

			if(item.getStyle('display')=='none') {
				//window.console.log('skip');
				return false;
			}

			//alert(item.getNext('div'));

			// hidden the input checkbox
			item.setStyle('display','none');

			// set the id of the item
			var itemId= item.getProperty('name');
			item.setProperty('id', itemId);

			// checked or not
			var checked= '';
			if (item.getProperty('checked')==true) checked= '-checked';

			// create tue image element
			var img = new Element('img',{
							'src' : themepath + '/graphics/checkbox' + checked + '.png',
							'styles' : {
								/*'display' : 'block',*/
								'vertical-align' : 'text-bottom',
								'cursor' : 'pointer'
							}
						});

			// add the event on the image
			img.addEvent('click', function(){

				var cb= $(itemId);
				if (cb.getProperty('checked')==false) {
					cb.setProperty('checked', true);
					this.setProperty('src', themepath + '/graphics/checkbox-checked.png');
				} else {
					cb.setProperty('checked', false);
					this.setProperty('src', themepath + '/graphics/checkbox.png');
				}

			})

			// inject it in the page
			img.inject(item, 'after');

		});

		return true;
	},
	// /decorateCheckbox //////////////////////////////

	// resetCheckbox //////////////////////////////
	resetCheckbox : function(items) {

		items= $$(items);

		items.each(function(item, index){

			item.getNext('img').setProperty('src', themepath + '/graphics/checkbox.png');

		});

		return true;
	},
	// /resetCheckbox //////////////////////////////

	// load //////////////////////////////
	load : function(url, container, onComplete) {

		var container= $(container);

		/* Avoid IE7 bug but don't execute JS in loaded page
		*/
		/*
		alert(url);

		var request = new Request({
							url: url,
							method: 'get',
							onFailure: function () {alert('error')},
							onComplete: function (response, xml) {
											container.set('html', response);
											alert(response);
											onComplete();
										}
						});

		request.send();
		return false;
		*/

		container.set('load', {
							onFailure: function() {
											alert('error')
							},
							onRequest: function(){
											byo.overlay.displayLoading();
											byo.overlay.showMessage('loading');
							},
							onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) {

											if ($defined(onComplete)) {
												onComplete();
											}

											byo.overlay.showMessage('done', true);
											byo.overlay.hideLoading();
							}
		});

		container.load(url);

		return false;

	}
	// /load //////////////////////////////

};
