﻿/******************************
 *Filename:      myAlert.js
 *Version:       1.0.0(2009-5-12)
 *Website:       http://
 *Author:        S.S.L
*******************************/

function MyAlert( newAlert,newDrag,newTip ){
	var theAlert,theDrag,theTip,newDragArea,newDragAreabg;
	
	var dragArea = {id:"dragArea"}
	var dragAreabg = { id:"dragAreabg"}
	
	var alpha_timer,display_timer;
	var display_time = 3000;//显示时间
	
	function $( id ){ return document.getElementById( id ); }

	this.setDisplay = function( sta,txt ){
		
		initPos();	
		
		//下面这些判断是用来模拟弹窗跟确认框相应参数的
		switch( sta ){
			case( 0 ):
				doTimeDisplay();
				break;
			case( 1 ):
				$( dragAreabg.id ).style.display = "block";
				//$( dragArea.id )这个是等到鼠标拖动的时候才触发显示的
				//$( dragArea.id ).style.display = "block";
				$( newAlert ).style.display = "block";
				setTipContent( txt );
				initTimeDisplay();
				break;
			default:
				$( dragAreabg.id ).style.display = "block";
				$( newAlert ).style.display = "block";
				setTipContent( txt );
				break;
		}
	}
	
	//设置提示内容
	function setTipContent( txt ){
		txt = txt || "";
		if( $( theTip ) == undefined ){
			alert( txt );
		} else {
			$( theTip ).innerHTML = txt;
		}
	}

	//拖动效果实现
	function doMove( subx,suby ){		
		$( dragArea.id ).style.display = "block";
		
		/*
		 *监听鼠标在dragArea上面的移动事件,获取鼠标的xy坐标
		 *重新设置弹窗的坐标
		 */
		$( dragArea.id ).onmousemove = function(e){
			e = window.event || e;
			
			$( theAlert ).style.left = e.clientX-subx + "px";
			$( theAlert ).style.top = e.clientY-suby + document.documentElement.scrollTop + "px";
			$( theAlert ).style.margin = "0";
		}
		
		//当鼠标松开的时候,撤销拖动事件
		$( dragArea.id ).onmouseup = function(){
			$( dragArea.id ).onmousemove = null;
			$( dragArea.id ).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(){
		$( theAlert ).style.left = "40%";
		$( theAlert ).style.top = (document.documentElement.scrollTop + 200) + "px";
		$( theAlert ).style.margin = "0";
	}
	
	//渐变效果实现
	function doAlpha(){
		var val = 2 * Math.pow(2, 10 * (t/100 - 1));
		
		if( document.all ){
			$( theAlert ).style.filter = "alpha( opacity=" +  + ")";
		}else{
			$( theAlert ).style.opacity = 20;
		}
	}
	
	function setAlpha(){
		alpha_timer = setInterval( doAlpha,200 );
	}
	
	//定时关闭对话框
	function doTimeDisplay(){
		if( display_timer != undefined )
				clearInterval( display_timer );
		$( dragAreabg.id ).style.display = "none";
		$( dragArea.id ).style.display = "none";
		$( newAlert ).style.display = "none";
	}
	function initTimeDisplay(){
		display_timer = setInterval( doTimeDisplay,display_time );
	}
	
	//初始化参数
	function initParams(){
		if( $( dragArea.id ) != undefined || $( dragArea.id ) != undefined ) return;
		
		//添加拖动层
		newDragArea = document.createElement( "div" );
		newDragAreabg = document.createElement( "div" );
		
		newDragArea.id = dragArea.id;
		newDragArea.style.height = document.documentElement.scrollHeight + "px";
		
		newDragAreabg.id = dragAreabg.id;
		newDragAreabg.style.height = newDragArea.style.height;
		
		document.body.style.height = "100%";
		document.body.insertBefore( newDragArea,document.body.childNodes[0] );
		document.body.insertBefore( newDragAreabg,document.body.childNodes[0] );
	}
	
	//构造器
	function init(){
		theAlert = newAlert;
		theDrag = newDrag;
		theTip = newTip;
		
		if( document.all ){
			window.attachEvent( "onload",initParams );
			window.attachEvent( "onload",doDrag );
		}else {
			window.addEventListener( "load",initParams,false );
			window.addEventListener( "load",doDrag,false );
		}
	}
	init();
}
