function Ajax(url, args) {
	if(typeof(args) != 'object'){
		args = {};
	}
    this.url = url || "";
    this.params = args.parameters || "";
    this.mime = args.mime || "text/html";
    this.onComplete = args.onComplete || this.defaultOnCompleteFunc;
    this.onLoading= args.onLoading || this.defaultOnLoadingFunc;
    this.onError = args.onError || this.defaultOnErrorFunc;
    this.method = args.method || "get";
    
    if(this.url.indexOf('?') != -1){
    	this.url += "&ajaxtimestamp=" + new Date().getTime();
    }else{
    	this.url += "?ajaxtimestamp=" + new Date().getTime();
    }

    if (typeof(args.sync) == "undefined" || args.sync == null) { 
		this.sync = true;
    } else {
		this.sync = args.sync ? true : false;
    }
    this.loadData();
}

Ajax.prototype = {
    READY_STATE_COMPLETE : 4,
    getRequest : function () {
		var funcs = [
		    function() {return new ActiveXObject('Msxml2.XMLHTTP')},
		    function() {return new ActiveXObject('Microsoft.XMLHTTP')},
		    function() {return new XMLHttpRequest()},
		];
	
		var req = null;
		for (var i = 0; i < funcs.length; i++) {
		    var f = funcs[i];
		    try {
				req = f();
				break;
		    } catch (e) {
		    
		    }
		}
	
		return req || false;
    },

    parseParams : function () {
		if (typeof (this.params) == "string") {
		    return this.params;
		} else {
		    var s = '';
		    for (var k in this.params) {
				s += k + "=" + this.params[k] + "&";
		    }
		    return s;
		}
    },

    loadData : function () {
		this.req = this.getRequest();
		
		if (this.req) {
		    this.onLoading();
		    try {
				var loader = this;
				this.req.onreadystatechange = function () {
				    if (loader.req.readyState == loader.READY_STATE_COMPLETE) {
						loader.onComplete.call(loader, loader.req);
				    }
				}
				this.req.open(this.method, this.url, this.sync);
		
				if (this.method == "post") {
				    this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				}
		
				if (this.req.overrideMimeType) {
				    this.req.overrideMimeType(this.mime);
				}
				this.req.send(this.method == "post" ? this.parseParams(this.params) : null);
		    } catch (e) {
				this.onError.call(this, e);
		    }
		}
    },

    defaultOnCompleteFunc : function () {
		//alert(this.req.responseText);
    },

    defaultOnLoadingFunc : function () {
    },

    defaultOnErrorFunc : function (error) {
	//alert(error);
    }
};


function CookieClass(prefix){
	this.prefix = prefix || 'sc_';
	this.ac = {};
	if( document.cookie == '' || document.cookie == null){
		return;
	}
	var rca = document.cookie.split(';');
	for(var i = 0; i < rca.length; i++){
		var regs = rca[i].match(/([^\s]+)=([^\s]+)/);
		this.ac[regs[1]] = regs[2];		
	}
	
	this.set = function(name, value, expire)
	{
		if(this.ac[name]){
			this.remove(name);
		}
		
		name = this.getRawName(name);
		var exp = new Date();
		exp.setTime(exp.getTime() + expire * 1000);
		document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
		this.ac[name] = value;
	};
	
	this.get = function(name)
	{
		name = this.getRawName(name);
		if(this.ac[name] != null && this.ac[name] != 'undefined'){
			var v = unescape(this.ac[name]); 
			v = decodeURIComponent(v);
			return v;
		}
		return null;
	};
	
	this.remove = function(name)
	{
		this.set(name, null, -1);
		this.ac[name] = null;
	};
	
	this.has = function(name)
	{
		name = this.getRawName(name);
		if(this.ac[name] == null || this.ac[name] == 'undefined'){
			return false;
		}
		return true;
	};
	
	this.getRawName = function(name)
	{
		return this.prefix + name;
	};
};
var cookie = new CookieClass();

Array.prototype.indexOf = function(value){
	for(var i = 0; i < this.length; i++){
		if(this[i] == value){
			return i;
		}
	}	
	return -1;
}

function FlyDiv(config)
{	
	var backgroundDiv; //背景DIV
	var winDiv; //内容DIV
	var _scrollTask;
	
	//如果背景Div存在，返回
	if(backgroundDiv){
		return;
	}
	
	var randomId = parseInt(Math.random() * 10000);
	var winDivId = 'flydiv-win-' + randomId;
	var contentDivId = 'flydiv-content-' + randomId;
	
	this.getId = function(){
		return winDivId;
	}
	
	this.getBodyId = function(){
		return contentDivId;
	}
	
	if(typeof(config) != 'object'){
		config = {}; 
	}
	
	config.height = (config.height != undefined ? config.height : 100);
	config.width = (config.width != undefined ? config.width : 300);	
	config.left = (config.left != undefined ? config.left : (screen.width / 2) - (config.width / 2));
	config.top = (config.top != undefined ? config.top : 170);
	config.frameClass = (config.frameClass ? config.frameClass : '');
	config.btnCloseClass = config.btnCloseClass ? config.btnCloseClass : '';
	config.btnConfirmClass = config.btnConfirmClass ? config.btnConfirmClass : '';
	//config.borderColor = (config.borderColor ? config.borderColor : '#A690A8');
	
	//是否设置半透明背景
	if(config.mask == undefined || config.mask){
		config.mask = true;
	}else{
		config.mask = false;
	}
	
	var contentHeight = config.height;
	if(config.btnClose == undefined || config.btnClose){
		config.btnClose = true;
		contentHeight -= 25;
	}else{
		config.btnClose = false;
	}
	
	if(config.btnConfirm == undefined || config.btnConfirm){
		config.btnConfirm = true;
		contentHeight -= 25;
	}else{
		config.btnConfirm = false;
	}
	
	if(config.scroll == undefined || config.scroll){
		config.scroll = true;
	}else{
		config.scroll = false;
	}
	
	
	config.onHide = typeof(config.onHide) == 'function' ? config.onHide : function(){};
	var _select = function(v)
	{
		var s = document.getElementsByTagName('select');
		for(var i = 0; i < s.length; i++){			
			s[i].style.visibility = v;
		}
	};
	
	var _background = function(){
		_select('hidden');	
		var h = document.body.clientHeight > document.documentElement.clientHeight ? document.body.clientHeight : document.documentElement.clientHeight;
		var w = screen.width - 18;
		//ie的滚动条宽度为22px
		if(window.navigator.userAgent.toLowerCase().indexOf('msie') != -1){
			w = screen.width - 22;
		}
		
		backgroundDiv = document.createElement('div');
		var bgStyle = {zIndex:10000,left:'0px', top:'0px', backgroundColor:'#cccccc', filter:'alpha(opacity=80)', opacity:0.8, paddingTop:'170px', position:'absolute', height: h + "px", width: w +"px"};
		for(var pro in bgStyle){
			backgroundDiv.style[pro] = bgStyle[pro];
		}
		document.body.appendChild(backgroundDiv);
	}
	
	this.hide = function(){
		winDiv.style.display = 'none'
		winDiv.parentNode.removeChild(winDiv);
		
		//移除背景
		if(config.mask && backgroundDiv){
			_select('visible');
			backgroundDiv.style.display = 'none'
			delete backgroundDiv;			
			backgroundDiv.parentNode.removeChild(backgroundDiv);
		}
		
		if(_scrollTask){
			clearInterval(_scrollTask);
		}
		
		config.onHide();
	};
	
	this.confirm = function()
	{
		config.onConfirm();
	}
	
	this.getX = function(){
		return config.left;
	}
	
	this.getY = function(){
		return config.top;
	}
	
	this.show = function(content){
		if(config.mask){
			_background();
		}
		
		if(content == 'undefined'){
			content = '';
		}
		
		winDiv = document.createElement('div');
		winDiv.id = winDivId;
		var winStyle = {zIndex:'10001', position:'absolute', width: config.width + "px", height:config.height + "px"};

		for(var pro in winStyle){
			winDiv.style[pro] = winStyle[pro];
		}
		
		if(config.right != undefined){
			winDiv.style.right = config.right + 'px';
		}else{
			winDiv.style.left = config.left + 'px';
		}
		
		if(config.bottom != undefined){
			winDiv.style.bottom = config.bottom + 'px';
		}else{
			winDiv.style.top = config.top + 'px';
		}
		
		winDiv.className = config.frameClass;

		if(config.btnClose){
			var winTitleDiv = document.createElement('div');
			winTitleDiv.style.textAlign = 'right';
			winTitleDiv.style.height = '25px';
			
			var btnClose = document.createElement('a');
			btnClose.href = 'javascript:void(0);';
			btnClose.className = config.btnCloseClass;
			btnClose.onclick = this.hide;
			btnClose.innerHTML = '关闭';
			winTitleDiv.appendChild(btnClose);
			winDiv.appendChild(winTitleDiv);
		}
		
		var contentDiv = document.createElement('div');
		contentDiv.id = contentDivId;
		contentDiv.style.height = contentHeight+"px";
		contentDiv.innerHTML = content;
		
		winDiv.appendChild(contentDiv);
		
		if(config.btnConfirm){
			var winFooterDiv = document.createElement('div');
			winFooterDiv.style.textAlign = 'center';
			winFooterDiv.style.height = '25px';
			
			var btnConfirm = document.createElement('a');
			btnConfirm.href = 'javascript:void(0);';
			btnConfirm.className = config.btnConfirmClass;
			btnConfirm.onclick = this.hide;
			btnConfirm.innerHTML = '确定';
			winFooterDiv.appendChild(btnConfirm);
			winDiv.appendChild(winFooterDiv);
		}

		document.body.appendChild(winDiv);

		//为flyDiv绑定window.onscroll	
		var _scroll = function(){
			var scrollY = window.pageYOffset || document.documentElement.scrollTop;
			if(config.bottom){
				winDiv.style.top = document.documentElement.clientHeight - config.height - 25 + scrollY - config.bottom + 'px';
			}else{
				winDiv.style.top = config.top + scrollY + 'px';
			}
		}
		
		if(config.scroll){			
			_scrollTask = setInterval(_scroll, 200);
		}
	}
};

