/*
/* themes/Maxthon/js/maxthon.js 
/*************************************
Site:    http://leotheme.cn/
Version: 3.0
Date:    2008/11/24
Author:  Await(等待)
Email:   yltfy2008@gmail.com
**************************************/

/*侧边栏tab
---------------------------------------------------------------------*/
function setTab(name,cursel,n){
	for(i=1;i<=n;i++){
		var menu=document.getElementById(name+i);
		var con=document.getElementById("con_"+name+"_"+i);
			 menu.className=i==cursel?"current_sidebar":"";
			 con.style.display=i==cursel?"block":"none";
		}
}

/*样式化拖拽
---------------------------------------------------------------------*/
var isIE = (document.all) ? true : false;

var maxthonID = function (id) {
	return "string" == typeof id ? document.getElementById(id) : id;
};

var Class = {
	create: function() {
		return function() { this.initialize.apply(this, arguments); }
	}
}

var Extend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
}

var Bind = function(object, fun) {
	return function() {
		return fun.apply(object, arguments);
	}
}

var BindAsEventListener = function(object, fun) {
	return function(event) {
		return fun.call(object, (event || window.event));
	}
}

var CurrentStyle = function(element){
	return element.currentStyle || document.defaultView.getComputedStyle(element, null);
}

function addEventHandler(oTarget, sEventType, fnHandler) {
	if (oTarget.addEventListener) {
		oTarget.addEventListener(sEventType, fnHandler, false);
	} else if (oTarget.attachEvent) {
		oTarget.attachEvent("on" + sEventType, fnHandler);
	} else {
		oTarget["on" + sEventType] = fnHandler;
	}
};

function removeEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.removeEventListener) {
        oTarget.removeEventListener(sEventType, fnHandler, false);
    } else if (oTarget.detachEvent) {
        oTarget.detachEvent("on" + sEventType, fnHandler);
    } else { 
        oTarget["on" + sEventType] = null;
    }
};

//拖放程序
var Drag = Class.create();
Drag.prototype = {
  //拖放对象
  initialize: function(drag, options) {
	this.Drag = maxthonID(drag);//拖放对象
	this._x = this._y = 0;//记录鼠标相对拖放对象的位置
	this._marginLeft = this._marginTop = 0;//记录margin
	//事件对象(用于绑定移除事件)
	this._fM = BindAsEventListener(this, this.Move);
	this._fS = Bind(this, this.Stop);
	
	this.SetOptions(options);
	
	this.Limit = !!this.options.Limit;
	this.mxLeft = parseInt(this.options.mxLeft);
	this.mxRight = parseInt(this.options.mxRight);
	this.mxTop = parseInt(this.options.mxTop);
	this.mxBottom = parseInt(this.options.mxBottom);
	
	this.LockX = !!this.options.LockX;
	this.LockY = !!this.options.LockY;
	this.Lock = !!this.options.Lock;
	
	this.onStart = this.options.onStart;
	this.onMove = this.options.onMove;
	this.onStop = this.options.onStop;
	
	this._Handle = maxthonID(this.options.Handle) || this.Drag;
	this._mxContainer = maxthonID(this.options.mxContainer) || null;
	
	this.Drag.style.position = "fixed";
	//透明
	if(isIE && !!this.options.Transparent){
		//填充拖放对象
		with(this._Handle.appendChild(document.createElement("div")).style){
			width = height = "100%"; backgroundColor = "#fff"; filter = "alpha(opacity:0)";
		}
	}
	//修正范围
	this.Repair();
	addEventHandler(this._Handle, "mousedown", BindAsEventListener(this, this.Start));
  },
  //设置默认属性
  SetOptions: function(options) {
	this.options = {//默认值
		Handle:			"",//设置触发对象（不设置则使用拖放对象）
		Limit:			false,//是否设置范围限制(为true时下面参数有用,可以是负数)
		mxLeft:			0,//左边限制
		mxRight:		9999,//右边限制
		mxTop:			0,//上边限制
		mxBottom:		9999,//下边限制
		mxContainer:	"",//指定限制在容器内
		LockX:			false,//是否锁定水平方向拖放
		LockY:			false,//是否锁定垂直方向拖放
		Lock:			false,//是否锁定
		Transparent:	false,//是否透明
		onStart:		function(){},//开始移动时执行
		onMove:			function(){},//移动时执行
		onStop:			function(){}//结束移动时执行
	};
	Extend(this.options, options || {});
  },
  //准备拖动
  Start: function(oEvent) {
	if(this.Lock){ return; }
	this.Repair();
	//记录鼠标相对拖放对象的位置
	this._x = oEvent.clientX - this.Drag.offsetLeft;
	this._y = oEvent.clientY - this.Drag.offsetTop;
	//记录margin
	this._marginLeft = parseInt(CurrentStyle(this.Drag).marginLeft) || 0;
	this._marginTop = parseInt(CurrentStyle(this.Drag).marginTop) || 0;
	//mousemove时移动 mouseup时停止
	addEventHandler(document, "mousemove", this._fM);
	addEventHandler(document, "mouseup", this._fS);
	if(isIE){
		//焦点丢失
		addEventHandler(this._Handle, "losecapture", this._fS);
		//设置鼠标捕获
		this._Handle.setCapture();
	}else{
		//焦点丢失
		addEventHandler(window, "blur", this._fS);
		//阻止默认动作
		oEvent.preventDefault();
	};
	//附加程序
	this.onStart();
  },
  //修正范围
  Repair: function() {
	if(this.Limit){
		//修正错误范围参数
		this.mxRight = Math.max(this.mxRight, this.mxLeft + this.Drag.offsetWidth);
		this.mxBottom = Math.max(this.mxBottom, this.mxTop + this.Drag.offsetHeight);
		//如果有容器必须设置position为relative来相对定位，并在获取offset之前设置
		!this._mxContainer || CurrentStyle(this._mxContainer).position == "relative" || (this._mxContainer.style.position = "relative");
	}
  },
  //拖动
  Move: function(oEvent) {
	//判断是否锁定
	if(this.Lock){ this.Stop(); return; };
	//清除选择
	window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
	//设置移动参数
	var iLeft = oEvent.clientX - this._x, iTop = oEvent.clientY - this._y;
	//设置范围限制
	if(this.Limit){
		//设置范围参数
		var mxLeft = this.mxLeft, mxRight = this.mxRight, mxTop = this.mxTop, mxBottom = this.mxBottom;
		//如果设置了容器，再修正范围参数
		if(!!this._mxContainer){
			mxLeft = Math.max(mxLeft, 0);
			mxTop = Math.max(mxTop, 0);
			mxRight = Math.min(mxRight, this._mxContainer.clientWidth);
			mxBottom = Math.min(mxBottom, this._mxContainer.clientHeight);
		};
		//修正移动参数
		iLeft = Math.max(Math.min(iLeft, mxRight - this.Drag.offsetWidth), mxLeft);
		iTop = Math.max(Math.min(iTop, mxBottom - this.Drag.offsetHeight), mxTop);
	}
	//设置位置，并修正margin
	if(!this.LockX){ this.Drag.style.left = iLeft - this._marginLeft + "px"; }
	if(!this.LockY){ this.Drag.style.top = iTop - this._marginTop + "px"; }
	//附加程序
	this.onMove();
  },
  //停止拖动
  Stop: function() {
	//移除事件
	removeEventHandler(document, "mousemove", this._fM);
	removeEventHandler(document, "mouseup", this._fS);
	if(isIE){
		removeEventHandler(this._Handle, "losecapture", this._fS);
		this._Handle.releaseCapture();
	}else{
		removeEventHandler(window, "blur", this._fS);
	};
	//附加程序
	this.onStop();
  }
};

/*JQuery应用
--------------------------------------------------------------------------*/

// 显示隐藏侧边栏
function showHideSidebar() {
		var w1=jQuery("#content").width();
			if (w1 != 524) {
				jQuery("#content").width(524);
				jQuery(".sidebar").show();
				jQuery(".post").removeClass("postshow");
			}else{
				jQuery("#content").width(768);
				jQuery(".sidebar").hide();
				jQuery(".post").addClass("postshow");
		}
}


//构造弹出消息框
var showbox=true; //初始化状态
	function simpletips(content,width,height,cssName) {
		if(showbox==true) {
			var simpletips_html = new String;
				
		}
	}
	function message(title,content,width,height,cssName){
		if(showbox==true) { //构造弹出信息窗口
		   var messagebox_html=new String;
				messagebox_html="<div id=\"messageboxbg\" style=\"height:"+jQuery(document).height()+"px;filter:alpha(opacity=0);opacity:0;\"></div>";
				messagebox_html+="<div id=\"messagebox\" class=\"messagebox\">";
				messagebox_html+="<div id=\"messagetitle\" class=\"m_top\"><span class=\"m_ltop\"></span><span class=\"m_rtop\"></span><div class=\"title\"><p></p><span></span></div></div>";
				messagebox_html+="<div id=\"messageconContainer\" class=\"m_con\"><div class=\"m_con2\">";
				messagebox_html+="<div class=\"content\"><span class=\"icon\"></span></div>";
				messagebox_html+="</div></div>";
				messagebox_html+="<div class=\"m_bottom\"><span class=\"m_lbottom\"></span><p class=\"m_cbottom\"><span class=\"enter\"></span><span class=\"esc\"></span></p><span class=\"m_rbottom\"></span></div>";
				messagebox_html+="</div>";
				jQuery("body").append(messagebox_html);//将信息窗口附加到BODY中
				showbox=false;
		}

		jQuery("#messagebox .title span,.esc").click(function(){ //设定关闭信息窗口的按钮
		jQuery("#messageboxbg").hide();
		jQuery("#messagebox").fadeOut(); 
		});

	jQuery("#messagebox .title p").html(title); //将标题附加到窗口
	contentType=content.substring(0,content.indexOf(":"));
	content=content.substring(content.indexOf(":")+1,content.length);
	switch(contentType){//获取窗口的内容。这里提供了4种方式。"url"|"text"|"id"|"iframe"
		case "url":
			var content_array=content.split("?");
				jQuery("#messagebox .content").ajaxStart(function(){
				jQuery(this).html("<img src='/wp-content/themes/Maxthon/images/loading.gif' />");
			});
		jQuery.ajax({
		type:content_array[0],
		url:content_array[1],
		data:content_array[2],
		error:function(){
			jQuery("#messagebox .content").html("<p class='messagebox_error'>没有加载任何数据...</p>");
		},
		success:function(html){
			jQuery("#messagebox .content").html(html);
			}
		});
		break;
		case "text":
		jQuery("#messagebox .content").html(content);
		break;
		case "id":
		jQuery("#messagebox .content").html(jQuery("#"+content+"").html());
		break;
		case "iframe":
		jQuery("#messagebox .content").ajaxStart(function(){
				jQuery(this).html("<img src='/wp-content/themes/Maxthon/images/loading.gif' />");
			});
		jQuery.ajax({
		error:function(){
			jQuery("#messagebox .content").html("<p class='messagebox_error'>没有加载任何数据...</p>");
		},
		success:function(html){
		jQuery("#messagebox .content").html("<iframe src=\""+content+"\" width=\"100%\" height=\""+(parseInt(height)-30)+"px"+"\" scrolling=\"auto\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>");
		}});
	}
	jQuery("#messageboxbg").show();
	jQuery("#messageboxbg").animate({opacity:"0.7"},"normal");//设置透明度
	jQuery("#messagebox").fadeIn("slow");
	jQuery("#messagebox").css({left:"50%",top:"50%",marginTop:-(parseInt(height)/2)+"px",marginLeft:-(parseInt(width)/2)+"px",width:width,height:height}); //让窗口垂直居中于浏览器
	jQuery("#messagebox").attr("class","messagebox "+cssName);
	jQuery("#messagebox .title,#messagebox .m_cbottom").css({width:(parseInt(width)-8)+"px"})//这里是消息框TOP-BOTTOM的宽度，因为有了两边的圆角，所以要减去这两个圆角的宽度
}
//onload...
jQuery(document).ready(function(){
	//侧边栏收缩与隐藏
	jQuery("#t_1").click(function(){
		jQuery("#c_1").animate({height:"toggle",opacity: "toggle" }); 
		jQuery(".t1").toggleClass("off");
	});
	jQuery("#t_2").click(function(){
		jQuery("#c_2").animate({height:"toggle",opacity: "toggle" }); 
		jQuery(".t2").toggleClass("off");
	});
	jQuery("#t_3").click(function(){
		jQuery("#c_3").animate({height:"toggle",opacity: "toggle" }); 
		jQuery(".t3").toggleClass("off");
	});
	jQuery("#t_4").click(function(){
		jQuery("#c_4").animate({height:"toggle",opacity: "toggle" }); 
		jQuery(".t4").toggleClass("off");
	});
	jQuery("#t_5").click(function(){
		jQuery("#c_5").animate({height:"toggle",opacity: "toggle" }); 
		jQuery(".t5").toggleClass("off");
	});
	jQuery("#t_6").click(function(){
		jQuery("#c_6").animate({height:"toggle",opacity: "toggle" }); 
		jQuery(".t6").toggleClass("off");
	});
	//去掉链接的虚线框
	jQuery("a").bind("focus",function(){ if(this.blur){  this.blur();}});

	//Ctrl+Enter快速回复
	jQuery("#comment").keydown( function(moz_ev) {
		var ev = null; 
                if (window.event){ 
                        ev = window.event; 
                }else{ 
                        ev = moz_ev; 
                } 
                if (ev != null && ev.ctrlKey && ev.keyCode == 13) { 
                        jQuery("#submit").click(); 
                } 
        });

	//给外部的链接添加标识图并在新窗口中打开
	jQuery(".entry a[href*='http://leotheme'],.entry a:has(img),.entry a[href*='#'],.entry a[href*='javascript:void(0);']").css({background: "none", margin: "0", padding: "0"}).attr("rel","external");
	jQuery(".entry a[rel!='external']").click(function(){window.open(this.href);return false;});

   //侧边栏拖拽
	var manager = new dbxManager('main'); 	//session ID [/-_a-zA-Z0-9/]
	var purple = new dbxGroup(
		'purple', 'vertical', '7','no','10','no','open','open','close','--->按住我可以拖动我哦！^_^<---','click to %toggle% this box',
		'use the arrow keys to move this box',
		', or press the enter key to %toggle% it',
		'%mytitle%  [%dbxtitle%]'
		);

	//管理AJAX
	jQuery("#wp_admin").click(function() {
		message("后台首页","iframe:http://leotheme.cn/wp-admin/","1200px","500px","");
		jQuery(".esc").addClass("addesc");
		jQuery(".title p").addClass("icon-info");
		var wp_admin = new Drag("messagebox", { mxContainer: "messageContainer", Handle: "messagetitle", Limit: true});
        return false;
	});
	jQuery("#post_new").click(function() {
		message("发表新文章","iframe:http://leotheme.cn/wp-admin/post-new.php","1200px","500px","");
		jQuery(".esc").addClass("addesc");
		jQuery(".title p").addClass("icon-info");
		var post_new = new Drag("messagebox", { mxContainer: "messageContainer", Handle: "messagetitle", Limit: true});
        return false;
	});
	jQuery("#themes_edit").click(function() {
		message("主题编缉","iframe:http://leotheme.cn/wp-admin/theme-editor.php","1200px","500px","");
		jQuery(".esc").addClass("addesc");
		jQuery(".title p").addClass("icon-info");
		var themes_edit = new Drag("messagebox", { mxContainer: "messageContainer", Handle: "messagetitle", Limit: true});
        return false;
	});

   //主题前台预览
   		jQuery(".bluesky").click(function() {
		message("Theme-BlueSky","iframe:http://leotheme.cn/?preview=1&template=bluesky&stylesheet=bluesky","1000px","500px","");
		jQuery(".esc").addClass("addesc");
		jQuery(".title p").addClass("icon-info");
		var bluesky = new Drag("messagebox", { mxContainer: "messageContainer", Handle: "messagetitle", Limit: true});
        return false;
	});
        jQuery(".maxthon").click(function() {
		message("Theme-Maxthon","iframe:http://leotheme.cn/?preview=1&template=Maxthon&stylesheet=Maxthon","1000px","500px","");
		jQuery(".esc").addClass("addesc");
		jQuery(".title p").addClass("icon-info");
		var maxthon = new Drag("messagebox", { mxContainer: "messageContainer", Handle: "messagetitle", Limit: true});
        return false;
	});
        jQuery(".shrink").click(function() {
		message("Theme-BlueSky","iframe:http://leotheme.cn/?preview=1&template=shrink&stylesheet=shrink","1000px","500px","");
		jQuery(".esc").addClass("addesc");
		jQuery(".title p").addClass("icon-info");
		var shrink = new Drag("messagebox", { mxContainer: "messageContainer", Handle: "messagetitle", Limit: true});
        return false;
	});
		jQuery(".simple-line").click(function() {
		message("Theme-simple-line","iframe:http://leotheme.cn/?preview=1&template=simple-line&stylesheet=simple-line","1000px","500px","");
		jQuery(".esc").addClass("addesc");
		jQuery(".title p").addClass("icon-info");
		var simpleline = new Drag("messagebox", { mxContainer: "messageContainer", Handle: "messagetitle", Limit: true});
        return false;
	});
	//Bookmark and Share
		jQuery("#bookmarkandshare").click(function() {
		message("Bookmark and Share","iframe:http://www.addthis.com/bookmark.php?wt=nw&pub=await&url='+encodeURIComponent(location.href)', 'addthis'","650px","520px","");
		jQuery(".esc").addClass("addesc");
		jQuery(".title p").addClass("icon-info");
		var simpleline = new Drag("messagebox", { mxContainer: "messageContainer", Handle: "messagetitle", Limit: true});
        return false;
	});

});

//All End
