﻿Object.extend(window, {addNamespace:function (ns) {
	var nsParts = ns.split(".");
	var root = window;
	for (var i = 0; i<nsParts.length; i++) {
		if (typeof root[nsParts[i]] == "undefined") {
			root[nsParts[i]] = {};
		}
		root = root[nsParts[i]];
	}
}, $:function () {
	var elements = new Array();
	for (var i = 0; i<arguments.length; i++) {
		var e = arguments[i];
		if (typeof e == 'string') {
			e = document.getElementById(e);
		}
		if (arguments.length == 1) {
			return e;
		}
		elements.push(e);
	}
	return elements;
}, gotoLogin:function () {
	self.location.href = "/passport.aspx?returnurl="+encodeURI(self.location.href);
}, RewriteUrl:function () {
	var url = "/"+arguments[0];
	if (arguments[1]) {
		var ar = arguments[1].split(",");
		var bookid;
		for (var i = 0; i<ar.length; i=i+2) {
			if (ar[i].toLowerCase() == "bookid") {
				bookid = ar[i+1];
				url = "/"+bookid+url;
			} else {
				url += "-"+ar[i]+"-"+ar[i+1];
			}
		}
	}
	url += ".shtml";
	return url;
}, Class:{create:function () {
	return function () {
		if (typeof this.initialize == "function") {
			this.initialize.apply(this, arguments);
		}
	};
}}}, false);
addNamespace("MS.Debug");
MS.Debug = {};
// has been removed to debug version of core.ashx
addNamespace("MS.Position");
Object.extend(MS.Position, {getLocation:function (ele) {
	var x = 0;
	var y = 0;
	var p;
	for (p=ele; p; p=p.offsetParent) {
		// if(p.style.position == "relative" || p.style.position == "absolute") break;
		if (p.offsetLeft) 
		{
			x += p.offsetLeft;			
		}
		if(p.offsetTop)
		{
			y += p.offsetTop;
		}
	}
	return {left:x, top:y};
}, getBounds:function (ele) {
	var offset = MS.Position.getLocation(ele);
	var width = ele.offsetWidth;
	var height = ele.offsetHeight;
	return {left:offset.left, top:offset.top, width:width, height:height};
}, setLocation:function (ele, loc) {
	ele.style.position = "absolute";
	ele.style.left = loc.left+"px";
	ele.style.top = loc.top+"px";
}, setBounds:function (ele, rect) {
	if (rect.left && rect.top) {
		MS.Position.setLocation(ele, rect);
	}
	ele.style.width = rect.width+"px";
	ele.style.height = rect.height+"px";
}}, false);
addNamespace("MS.Keys");
Object.extend(MS.Keys, {TAB:9, ESC:27, KEYUP:38, KEYDOWN:40, KEYLEFT:37, KEYRIGHT:39, SHIFT:16, CTRL:17, ALT:18, ENTER:13, getCode:function (e) {
	e = MS.getEvent(e);
	if (e != null) {
		return e.keyCode;
	}
	return -1;
}}, false);
Object.extend(MS, {setCookie:function (arg_CookieName, arg_CookieValue, arg_CookieExpireDays) {
	var todayDate = new Date();
	todayDate.setDate(todayDate.getDate()+arg_CookieExpireDays);
	document.cookie = arg_CookieName+"="+escape(arg_CookieValue)+";path=/;expires="+todayDate.toGMTString()+";";
}, getCookie:function (arg_CookieName) {
	var Found = false;
	var start, end;
	var i = 0;
	while (i<=document.cookie.length) {
		start = i;
		end = start+arg_CookieName.length;
		if (document.cookie.substring(start, end) == arg_CookieName) {
			Found = true;
			break;
		}
		i++;
	}
	if (Found == true) {
		start = end+1;
		end = document.cookie.indexOf(";", start);
		if (end<start) {
			end = document.cookie.length;
		}
		return unescape(document.cookie.substring(start, end));
	}
	return "";
}, cleanCookie:function (arg_CookieName, arg_CookieValue) {
	document.cookie = arg_CookieName+"="+escape(arg_CookieValue)+";path=/;expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}, getParameter:function (varName) {
	var query = location.search;
	if (query != null || query != "") {
		var re = new RegExp("^\\?+");
		query = query.replace(re, "");
		var qArray = query.split("&");
		var len = qArray.length;
		if (len>0) {
			for (var i = 0; i<len; i++) {
				var sArray = qArray[i].split("=", 2);
				if (sArray[0] && sArray[1] && sArray[0].toLowerCase() == varName.toLowerCase()) {
					return unescape(sArray[1]);
				}
			}
		}
	}
	return null;
}, setText:function (ele, text) {
	if (ele == null) {
		return;
	}
	if (document.all) {
		ele.innerText = text;
	} else {
		ele.textContent = text;
	}
}, getText:function (ele) {
	if (ele == null) {
		return;
	}
	if (document.all) {
		return ele.innerText;
	} else {
		return ele.textContent;
	}
}, appendHtml:function (ele, html) {
	if (ele == null) {
		return;
	}
	ele.innerHTML += html;
}, setHtml:function (ele, html) {
	if (ele == null) {
		return;
	}
	ele.innerHTML = html;
}, cancelEvent:function (e) {
	e = MS.getEvent(e);
	if (window.event) {
		e.returnValue = false;
	} else if (e) {
		e.preventDefault();
		e.stopPropagation();
	}
}, getEvent:function (e) {
	if (window.event) {
		return window.event;
	}
	if (e) {
		return e;
	}
	return null;
}, getTarget:function (e) {
	e = MS.getEvent(e);
	if (window.event) {
		return e.srcElement;
	}
	if (e) {
		return e.target;
	}
}, doObjClick:function (obj) {
	obj.click();
}}, false);
Object.extend(String.prototype, {toLength:function () {
	var re = new RegExp("[^\\x00-\\xff]", "g");
	return this.replace(re, "xx").length;
}}, false);
var StringBuilder = function () {
	this.v = [];
};
Object.extend(StringBuilder.prototype, {append:function (s) {
	this.v.push(s);
}, appendLine:function (s) {
	this.v.push(s+"\r\n");
}, clear:function () {
	this.v.clear();
}, ToString:function () {
	return this.v.join("");
}}, true);



////
var FreeReadWindow;
var FreeReaderCheckTimer;
var w=null;
function doFreeRead(strBookid)
{
	if (isNewActiveX || 1==1){

				
				if (token=="")
					token="xxxxx";
					
				//alert(token);
				var strURL="http://digiReader.digibook.cn/?bookid="+strBookid+"&token="+token;
        if (w!=null)
        {
        	try
        	{
        	w.window.location.href;
        	}catch(e){w=null;}
        	if (w!=null){
        		if (w.closed)
        				w=null;
        	}
        }
        if (w==null){
        	w=window.open(strURL,"DigiBook2007","status=0, resizable=no,toolbar=0, fullscreen=0,width=640,height=480,location=0",false);
        }else{
          if (confirm("您目前正在阅读状态，是否要关闭目前正在的阅读，继续新的阅读？"))
        	{
        	w.location.href=strURL;
         	}
        }
        w.focus();		

	}else{
		clearTimeout(FreeReaderCheckTimer)
		FreeReadWindow=window.open('/Sys/ClientFunction/goNoActivexReader.aspx?bookid='+strBookid,'FreeReadWindowX','location=no,width='+(screen.width-10)+',height='+(screen.height-60));
		if (FreeReadWindow!=null)
		{
			FreeReaderCheckTimer=setTimeout("CheckFreeReadState()",3000);
		}else
		{
			clearTimeout(FreeReaderCheckTimer)
			alert("在线阅读使用了弹出窗口，当前的阅读窗口被浏览器拦截，请关闭浏览器防止弹出窗口的功能");
		}
	}
	return false;
}

function CheckFreeReadState()
{
	var FreeReaderError=false;
	try{
		var FreeReaderTemp=FreeReadWindow.window;
	}
	catch(FreeReaderErrorE)
	{
		FreeReaderError=true;
	}

	if (!FreeReaderError)
	{
		FreeReadWindow.window.close();
		alert("浏览器出现故障，在线阅读将无法正常继续，请您关闭所有浏览器窗口后重新打开");
	}
}


window._open=window.open;
window.open=window_new_open;
function window_new_open(a,b,c)
{
	var win;
	if(c)
		win=window._open(a,b,c);
	else if(b)
		win=window._open(a,b);
	else
		win=window._open(a);

	if( win!=null&&!win.closed)
	{
		return win;
	}else{
		alert("您刚才点击的链接需要弹出一个新窗口,但是弹出窗口被浏览器拦截了,请关闭浏览器拦截弹出功能.");
	}

}
