/*!
 * byo - 2008
 * tools 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;
			}

			// 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',
								'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.getParent());

		});

		return true;
	},
	// /decorateCheckbox //////////////////////////////

	// load //////////////////////////////
	load : function(url, container, onComplete) {

		var container= $(container);

		/* Avoid IE7 bug but don't execute JS in loaded page
		var request = new Request({
							url: url,
							method: 'get',
							onFailure: function () {alert('error')},
							onComplete: function (response, xml) {
											container.set('html', response);
											//alert(responseText);
											onComplete();
										}
						});

		request.send();
		return false;
		*/

		container.set('load', {
							onFailure: function() {alert('error')},
							//onComplete: function() {alert('finish')}
							onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) {
											//alert(responseHTML);
											//fading.start(1).chain(function(){
											//});
											onComplete();
										}
							});

		// the fading effect
		//var fading = new Fx.Tween(container, {property:'opacity', duration: 200, wait: false}).set(1);



		//fading.start(0).chain(function(){
			// when finished
			container.load(url);
		//});

		return false;

	}
	// /load //////////////////////////////

};





