/*
	@author Frédéric Vauchelles
	datas for Gammunity
*/

// Firefox 2 compatibility :
jQuery.ui.tabs.defaults = jQuery.extend({}, jQuery.ui.tabs.defaults, {ajaxOptions: {success: function(r, s) {}}}) ;

// data creator
jQuery.data_creator = {};

jQuery.fn.data_creator = function(options) {
	var opt_ = jQuery.extend({}, jQuery.data_creator.data.settings, options) ;
	if (typeof opt_.type == 'undefined') {
		jQuery.data_creator.error('no data type given for construct') ;
		return this ;
	}
	return this.each(function() {
		var opt = jQuery.meta ? jQuery.extend({}, opt_.options, jQuery(this).data()) : opt_.options;
		if (typeof opt_.subtype == 'undefined') {
			var el = new jQuery.data_creator[opt_.type](jQuery(this), opt) ;
			jQuery(this).data(opt_.type, el) ;
		}
		else {
			var el = new jQuery.data_creator[opt_.type][opt_.subtype](jQuery(this), opt) ;
			jQuery(this).data(opt_.subtype, el) ;
		}
	}) ;
}

jQuery.data_creator.data = {} ;
jQuery.data_creator.data.settings = {
	options: {},
	type:'none'
} ;

// Error
(function($) {
	$.data_creator.error = function(msg) {
		$.data_creator.error.stack.push(msg) ;
		if ($.data_creator.error.throw_mode == 'automatic') {
			throw msg ; 	
		}
		else if ($.data_creator.error.throw_mode == 'open_window') {
			var myWindow = window.open('', 'Error', 'width=800,height=500') ;
			$(myWindow.document.body).append(msg) ;
		}
	}
	
	$.data_creator.error.throw_mode = 'automatic' ;
	
	$.data_creator.error.show = function() {
		var text = '\n' ;
		for (msg in $.data_creator.error.stack) {
			text += 'Error n°'+msg+' : '+$.data_creator.error.stack[msg]+'\n' ;	
		}
		throw text ;
	}
	
	$.data_creator.error.stack = new Array() ;
})(jQuery) ;

// Clickable
(function($) {$.fn.clickable = function() {
		return this.each(function() {
			$(this).addClass('ui-state-default').addClass('clickable')
					.mousedown(function() {
					$(this).addClass('ui-state-active') ;
				}).mouseup(function() {
					$(this).removeClass('ui-state-active') ;
				}).mouseover(function() {
					$(this).addClass('ui-state-hover') ;
				}).mouseout(function() {
					$(this).removeClass('ui-state-hover') ;
				}).focus(function() {
					$(this).addClass('ui-state-focus') ;
				}).blur(function() {
					$(this).removeClass('ui-state-focus') ;
				}) ;
		}) ;
	} ;
})(jQuery) ;

// State
(function($) {
	$.fn.state = function(state_) {
		var state = $.extend({}, {name:'clear'}, state_) ;
		var text = '' ;
		if (state.name == 'clear') {
			
		}
		else if(state.name == 'refresh') {
			text = '<span class="ui-icon ui-icon-refresh"></span>' ;
		}
		return this.each(function() {
			$(this).html(text) ;
		}) ;
	} ;
})(jQuery) ;

// Alert
(function() {jQuery.alert = function(options) {
	var opt = jQuery.extend({text:"alert", title:"alert"}, options) ;
	var name = 'alert-div' ;
	$("body").append('<div id="'+name+'" title="'+opt.title+'">'+opt.text+'</div>') ;
	$('#'+name).dialog({
		hide:'drop',
		show:'drop',
		modal: false,
		buttons: {"Ok": function(){$(this).dialog('close').remove() ;}}
	}) ;
}})() ;

error = jQuery.data_creator.error.show ;

jQuery.call_method = function(method, object) {
	if (typeof method == 'string') {
		return function() {
			object[method]() ;
		}
	}
	else {
		return null ;	
	}
}

// Data base

jQuery.data_creator.base = function(jElement, options) {
	this.jElement = jElement ;
	this.options = jQuery.extend({}, jQuery.data_creator.base.settings, options) ;
	if (this.options.name == '') {
		this.options.name = 'base#' + Math.random() ;	
	}
	jQuery.data_creator.base.stack[this.options.name]=this ;
}

jQuery.data_creator.base.prototype = {
}

jQuery.data_creator.base.settings = {} ;
jQuery.data_creator.base.effect = {
	effect:'blind',
	data:{},
	duration:1000,
	callback:function() {}
}

// Dialog creator
jQuery.data_creator.dialog = function (jElement, options) {
	this.jElement = jElement ;
	this.options = jQuery.extend({}, jQuery.data_creator.dialog.settings, options) ;
	if (this.options.name == '') {
		this.options.name = Math.random() ;	
	}
	jQuery.data_creator.dialog.stack[this.options.name]=this ;
	
	this.init() ;
}

jQuery.data_creator.dialog.prototype.init = function() {
	if (this.options.load_url == '') {
		return ;	
	}
	this.jElement.append('<div id="'+this.options.name+'"></div>') ;
	this.jElement.find('div').load(this.options.load_url, this.options.data) ;
	this.jElement.dialog({
		bgiframe: true,
		show: CONF.effects.dialog.show.effect,
		hide: CONF.effects.dialog.hide.effect,
		modal: true,
		autoOpen: false,
		resizable:false,
		buttons: {}
	});
}

jQuery.data_creator.dialog.prototype.open = function() {
	this.jElement.dialog('open') ;
}

jQuery.data_creator.dialog.prototype.close = function() {
	this.jElement.dialog('close') ;	
}

jQuery.data_creator.dialog.prototype.load_page = function(data) {
	var _data = jQuery.extend({}, this.options.data, data) ;
	this.jElement.find('div').load(this.options.load_url, _data) ;
}

jQuery.data_creator.dialog.prototype.set_url = function(url) {
	this.options.load_url = url ;
}

jQuery.data_creator.dialog.settings = {
	w_index:-1,
	load_url: '',
	name:'',
	data: {}
}

jQuery.data_creator.dialog.stack = {} ;

// data modal form
jQuery.data_creator.modal_form = function (jElement, options) {
	this.jElement = jElement ;
	this.options = jQuery.extend({}, jQuery.data_creator.modal_form.settings, options) ;
	if (this.options.name == '') {
		this.options.name = Math.random() ;	
	}
	jQuery.data_creator.modal_form.stack[this.options.name]=this ;
	
	this.init() ;
}

jQuery.extend(jQuery.data_creator.modal_form.prototype, jQuery.data_creator.dialog.prototype) ;

jQuery.data_creator.modal_form.prototype.init = function() {
	if (this.options.load_url == '') {
		return ;
	}
	this.jElement.load(this.options.load_url, this.options.data) ;
	this.jElement.dialog({
		autoOpen: false,
		bgiframe: true,
		modal: true,
		show: CONF.effects.dialog.show.effect,
		hide: CONF.effects.dialog.hide.effect,
		resizable:false,
		width:this.options.size.width,
		height:this.options.size.height,
		buttons: {
			'Envoyer': function() {
				$(this).find('form').get()[0].submit() ;
			},
			'Annuler': function() {
				$(this).dialog('close');
			}
		}
	});
}

jQuery.data_creator.modal_form.settings = {
	w_index:-1,
	load_url: '',
	name:'',
	data: {},
	size: {width:200, height:200}
}

jQuery.data_creator.modal_form.stack = {} ;

// None data
jQuery.data_creator.none = function() {}

// Fonctions

jQuery.hashURL = function() {
	var result = {} ;
	if (window.location.hash != '') {
		var strings = window.location.hash.split(/#/)[1].split(/&/) ;
		for  (i = 0 ; i < strings.length ; i++) {
			result[strings[i].split(/=/)[0]]=strings[i].split(/=/)[1] ;	
		}
	}
	return result ;
}

jQuery.addHash = function(properties) {
	var hash = jQuery.hashURL() ;
	jQuery.extend(hash, properties) ;
	jQuery.setHash(hash) ;
}

jQuery.setHash = function(properties) {
	window.location.hash = jQuery.param(properties) ;	
}

jQuery.uniqid = function() {
	return Math.floor(Math.random()*100000) ;	
}

jQuery.arrayToObject = function(arr) {
	var result = {} ;
	for (x in arr) {
		result[arr[x].name] = arr[x].value ;
	}
	return result ;
}

