var DMZ = window.DMZ || {};
DMZ.util = function(){
	return {
		getEle:function(id){
			return ("string" == typeof(id))?document.getElementById(id):id;
		},
		debug:function(s){
				alert(s);
			},//end debug
		testItem:function(o,nois){
			try{
				if(eval(o)){
					(!nois)?DMZ.util.debug(o.toSource()+'\n\n-------------------------\n对象已存在'):null;
					//(!nois)?DMZ.util.debug(o+'\n\n-------------------------\n对象已存在'):null;
					return true;
				}else{
					(!nois)?DMZ.util.debug('对象未定义'):null;
					return false;
				}
			}catch(err){
				(!nois)?DMZ.util.debug(err):null;
				return false;
			}//end try
		}//end testItem
	}// end return
}();
DMZ.hasjQuery = function(){
	return DMZ.util.testItem('jQuery',true);
}();
DMZ.isIE = (document.all)?true:false;
var userAgent = navigator.userAgent.toLowerCase();
DMZ.browser = {
	version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
	safari: /webkit/.test( userAgent ),
	opera: /opera/.test( userAgent ),
	msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
	mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
};
DMZ.isIE6 = DMZ.isIE && (DMZ.browser.version == 6.0);
var $ =  DMZ.hasjQuery ? jQuery : DMZ.util.getEle;
DMZ.namespace = function(ns){
	if (!ns || !ns.length) { 
		return null; 
	}
	var nsHandle = DMZ;
	var levels = ns.split('.');
	var levelsLen = levels.length;
		for(var i=(levels[0]=='DMZ') ? 1 : 0; i<levelsLen; ++i){
			nsHandle[levels[i]] = nsHandle[levels[i]] || {};
			//DMZ.util.debug(nsHandle[levels[i]].toSource());
			nsHandle = nsHandle[levels[i]];
			
		}//end for
	return nsHandle;
};
DMZ.init = function(){
	if(!DMZ.hasjQuery){
		DMZ.util.debug('jQuery库还未载入，DMZ.init()基于jQuery库运行');
		return;
	}
	var argus = arguments;
	var len = argus.length;
	if(len > 0){
		$(document).ready(function(){
			for(var i=0;i<len;i++){
				eval(argus[i]);
			}
		});
	}
	
};//DMZ.init();

DMZ.namespace('widget');
DMZ.widget = {
	selectCheckbox : function(status,fId){
		/**
		<a href="javascript:void(0);" onclick="DMZ.widget.selectCheckbox(true,'form1');">全选</a>
		<a href="javascript:void(0);" onclick="DMZ.widget.selectCheckbox(false,'form1');">取消选择</a>
		<a href="javascript:void(0);" onclick="DMZ.widget.selectCheckbox('inverse','form1');">反选</a>
		*/
		//var oForm = DMZ.util.getEle(fId).getElementsByTagName('input');
		//var oForm = $('#'+fId)[0].getElementsByTagName('input');
		var oForm = DMZ.hasjQuery?$('#'+fId)[0]:$(fId);
		oForm = oForm.getElementsByTagName('input');
		var checkboxArr = [];
		for(var i = 0, oFLen = oForm.length; i < oFLen; i++){
			if(oForm[i].getAttribute('type').toLowerCase()=='checkbox'){
				checkboxArr.push(oForm[i]);
			}
		}
		checkboxArr.join('');
		if(status!='inverse'){
			for (var i = 0, oFLen = checkboxArr.length; i < oFLen; i++) {
				checkboxArr[i].checked = status;
			}
		}else{
			for (var i = 0, oFLen = checkboxArr.length; i < oFLen; i++) {
				checkboxArr[i].checked?checkboxArr[i].checked = false:checkboxArr[i].checked = true;
			}
		}
	},//end selectCheckbox
	autoCheckbox:function(){
		/**
		<a href="javascript:void(0);" rel="checkbox[true,'form1']">全选</a>
		<a href="javascript:void(0);" rel="checkbox[false,'form1']">取消选择</a>
		<a href="javascript:void(0);" rel="checkbox['inverse','form1']">反选</a>
		*/
		var elA = document.getElementsByTagName('a');
		for(var i = 0,aLen = elA.length;i<aLen;i++){
			if(elA[i].getAttribute('rel')&&elA[i].getAttribute('rel').indexOf('checkbox')!=-1){
				elA[i].onclick = function(){
					var eleArr = eval(this.getAttribute('rel').substr(8));
					//DMZ.util.debug(eleArr[0]);
					DMZ.widget.selectCheckbox(eleArr[0],eleArr[1]);
				};//end click event
				
			}//end if
		}// end for
	},//end autoCheckbox
	cookie:{
		set:function(name,value,option){
			var str=name+"="+escape(value);
				if(option){
					if(option.expireDays){
						var date=new Date();
						var ms=option.expireDays*24*3600*1000;
						date.setTime(date.getTime()+ms);
						str+="; expires="+date.toGMTString();
					}
					if(option.path)str+="; path="+option.path;
					if(option.domain)str+="; domain="+option.domain;
					if(option.secure)str+="; true";
				}
				document.cookie=str;
		},//end set
		read:function(name){
			var cookieArray=document.cookie.split("; ");
			var cookie=new Object();
				for(var i=0;i<cookieArray.length;i++){
					var arr=cookieArray[i].split("=");
					if(arr[0]==name)return unescape(arr[1]);
				}
			return "";
		},//end read
		clear:function(name){
			this.set(name,"",{expireDays:-1});
		},//end clear
		testCookie:function(name){
			DMZ.util.debug(this.read(name));
		}
	},//end cookie
	simpleScroll:function(option){
		var o = DMZ.hasjQuery?($('#'+option.id)[0]):$(option.id);
		var isPause = false;
		var lineheight = option.lineheight||24;
		o.onmouseover = function(){isPause = true;};
		o.onmouseout = function(){isPause = false;};
		window.setInterval(function(){scrollup(o,lineheight,option.interval?(lineheight):0);},(option.time||3000));
		function scrollup(o,d,c){
			if(isPause) return;
			if(d==c){
				var t=getFirstChild(o.firstChild).cloneNode(true);
				o.removeChild(getFirstChild(o.firstChild));
				o.appendChild(t);
				t.style.marginTop="0px";
			}else{
				c+=2;
				getFirstChild(o.firstChild).style.marginTop=-c+"px";
				window.setTimeout(function(){scrollup(o,d,c)},20);
			}
		}
		function getFirstChild(node){
			while (node.nodeType!=1){
				node = node.nextSibling;
			}
			return node;
		}
	},//simpleScroll end
	simpleFade:function(args){
		if(!DMZ.hasjQuery){
			DMZ.util.debug('jQuery库还未载入，simpleFade基于jQuery库运行。');
			return;
		}
		var o = args.id?$('#'+args.id+' ul'):$('.fade ul');	//实际上是拿到列表而不是外围容器
		var time = args.time?args.time:4000;	//停留时间
		var speed = args.speed?args.speed:1000;	//过渡速度
		var mode = args.mode?args.mode:0;	//fade模式
		var isPause = args.stop?true:false;
		var up = function(){
				if(mode!=1){
					return (args.up?true:false);
				}else{
					return false;
				}
			}	//滚动方向,mode==1时无效
			
		//var _width = o.parent().width();
		//var _height = o.parent().height();
		var _$_ = function(){
			if(!up()){
				return (o.parent().width());
			}else{
				return (o.parent().height());
			}
		};
		var iWidth = $(o.children()[0]).width();
		var iHeight = $(o.children()[0]).height();
		var multiple = function(){
				if(!up()){
					return (Math.floor(Math.abs(_$_()/iWidth)));
				}else{
					return (Math.floor(Math.abs(_$_()/iHeight)));
				}
			};
		var _len = o.children().length;
		var i = 0;
		var j = Math.floor(_len/multiple());
		//DMZ.util.debug(multiple());
		var _forward = false;
		//var operate = $('#'+args.id).siblings('p[class=operate]').children('a');
		var operate = $('#'+args.id).siblings('p[class*=operate]');
		
		if(j==1){
			operate.attr('class','').addClass('operate page-turning only-page');
			isPause = true;
			return;
		}
		
		//settings
		if(!up()){
			o.css({
				width:_$_() * _len + 'px',
				height:o.parent().height() + 'px',
				position:'absolute'
			});
		}else{
			o.css({
				height:_$_() * _len + 'px',
				position:'absolute'
			});
		}
		
		var setStatus = function(){
			if(i==0){
				operate.attr('class','').addClass('operate page-turning first-page');
			}else if(i==j-1){
				operate.attr('class','').addClass('operate page-turning last-page');
			}else{
				operate.attr('class','').addClass('operate page-turning mid-page');
			}
		};
		
		if(!isPause){
			var interval = window.setInterval(function(){fade.auto()},time);
			
			
			operate.hover(function(){
				//isPause = true;
				window.clearInterval(interval);
			},function(){
				//isPause = false;
				interval = window.setInterval(function(){fade.auto()},time);
			});
			
			o.hover(function(){
				//isPause = true;
				window.clearInterval(interval);
			},function(){
				//isPause = false;
				interval = window.setInterval(function(){fade.auto()},time);
			});
		}
		//change to manually
		operate.children('a').click(function(){
			if($(this).attr('rel')=='prev'&&i==0){
				return false;
			}
			fade.manually($(this).attr('rel'));
		});
		
		var fade = {
			auto:function(){
				//$(o.children()[i]).attr('title',i);	//-debug
				if(mode==1){
					fadeTo();
				}else if(mode==2){
					fadeForward();
				}else{
					fadeBack();
				}
			},//auto scroll
			manually:function(step){
				//$(o.children()[i]).attr('title',i);	//-debug
				if(mode==1){
					fadeTo(step);
				}else if(mode==2){
					fadeForward(step);
				}else{
					fadeBack(step);
				}
			
			}//manual scroll
		};//fade
		
		//fade-1
		var fadeTo = function(s){
			var step = s||'auto';
			if(i==0){
				if(step=='auto'){
					$(o.children()[i]).animate({'opacity':'hide'},{duration:speed,complete:function(){i++;}});
				}else if(step=='next'){
					$(o.children()[0]).animate({'opacity':'hide'},{duration:speed,complete:function(){
						$(o.children()[i+1]).animate({'opacity':'show'},{duration:speed,complete:function(){i++;}});
					}});
				}
			}else if(i==_len-1){
				//alert(i);
				if(step!='prev'){
					o.children().animate({'opacity':'show'},{duration:speed,complete:function(){i=0;}});
				}else{
					//$(o.children()[i]).animate({'opacity':'hide'},{duration:speed,complete:function(){
						$(o.children()[i-1]).animate({'opacity':'show'},{duration:speed,complete:function(){i--;}});
					//}});
				}
			}else{
				if(step!='prev'){
					$(o.children()[i]).animate({'opacity':'hide'},{duration:speed,complete:function(){
						$(o.children()[i+1]).animate({'opacity':'show'},{duration:speed,complete:function(){i++;}});
					}});
				}else{
					//$(o.children()[i]).animate({'opacity':'hide'},{duration:speed,complete:function(){
						$(o.children()[i-1]).animate({'opacity':'show'},{duration:speed,complete:function(){i--;}});
					//}});
				}
			}//end out if
		};
		//fade-2
		var fadeForward = function(s){
			var step = s||'auto'; 
			if(i==0){
				_forward = false;
				if(step!='prev'){
					i++;
					up() ? o.animate({'top':-_$_() +'px'},speed) : o.animate({'left':-_$_() +'px'},speed);
				}else{
					return;
				}
				setStatus();
			}else if(i==j-1){
				_forward = true;
				if(step!='next'){
					up() ? o.animate({'top':-_$_()*i+_$_() + 'px'},speed) : o.animate({'left':-_$_()*i+_$_() + 'px'},speed);
					i--;
				}else{
					return;
				}
				setStatus();
			}else{
				if(step=='auto'){
					if(!_forward){
						i++;
						up() ? o.animate({'top':-_$_()*i +'px'},speed) : o.animate({'left':-_$_()*i +'px'},speed);
					}else{
						up() ? o.animate({'top':-_$_()*i+_$_() +'px'},speed) : o.animate({'left':-_$_()*i+_$_() +'px'},speed);
						i--;
					}
				}else if(step=='next'){
					i++;
					up() ? o.animate({'top':-_$_()*i +'px'},speed) : o.animate({'left':-_$_()*i +'px'},speed);
				}else{
						up() ? o.animate({'top':-_$_()*i+_$_() +'px'},speed) : o.animate({'left':-_$_()*i+_$_() +'px'},speed);
						i--;
				}
				setStatus();
			}
		};//fadeForward
		//fade-0
		var fadeBack = function(s){
			var step = s||'auto';
			if(i==0){
				if(step!='prev'){
					i++;
					up() ? o.animate({'top':-_$_() +'px'},speed) : o.animate({'left':-_$_() +'px'},speed);
				}else{
					return;
				}
				setStatus();
			}else if(i==j-1){
				if(step=='prev'){
					i--;
					up() ? o.animate({'top':-_$_()*i +'px'},speed) : o.animate({'left':-_$_()*i +'px'},speed);
				}else{
					i=0;
					up() ? o.animate({'top':0 +'px'},speed) : o.animate({'left':0 +'px'},speed);
				}
				setStatus();
			}else{
				if(step=='prev'){
					i--;
				}else{
					i++;
				}
				up() ? o.animate({'top':-_$_()*i +'px'},speed) : o.animate({'left':-_$_()*i +'px'},speed);
				setStatus();
			}
		};//fadeBack
	},//simpleFade end
	simpleTab:{
		decorate:function(id,data,callback){

			var o = id?'#'+id:DMZ.util.debug('simpleTab(),id不能为空');
			data = data||{};
			var eventType = data.eventType||'click';
			var currentClass = data.currentClass||'selected';
			var index = data.index||0;
			var tabs = $(o + ' > ul');
			var len = tabs.children('li').length;
			var tabSection = $(o + ' > div');
			var sectLen = tabSection.length;
			
			tabSection.not($(tabSection[index])).hide();
			
			$(tabs.children('li')[index]).addClass(currentClass).siblings('li').removeClass(currentClass)
			tabs.children('li').bind(eventType, {'eventType':eventType,'currentClass':currentClass,'tabs':tabs,'tabSection':tabSection,'callback':callback}, DMZ.widget.simpleTab.switchTab);
		},
		switchTab:function(d){
			//$(d.data.tabSection[]);
			var currentIdx = d.data.tabs.children('li').index(this);
			d.data.tabSection.not($(d.data.tabSection[currentIdx]).show()).hide();
			$(this).addClass(d.data.currentClass).siblings('li').removeClass(d.data.currentClass);
			if(d.data.callback){
				d.data.callback();
			}
		}
	}//simpleTab end
};//end widget
DMZ.namespace('expand');
DMZ.expand = {
	loginForm:function(args){
		if(!DMZ.hasjQuery){
			DMZ.util.debug('jQuery库还未载入，该应用基于jQuery库运行');
			return;	
		}
		var count = 0,clear;
		var o = args.id?('#'+args.id):'#login';
		
		var bindEvent = function(){
			
			
			$(o+' input[type=text]').add('input[type=password]').focus(function(){
				$(this).css({
					'background-position':'0 20px'
				});
			}).blur(function(){
				if($.trim($(this).val())==''){
					$(this).removeAttr('style');
				}
			});
		};//end bindEvent()
		
		var rebindEvent = function(){
			bindEvent();
			if(count>9){
				clearInterval(clear);
			}else{
				count++;
			}
		};
		if(!DMZ.util.testItem($(o),true)){
			bindEvent();
		}else{
			clear = setInterval(rebindEvent,60);
		}
	},//loginForm end
	pageBar:function(){
		var oPage = $('.page');
		var pageLen = oPage.length;
		var oLi;
		var len = 0;
		var total = 0;
		for(var i=0;i<pageLen;i++){
			oLi = oPage.eq(i).children('li');
			len = oLi.length;
			total = 0;
			for(var j = 0; j<len; j++){
				total += oLi.eq(j).width();
			}
			oPage.eq(i).css('width',total);
		}
	},//pageBar end
	showFlashObject:function(objID,objSource,objWidth,objHeight,objQuality,objWmode,objBgcolor,objXML){
		if(objXML){
		var chkMovie=0;
		if(objSource.match(/=/))objSource=objSource+"&server="+objXML+"&chkMovie="+chkMovie;
		else objSource=objSource+"?server="+objXML+"&chkMovie="+chkMovie;
		}
		var pageUrl=self.window.location.href;
		if(pageUrl.substring(0,5)=="https") swfUrl="https";
		else swfUrl="http";
		if(!objID)objID="ShockwaveFlash1";
		if(!objWidth)objWidth="0";
		if(!objHeight)objHeight="0";
		if(!objQuality)objQuality="high";
		if(!objWmode)objWmode="transparent";
		var strFlv = [];
		strFlv.push('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+swfUrl+'://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,124,0" width="'+objWidth+'" height="'+objHeight+'" id="'+objID+'" align="middle">');
		strFlv.push('<param name="allowFullScreen" value="false" />');
		strFlv.push('<param name="scale" value="noscale" />');
		strFlv.push('<param name="movie" value="'+objSource+'" />');
		strFlv.push('<param name="allowScriptAccess" value="always" />');
		strFlv.push('<param name="menu" value="false" />');
		strFlv.push('<param name="quality" value="high" />');
		strFlv.push('<param name="bgcolor" value="'+objBgcolor+'" />');
		strFlv.push('<param name="wmode" value="'+objWmode+'" />');
		strFlv.push('<embed src="'+objSource+'" allowScriptAccess="always" menu="false" quality="high" bgcolor="'+objBgcolor+'" wmode="'+objWmode+'" width="'+objWidth+'" height="'+objHeight+'" name="'+objID+'" align="middle" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
		strFlv.push('</object>');
		return strFlv.join('');
		
	},//showFlashObject end
	shadeLayer:{
		init:function(id){
			this.shadeBox = '';
			//this._top = this._left = 0;
			$('select').css('visibility','hidden');
			if(!DMZ.util.testItem(DMZ.util.getEle('shadeBox'),true)){
				this.create();
				this.normal(id);
			}else{
				this.normal(id);
			}
		},
		create:function(){
			this.shadeBox = [];
			this.shadeBox.push('<div id="shadeBox">');
			this.shadeBox.push('	<div id="alphaBox"></div>');
			this.shadeBox.push('</div>');
			this.shadeBox.join('');
			this.shadeBox = $(this.shadeBox.join('')).appendTo('body');
		},
		normal:function(id){
			var fillBox = $('#'+id);
			this.shadeBox = $('#shadeBox');
			this.shadeBox.show();
			if(DMZ.isIE6){
				this.shadeBox.css({
					height:this.shadeBox.parent().height()+'px'
				});
				this.setScroll();
				$(window).scroll(function(){DMZ.expand.shadeLayer.setScroll();});
				$(window).resize(function(){DMZ.expand.shadeLayer.setScroll();});
			}
			fillBox.show().css({
				'position':'relative',
				'top':'180px',
				'z-index':1002,
				'margin':'0 auto'
			}).appendTo(this.shadeBox);
		},
		closed:function(callback){
			this.shadeBox.hide();
			$('select').css('visibility','visible');
			if(callback){
				callback();
			}
		},
		setScroll:function(){
			this.shadeBox.css({
					top : $(document).scrollTop() + "px",
					left : $(document).scrollLeft() + "px"
			});
		}
	}//shadeLayer end
};//end expand


function dmzgamebanner(myurl, mywidth, myheight, isscroll) {
	//alert("mywidth="+mywidth+"- myheight="+myheight+"- isscroll="+isscroll+"- myurl="+myurl);
	if(mywidth == "0" && myheight == "0") {
		if(isscroll == "0" ) {
			document.location.href= myurl;
		} else {
			window.open(myurl,"_blank");
		}
	} else {
		open_popup(myurl,mywidth,myheight,isscroll,"lolgamebanner" );
	}
}
if(DMZ.isIE6){
	document.execCommand("BackgroundImageCache", false, true);
}