/**
 * Helper for window overlays creation
 * @author sevir
 */

	var open_msgbox = null;
	var default_options = null;
	
	msgbox = function(content, type, options){
		default_options = {
			"-moz-border-radius":"6px",
			"-webkit-border-radius":"6px",
			"-moz-box-shadow": "0 0 50px #ccc",
			"-webkit-box-shadow": "0 0 50px #ccc",
			
			overlay: {
				expose:
				{
					color: '#000',
					loadSpeed: 0,
					opacity: 0.5
				},
				
				onLoad: function(){
					if(typeof(content) == 'undefined'){
						content = 'Undefined';
					}
					if (typeof($(this.getClosers().get(0)).parents(".msgbox:first").get(0).msgbox)=='object')
					{
						open_msgbox = $(this.getClosers().get(0)).parents(".msgbox:first").get(0).msgbox;
					}
					
					this.getClosers().click(function(e)
					{
						msgbox_close(this);
						return true;
					});
				},
				
				top: 'center',
				left: 'center',
				closeOnClick:true,
				closeOnEsc:true,
				oneInstance:true,
				stack:true
			}
		}
		
		var opts = $.extend(true, default_options, options);
		
		//needed for showing overlay above expose. version 1.9 is FF3
		if($.browser.mozilla && parseFloat($.browser.version) < 1.9){
			$('#template').css({
				overflow: 'visible'
			});
		}
		
		if (typeof(type)=='undefined'){ type = ''; }
		var css_options = {};
		$.each(opts, function(i, val){
			if (i != "overlay") {
				css_options[i] = val;
			}
		});
		
		var old_open_msgbox = open_msgbox;
		var o_msgbox = '';
		
		if (typeof(content) == 'string')
		{
			o_msgbox = $('<div class="msgbox"></div>').addClass(type).html(content).css(css_options).appendTo("body");
		}
		else
		{
			if (typeof($(content).attr('rel')) == 'string')
			{
				if (typeof(content.jquery) != 'undefined')
				{
					o_msgbox = content;
					$($(content).attr('rel')).addClass("msgbox").addClass(type).css(css_options).get(0).msgbox = $(content);
				}
				else
				{
					o_msgbox = $(content.rel).addClass("msgbox").addClass(type).css(css_options);
					o_msgbox.get(0).msgbox = $(content);
				}
			}
			else
			{
				o_msgbox = $(content).addClass("msgbox").addClass(type).css(css_options);
			}
		}
		o_msgbox.overlay(opts.overlay);
		
		if (typeof($(content).attr('rel'))=='undefined')
		{
			o_msgbox.old = old_open_msgbox;
			o_msgbox.overlay().load();
		}
		
		if (opts.overlay.stack)
		{
			open_msgbox = o_msgbox;
		}
		
		var window_dimensions = {
			width: $(window).width(),
			height: $(window).height()
		}
		
		var overlay_div = $('.msgbox:visible');
		
		var overlay_dimensions = {
			width: overlay_div.width(),
			height: overlay_div.height()
		}
		
		//window height smaller than overlay div
		if(overlay_dimensions.height > window_dimensions.height)
		{
			overlay_div.css({
				position: 'absolute',
				top: 10
			})
		}
		//window width smaller than overlay div
		if(overlay_dimensions.width > window_dimensions.width)
		{
			overlay_div.css({
				position: 'absolute'
			})
		}
			
	}
	
	 msgbox_close = function(o)
	 {
	 	o_msgbox = open_msgbox;
		open_last = true;
		if (o == "all") // empties the stack
		{
			if (o_msgbox)
			{
				try{
					o_msgbox.overlay().getConf().stack = false;	
				}catch(e){}
				try{
					o_msgbox.overlay().close();	
				}catch(e){}
				o_msgbox = null;
				//return null;
			}
			
			$(".msgbox").each(function(){ 
				if($(this).children('.msgbox_content').hasClass('progress')){
					var el = $(this);
					setTimeout(function(){
						el.fadeOut('slow', function(){
							el.overlay().close();		
						});
					}, 1000);
				}else{
					try{
						$(this).overlay().close();	
					}catch(e){}
				} 
	    	});
			
			open_msgbox = null;
			open_last = false;
		}
		else if (typeof(o)=='object')
		{
			if ($(o).is(".msgbox")){
				$(o).overlay().close();
				open_last = $(o).overlay().getConf().stack
			}else{
				if (typeof($(o).parents('.msgbox:first')[0].msgbox)!="undefined"){
					$(o).parents('.msgbox:first')[0].msgbox.overlay().close();
					open_last = $(o).parents('.msgbox:first')[0].msgbox.overlay().getConf().stack
				}else{
					$(o).parents('.msgbox:first').overlay().close();
					open_last = $(o).parents('.msgbox:first').overlay().getConf().stack
				}
			}
		}
		else
		{
			if (o_msgbox) {
				o_msgbox.overlay().close();
				open_last = o_msgbox.overlay().getConf().stack
			}
		}
		
		if (open_last && open_msgbox && open_msgbox.old)
		{
			open_msgbox = o_msgbox.old;
			$(open_msgbox).overlay().load();
		}
		else
		{
			open_msgbox = null;
		}
		
		return open_msgbox;
	 }
	
	 msgbox_alert = function(content, in_stack){
	 	if(typeof(content) == 'undefined'){
			content = 'Undefined';
		}
	 	buttonbox = "<div class='buttonbox'>"
					+"<div class='button_wrapper'><button class='button_big' onclick='msgbox_close(this);'>Ok</button></div>"
					+"</div>";
		msgbox('<div class="msgbox_content">'+( (typeof(content)=='string')?content:$(content).html() )+'</div>' + buttonbox, 'alert',{
			overlay:{
				closeOnEsc: false,
				stack: in_stack
				
			}
		});
	 }
	 
	 msgbox_confirm = function(title, text, callback){
	 	if(typeof(content) == 'undefined'){
			content = 'Undefined';
		}
		buttonbox = "<div class='buttonbox'>"
						+"<div class='button_wrapper'>"
							+"<button id='msgbox_confirm_yes' class='button_big yes'><img src='/imgs/common/accept.png' alt='' /> " + msg.YES + "</button>" 
							+"<button id='msgbox_confirm_no' class='button_big no'><img src='/imgs/common/cancel.png' alt='' /> " + msg.NO + "</button>"
						+"</div>"
					+"</div>";
		content = '<h2 class="modal_title">' + title + '</div>';
		try{
			if((text) && (text != 'undefined') && (text.length > 0)){
				content += '<p class="modal_detail">' + text + '</p>'
			}	
		}catch(e){}
		$('.msgbox.confirm').remove();
		msgbox('<div class="modal">' + content + buttonbox + '<div>', 'confirm',{
			overlay:{
				close: false,
				closeOnEsc: false,
				closeOnClick: false
				
			}
		});
		$('.msgbox.confirm button').click(function(){
			msgbox_close(this);
			if(callback != 'undefined')
				return callback($(this).attr('id') == 'msgbox_confirm_yes' ? true : false);
		});
	 }
	 
	 msgbox_info = function(content, in_stack){
	 	if(typeof(content) == 'undefined'){
			content = 'Undefined';
		}
		buttonbox = "<div class='buttonbox'>"
					+"<div class='button_wrapper'><button class='button_big' onclick='msgbox_close(this);'>Ok</button></div>"
					+"</div>";
		msgbox('<div class="msgbox_content">'+( (typeof(content)=='string')?content:$(content).html() )+'</div>' + buttonbox, 'info',{
			overlay:{
				closeOnEsc: false,
				stack: in_stack
			}
		});
	 }
	
	 msgbox_infotop = function(content, id){
	 	if(typeof(content) == 'undefined'){
			content = 'Undefined';
		}
	  	if (typeof(id)=='undefined'){ id = "progress"+Math.floor(Math.random()*1000) }
		msgbox('<div class="msgbox_content" id="'+id+'">'+( (typeof(content)=='string')?content:$(content).html() )+'</div>', 'infotop',{
			overlay:{
				top: 0,
				left: 0,
				stack: false,
				oneInstance: false,
				expose: null
			},
			"-moz-border-radius":0,
			"-webkit-border-radius":0,
			"-moz-box-shadow": "none",
			"-webkit-box-shadow": "none"
		});
	 }
	 
	 msgbox_progress = function(){
	 	//$('.msgbox.progress').remove();
		var id = 'progress' + Math.floor(Math.random()*1000);
		msgbox('<div class="msgbox_content progress" id="'+ id +'"></div>', 'progress',{
			overlay:{
				top: $(window).height() - 75,
				stack: false,
				oneInstance: false,
				expose: null
			},
			"-moz-border-radius":0,
			"-webkit-border-radius":0,
			"-moz-box-shadow": "none",
			"-webkit-box-shadow": "none",
			bottom: 0,
			left: 0,
			top: 'auto'
		});
	 }
	 
	 msgbox_success = function(text)
	 {
		humanMsg.displayMsg(text);
	 }

