// JavaScript Document
//取JS文件相对路径
var getPath = function(){
	var path;
	var scripts = document.getElementsByTagName('script');
	for(var i=0;i<scripts.length;i++){
		var src = scripts[i].src;
		if(src.indexOf('alert.js')!=-1){
			path = src.replace('js/alert.js','');
			break;
		}
	}

	return path;
};

var path = getPath();

var GccAlert = function(desc,type){
	this.type = type;
	initWin(this);
	this.header.desc.innerHTML = desc;
	var win = this.win;
	var win_bg = this.win_bg;
	this.header.cloze.onclick = function(){
		closeWin(win);
		closeWin(win_bg);
	}
}

GccAlert.prototype.setCallBack = function(callback,params){
	var win = this.win;
	var win_bg = this.win_bg;
	this.winFoot.sure.onclick = function(){
		closeWin(win);
		closeWin(win_bg);
		callback(params);
	}
	if(this.winFoot.cancle)
	this.winFoot.cancle.onclick = function(){
		closeWin(win);
		closeWin(win_bg);
	}
}

GccAlert.prototype.setFocus = function(id){
	this.focusEle = document.getElementById(id);
}

GccAlert.prototype.show = function(message){
	this.winBody.innerHTML = message;
	document.body.appendChild(this.win_bg);
	document.body.appendChild(this.win);
	this.win.style.left = (getWindowMessage().width)/2-(this.win.offsetWidth/2)+'px';

	
	if(typeof this.winFoot!='undefined')
		this.winFoot.sure.focus();
}

GccAlert.prototype.setWidth = function(width){
	this.win.style.width = width;
}

GccAlert.prototype.setButtonValues = function(sure,cancle){
	this.winFoot.sure.value = sure;
	this.winFoot.cancle.value = cancle;
}

var closeWin = function(win){
	win.parentNode.removeChild(win);
}

var initWin = function(obj){
	var win_bg = getWinBg();
	var win = getWin();
	var header = getHeader();
		win.appendChild(header);
	var winBody = getWinBody();
		 win.appendChild(winBody);
	if(obj.type=='confirm'){
		
		var winFoot = getFoot();
		win.appendChild(winFoot);
		obj.winFoot = winFoot;
	}else{
		var winFoot = getAlertFoot();
		winFoot.sure.onclick = function(){
			
			closeWin(win);
			closeWin(win_bg);
			if(typeof obj.focusEle!='undefined')
				obj.focusEle.focus();
		}
		obj.winFoot = winFoot;
		win.appendChild(winFoot);
	}
		obj.win = win;
		obj.header = header;
		obj.winBody = winBody;
		obj.win_bg = win_bg;
		
}

var getAlertFoot= function(){
	var div = $create('div');
		 div.className = 'winFoot';
	var sure = $create('input');
		sure.type = 'button';
		sure.className = 'btn';
		sure.value = '确定';	
		div.sure = sure;
		
		div.appendChild(sure);
	return div;
}

var getWinBg = function(){
	var winBg = $create('div');
		winBg.className = 'win_bg';
	var windowMessage = getWindowMessage();
		winBg.style.width = windowMessage.width+'px';
		winBg.style.height = windowMessage.height+'px';
	return winBg;
}

var getFoot = function(){
	var div = $create('div');
		 div.className = 'winFoot';
	var sure = $create('input');
		sure.type = 'button';
		sure.className = 'btn';
		sure.style.marginRight = '12px';
		sure.value = '确定';
	var cancle = $create('input');
		cancle.type = 'button';
		cancle.value = '取消';
		cancle.className = 'btn';
		
		div.sure = sure;
		div.cancle = cancle;
		div.appendChild(sure);
		div.appendChild(cancle);
	return div;
}

var getWin = function(){
	var win = $create('div');
		win.className = 'win'; 
		win.style.width = '300px';
	return win;
}

var getHeader = function(){
	var header = $create('div');
		header.className = 'winHead';
	var left = $create('div');
		left.className = 'header_left';
	var left_img = $create('img');
		left_img.src = path+'imgs/alert_desc.png';
		left_img.className = 'header_left_img';
	var right = $create('img');
		right.className = 'header_right';
		right.src = path+'imgs/alert_close.gif';
	header.appendChild(left_img);
	header.appendChild(left);
	header.appendChild(right);
	header.desc = left;
	header.cloze = right;
	return header;
}

var getWinBody = function(){
	var winBody = $create('div');
		 winBody.className = 'winBody';
	return winBody;
}

var $create = function(tagName){
	return document.createElement(tagName);
}

var $get = function(id){
	return document.getElementById(id);	
}

//得到浏览器显示的屏幕高度
var getViewportHeight = function(){
	var screenHeight = 0;
	var scrollHeight=0;
	if (window.innerHeight!=window.undefined) screenHeight = window.innerHeight;
	else if (document.compatMode=='CSS1Compat') {
		screenHeight = document.documentElement.clientHeight;
		scrollHeight = document.documentElement.scrollHeight;
	}
	else if (document.body) 
		screenHeight =  document.body.clientHeight; 
	if(document.documentElement.scrollHeight)
		scrollHeight = document.documentElement.scrollHeight;
	return screenHeight>scrollHeight?screenHeight:scrollHeight; 
}
//得到浏览器显示的屏幕宽度
var getViewportWidth = function(){
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
	
}

var getWindowMessage = function(){
	var width = getViewportWidth();
	var height = getViewportHeight();
	
	return {width:width,height:height};
}


