/*
	@file window.js
	@brief jQuery extension for creating os-window like element. Theses can be minimized into a bar holder (type bar/main)
*/

//Constructor
jQuery.data_creator.window = function(jElement, options) {
	this.jElement = jElement ;
	this.options = jQuery.extend({}, jQuery.data_creator.window.settings, options) ;
	if (this.options.name == '') {
		this.options.name = 'window#' + Math.random() ;	
	}
	this.options.dialog_options.title = this.options.title ;
	jQuery.data_creator.window.stack[this.options.name]=this ;
	
	this.jElement.css('padding', 0) ;
	
	this.init() ;
}

jQuery.data_creator.window.prototype.minimize = function() {
	this.jElement.dialog('close') ;
	jQuery.data_creator.bar.main.stack[this.options.bar_holder].add_window({
		name:this.options.name,
		title:this.options.title,
		icon:this.options.icon_dir+this.options.minimized_icon,
		window:this
	});
}

// resize the dialog to fit the content
jQuery.data_creator.window.prototype.pack = function() {
	this.jElement.parent().width(this.jElement.children().outerWidth(true)) ;
	this.jElement.parent().height(this.jElement.children().outerHeight(true)) ;
}

//Initialisation
jQuery.data_creator.window.prototype.init = function() {
	this.jElement.dialog(this.options.dialog_options);
	var self = this ;
	var uiWindowTitlebarMinimize = $('<a href="#"/>')
		.addClass(
			'ui-window-titlebar-minimize ' +
			'ui-corner-all'
		)
		.attr('role', 'button')
		.hover(
			function() {
				uiWindowTitlebarMinimize.addClass('ui-state-hover');
			},
			function() {
				uiWindowTitlebarMinimize.removeClass('ui-state-hover');
			}
		)
		.focus(function() {
			uiWindowTitlebarMinimize.addClass('ui-state-focus');
		})
		.blur(function() {
			uiWindowTitlebarMinimize.removeClass('ui-state-focus');
		})
		.click(function() {
			self.minimize() ;
		}) ;
		
	var uiWindowTitlebarMinimizeText = $('<span/>')
		.addClass(
			'ui-icon ' +
			'ui-icon-arrowthickstop-1-w'
		)
		.appendTo(uiWindowTitlebarMinimize) ;
		
	uiWindowTitlebarMinimize.appendTo(this.jElement.siblings('.ui-dialog-titlebar')) ;
	this.reload() ;
}

/*
	reload the content via ajax (POST method)
	@param data ajax data passed to jQuery ajax
*/
jQuery.data_creator.window.prototype.reload = function(data) {
	jQuery.extend(this.options.ajax_data, data),
	this.jElement.load(this.options.load_url, this.options.ajax_data);
	return this ; ;
}
/*
	reload the content via ajax (POST method)
	@param data ajax data passed to jQuery ajax
	@param url url to load
*/
jQuery.data_creator.window.prototype.load_url = function(url, data) {
	jQuery.extend(this.options, {load_url:url, ajax_data:data}),
	this.jElement.load(this.options.load_url, this.options.ajax_data) ;
	return this ;
}
/*
	open window
*/
jQuery.data_creator.window.prototype.open = function() {
	jQuery.data_creator.bar.main.stack[this.options.bar_holder].remove_window({name:this.options.name}) ;
	this.jElement.dialog('open') ;	
	return this ;
}
/*
	close window
*/
jQuery.data_creator.window.prototype.close = function() {
	this.jElement.dialog('close') ;	
	delete this;
}
// window stack
jQuery.data_creator.window.stack = {} ;
// window default settings
jQuery.data_creator.window.settings = {
	load_url:'',
	title:'default',
	ajax_data:{},
	dialog_options: {
		autoOpen:false,
		bgiframe:true,
		resizable:false,
		title:'default'
	},
	bar_holder:null,
	minimized_icon:'minimized_icon.jpg',
	icon_dir:'Data/bar/window/'
} ;
