// JavaScript Document

if (typeof jQuery.data_creator.bar == 'undefined') {jQuery.data_creator.bar = {}} ;

jQuery.data_creator.bar.main = function(jElement, options) {
	this.jElement = jElement ;
	this.options = jQuery.extend({}, jQuery.data_creator.bar.main.settings, options) ;
	this.page = 0 ;
	this.minimized = true;
	this.top = this.jElement.offset().top ;
	this.windows_stack = {} ;
	this.window_link_callback = this.callback_maker_window_link() ;

	if (this.options.name == '') {
		this.options.name = 'bar-main#' + Math.random() ;	
	}
	jQuery.data_creator.bar.main.stack[this.options.name]=this ;
	
	bar_main_click_callback_maker = function(context_) {
		var context = context_ ;
		return function() {
			context.minimize() ;
		}
	}
	this.jElement.find('.bar-side').click(bar_main_click_callback_maker(this)) ;
	
	self = this ;
	$(window).scroll(function() {
		self.jElement.animate({
			top:self.top+$(window).scrollTop()
		}, {
			queue:false
		}) ;
	}) ;
}

jQuery.data_creator.bar.main.prototype.minimize = function() {
	if (this.minimized) {
		this.minimized = false ;
		this.jElement.animate({
			left:'0'	
		}) ;
		this.jElement.find('.bar-side .ui-icon').switchClass('ui-icon-circle-triangle-w', 'ui-icon-circle-triangle-e') ;
	} else {
		this.minimized = true ;
		this.jElement.animate({
			left:'-62px'	
		}) ;
		this.jElement.find('.bar-side .ui-icon').switchClass('ui-icon-circle-triangle-e', 'ui-icon-circle-triangle-w') ;
	}
}

jQuery.data_creator.bar.main.prototype.add_window = function(window_data) {
	var opt = jQuery.extend({}, jQuery.data_creator.bar.main.window_data, window_data) ;
	if (typeof this.windows_stack[window_data.name] == 'undefined') {
		this.windows_stack[window_data.name] = window_data ;
		var api = this.jElement.find('.bar-windows').scrollable() ;
		api.getItemWrap()
			.append('<div class="window-link window-'+window_data.name+' clickable" window="'+window_data.name+'"><img src="'+window_data.icon+'" alt="'+window_data.title+'" height="35" width="35" title="'+window_data.title+'" /></div>')
			.find('.window-'+window_data.name)
			.data('window', window_data.window)
			.show('blind', {}, 500)
			.click(this.window_link_callback) ;
		api.reload() ;
	}
}

jQuery.data_creator.bar.main.prototype.remove_window = function(window_data) {
	var opt = jQuery.extend({}, jQuery.data_creator.bar.main.window_data, window_data) ;
	delete this.windows_stack[opt.name] ;
	api = this.jElement.find('.bar-windows').scrollable() ;
	api.getItems()
		.filter('.window-'+window_data.name)
		.hide('blind', {}, 500, function() {
			$(this).remove() ;
			api.reload() ;
		}) ;
}

jQuery.data_creator.bar.main.prototype.callback_maker_window_link = function() {
	var self = this ;
	return function() {
		self.remove_window({name:$(this).attr('window')});
		jQuery.data_creator.window.stack[$(this).attr('window')].open() ;
	}
}

jQuery.data_creator.bar.main.stack = {} ;

jQuery.data_creator.bar.main.settings = {
	w_index: -1,
	name: '',
} 

jQuery.data_creator.bar.main.window_data = {
	name:'default',
	title:'default',
	icon:'',
	window:null
};