// JavaScript Document

function GoldDialog(element, options){
	this.options = jQuery.extend(GoldDialog.options, options || {});
	
	this.dbox = jQuery(GoldDialog.layout);
	this.dbox.css({display : 'none', width:this.options.width});
	
	jQuery('body').append(this.dbox);	
	
	this.setContent(element || this.options.content);
	this.setupTitleBar();
	if(this.options.show) this.show();	
}

GoldDialog.FN = {}

jQuery.extend(GoldDialog, {
		 layout:'<div class="nDialogBox" style="display:none"><div class="ndClose"></div><div class="ndTit"></div>'
					+'<table width="100%" border="0" cellspacing="0" cellpadding="0">'
					  +'<tr>'
						+'<td width="15" height="15" class="dialogTop_l"></td>'
						+'<td class="dialogTopBg"></td>'
						+'<td width="20" class="dialogTop_r"></td>'
					  +'</tr>'
					  +'<tr>'
						+ '<td align="right" valign="bottom" class="dialogLeft"><img src="/skin/images/box_contbg.png" width="10" height="160" /></td>'
						+'<td valign="top" class="ndCont"></td>'
						+ '<td align="left" valign="bottom" class="dialogRight"><img src="/skin/images/box_contbg.png" width="8" height="160" /></td>'
					  +'</tr>'
					  +'<tr>'
						+'<td width="15" height="20" class="dialogBot_l"></td>'
						+'<td class="dialogBotBg"></td>'
						+'<td class="dialogBot_r"></td>'
					  +'</tr>'
					+'</table>'
					+'</div>',
		 options:{
			id:"lightboxID",
			width:550,
			height:420,
			title:"",
			show:true,
			mask:true,
			content:'<div>content...</div>',
			overlayBgColor:'#000',
			overlayOpacity:0.5,
			overlaybox:null,
			useIframe:false,
			url:'',
			callback : GoldDialog.FN
		 },
		 init:function(){
			
		 }
	
});

jQuery.extend(GoldDialog.prototype, {	
	getContent:function(){
		return jQuery(".dboxContent", this.dbox);
	},
	setWHoption:function(h){
		this.options.height = h;
		//jQuery(".dboxContent", this.dbox).css({height:this.options.height});
		this.reSize();
	},
	setTitle:function(tit){
		if(tit) jQuery(".ndTit", this.dbox).html(tit);
	},
	
	setContent:function(cont){
		if(cont){
			var self = this;
			cont = jQuery(cont).css({display: 'block'}).addClass('dboxContent');
			this.getContent().remove();
			if(this.options.useIframe && this.options.url && this.options.url.length > 0){
				var ifr = jQuery('<iframe src="" frameborder="0" name="ifr" scrolling="auto"></iframe>')
							.css({width: (self.options.width - 60), height: '520px'}).addClass('dboxContent')
							.load(function(){jQuery(this).css({height:(this.contentWindow.document.body.offsetHeight+25) +"px"});});
				ifr[0].src = this.options.url;
				this.getInner().append(ifr);
			}else{
				this.getInner().append(cont);
			}
			
		}
	},
	
	getInner:function(){
		return jQuery(".ndCont", this.dbox);
	},
	
	reload : function(attr) {
		var url = null;
		if (typeof attr == 'string') {
			url = attr;
		} else if (attr && attr.url){
			//do something.
			url = attr.url;
		} else {
			url = this.options.url;
		} 
		url = url + ((url.indexOf("?") == -1) ? "?" : "&")  + "_TIMESTAMP=" + new Date().getTime();
		this.getInner().children(":first").attr("src", url);
		this.dbox.show();
		this.maskBox.show();
	},
	
	setupTitleBar:function(){
		var self = this;
		if(this.options.title) this.setTitle(this.options.title);	
		jQuery(".ndClose", self.dbox).click(function(){
				eval("if(self.options.closable==false){self.hide();}else{self.close(); }")
				return false; 
			}
		).mousedown(function(evt){evt.stopPropagation();});
		
	},
	
	isShow : true,
	
	show : function(){
		this.isShow = true;
		var self = this;
		if(this.options.mask){
			 var pSize = this.getPageSize();
			 this.maskBox = jQuery('<div id="jQoverlay"></div>')
                .css({zIndex: 90,
                      backgroundColor:	this.options.overlayBgColor,
					  opacity:			this.options.overlayOpacity,
                      width: pSize.pageWidth,
                      height: pSize.pageHeight});
				
			if (jQuery.browser.msie) {
				var html = '<'+'?xml version="1.0" encoding="UTF-8"?'+'><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body style="margin:2px 0 2px 5px ;border:0;">INITIAL_CONTENT</body></html>';
				
				this.maskBox = jQuery('<iframe id="jQoverlay" scrolling="no" frameborder="0"></iframe>')
				.css({zIndex: 90,
                      backgroundColor:	this.options.overlayBgColor,
					  opacity:			this.options.overlayOpacity,
                      width: pSize.pageWidth,
                      height: pSize.pageHeight});
				
				//this.maskBox.contentWindow.document.open();
          		//this.maskBox.contentWindow.document.write(this.options.html);
           	 	//this.maskBox.contentWindow.document.close();
			}
			
			this.maskBox.appendTo(document.body);		
			
		}
		this.reSize();
		jQuery(window).resize(function(){self.reSize();});
		jQuery(window).scroll(function(){self.reSize();});
		self.dbox.stop().css({opacity: 1}).fadeIn("slow");
	},
	
	
	hide : function() {
		this.isShow = false;
		this.dbox.hide();
		this.maskBox.hide();
	},
	
	close : function(){
		if (this.options.beforeClose) {
			this.options.beforeClose();
		}
		var self = this;
		if(this.options.mask){
			this.maskBox.animate({opacity: 0}, function() { jQuery(this).remove(); });
		}
		self.dbox.remove();
		/*
		self.dbox.stop().animate({opacity: 0}, 300, function() {
        self.dbox.css({display: 'none'});
        });
        */
	},
	
	getFocus:function(){
		this.dbox[0].focus();
	},
	
	getWindowScroll : function() {
		var w = window;
		  var T, L, W, H;
		  L = window.pageXOffset || document.documentElement.scrollLeft;
		  T = window.pageYOffset || document.documentElement.scrollTop;
	
		  if (window.ie) 
			W = Math.max(document.documentElement.offsetWidth, document.documentElement.scrollWidth);
			else if (window.khtml) 
			  W = document.body.scrollWidth;
			else 
			  W = document.documentElement.scrollWidth;
			  
			if (window.ie) 
			  H = Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight);
			else if (window.khtml) 
			  H = document.body.scrollHeight;
			else
			  H = document.documentElement.scrollHeight;
			
		  return { top: T, left: L, width: W, height: H };
	},
	
	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;
		}  
		var pageHeight, pageWidth;
		
		if(yScroll < windowHeight){
		  pageHeight = windowHeight;
		} else { 
		  pageHeight = yScroll;
		}
		
		if(xScroll < windowWidth){  
		  pageWidth = windowWidth;
		} else {
		  pageWidth = xScroll;
		}
		
		return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight};
	},
	
	reSize : function(){
		var self = this;
		var dW = this.dbox.width();
		//var dH = this.dbox.height();
		//alert(dH);
		
		var pSize = this.getPageSize();
		var pScroll = this.getWindowScroll();
			if(this.options.mask){
				this.maskBox.css({ width : pSize.pageWidth, height : pSize.pageHeight });
			}	
			
			var sTop = pSize.windowHeight/2;
				sTop += pScroll.top;
			var sLeft = (pSize.windowWidth - dW)/2;
				sLeft += pScroll.left;
				
			self.dbox.css({ top : sTop/2, left : sLeft });
	}
});
