//fiximage begin
function fix_to_array(obj){
	var result=[];
	for(var i=0,l=obj.length;i<l;i++){
		result.push(obj[i]);
	}
	return result;
}
function fiximage(){
	var max=600;
	imgs = document.getElementsByTagName('img');
	for(i=0;i<imgs.length;i++){
	w=imgs[i].width;h=imgs[i].height;
	if(w>max) {imgs[i].width=max;imgs[i].height=h/(w/max);}
    }
}
window.onload = function (){
	tooltip.init();
	fiximage();
	
}
//fiximage end
//qtip begin
var qtipTag = ["a","img","input"];
var qtipX = -30;
var qtipY = 25;
tooltip = {
	name : "qtip",
	offsetX : qtipX,
	offsetY : qtipY,
	tip : null
}
tooltip.init = function (){
	var tipNameSpaceURI = "http://www.w3.org/1999/xhtml";
	if(!tipContainerID){var tipContainerID = "qtip";}
	var tipContainer = document.getElementById(tipContainerID);
	if(!tipContainer){
		tipContainer = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		tipContainer.setAttribute("id", tipContainerID);
		document.getElementsByTagName("body").item(0).appendChild(tipContainer);
	}
	if (!document.getElementById) return;
	this.tip = document.getElementById (this.name);
	if (this.tip) document.onmousemove = function (evt) {tooltip.move (evt)};
	var a, sTitle;
	var anchors;
	if(typeof qtipTag=='str')  anchors = document.getElementsByTagName (qtipTag);
	else{
		anchors=[];
		for(var i=0;i<qtipTag.length;i++){
			anchors=anchors.concat(fix_to_array(document.getElementsByTagName (qtipTag[i])));
		}
	}
	for (var i = 0; i < anchors.length; i ++){
		a = anchors[i];
		sTitle = a.getAttribute("title");
		if(sTitle){
			sTitle=sTitle.replace(/\/{2}/g,'<br />');
			a.setAttribute("tiptitle", sTitle);
			a.removeAttribute("title");
			a.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
			a.onmouseout = function() {tooltip.hide()};
		}
	}
}
tooltip.move = function (evt){
	var x=0, y=0;
	if (document.all){//IE
		x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
		y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		x += window.event.clientX;
		y += window.event.clientY;
	}else{//Good Browsers
		x = evt.pageX;
		y = evt.pageY;
	}
	this.tip.style.left = (x + this.offsetX) + "px";
	this.tip.style.top = (y + this.offsetY) + "px";
}
tooltip.show = function (text){
	if (!this.tip) return;
	this.tip.innerHTML = text;
	this.tip.style.display = "block";
}
tooltip.hide = function (){
	if (!this.tip) return;
	this.tip.innerHTML = "";
	this.tip.style.display = "none";
}
//qtip end


//Run code & Copy code Begin
function htmlencode(str){

	return str.replace(/<br\s?[\/]?>/i,'\n').replace(/<[^>]+>/g,'').replace(/&amp;/g,'&').replace(/&quot;/g,'"').replace(/&#039;/g,"'").replace(/&lt;/g,'<').replace(/&gt;/g,'>');

}
function runcode_open_new(element){
	var code = htmlencode(element.innerHTML);
	
var win = window.open("", "", "");
	win.opener = null;
	win.document.write(code);
	win.document.close();
}

function insertAfter(newEl, targetEl){

	var parentEl = targetEl.parentNode;

	
if(parentEl.lastChild == targetEl)
{
		parentEl.appendChild(newEl);

	}else
{
		parentEl.insertBefore(newEl,targetEl.nextSibling);

	}

}




function runcode_copy(element){
	var codeobj = element;
	var meintext = htmlencode(element.innerHTML);
	try {
		if (window.clipboardData){
			window.clipboardData.setData("Text", meintext);
		}
		else if (window.netscape){
			netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
	  		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
			if (!clip) return;
	  		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
			if (!trans) return;
	  		trans.addDataFlavor('text/unicode');
	  		var str = new Object();
			var len = new Object();
			var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
			var copytext=meintext;
			str.data=copytext;
			trans.setTransferData("text/unicode",str,copytext.length*2);
	  		var clipid=Components.interfaces.nsIClipboard;
	  		if (!clip) return false;
	  		clip.setData(trans,null,clipid.kGlobalClipboard);
		}
	}
	catch(e){
		alert('因为安全策略的原因，自动Copy功能已被您的浏览器禁止，请手动Copy代码。');
		codeobj.focus();
	}
codeobj.select();
return false;
}




function bind_code(){

	var elements=document.getElementsByTagName('pre');

	var tmp,rel;

	for(var i=0,l=elements.length;i<l;i++){
		tmp=elements[i];

		rel=tmp.rel || tmp.getAttribute('rel');
		if(rel=='code'){
			var $runbutton=document.createElement('div');
			$runbutton.className="runbutton";

			var _=document.createElement('button');

			_.onclick=function(){
runcode_open_new(tmp);
}

			_.innerHTML='运行代码';

			
$runbutton.appendChild(_);

			var $_=document.createElement('button');

			$_.onclick=function(){
runcode_copy(tmp);
}

			$_.innerHTML='复制代码';

			$runbutton.appendChild($_);insertAfter($runbutton,tmp);

		}

	}

}



var last_onload=window.onload;


window.onload=function(){
	if(last_onload) {
		last_onload();
	}
	bind_code();
}
//Run code & Copy code end
//Change CSS begin
var Cookie={
    test:function(){
        if(this.read('__browse')=='1') return 1;
        else{
			 return 0;
        }
    },
	set:function(name,value,time,path,domain){
		var cid=name+"="+encodeURIComponent(value);
		if(time){
			var end=Date.parse(new Date())+time*1000;
			cid +="; expires="+new Date(end).toUTCString();
		}
		if(path){
			cid +="; path="+path;
		}
		if(domain){
			cid +="; domain="+domain;
		}
		document.cookie=cid;
	},
	read:function(name){
		var re=new RegExp("(?:; )?"+name+"=([^;]*);?");
		if(re.test(document.cookie)){
			return decodeURIComponent(RegExp["$1"]);
		}else return null;
	},
	del:function(name,path,domain){
		this.set(name,'',-999999,path?path:'',domain?domain:'');
	}
}

var style_file=Cookie.read("style");


style_file=style_file ? style_file:'templates/'+(_style?_style:'')+'/style.css';

if(!_style){

	alert("模板未定义");

}


document.write('<link href="'+style_file+'" rel="stylesheet" type="text/css" media="all" />');

var $_es=window.onload;
window.onload=function(){
	if($_es) $_es();
	var s=document.getElementById('change_css');
	s.value=style_file;
	s.style.display='inline';
	s.onchange=function(){
		Cookie.set('style',this.value);

		var $links=document.getElementsByTagName('link');
	
	var tmp;

		for(var _i=0,_l=$links.length;_i<_l;_i++){
			tmp=$links[_i];

			if(tmp.rel=='stylesheet' && tmp.type=='text/css'){
				tmp.href=this.value;

				break;

			}

		}
	}
}
//Change CSS end



