/******************************
 *Filename:      my_alert.js
 *Version:       1.0.0(2009-5-12)
 *Website:       http://
 *Author:        S.S.L
 *Modify:        2009-6-24
*******************************/

function mmoAlert( newAlert,newDrag,newtip ){
	var theDrag = "defaultAlert_top";
	var theAlert = "defaultAlert";
	var theTip = "";
	
	var timer;
	var isOpen = -1;
	var isDefault = true;
	var isAlert = true;
	
	var nodefault;
	
	function $( id ){ return document.getElementById( id ); }

	//拖动效果实现
	function doMove( subx,suby ){
		$( "alert_mask2" ).style.display = "block";
		/*
		 *监听鼠标在alert_mask2上面的移动事件,获取鼠标的xy坐标
		 *重新设置弹窗的坐标
		 */
		$( "alert_mask2" ).onmousemove = function(e){
			e = window.event || e;
			
			$( theAlert ).style.left = e.clientX-subx + "px";
			$( theAlert ).style.top = e.clientY-suby + document.documentElement.scrollTop + "px";
		}
		
		//当鼠标松开的时候,撤销拖动事件
		$( "alert_mask2" ).onmouseup = function(){
			$( "alert_mask2" ).onmousemove = null;
			
			$( "alert_mask2" ).style.display = "none";
			//$( "alert_mask1" ).style.display = "none";
		};
	}
	
	//添加鼠标点击拖动事件
	function doDrag(){
		$( theDrag ).onmousedown = function( e ){
			e = window.event || e;
					
			//获取鼠标点击时,在拖动对象上面的相对距离
			var newx = e.layerX || e.offsetX;
			var newy = e.layerY || e.offsetY;

			doMove( newx,newy );
		}
	}
	
	//初始化弹窗的位置坐标
	function initPos(){
		if( $( theAlert ) == null ) return;
		var doc = ( document.compatMode.toLowerCase()=="css1compat" ) ? document.documentElement : document.body;
		
		$( theAlert ).style.left = (doc.clientWidth-$( theAlert ).clientWidth)/2 + "px";
		$( theAlert ).style.top = doc.scrollTop + ( doc.clientHeight - $( theAlert ).clientHeight )/2 - 50 + "px";
	}
	
	//初始化样式
	function initDefaultStyle(){
		var str = '';
	  	
		str += '<style type="text/css">';
		
		str += '/*弹出拖动层样式*/';
		str += '#alert_mask2,#alert_mask1{ position:absolute; width:100%; left:0; top:0; background-color:#ccc; filter: alpha(opacity=0); opacity: 0; -moz-user-select:none; z-index:9999; display:none;}';
		str += '	#alert_mask1{filter: alpha(opacity=40); opacity: 0.4;z-index:9997;background-color:#000;}';
		
		if( isDefault ){		
		  str += '/*默认弹窗的样式*/';
		  str += '#defaultAlert{position: absolute;text-align:center; color:#333; width: 330px;z-index:9998;}';
		  str += '	.defaultAlert_1{ background-color:#ccc; filter: alpha(opacity=50); -moz-opacity: 0.5; position: absolute; left:0; top:0; width: 430px; height:150px; z-index:1; }';
		  str += '	.defaultAlert_2{ position: absolute; width: 420px; height: 140px; top:5px; left:5px; z-index:2; background-color:#fff;}';
		  str += '		.defaultAlert_top{ background-color:#efefef; height:25px; line-height:25px; padding-left:10px; cursor:move; text-align:left; font-weight:bold;border-bottom:1px solid #eae7e7;}';
		  str += '		.defaultAlert_cnt{  height:87px; line-height:65px;}';
		  str += '		.defaultAlert_btm{ background-color:#efefef; height:26px;border-top:1px solid #eae7e7;}';
		  str += '		.defaultAlert_btm input{ margin-top:3px; background-color:#333; color:#fff; border:1px solid #666;; cursor:pointer;width:100px;height:22px;}';
		  str += '		.defaultAlert_btm input:hover{ background-color:#fff; color:#333; border:1px solid #666;;}';
		}

		str += '</style>';
		
		document.write( str );
	}
	
	//将新元素添加到页面里面
	function addEleToBody( newid,newclass,str ){
		var fordrag = document.createElement( "div" );
		
		fordrag.id = newid;
		fordrag.className = newclass;
		
		fordrag.innerHTML = str;
		document.body.appendChild( fordrag );
	}
	
	//初始化遮罩层
	function initMaskDiv(){
		if( $( "alert_mask1" ) != undefined || $( "alert_mask2" ) != undefined ) return;
		var doc = ( document.compatMode.toLowerCase()=="css1compat" ) ? document.documentElement : document.body;
		var str = '';
		
		str += '<div id="alert_mask1" style="height:' + Math.max( doc.scrollHeight,doc.clientHeight ) + 'px;"></div>';
		str += '<div id="alert_mask2" style="height:' + Math.max( doc.scrollHeight,doc.clientHeight ) + 'px;"></div>';
		
		addEleToBody( "","",str );
		
	}
	
	//销毁弹窗
	function destroyAlert(){
		if( $( theAlert ) == null )return;
		$( theAlert ).parentNode.removeChild( $( theAlert ) );
	}
	
	//将弹窗写入到页面上
	function initDefaultAlert( info ){
		if( isDefault ){
			destroyAlert(); 
			
			var str = '';
		
			str += '<div class="defaultAlert_1"></div>';
			str += '<div class="defaultAlert_2">';
			str += '	<div class="defaultAlert_top" id="defaultAlert_top"></div>';
			str += '	<div class="defaultAlert_cnt" id="defaultAlert_cnt">' + info + '</div>';
			str += '	<div class="defaultAlert_btm">';
			str += '		<input type="button" value="ok" onclick="mya.isBtn(1)" />';
			
			if( isAlert ){
				str += '	<input type="button" value="cancel" onclick="mya.isBtn(0)" />';
			}
			
			str += '	</div>';
			str += '</div>';
			
			addEleToBody( theAlert,"defaultAlert",str );	
		} else {
			addEleToBody( theAlert,$( theAlert ).className,nodefault );
		}
		
		$( "alert_mask1" ).style.display = "block";
		
		initPos();
		doDrag();

	}
	
	//用来监听页面上用户的操作
	this.isBtn = function( k ){
		isOpen = k;
	}
	
	//默认弹窗的入口
	this.my_alert = function( newinfo,newfunc1,newfunc2,k ){
		if( newinfo == null ) return;
		
		isAlert = k;
		
		if( isDefault )	{
			destroyAlert();
		}else{
			$( theTip ).innerHTML = newinfo;
			nodefault = $( theAlert ).innerHTML;
			$( theAlert ).style.display = "block";
		}
		
		initDefaultAlert( newinfo );
		
		if( newfunc1 != null && typeof( newfunc1 ) == "function" ){
			if( timer != null )clearInterval( timer );
			timer = setInterval( function(){
				if( isOpen == 1 ){
					newfunc1();
					destroyAlert();
					isOpen = -1;
					clearInterval( timer );
				} else if( isOpen == 0 ){
					clearInterval( timer );
					if( newfunc2 == null || typeof( newfunc2 ) != "function" ) return;
					newfunc2();
					destroyAlert();
					$( "alert_mask1" ).style.display = "none";
					isOpen = -1;
				}
			},10 );
		}
		
	}
	
	//构造器
	function init(){
		if( newAlert != null && newDrag != null ){
			isDefault = false;
			theAlert = newAlert;
			theDrag = newDrag;
			theTip = newtip;
		}
		
		initDefaultStyle();
		
		if( document.all ){
			window.attachEvent( "onload",initMaskDiv );
			window.attachEvent( "onscroll",initPos );
		}else {
			window.addEventListener( "load",initMaskDiv,false );
			window.addEventListener( "scroll",initPos,true );
		}
	}
	init();
}

var mya = new mmoAlert();
