﻿/*
 * @author dashzhao
 * @update 2008.5.30 
 * @javascript base frameWork 1.3
 */
(function(){
	var dashzhao=window.dashzhao={
		extend:function(e, methods) {if(e.type=="textarea"){return false};
	for (property in methods) { 
		e[property] = methods[property];  }
	return e;},
		  getPos: function() {
		var el=this, valueT = 0, valueL = 0;
		do {
			valueT += el.offsetTop  || 0;
			valueL += el.offsetLeft || 0;
			el = el.offsetParent;
		} while (el);
		return [valueL, valueT];
    	},
	    Height:function(){return parseInt(this.offsetHeight)||parseInt(this.clientHeight);},
		Width:function(){return parseInt(this.offsetWidth)||parseInt(this.clientWidth);},
	    wrap:function(){this.style.display = (this.getStyle().display != 'none' ? 'none' : 'block' );return this},
		getStyle:function(){return this.currentStyle||document.defaultView.getComputedStyle(this,null)||this.style},
		css: function(val,key){if(key){this.style[val] = key;return this;} else { return this.style[val];}},
		setStyle:function(s){
            var sList = s.split(";");
            for (var i=0,j; j=sList[i]; i++){
                var k = j.split(":");
                this.style[k[0]] = k[1];
            }
            return this;
        },
		hasClassName:function(Name){
			var h = false;
		this.className.split(' ').each(function(s){
			if (s == Name) {h = true}
		});
		return h;
			}, 
		addClass:function(Name){if(!this.hasClassName(this,Name)){this.className+=" "+Name};return this;},
	    removeClass:function(Name){if(this.hasClassName(Name)){var cn=this.className.replace(Name,"").replace(/(^\s*)/g, "");this.className=cn;};return this;},
			//dom; 
		nextElement:function(){
            var n = this;
            for (var i=0,n; n = n.nextSibling; i++){
                if(n.nodeType==1) return n;
            }
            return null;
        },
		previousElement:function(){
            var n = this;
            for (var i=0,n; n = n.previousSibling; i++){
                if(n.nodeType==1)return n;
            }
            return null;
        },
		moveAhead:function(){
            if (this.previousElement()){
                this.parentNode.insertBefore(this,this.previousElement());
            }
            return this;
        },
		moveNext:function(){
            var n = this.nextElement();
            if (n){
                this.parentNode.removeChild(n);
                this.parentNode.insertBefore(n,this);
            }
            return this;
        },
		appendBefore:function(e){this.insertBefore(e,this.firstChild);return this;},
		html: function(v){if(v){this.innerHTML = v;return this;}else{return this}},
		remove:function(){this.parentNode.removeChild(this);},
		empty:function(){while(this.firstChild)this.removeChild( this.firstChild );return this;},
		//html deal;
		stripTags: function(){this.innerHTML=this.innerHTML.replace(/<\/?[^>]+>/gi, '');return this},
		addEventHandler:function (e,fn) {
               if (this.addEventListener) { this.addEventListener(e, fn, false);
                 } else if (this.attachEvent) { this.attachEvent("on" + e, fn);} 
	            else { this["on" + e] = fn;}
				return this
        },
		toString : function(){return ['QQ:8127174', 'Author: dashzhao', 'Email: dashzhao@163.com','Copy:ysyuan.com'].join(' ');}
	};
	
String.prototype.trim=function(type){//"l","r","";
  switch (type)
  {
  case "l": return this.replace(/(^\s*)/g,"");
  case "r": return this.replace(/(\s*$)/g,"");
  default : return this.replace(/(^\s*)|(\s*$)/g, "");
  }
};
Array.prototype.each=function(func){
	for(var i=0,l=this.length;i<l;i++) {func(this[i],i);dashzhao.extend(this[i],dashzhao)};
    };

var $=window.$=function(e){
    var elem = typeof(e)=="string"?document.getElementById(e):e;
    if (!elem){return null}
    else{
		dashzhao.extend(elem,dashzhao);
		if(dashzhao.methods){dashzhao.extend(elem,dashzhao.methods)};
		};
    return elem;
    };
//扩展create工具
$.create = function(){
	return function(){this.init.apply(this,arguments)};
};
$.plug = {};
$.c=function(array){var nArray = [];for (var i=0,l=array.length;i<l;i++) nArray.push(array[i]);return nArray;};
$.broswer=function(){var b = navigator.userAgent.toLowerCase();
    if(/webkit/.test(b)){return "safari";}
	if(/opera/.test(b)){return "opera";}
	if(/msie/.test(b) && !/opera/.test(b)){return "msie";}
	if(/mozilla/.test(b) && !/(compatible|webkit)/.test(b)){return "mozilla";} };
$.ajax= {
    xmlPool: [],
	length:0,
    pull: function ()
    {
        for (var i = 0,l=this.xmlPool.length; i <l; i ++)
        {
            if (this.xmlPool[i].readyState == 0 || this.xmlPool[i].readyState == 4)
            {
                return this.xmlPool[i];
            }
        }
        this.xmlPool[this.xmlPool.length] = this.create();
        return this.xmlPool[this.xmlPool.length - 1];
    },
    create: function ()
    {
        if (window.XMLHttpRequest)
        {
            var XMLHttp = new XMLHttpRequest();
        }
        else
        {
           var XMLHttp = new ActiveXObject(navigator.userAgent.indexOf("MSIE 5") >=0?"Microsoft.XMLHTTP":"MSXML2.XMLHTTP")
         }          
         
      return XMLHttp;
		if (!XMLHttp) {alert('XMLHTTP create failed');return false;};
    },
	request: function (config,data,args){
	   var setTime=setTimeout(function(){if(config.ontimeout){config.ontimeout()}},5*1000);//超时处理;
		if(config.onstart){config.onstart();}//add for ajax request start;
	    config.method=config.method||"GET";
		config.async=config.async||true;
		if(!data){data=null};
        var XMLHttp = this.pull();
        with(XMLHttp)
        {
            try
            {
                // random;
                if (config.url.indexOf("?") > 0)
                {
                    config.url += "&randnum=" + Math.random();
                }
                else
                {
                    config.url += "?randnum=" + Math.random();
                }
                open(config.method, config.url, config.async);
                setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
                send(data);
                onreadystatechange = function ()
                { 
                    if (XMLHttp.readyState == 4 && ((XMLHttp.status >= 200&&XMLHttp.status< 300) || XMLHttp.status == 304))
                    {
                     config.oncomplete(XMLHttp,args);XMLHttp=null;clearTimeout(setTime);
					
                    };										
                };
				 
            }
            catch(e)
            {
                alert("数据请求出错:"+e);if(config.error){config.error();};//error;
            };
			
        };
		//alert(XMLHttp)
    }
};
$.cookies={
   read : function(n){
        var dc = "; "+document.cookie+"; ";
        var coo = dc.indexOf("; "+n+"=");
        if (coo!=-1){
            var s = dc.substring(coo+n.length+3,dc.length);
            return unescape(s.substring(0, s.indexOf("; ")));
        }else{
            return null;
        }
    },
    set : function(name,value,expires){
        var expDays = expires*24*60*60*1000;
        var expDate = new Date();
        expDate.setTime(expDate.getTime()+expDays);
        var expString = expires ? "; expires="+expDate.toGMTString() : "";
        var pathString = ";path=/";
        document.cookie = name + "=" + escape(value) + expString + pathString;
    },
    del : function(n){
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval=this.get(n);
        if(cval!=null) {document.cookie= n + "="+cval+";expires="+exp.toGMTString()};
    }
};

})();

onready = (function(){
    // create event function stack
    var load_events = [],
        load_timer,
        script,
        done,
        exec,
        old_onload,
        init = function () {
            done = true;

            // kill the timer
            clearInterval(load_timer);

            // execute each function in the stack in the order they were added
            while (exec = load_events.shift())
                exec();

            if (script) script.onreadystatechange = '';
        };

    return function (func) {
        // if the init function was already ran, just run this function now and stop
        if (done) return func();

        if (!load_events[0]) {
            // for Mozilla/Opera9
            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", init, false);

            // for Internet Explorer
            /*@cc_on @*/
            /*@if (@_win32)
                document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
                script = document.getElementById("__ie_onload");
                script.onreadystatechange = function() {
                    if (this.readyState == "complete")
                        init(); // call the onload handler
                };
            /*@end @*/

            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                load_timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState))
                        init(); // call the onload handler
                }, 10);
            }

            // for other browsers set the window.onload, but also execute the old window.onload
            old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
        }

        load_events.push(func);
    }
})();

formToStr=function(fc) {
		var i,qs="",and="",ev="";
		for(i=0;i<fc.length;i++) {
			e=fc[i]; 
			if (e.name!='') {
				if (e.type=='select-one'&&e.selectedIndex>-1) ev=e.options[e.selectedIndex].value;
				else if (e.type=='checkbox' || e.type=='radio') {
					if (e.checked==false) continue;
					ev=e.value;
				}
				else ev=e.value;
				//ev=escape(ev);
				qs+=and+e.name+'='+ev;
				and="&";
			}
		}
		return qs;
	};


//内容滚动
function ScrollText(content,btnPrevious,btnNext,autoStart,timeout,isSmoothScroll){
    this.Speed = 10;
    this.Timeout = timeout;
	this.stopscroll =false;//是否停止滚动的标志位
	this.isSmoothScroll= isSmoothScroll;//是否平滑连续滚动
    this.LineHeight = 24;//默认高度。可以在外部根据需要设置
    this.NextButton = this.$(btnNext);
    this.PreviousButton = this.$(btnPrevious);
    this.ScrollContent = this.$(content);
    this.ScrollContent.innerHTML += this.ScrollContent.innerHTML;//为了平滑滚动再加一遍

	if(this.PreviousButton){
		this.PreviousButton.onclick = this.GetFunction(this,"Previous"); 
		this.PreviousButton.onmouseover = this.GetFunction(this,"MouseOver");
		this.PreviousButton.onmouseout = this.GetFunction(this,"MouseOut");
	}
	if(this.NextButton){
		this.NextButton.onclick = this.GetFunction(this,"Next");
		this.NextButton.onmouseover = this.GetFunction(this,"MouseOver");
		this.NextButton.onmouseout = this.GetFunction(this,"MouseOut");
    }
    this.ScrollContent.onmouseover = this.GetFunction(this,"MouseOver");
    this.ScrollContent.onmouseout = this.GetFunction(this,"MouseOut");
    if(autoStart){
        this.Start();
    }
}

ScrollText.prototype = {
	$:function(element){
		return document.getElementById(element);
	},
	Previous:function(){
		this.stopscroll = true;
		this.Scroll("up");
	},
	Next:function(){
		this.stopscroll = true;
		this.Scroll("down");
	},
	Start:function(){
		if(this.isSmoothScroll){
			this.AutoScrollTimer = setInterval(this.GetFunction(this,"SmoothScroll"), this.Timeout);
		}else{		
			this.AutoScrollTimer = setInterval(this.GetFunction(this,"AutoScroll"), this.Timeout);
		}
	},
	Stop:function(){
		clearTimeout(this.AutoScrollTimer);
		this.DelayTimerStop = 0;
	},
	MouseOver:function(){	
		this.stopscroll = true;
	},
	MouseOut:function(){
		this.stopscroll = false;
	},
	AutoScroll:function(){
		if(this.stopscroll) {
			return;
		}
		this.ScrollContent.scrollTop++;
		if(parseInt(this.ScrollContent.scrollTop) % this.LineHeight != 0){
			this.ScrollTimer = setTimeout(this.GetFunction(this,"AutoScroll"), this.Speed);
		}else{
			if(parseInt(this.ScrollContent.scrollTop) >= parseInt(this.ScrollContent.scrollHeight) / 2){
				this.ScrollContent.scrollTop = 0;
			}
			clearTimeout(this.ScrollTimer);
			//this.AutoScrollTimer = setTimeout(this.GetFunction(this,"AutoScroll"), this.Timeout);
		}
	},
	SmoothScroll:function(){
		if(this.stopscroll){
			return;
		}
		this.ScrollContent.scrollTop++;
		if(parseInt(this.ScrollContent.scrollTop) >= parseInt(this.ScrollContent.scrollHeight) / 2){
			this.ScrollContent.scrollTop = 0;
		}
	},
	Scroll:function(direction){
		if(direction=="up"){
			this.ScrollContent.scrollTop--;
		}else{
			this.ScrollContent.scrollTop++;
		}
		if(parseInt(this.ScrollContent.scrollTop) >= parseInt(this.ScrollContent.scrollHeight) / 2){
			this.ScrollContent.scrollTop = 0;
		}else if(parseInt(this.ScrollContent.scrollTop)<=0){
			this.ScrollContent.scrollTop = parseInt(this.ScrollContent.scrollHeight) / 2;
		}
		if(parseInt(this.ScrollContent.scrollTop) % this.LineHeight != 0){
			this.ScrollTimer = setTimeout(this.GetFunction(this,"Scroll",direction), this.Speed);
		}
	},
	GetFunction:function(variable,method,param){
		return function(){
			variable[method](param);
		}
	}
}
//内容滚动完

/** 填充一个frame **/
function applyFrame(){
	var dom = document.getElementById("_iframe_bg");
	if(dom == null){
		var frame = document.createElement("span");
			frame.innerHTML = '<div id="_iframe_bg" style="background:none;display:none;z-index:800;left:0;position:absolute;top:0;width:100%;height:100%"><iframe id="_iframe_cont" style="filter:alpha(opacity=0);width:100%;height:100%;opacity:0;background:#333" ></iframe></div>';
		document.body.appendChild(frame);
			frame = null;
			dom = document.getElementById("_iframe_bg");
	}
	var dis = dom.style.display == "none" ? "" : "none"; 
	dom.style.display = dis;

}

function setInnerHTML(id,str){
	document.getElementById(id).innerHTML = str;
}
// op 参数说明：{url:,cls:,fn:} cls:true||false; 是否点击面板关闭层,默认是；
function showBodyMenu(op){
	var IE6 = (navigator.appVersion.indexOf('MSIE') != -1) && (parseFloat(navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE')+5,3))) <= 6 ? true : false;
	if(IE6){applyFrame();}
	var dom = document.getElementById("bodyMenu");
	if(dom == null){
		var bodyMenu = document.createElement("div");
			bodyMenu.id = "bodyMenu";
			if(!op || op.cls == true){
				bodyMenu.onclick = showBodyMenu;
			}
			bodyMenu.style.display = "none";
			document.body.appendChild(bodyMenu);
			bodyMenu = null;
			dom = document.getElementById("bodyMenu");
			$.ajax.request({
				url:typeof op != "undefined" ? op.url : "http://"+location.host+"/images/ajaxFile/zhengxingSorts.html",
				oncomplete:function(data){
					setInnerHTML("bodyMenu",data.responseText);
				}
			});
	}
	var dis = dom.style.display == "none" ? "" : "none"; 
	dom.style.display = dis;
	if(dis != "none"){
		toPageCenter(dom);
		if(IE6){scrollTop(dom);}
	}else{
		if(IE6){clearInterval(_timerBodyMenu)}
	}
	if(op && op.fn){op.fn()}
	
}
var _timerBodyMenu;
function scrollTop(id){
	var dom = typeof id == "string" ? document.getElementById(id) : id,d1=document.body,d2=document.documentElement,top = dom.offsetTop - d2.scrollTop;
	if(dom.style.display == "none"){return false}
	_timerBodyMenu = setInterval(function(){dom.style.top=parseInt(dom.style.top)+(Math.max(d1.scrollTop,d2.scrollTop)+top-parseInt(dom.style.top))*0.1+'px';},10);
};

function toPageCenter(dom){
	var IE6 = (navigator.appVersion.indexOf('MSIE') != -1) && (parseFloat(navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE')+5,3))) <= 6 ? true : false;
	var cont_w = (document.documentElement.clientWidth - dom.offsetWidth)/2;
	var cont_h = (document.documentElement.clientHeight - dom.offsetHeight)/2;
	if(IE6){
		cont_h += document.documentElement.scrollTop;
	}
	dom.style.left = cont_w + "px";
	dom.style.top = cont_h + "px";
}

function setFixed(id){
	var el = typeof id == "string" ? document.getElementById(id) : id;
	var top = el.offsetTop,left = el.offsetLeft;
	if(el.style.display == "none"){return false}
	if (!window.XMLHttpRequest && window.ActiveXObject){
		el.style.position = "absolute";document.body.style.height = "100%";document.body.style.overflow = "auto";
		var key = "*html",value = "overflow-x:auto;overflow-y:hidden;";
		var css = document.styleSheets[document.styleSheets.length-1];
			css.cssRules ? (css.insertRule(key +"{"+ value +"}", css.cssRules.length)) : (css.addRule(key ,value));
	}
	else{
		el.style.position = "fixed";
	}
	el.style.top = top +"px";el.style.left = left +"px";
};

function initHeaderSuggest(id){
	var _suggest = new suggest(id,{setResult:function(sheng,shi){
	this.$("sugg_sheng").value = sheng;this.$("sugg_shi").value = shi;
	}},
	function(){
		var left = parseInt(this.dom_cont.style.left),top = parseInt(this.dom_cont.style.top),s = this.browser();
		if(s == "firefox"){

		}else if(s == "chrome"){
			top = 14;
		}else if(s == "msie"){
			if(window.navigator.userAgent.indexOf("MSIE 6.0")>=1){

			}else{

			}
		}else{
			left = 79;
			top = 17;
		}
		this.setStyle(this.dom_cont,{left:left+"px",top:top+"px"});
	});
	return _suggest;
}







