﻿function bluebirdWindow(){}
bluebirdWindow.prototype={
	w_window:null,
	//contentDiv:null;
	w_title:'系统窗口',
	init:function(){
		this.w_window=huaC('div',{'class':'bluebirdWindow'});
		var closeImg=huaC('div',{'class':'closeimg'});
		eve.observe(closeImg,'mouseover',function(){closeImg.className='closeimg_over'});
		eve.observe(closeImg,'mouseout',function(){closeImg.className='closeimg'});
		eve.observe(closeImg,'click',this.closeWindow.bind(this));
		this.block=huaC('div',{'class':'shadow'});
		this.block.style.height=document.body.offsetHeight+'px';//整个主窗口被蒙上阴影
		this.w_window.appendChild(huaC('div',{'class':'closediv'},closeImg));
		this.w_window.appendChild(this.createHtml());
		this.contentDiv.style.width=this.w_width+'px';
		this.w_window.style.top=document.documentElement.scrollTop+100+'px';
		this.w_window.style.left='-1000px';
		document.body.appendChild(this.block);
		document.body.appendChild(this.w_window);
		var w_width=this.w_width||this.w_window.clientWidth;
		this.w_window.style.left=parseInt((document.body.clientWidth-w_width)/2)+'px';
		new psbirdDrag(this.w_window.getElementsByTagName('td')[1],this.w_window);
	},
	createHtml:function(){
		this.contentDiv=huaC('div',{'class':'w_content'},this.innerDiv);
		return huaC('table',{'cellPadding':'0','cellSpacing':'0'},huaC('tbody',{},huaC('tr',{},huaC('td',{'class':'w_all'},huaC('img',{'src':'/style/images/window/lt_corner.gif'})),huaC('td',{'class':'w_all t_bg'},huaC('div',{'class':'w_title'},this.w_title)),huaC('td',{'class':'w_all'},huaC('img',{'src':'/style/images/window/rt_corner.gif'}))),huaC('tr',{},huaC('td',{'class':'w_all l_bg'}),huaC('td',{'class':'w_center'},this.contentDiv),huaC('td',{'class':'w_all r_bg'})),huaC('tr',{},huaC('td',{'class':'w_all lrb_corner'}),huaC('td',{'class':'w_all b_bg'}),huaC('td',{'class':'w_all lrb_corner moveposition'}))));		
	},
	closeWindow:function(){
		document.body.removeChild(this.block);
		document.body.removeChild(this.w_window);
	}
};
Function.prototype.bind=function(obj){//改变this
	var t=this;
	return function(){
		return t.apply(obj,arguments);
	}
}
var bluebirdDialog=new Object();
bluebirdDialog.warn=function(str,cssName,titlestr,w_width)
    {
	var frame=new bluebirdWindow();
	frame.w_width=w_width;
	frame.w_title=titlestr;
	frame.innerDiv=huaC('table',{'width':'100%'},huaC('tbody',{},huaC('tr',{},huaC('td',{'class':'w_imgtd '+cssName},str)),huaC('tr',{},huaC('td',{'class':'cen'},this.createButton('确定',frame.closeWindow.bind(frame),emptyFunc,frame)))));
	frame.init();	
}
bluebirdDialog.alert=function(str,w_width){
	this.warn(str,'w_alert','系统提示',w_width||300);
}
bluebirdDialog.error=function(str,w_width){
	this.warn(str,'w_error','出错了',w_width||300);
}
bluebirdDialog.ok=function(str,w_width){
	this.warn(str,'w_ok','成功了',w_width||300);
}
bluebirdDialog.info=function(str,w_width){
	this.warn(str,'w_info','系统消息',w_width||300);
}
bluebirdDialog.confirm=function(str,btnObj,w_width){
	var btnTd=huaC('td',{'class':'cen'});
	var frame=new bluebirdWindow();
	for(var btn in btnObj)
		btnTd.appendChild(this.createButton(btn,btnObj[btn],frame.closeWindow.bind(frame)));
	frame.w_width=w_width||250;
	frame.w_title='确认对话框';
	frame.innerDiv=huaC('table',{'width':'100%'},huaC('tbody',{},huaC('tr',{},huaC('td',{'class':'w_imgtd w_confirm'},str)),huaC('tr',{},btnTd)));
	frame.init();
   return false;
}

bluebirdDialog.createButton=function(val,act,closeAct){
	var btn=huaC('input',{'class':'btn','type':'button','value':val});
	eve.observe(btn,'click',act);
	eve.observe(btn,'click',closeAct);
	switchEffect(btn,'btn_over');//鼠标移入移出效果
	return btn;
}
bluebirdDialog.html=function(inHtml,w_title,w_width){
	//var btnTd=huaC('td',{'class':'cen'});
	var frame=new bluebirdWindow();
	//frame.w_width=500;
	frame.w_width=w_width||350;
	frame.w_title=w_title||'系统窗口';
	frame.innerDiv=huaC('table',{'width':'100%'},huaC('tbody',{},huaC('tr',{},huaC('td',{},inHtml))));
	frame.init();
	return frame.closeWindow.bind(frame);
}
function psbirdDrag(hotArea,wholeArea){//需要改进
	var self=this;
	this.divX=0;
	this.divY=0;
	this.mouseX=0;
	this.mouseY=0;
	this.initialize=function(e){
		var divPosition=self.getPosition(wholeArea);
		self.divX=divPosition[1];
		self.divY=divPosition[0];
		self.mouseX=e.clientX;
		self.mouseY=e.clientY;
		eve.observe(document.body,'mousemove',self.drag);
		eve.observe(document.body,'mouseup',self.stopDrag);
		eve.observe(document.body,'select',self.noselect);
	}
	this.drag=function(e){
		var mx=e.clientX-self.mouseX;
		var my=e.clientY-self.mouseY;
		wholeArea.style.left=self.divX+mx+'px';
		wholeArea.style.top=self.divY+my+'px';
		self.divX+=mx;
		self.divY+=my;
		self.mouseX+=mx;
		self.mouseY+=my;
	}
	this.stopDrag=function(){
		eve.stopObserve(document.body,'mousemove',self.drag);
		eve.stopObserve(document.body,'mouseup',self.stopDrag);
		eve.stopObserve(document.body,'select',self.noselect);
	}
	this.getPosition=function(obj){
		return [obj.offsetTop,obj.offsetLeft];
	}
	this.noselect=function(){
		document.selection.empty();
	}
	eve.observe(hotArea,'mousedown',self.initialize);
}
function huaC(eName,propertys){//DOM对象创建函数。
	var childObj,argLen=arguments.length;
	var domAttr;
	var tmpObj=document.createElement(eName);
	var fix={'class':'className','colspan':'colSpan','rowspan':'rowSpan'};
	for(var pro in propertys){
		domAttr=fix[pro]||pro;
		tmpObj[domAttr]=propertys[pro];
	}
	if(argLen==2)	return tmpObj;
	for(var i=2;i<argLen;i++){
		childObj=arguments[i];
		if('string'==typeof(arguments[i]))
		tmpObj.innerHTML+=arguments[i];
		else{
			try{
				tmpObj.appendChild(childObj);
			}catch(e){
				if('number'==typeof(arguments[i]))
					tmpObj.innerHTML=arguments[i]+'';
				else
					alert('创建'+eName+'参数中参数:'+arguments[i]+'有问题');
			}
		}
	}
	return tmpObj;
}
var eve=new Object();//事件绑定
eve.observe=function(element,name,observer){
	if(browser.ie)
		element.attachEvent("on"+name,observer);
	else
		element.addEventListener(name,observer,false);
}
eve.stopObserve=function(element,name,observer){
	if(browser.ie)
		element.detachEvent("on"+name,observer);
	else
		element.removeEventListener(name,observer,false);		
}
var browser=(function(){//浏览器判断
	var b=navigator.userAgent.toLowerCase();
	return {
	safari:/webkit/.test(b),
	opera:/opera/.test(b),
	ie:/msie/.test(b) && !/opera/.test(b),
	mozilla:/mozilla/.test(b) && !/(compatible|webkit)/.test(b)
};})();

function emptyFunc(){
    if (emptyFunc.url)
        location=(emptyFunc.url);
}
emptyFunc.url = '';
function hua(id){
	return document.getElementById(id);
}

function switchEffect(elem,cname){//靠CSS和鼠标移入移出事件来达到两种效果替换
    (new ourMouseover(elem,cname)).init();
}
function ourMouseover(elem,cname){
    this.element=elem;
    this.cname=cname;
    isOver=false;//是否onmouseover
}
ourMouseover.prototype={
    init:function(){
        eve.observe(this.element,'mouseover',this.mouseOver.bind(this),false);
        eve.observe(this.element,'mouseout',this.mouseOut.bind(this),false);
    },
    mouseOver:function(){
        if(true==this.isOver)    return;
        addClass(this.element,this.cname);
        this.isOver=true;
    },
    mouseOut:function(){//可以不用判断是否已经mouseout
        removeClass(this.element,this.cname);
        this.isOver=false;
    }
};
function addClass(elem,cName){//增加一个className
	if(elem.className.indexOf(cName)>=0)	return;//如果已经有了这个classname,return
	var s=(elem.className=='')?'':' ';
	elem.className+=s+cName;
}
function removeClass(elem,cName){//去掉一个className
	var t=elem.className.indexOf(cName);
	if(cName==elem.className)	return elem.className='';
	var s='';
	if(-1==t)	return;
	if(0==t)
		s=cName+' ';
	else
		s=' '+cName;
	elem.className=elem.className.replace(s,'');
}
//页面加载
 function LinkUrl()
 {
     top.div = top.document.createElement("div");
     div.innerHTML="<div style='top:300px;left:550px;z-index:9999;position:absolute;'><div id='loader' style='width: 220px; height: 20px'><div align='center'><font color='#FF9900'>程序加载中...</font></div><div><img src='js/images/200803131036217272.gif' /></div></div></div></div> ";
     top.document.body.appendChild(top.div);
 }
 function removediv(){
     if (top.div)
     top.document.body.removeChild(top.div);
 }
 
 function $$$(title)
	{
	    return document.getElementById(title).value;
	}