// Prototyp MsgBoxu
App.modules.msgbox = {
	getPageScroll : function() {
		var yScroll = $(document).scrollTop();
		arrayPageScroll = new Array('',yScroll) 
		return arrayPageScroll;
	},
	
	getPageSize : function() {
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	},
	constructor : function(id) {
		this.initiate = function(id) {
			
			this.msgbox = $('<div class="msgbox" id="'+id+'" />');
			this.msgbox.data('parent', this);
			this.msgbox.bind('mousedown', {parent: this}, function(e) {
				e.data.parent.raise();
			});

			this.objBar = $('<div class="msgbox-bar" />');
			this.objBar.appendTo(this.msgbox);
			
			this.objClose = $('<div class="msgbox-close" />');
			
			this.objClose.appendTo(this.objBar);
						
			this.objTitle = $('<div class="msgbox-title" />');
			this.objTitle.appendTo(this.objBar);			
			
			this.overlay = $('<div class="msgbox-overlay" />');
			this.overlay.css({
				position : 	'absolute',
				top :		'0',
				left :		'0',
				zIndex :	'999',
				width :		'100%'
			});
			
			// default settings
			this.setting = {};
			this.setting.overlay = true;
			this.setting.permanent = false;
			this.setting.x = null;
			this.setting.y = null;
			this.setting.z = App.modules.msgbox.msgboxes.length;
			this.setting.active = false;
		};
		
		// setters
		this.setOverlay = function(enable) {
			this.setting.overlay = enable;
		};
		this.setPermanent = function(enable) {
			this.setting.permanent = enable;
		};		
		this.setPosition = function(x,y) {
			this.setting.x = x;
			this.setting.y = y;
		}
		this.setTitle = function(title) {
			this.objTitle.html(title);
		}
		
		this.getZIndexLevel = function() {
			if (this.setting.overlay) {
				return 1000;
			} else {
				return 500;
			}
		};
		
		this.raise = function() {
			var max = 0;
			var msgboxes = App.modules.msgbox.msgboxes;
			for (i in msgboxes) {
				if (!msgboxes[i].setting.active) continue;
				var z = msgboxes[i].setting.z;
				if (z > max) max = z;
			}
			if (this.setting.z != max) {
				this.setting.z = parseInt(max) + 1;
				this.msgbox.css('zIndex', this.getZIndexLevel() + this.setting.z);
				this.saveCookies();
			}
		}
		
		this.updatePosition = function() {
			var arrayPageScroll = App.modules.msgbox.getPageScroll();
			var arrayPageSize = App.modules.msgbox.getPageSize();
			
			if (this.setting.x == null) {	
				this.msgbox.css('left', ((arrayPageSize[0] - 20 - this.msgbox.outerWidth()) / 2) + 'px');
			} else {
				this.msgbox.css('left', this.setting.x + 'px'); 
			}
			if (this.setting.y == null) {	
				this.msgbox.css('top', arrayPageScroll[1] + ((arrayPageSize[3] - 35 - this.msgbox.outerHeight()) / 2) + 'px');
			} else {
				this.msgbox.css('top', this.setting.y + 'px'); 
			}
			
			if (this.setting.z != null) {
				this.msgbox.css('zIndex', this.getZIndexLevel() + this.setting.z);
			}
			
			this.overlay.css('height', arrayPageSize[1] + 'px');
		};
		this.addAlert = function(text, className) {
			var alert = $('<p class="'+className+'" />');
			
			var alertImg = $('<img class="icon" alt="'+className+'" />');
			alertImg.attr('src', App.rewrite.rootUrl + "/images/alert-" + className + ".png");
			alertImg.appendTo(alert);
			
			var alertSpan = $('<span>'+text+'</span>');
			alertSpan.appendTo(alert);
			
			alert.appendTo(this.msgbox);
			this.active = false;
		};
		this.loadCookies = function() {
			setting = App.cookies.get('msgbox[' + this.id + ']');
			if (setting) {
				this.setting = unserialize(setting);
			}
		};
		this.load = function() {
			if (this.setting.permanent) {
				this.loadCookies();
				if (this.setting.active) {
					this.show();
				}
			}			
		};
		this.saveCookies = function() {
			App.cookies.set('msgbox[' + this.id + ']', serialize(this.setting), null, App.rewrite.cookiesPath);
		};
		this.show = function() {
			if(this.active) return;
			
			var objBody = $("body:first");
			this.msgbox.appendTo(objBody);
			this.overlay.appendTo(objBody);
			
			if(this.setting.overlay) {
				this.overlay.show();
			} else {
				this.overlay.hide();
			}
			
			this.objClose.bind('click', {instance: this}, function(e) {
				e.data.instance.hide(); return false;
			});
			this.overlay.bind('click', {instance: this}, function(e) { 
				e.data.instance.hide(); return false;
			});

			App.modules.dragDrop.makeDraggable(this.msgbox, this.objBar);
			this.msgbox.bind('mousedown', {msgbox: this}, function(e) {				// nastavime pouze pri zacatku dragndropu
				msgbox = e.data.msgbox;
				App.modules.dragDrop.onDrop = function() {
					msgbox.setting.x = msgbox.msgbox.offset().left;
					msgbox.setting.y = msgbox.msgbox.offset().top;
					msgbox.saveCookies();
				}
			});
			
			this.updatePosition();
			this.active = true;
			this.setting.active = true;
			
			this.saveCookies();
			
			if (this.onshow) {
				this.onshow();
			}
		};
		this.hide = function() {
			if(!this.active) return;
			this.msgbox.remove();
			this.overlay.remove();
			this.active = false;
			this.setting.active = false;
			
			this.saveCookies();
			
			if (this.onhide) {
				this.onhide();
			}
		};
		this.validatePosition = function() {
			var x = this.setting.x;
			var y = this.setting.y;
			
			var size = App.modules.msgbox.getPageSize();

			
			if (x > size[0] - this.msgbox.outerWidth()) x = size[0] - this.msgbox.outerWidth();
			if (x < 0) x = 0;
			
			if (y > size[1] - this.msgbox.outerHeight()) y = size[1] - this.msgbox.outerHeight();
			if (y < 0) y = 0;
			
			if (x != this.setting.x || y != this.setting.y) {
				this.setting.x = x;
				this.setting.y = y;
				this.updatePosition();
				this.saveCookies();
			}
		}
		this.showhide = function() {
			if (this.active) {
				this.hide();
			} else {
				this.show();
			}
		}
		this.id = id;
		this.initiate(id);
		
		if (!this.windowResizeListener) {
			$(window).resize(function(event) {
				App.modules.msgbox.windowResize();
			});
			this.windowResizeListener = true;
		}
		
		App.modules.msgbox.msgboxes.push(this);
	},
	msgboxes : [],
	hideAll : function() {
		for(var i in this.msgboxes) {
			this.msgboxes[i].hide();
		}
	},
	hide : function(id) {
		for(i = 0; i < this.msgboxes.length; i ++) {
			if(this.msgboxes[i].id == id) {
				this.msgboxes[i].hide();
			}
		}
	},
	windowResize : function() {
		for(var i in this.msgboxes) {
			this.msgboxes[i].validatePosition();
		}
	},
	alert : function(text, type) {
		var msgbox = new App.modules.msgbox.constructor('msgbox' + this.msgboxes.length);
		msgbox.addAlert(text, type);
		msgbox.show();
	},
	parseXML : function(xml) {
		var msgbox = new App.modules.msgbox.constructor('msgbox' + this.msgboxes.length);
		var alerts = $('alert', xml);
		for (var i = 0; i < alerts.length; i++) {
			var alert = alerts[i];
			var text = $('text', alert).text();
			var className = $('class', alert).text();
			msgbox.addAlert(text, className);
		}
		if (alerts.length > 0) {
			msgbox.show();
		}
	}
};
function msgboxAlert(text, type) {
	App.modules.msgbox.alert(text, type);
};
function msgbox(text) {
	msgboxAlert(text, 'normal');
};
function msgboxError(text) {
	msgboxAlert(text, 'error');
};
function msgboxSuccess(text) {
	msgboxAlert(text, 'success');
};
