$(document).ready(function(){
	$('.dialog').dialog();
	//Add Flash
	//var flash_top_1 = UI.G('flash_top_1'),flash_top_2 = UI.G('flash_top_2'),banner = UI.G('banner'),news_bar = UI.G('news_bar');
	var flash_top_1 = UI.G('flash_top_1'),flash_top_2 = UI.G('flash_top_2'),flash_top_3 = UI.G('flash_top_3'),banner = UI.G('banner'),news_bar = UI.G('news_bar');
	if (UI.C(flash_top_1,'backgroundImage')!='none') {	//Add Flash When CSS Loaded
		if (flash_top_1) UI.Flash(flash_top_1,'/site_media/styles/images/top_1.swf',11,50);
		if (flash_top_2) UI.Flash(flash_top_2,'/site_media/styles/images/top_2.swf',49,50);
		if (flash_top_3) UI.Flash(flash_top_3,'/site_media/styles/images/top_1.swf',11,50);
	}
	if (banner) UI.Flash(banner,'/site_media/styles/images/banner.swf',900,335);
	flash_top_1.style.backgroundImage = 'none';
	if (flash_top_2)flash_top_2.style.backgroundImage = 'none';
	if (flash_top_3)flash_top_3.style.backgroundImage = 'none';

	//CSS Hack
	(function(){
		if (UI.Browser.ie6) {
			//Min Windth
			var min = UI.hasClass(document.body,'auto') ? 802 : 922;
			var topbar = UI.G('topbar');
			var Min = function() {
				//topbar.style.width = document.body.clientWidth <= min ? min + 'px' : '';
				document.body.style.width = document.documentElement.clientWidth <= min ? min + 'px' : '';
			}
			Min();
			UI.EA(window,'resize',Min);
		}
	})();

	if (news_bar) {
		oNews = new UI.ShowBar({name:'oNews',id:'news_bar',connect:true,action:'marginTop',pause:true});
	}

	//Login Form Event
	var username = UI.G('username'),password = UI.G('password'),url = UI.G('url'),check_num = 0;
	if (username) 
	{
		username.onfocus = password.onfocus = function() {
			this.previousSibling.className = 'hide';
		}
		username.onblur = password.onblur = function() {
			this.previousSibling.className = this.value!='' ? 'hide' : '';
		}
	}
	if (url) {
		url.onfocus = username.onfocus;
		url.onblur = username.onblur;
	}
	Check();
	var check = setInterval(function(){
		Check();
		try{
			if (check_num > 5 || (username.previousSibling.className == 'hide' && password.previousSibling.className == 'hide' && url.previousSibling.className == 'hide')) {
				clearInterval(check);
			}
		}catch(e){};
	},200);
	function Check() {
		if (username) {
			if (username.value!='') username.previousSibling.className = 'hide';
		}
		if (password) {
			if (password.value!='') password.previousSibling.className = 'hide';
		}
		if (url) {
			if (url.value!='') url.previousSibling.className = 'hide';
		}
		check_num++;
	}
});

(function($) {
$.fn.dialog = function() {
	/*
		  JS:	$('.dialog').dialog();
		HTML:	<a href="test.html?width=400&height=180" class="dialog" title="Title">Test</a>
				<input type="button" value="Test" rel="test.html?width=400&height=180" class="dialog" title="Title"/>
	*/
	$(this).click(function(){
		var o = $(this);
		if (o.hasClass('close')) {
			if (o.hasClass('refresh')) {
				Dialog.refresh = true;
			}
			Dialog.close();
		}
		else {
			var width,height,content,close='',bg='';
			var title = o.attr('title')||"&nbsp;";
			var href = o.is('a') ? o.attr('href') : o.attr('rel');
			if (!href) return false;
			var arr = href.split('?');
			var style = arr.length>1 ? arr[1].replace(/&/gi,';') : '';
			eval(style);
			var json = {title:title,width:width,height:height,href:href,message:o.attr('rel')};
			if (o.hasClass('confirm')) {
				Dialog.iframe = false;
				json.call = '$(\'#'+o.parents('form').attr('id')+'\').submit()';
				Dialog.open(json);
			}
			else {
				Dialog.iframe = true;
				Dialog.open(json);
			}
		}
		o = null;
		return false;
	});
};
})(jQuery);

var Dialog = {
	across_iframe : true,
	animate : true,
	move : false,
	refresh : false,
	bg_close : false,
	iframe : true,
	body : document.createElement('div'),
	build : function() {
		if (this.body.innerHTML=='') {
			document.body.appendChild(this.body);
			this.body.id = 'dialog';
			this.body.innerHTML = '<iframe src="about:blank" class="cover_select"></iframe><div class="bg" id="dialog_bg"></div><div class="wrap" id="dialog_wrap"><div class="title" id="dialog_title"></div><div class="close"" title="ر" id="Dialog.close"></div><div class="cont" id="dialog_cont"></div>';
			this.bg = G('dialog_bg');
			this._close = G('Dialog.close');
			this.title = G('dialog_title');
			this.cont = G('dialog_cont');
			this.wrap = G('dialog_wrap');
			this._close.onclick = function(){
				Dialog.close();
			}
			this.bg.onclick = function(){
				if (Dialog.bg_close) Dialog.close();
			}
		}
	},
	open : function(o) {
		this.build();
		if (!this.iframe) {
			var check;
			try{
				var check = this.check();
			}catch(e){}
			if (check!=undefined) {
				this.alert({message:check,title:o.title});
			}
			else this.confirm(o);
			this.wrap.className = 'wrap confirm';
			this.wrap.style.cssText= '';
		}
		else {
			this.wrap.className = 'wrap';
			this.wrap.style.cssText = 'width:'+o.width+'px;height:'+o.height+'px;margin:-'+o.height/2+'px 0 0 -'+o.width/2+'px;';
			if (o.href!=undefined) {
				this.cont.innerHTML = '<div class="loading"><span>loading...</span></div><iframe frameborder="no" allowtransparency="true" scrolling="auto" name="'+(new Date()).getTime() +'" src="'+o.href+'" style="height:'+(o.height-50)+'px" onload=\'this.setAttribute("scrolling","auto");parent.$(".loading").hide()\'></iframe>';
			}
			this.reset(o);
		}
		this.body.style.display = 'block';
		this.move ? this.title.style.cursor = 'move' : this.title.style.cursor = 'default';
		this.hack();
	},
	reset : function(o) {
		if (this.body.innerHTML=='') {
			return false;
		}
		this.title.innerHTML = o.title;
		if (this.animate) {
			$(this.wrap).animate({height:o.height+'px',marginTop:-o.height/2+'px',width:o.width+'px',marginLeft:-o.width/2+'px'},'normal').find('iframe').animate({height:(o.height-50)+'px'},'normal');
		}
		else {
			this.wrap.style.cssText = 'width:'+o.width+'px;height:'+o.height+'px;margin:-'+o.height/2+'px 0 0 -'+o.width/2+'px;';
			this.wrap.getElementsByTagName('iframe')[0].style.cssText = 'height:'+(o.height-50)+'px';
		}
		this.hack();
	},
	alert : function(o) {
		this.build();
		o ? null : o = {};
		if (o.code == 1) o.ico = 'success';
		else if (o.code == 0) o.ico = 'fail';
		else o.ico = 'alert';
		o.message == undefined ? o.message = '操作出错' : null ;
		if(o.next != undefined && o.next != '') this.refresh = o.next;
		this.cont.innerHTML = '<div class="box"><b class="ico '+o.ico+'"></b> <strong>'+o.message+'</strong><div class="line"></div><p class="right"><span class="cmn_btn"><input type="button" onclick="Dialog.close();" value="ȷ " /></span></p></div>';
		this.title.innerHTML = o.title;
		this._confirm();
	},
	confirm : function(o) {
		this.build();
		this.cont.innerHTML = '<div class="box"><b class="ico info"></b> <strong>'+o.message+'</strong><div class="line"></div><p class="right"><span class="cmn_btn"><input type="button" id="dialog_confirm" value="ȷ " onclick="'+o.call+'" /></span> <span class="cmn_btn"><input type="button" id="cancel" value="ȡ " onclick="Dialog.close()" /></span></p></div>';
		this.title.innerHTML = o.title;
		this._confirm();
	},
	_confirm : function() {
		this.iframe = false;
		this.wrap.style.cssText = '';
		this.wrap.className = 'wrap confirm';
		this.body.style.display = 'block';
		this.hack();
	},
	close : function() {
		$('html').attr('style','');
		this.body.style.display = 'none';
		if(this.refresh != false) {
			if(this.refresh == true) location.reload();
			else location.href = this.refresh;
		}
		try{
			$dp.dd.style.display = 'none';	//Close WdatePicker
		}catch(e){}
	},
	hack : function() {
		document.documentElement.style.overflow = 'hidden';
		var ua = navigator.userAgent;
		if (ua.indexOf('MSIE 6') != -1 && ua.indexOf('MSIE 7') == -1) {
			$('html').attr('style','background:#666666;').click(function(e){	// Remove IE6 Bug When Dialog Background Is Not White
				var o = e.target || e.srcElement;
				if (o==this && Dialog.bg_close) Dialog.close();
			});
			this.wrap.style.top = (document.documentElement.offsetHeight>document.body.offsetHeight?document.body.offsetHeight:document.documentElement.offsetHeight)/2+ (document.documentElement.scrollTop || document.body.scrollTop)+'px';
			this.wrap.style.marginTop = 0 - parseInt(this.wrap.offsetHeight / 2) + 'px';
		}
	},
	_move : function(e) {
		
	}
}
if (Dialog.across_iframe && top!=self) Dialog = top.Dialog;

var G = function(id) { return document.getElementById(id); }

