/**********************************************************************
 * kWindows Class
 * author: Tomasz Stankiewicz
 **********************************************************************
 * Copyright (c) 2009 PB O/Olsztyn Sprint Sp. z o.o.
 **********************************************************************/

var windowManager = new Hash({
	indexLevel: 100,
	focusedWindow: '',

	closeWindow: function(windowId) {
		var instance = $(windowId);
		if(instance) {
			/*if(typeof instance.options.get('closeWindow') == 'function')
				eval('instance.get("closeWindow")');*/
			instance.destroy();
		}
	},
	focusWindow: function(windowInstance) {
		if(windowInstance && windowManager.focusedWindow != windowInstance.id) {
			windowManager.indexLevel++;
			windowManager.focusedWindow = windowInstance.id;
			windowInstance.setStyle('zIndex', windowManager.indexLevel);
			
		}
	}
});


/******************************************************************************
 * kTab Class
 *****************************************************************************/
var kTabsPanel = new Class({
	Implements: [Events, Options],

	options: {
		parentDiv:		null,
		contentDiv:		null,
		loaderDiv:		null,
		tabs:			null,
		tabs_div:		new Array(),
		activeTab:		null
	},

	initialize: function(options) {
		this.setOptions(options);

		if(!this.options.tabs)
			return;

		for(var i=0;i<this.options.tabs.length;i++) {
			this.addTab(this.options.tabs[i],i);
		}
	},
	
	addTab: function() {
		var tabObject = arguments[0] ? arguments[0] : null;
		var index = arguments[1]!=null ? arguments[1] : ++this.options.tabs.length;
		
		if(!tabObject)
			return;

		var newTab = new Element('div', {
			'class': 'kWindowTab',
			'html' : tabObject.text
		}).inject(this.options.parentDiv);

		this.options.tabs_div[index] = newTab;

		newTab.addEvent('click', function() {
			this.activateTab(index);
		}.bind(this));

		if(tabObject.disabled)
			newTab.addClass('disabledTab');
	},
	
	removeTab: function(tab) {
		
	},

	activateTab: function() {
		var index = arguments[0] ? arguments[0] : 0;
		var tab = this.options.tabs[index];
		var tab_div = this.options.tabs_div[index];

		if(!tab || !tab_div)
			return;

		if(tab.disabled || tab_div == this.activeTab)
			return;
 	
		if(this.activeTab) {
			$each(this.options.tabs_div,function(tabs) {
				tabs.removeClass('activeTab');
			});
		}
		tab_div.addClass('activeTab');

		this.activeTab = tab_div;

		if(tab.method == 'html') {
			this.options.contentDiv.set('html',tab.content);
		}
		else {
			if(tab.url) {
				var request = new Request({
					url: tab.url,
					method: 'get',
					onRequest: function() { this.options.loaderDiv.setStyle('display','block'); }.bind(this),
					onSuccess: function(responseText){
						this.options.contentDiv.set('html',responseText);
						this.options.loaderDiv.setStyle('display','none');
							
					}.bind(this)
				});
				request.send();
			}
		}
		
		if(typeof tab.eventActivate == 'function')
			tab.eventActivate();
	}
	
});


/******************************************************************************
 * kWindow Class
 *****************************************************************************/
var kWindow = new Class({
	Implements: [Events, Options],

	options: {
		id:		null,
		title:	'',
		icon:	false,
		type:	'window',
		x:		0,
		y:		0,
		width:	300,
		height: 125,
		window_height: 0,

		draggable:	true,
		resizable:  true,
		closable:	true,
		footer:		true,

		tabs: 		null,
		method: 	'ajax', // ajax, html

		eventBuild:	$empty,
		eventClose:	$empty
	},

	initialize: function(options) {
		this.setOptions(options);
		
		if(typeof this.options.eventBuild == 'function')
			this.options.eventBuild();

		windowManager.indexLevel++;
		// tworzenie glownego diva okna
		this.windowDiv = new Element('div', {
			'class': 'kWindow',
			'id':    this.options.id,
			'styles': {
				'left':		this.options.x,
				'top':		this.options.y,
				'width':   this.options.width,
				'zIndex':  windowManager.indexLevel += 2
			}
		});

		this.options.window_height = this.options.height;
		
		this.windowDiv.addEvent('mousedown', function() {
			windowManager.focusWindow(this.windowDiv);
		}.bind(this));

		// elementy podrzedne - cala zawartosc
		this.windowContainer = new Element('div', {
			'id': this.options.id + '_windowContainer',
			'class': 'kWindowContainer'
		}).inject(this.windowDiv);
		
		// gorny pasek okna
		this.windowTitleBar = new Element('div', {
			'id': this.options.id + '_windowTitleBar',
			'class': 'kWindowTitleBar',
			'styles': {
				'cursor': this.options.draggable ? 'move' : 'default'
			}
		}).inject(this.windowDiv);

		// ikona
		if(this.options.icon) {
			var titleIcon = new Element('img', {
				'src': this.options.icon
			}).inject(this.windowTitleBar);
		}

		// tytul okna
		var titleSpan = new Element('span', {
				'html': this.options.title
			}).inject(this.windowTitleBar);

		this.options.window_height += 20;

		// panel ponizej gornego paska
		this.windowPanel = new Element('div', {
			'id': this.options.id + '_windowPanel',
			'class': 'kWindowPanel'
		}).inject(this.windowContainer);
	
		// tresc
		this.windowContent = new Element('div', {
			'id': this.options.id + '_windowContent',
			'class': 'kWindowContent'
		}).inject(this.windowPanel);

		// stopka (opcjonalna)
		if(this.options.footer) {
			this.windowFooter = new Element('div', {
				'id': this.options.id + '_windowFooter',
				'class': 'kWindowFooter'
			}).inject(this.windowContainer);
			this.options.window_height += 23;
		}

		// loader
		this.windowLoader = new Element('div', {
				'id': this.options.id + '_windowLoader',
				'class': 'kLoader'
			}).inject(this.windowContainer);

		// cien			
		/*var shadow = new Element('div', {
			'class': 'kShadow'
		}).inject(this.windowContainer);*/

		this.drawButtons();

		// dodaj do drzewka DOM
		this.windowDiv.injectInside(document.body);
		
		this.attachDraggable();
		this.attachResizable();

		this.attachTabs();
		
		this.windowDiv.setStyle('left',this.options.x);
		this.windowDiv.setStyle('top',this.options.y);
		this.windowDiv.setStyle('height',this.options.window_height);
		this.windowPanel.setStyle('height',this.options.height);
		
		// dodaj cienie
		/*this.windowShadowRight = new Element('div', {
			'class': 'kWindowBackground',
			'styles': {
				'left':		this.options.x+this.options.width,
				'top':		this.options.y+5,
				'width':    '6px',
				'height':   this.options.window_height-5
			}
		}).inject(document.body);

		this.windowShadowBottom = new Element('div', {
			'class': 'kWindowBackground',
			'styles': {
				'left':		this.options.x+5,
				'top':		this.options.y+this.options.window_height,
				'width':    this.options.width,
				'height':   '6px'
			}
		}).inject(document.body);*/
	},
	
	saveValues: function() {
		//var coordinates = this.windowDiv.getCoordinates();
		//this.options.x = coordinates.left.toInt();
		//this.options.y = coordinates.top.toInt();
	},

	attachDraggable: function() {
		if(!this.options.draggable) return;
		this.windowDrag = new Drag.Move(this.windowDiv, {
			handle: this.windowTitleBar,
			onStart: function() {
				this.windowDiv.setStyle('opacity','0.8');
				this.windowContainer.setStyle('display','none');
			}.bind(this),
			onComplete: function() {
				this.windowDiv.setStyle('opacity','1');
				this.windowContainer.setStyle('display','block');
				this.saveValues();
			}.bind(this)
		});
	},

	attachResizable: function() {
		return;
		if(!this.options.resizable) return;
		this.resizableBorder = new Element('div', {
			'class': 'kResizableBorder'
		}).injectBottom(document.body);
		var n = new Element('div', {
				'class': 'kHandle',
				'styles': { 'top': 0, 'left': 10, 'width': '95%', 'cursor': 'n-resize' }
			}).inject(this.windowDiv);
		var e = new Element('div', {
				'class': 'kHandle',		
				'styles': { 'top': 10, 'right': 0, 'height': '95%', 'cursor': 'e-resize' }
			}).inject(this.windowDiv);
		var s = new Element('div', {
				'class': 'kHandle',		
				'styles': { 'bottom': 0, 'left': 10, 'width': '95%', 'cursor': 's-resize' }
			}).inject(this.windowDiv);
		var w = new Element('div', {
				'class': 'kHandle',		
				'styles': { 'top': 10, 'left': 0, 'height': '95%', 'cursor': 'w-resize' }
			}).inject(this.windowDiv);
		var ne = new Element('div', { 
					'class': 'kHandle kCorner',
					'styles': { 'top': 0, 'right': 0, 'cursor': 'ne-resize' }
			}).inject(this.windowDiv);
		var se = new Element('div', { 
					'class': 'kHandle kCorner',
					'styles': { 'bottom': 0, 'right': 0, 'cursor': 'se-resize' }
			}).inject(this.windowDiv);
		var sw = new Element('div', { 
					'class': 'kHandle kCorner',
					'styles': { 'bottom': 0, 'left': 0, 'cursor': 'sw-resize' }
			}).inject(this.windowDiv);
		var nw = new Element('div', { 
					'class': 'kHandle kCorner',
					'styles': { 'top': 0, 'left': 0, 'cursor': 'nw-resize' }
			}).inject(this.windowDiv);

		this.resizable1 = this.resizableBorder.makeResizable({
			handle: [n, ne, nw],
			limit: {
					x: false,
					y: [20,650]
				},
			modifiers: {x: false, y: 'top'},
			onStart: function(){
				this.coords = this.windowDiv.getCoordinates();
				this.resizableBorder.setStyles({
					'display': 'block',
					'top': this.coords.top,
					'left': this.coords.left,
					'width': this.windowDiv.getSize().x-2,
					'height': this.windowDiv.getSize().y-2
				});

			}.bind(this),
			onDrag: function(){
				
			}.bind(this),
			onComplete: function(){
				this.resizableBorder.setStyle('display','none');
			}.bind(this)
		});
	},

	attachTabs: function() {
		if(!this.options.tabs || this.options.tabs.length < 1) return;

		this.windowTabs = new Element('div', {
			'id': this.options.id + '_windowTabs',
			'class': 'kWindowTabs'
		}).injectTop(this.windowContainer);

		new Element('div', { 'class': 'kWindowTabBorder'}).injectTop(this.windowTabs);
		
		this.options.window_height += 27;

		this.tabPanel = new kTabsPanel({
			'tabs': this.options.tabs,
			'parentDiv': this.windowTabs,
			'contentDiv': this.windowContent,
			'loaderDiv': this.windowLoader
		});
		this.tabPanel.activateTab();
	},

	drawButtons: function() {
		if(this.options.closable) {
			this.closeButton = new Element('div', {
			'id': this.options.id,
			'class': 'kCloseButton'
			}).inject(this.windowDiv);
			
			this.closeButton.addEvent('mouseenter', function(){
				this.addClass('kCloseButtonOn');
			});
			this.closeButton.addEvent('mouseleave', function(){
				this.removeClass('kCloseButtonOn');
			});
			this.closeButton.addEvent('click', function(){
				windowManager.closeWindow(this.id);
			});
		}
		if(this.options.resizable && this.options.footer) {
			this.resizeButton = new Element('div', {
			'class': 'kResizeButton'
			}).inject(this.windowDiv);
		}
	},

	updateStatusBar: function(text_str) {
		if(!this.options.footer) return;
		this.windowFooter.set('html',text_str);
	},

	loadContent: function(str) {
		var windowContent = this.windowContent;
		var windowLoader  = this.windowLoader;
		if(this.options.method == 'html') {
			windowContent.set('html',str);
		}
		else {
			var request = new Request({ 
				url: str,
				onRequest: function() { windowLoader.setStyle('display','block'); },
				onSuccess: function(){
					windowContent.set('html',this.response.text);
					windowLoader.setStyle('display','none');
				
				}
			});
			request.send();
		}
	},

	showLoader: function () {
		this.windowLoader.setStyle('display','block');
	},
	hideLoader: function () {
		this.windowLoader.setStyle('display','none');
	},
	show: function() {
		this.windowDiv.setStyle('display','block');
	}
});
