//var Modalbox = new Object();

var ModalBox  = { 
	initialized: false,
	$bg: null,
	$fade: null,
	$mbWrapper: null,
	$window: null,
	$body: null,
	$close: null,
	$mbContent:null,
	$mbLoading:null,
	$title:null,
	$ltitle:null,
	width: 600,
	height: 300,
	widthLoading: 300,
	heightLoading: 120,
	txtLoading: '',
	ie: false,
	visible: false,
	lowAnim: false,
	lowResolution: true,
	mTop: -60,
	border: 62,
	threadFS: false,
	_init: function() {
			//vince
		
		var strChUserAgent = navigator.userAgent;
		var intSplitStart = strChUserAgent.indexOf("(",0);
		var intSplitEnd = strChUserAgent.indexOf(")",0);
		var strChStart = strChUserAgent.substring(0,intSplitStart);
		var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
		var strChEnd = strChUserAgent.substring(strChEnd); 
		
		if(strChMid.indexOf("MSIE") != -1){
			this.ie = true;
			this.heightLoading = 40;
		}
		var mb = '<div id="screen_overay">';
		mb += '<div id="fade"></div>';
		mb += '<div id="mbWrapper">';
		mb += '<table id="MB_tableBorder">';
		mb += '  <tr>';
		mb += '    <td id="MB_cornerTopLeft" />';
		mb += '    <td id="MB_cornerTop" />';
		mb += '    <td id="MB_cornerTopRight" />';
		mb += '  </tr>';
		mb += '  <tr>';
		mb += '    <td id="MB_cornerLeft"/>';
		mb += '    <td style="height:100%">';
		mb += '      <table id="MB_internalTableBorder">';
		mb += '        <tr>';
		mb += '          <td id="MB_internalCornerTopLeft" />';
		mb += '          <td id="MB_internalCornerTop"><div id="title"></div><a id="close"></a></td>';
		mb += '          <td id="MB_internalCornerTopRight"></td>';
		mb += '        </tr>';
		mb += '        <tr>';
		mb += '          <td id="MB_internalCornerLeft" />';
		mb += '          <td id="contentWrapper" valign="top">';
		mb += '          <div id="mbLoading"><div></div><span id="ltitle"></span></div>';
		mb += '          <div id="mbContent"></div>';
		mb += '          </td>';
		mb += '          <td id="MB_internalCornerRight" />';
		mb += '        </tr>';
		mb += '        <tr>';
		mb += '          <td id="MB_internalCornerBottomLeft" />';
		mb += '          <td id="MB_internalCornerBottom" />';
		mb += '          <td id="MB_internalCornerBottomRight" />';
		mb += '        </tr>';
		mb += '      </table>';
		mb += '    </td>';
		mb += '    <td id="MB_cornerRight"/>';
		mb += '  </tr>';
		mb += '  <tr>';
		mb += '    <td id="MB_cornerBottomLeft" />';
		mb += '    <td id="MB_cornerBottom" />';
		mb += '    <td id="MB_cornerBottomRight" />';
		mb += '  </tr>';
		mb += '</table>';
		mb += '</table>';
		mb += '</table>';
		mb += '</div>';
		mb += '</div>';
		this.$body = $('body');
		this.$body.append($(mb).hide());
		this.$bg = $('#screen_overay');
		this.$fade =  $('#fade');
		this.$window = $(window);
		this.$mbWrapper = $('#mbWrapper');
		this.$mbContent = $('#mbContent');
		this.$mbLoading = $('#mbLoading').hide();
		this.$close = $('#close');
		this.$title = $('#title');
		this.$ltitle = $('#ltitle');
		this.$window.resize(this._resize);
		this.$close.click(this.hide);
		this.$fade.click(this.hide);
		//this.$mbContent.change(this.controlSize);

		this.$ltitle.html(this.txtLoading);
		this.mTop = -20;
		/*if (screen.width <= 1280 && screen.height <= 800){
			this.lowResolution = true;
			this.mTop = 0;
		}else{
			this.lowResolution = false;
			this.mTop = (this.$window.height() / 10);
		}*/
		this.initialized = true;
		
	},
	show: function(title,xajax,content,lowAnim) {
		if (!this.initialized){
		 	this._init();	
		}
		
		if (!this.visible){
			this.$title.html(title);
			this._positionPrompt();
			this._stylePrompt();
	
			this.$bg.show();
			this.$fade.fadeIn();
			this.$mbWrapper.css({
				width: '0px',
				height: '0px',
				marginLeft: (this.$bg.outerWidth() / 2)
			}).animate({
				width: this.widthLoading + 'px',
				height: this.heightLoading + 'px',
				marginTop: this.mTop,
				marginLeft: (this.$bg.outerWidth() / 2) - (this.widthLoading / 2)
			}).show();

			this.$mbLoading.fadeIn();
			this.$close.fadeIn();
			this.visible = true;
		
		}else{
			this.$title.html(title);
			this.$mbLoading.fadeIn();
		
			this.$mbWrapper.animate({
				width: this.widthLoading + 'px',
				height: this.heightLoading + 'px',
				marginTop: this.mTop,
				marginLeft: (this.$bg.outerWidth() / 2) - (this.widthLoading / 2)
			});
		}
		this.$mbContent.hide();
		if (arguments[3]){
			this.lowAnim = arguments[3];
		}else{
			this.lowAnim = false;
		}

		if (arguments[2]){
			setTimeout(function(){ModalBox._showContent(content)},1000);
		}else{
			setTimeout(function(){ModalBox._runXajax(xajax)},1000);
		}
	},
	_showContent: function(content){
		ModalBox.$mbContent.html(content);
		ModalBox.updateContent();
	},
	afterUpdateContent: function(){
		//callback used after content updated
	//	while($('#mbContent').height() == null){
			
	//	}
	/*	if ($('#mbContent').height() > (ModalBox.$window.height() - 50)){
			$('#MB_tableBorder').css({
				height: (ModalBox.$window.height()) + "px"
			});	
			$('#mbContent').css({
				height: (ModalBox.$window.height() - 80) + "px",
				'overflow' : 'auto'
			});	
		}	*/
	},
	_runXajax: function(xajax){
		eval(xajax);	
	},
	controlSize: function(){
		if ($('#MB_tableBorder').height() > ModalBox.$window.height() - ModalBox.mTop){		
			ModalBox.$mbWrapper.css({
				height: (ModalBox.$window.height()) + 'px',
				widht: (ModalBox.$mbWrapper.widht() + 10) + 'px'
			});
			alert(ModalBox.$mbContent.widht() + ' '+ModalBox.$mbWrapper.widht());
			ModalBox.$mbContent.css({
				height: (ModalBox.$window.height() - 80) + 'px',
				widht: (ModalBox.$mbContent.widht() + 10) + 'px',
				'overflow-y' : 'auto'
			});
		}
		if (ModalBox.threadFS && ModalBox.visible)
			setTimeout(function(){ModalBox.controlSize()},1000);
		
	},
	updateContent: function(hideTimeOut,fullscreen){
		ModalBox.$mbContent.fadeIn();
		ModalBox.$mbLoading.fadeOut();
		
		if (ModalBox.ie)
			var adjust = 60;
		else
			var adjust = 0;

		if (ModalBox.lowAnim){
			ModalBox.$mbWrapper.css({
				width:(ModalBox.$mbContent.outerWidth() + ModalBox.border) + "px",
				height:(ModalBox.$mbContent.outerHeight() + ModalBox.border - adjust) + "px",
				marginTop: ModalBox.mTop + "px",
				marginLeft: ((ModalBox.$bg.outerWidth() / 2) - ((ModalBox.$mbContent.outerWidth() + ModalBox.border) / 2))  + "px"
			});
		}else{
			ModalBox.$mbWrapper.animate({
				width:(ModalBox.$mbContent.outerWidth() + ModalBox.border) + "px",
				height:(ModalBox.$mbContent.outerHeight() + ModalBox.border - adjust) + "px",
				marginTop: ModalBox.mTop + "px",
				marginLeft: ((ModalBox.$bg.outerWidth() / 2) - ((ModalBox.$mbContent.outerWidth() + ModalBox.border) / 2)) + "px"
			});
		}
		
		ModalBox.afterUpdateContent();
		if (arguments[0] && arguments[0] > 0){
			setTimeout(function(){ModalBox.hide()},hideTimeOut * 1000);
		}
		if (arguments[1]){
			ModalBox.threadFS = true;
			ModalBox.controlSize();
		}else{//size auto
			ModalBox.$mbWrapper.css({
				height: 'auto'
			});
			ModalBox.$mbContent.css({
				height: 'auto',
				'overflow' : 'auto'
			});	
		}
		
		//setTimeout(function(){ModalBox.controlSize()},1000); //need to solve
		
	},
	_resize: function(){
		ModalBox.$bg.css({
			height: ModalBox.$window.height()
		});
		ModalBox.$fade.css({
			height: ModalBox.$window.height()
		});
		ModalBox.$mbWrapper.animate({
			marginTop: ModalBox.mTop,
			marginLeft: (ModalBox.$bg.outerWidth() / 2) - (ModalBox.$mbWrapper.outerWidth() / 2)
		});
		
		
	},
	_stylePrompt: function(){
		var zIndex = 195;
		var opacity = 0.5;
		this.$fade.css({
			zIndex: zIndex,
			display: "none",
			opacity: opacity
		});
		this.$bg.css({
			zIndex: 196,
			display: "none"
		});
		this.$mbWrapper.css({
			zIndex: zIndex
		});
	},
	_positionPrompt: function(){
		this.$bg.css({
			position: "fixed",
			height: ModalBox.$window.height(),
			width: "100%",
			top: 0,
			left: 0,
			right: 0,
			bottom: 0
		});
		this.$bg.show();
		this.$fade.css({
			position: "absolute",
			height: ModalBox.$window.height(),
			width: "100%",
			top: 0,
			left: 0,
			right: 0,
			bottom: 0
		});
		this.$mbWrapper.css({
			position: "absolute",
			top: 15+"px"
		});
	},
	hide: function(){
		ModalBox.$close.fadeOut();
		ModalBox.$mbContent.fadeOut();
		if (ModalBox.lowAnim){
			ModalBox.$mbWrapper.fadeOut(
				{complete: function() {				
					ModalBox.$mbWrapper.hide();
					ModalBox.$fade.fadeOut({complete:function(){ModalBox.$bg.hide();}});
			}});	
		}else{
			ModalBox.$mbWrapper.animate({
				width: '0px',
				height: '0px',
				marginLeft: (ModalBox.$bg.outerWidth() / 2)
			},{complete: function() {				
					ModalBox.$mbWrapper.hide();
					ModalBox.$fade.fadeOut({complete:function(){ModalBox.$bg.hide();}});
			}});
		}
		
		ModalBox.visible = false;
		ModalBox.threadFS = false;

	},
	startLoading: function(ltitle) {
		widthLoading = 300;
		heightLoading = 100;
		this.$ltitle.html(ltitle);
		mbLoading.fadeIn();
		
		this.$mbWrapper.animate({
			width: widthLoading + 'px',
			height: heightLoading + 'px'
		});
	}
};