
(function($) {
    $.extend($.fn, {
        getCss: function(key) {
            var v = parseInt(this.css(key));
            if (isNaN(v))
                return false;
            return v;
        }
    });
    $.fn.resizable = function(opts) {
		
    	var framedoc ;
        var ps = $.extend({
            handler: null,
            min: { width: 0, height: 0 },
            max: { width: $(document).width(), height: $(document).height() },
            onResize: function() { },
            onStop: function() { }
        }, opts);
        var resize = {
            resize: function(e) { 
                var resizeData = e.data.resizeData;
                resizeData.target.css({
                    width: Math.min(Math.max(e.pageX - resizeData.offLeft + resizeData.width, resizeData.min.width), ps.max.width),
                    height: Math.min(Math.max(e.pageY - resizeData.offTop + resizeData.height, resizeData.min.height), ps.max.height)
                });
                doane(e);
            },
            stop: function(e) {
                e.data.resizeData.onStop(e);

                document.body.onselectstart = function() { return true; }
                e.data.resizeData.target.css('-moz-user-select', '');
                e.data.resizeData.target.parents(".arcarea").unbind('dblclick', resize.stop);
				// $(document).add(framedoc).unbind
                $(document).unbind('mousemove', resize.resize)
                    .unbind('mouseup', resize.stop).unbind('click', resize.stop);
                doane(e);
				$('#dragfloatbg').remove();
            }
        }
        return this.each(function() {
            var me = this;
            var handler = null;
            if (typeof ps.handler == 'undefined' || ps.handler == null)
                handler = $(me);
            else
                handler = (typeof ps.handler == 'string' ? $(ps.handler, this) : ps.handle);
	            handler.bind('mousedown', { e: me }, function(s) {
	                var target = $(s.data.e);
	                var resizeData = {
	                    width: target.width() || target.getCss('width'),
	                    height: target.height() || target.getCss('height'),
	                    offLeft: s.pageX,//pageX() 属性是鼠标指针的位置，相对于文档的左边缘。属于jquery的事件
	                    offTop: s.pageY,
	                    onResize: ps.onResize,
	                    onStop: ps.onStop,
	                    target: target,
	                    min: ps.min,
	                    max: ps.max,
						zhuobiao:$(handler).is('.dragrightbottom')?'lefttop':'righttop'
	                }
	                document.body.onselectstart = function() { return false; }
	                target.css('-moz-user-select', 'none');
	                handler.bind('mouseup', { resizeData: resizeData }, resize.stop);
	                target.parents(".arcarea").bind("dblclick", { resizeData: resizeData }, resize.stop);
	                //framedoc = handler.parent().find("iframe").get(0).contentDocument || handler.parent().find("iframe").get(0).document;
	               // http://zhidao.baidu.com/link?url=NzC4rtxdA2bwqOWco6NUuhRZLFcg1-DCap2ZdneNPFkaky872hL61P1iASDuOxtmZhI7rYKt70rnaCwvVTjh0_
				//	我试过了 document.getElementById('myf').contentWindow.document 
	               $(document).bind('mousemove', { resizeData: resizeData }, resize.resize)
	                    .bind('click', { resizeData: resizeData }, resize.stop).bind('mouseup', { resizeData: resizeData }, resize.stop);
	                doane(s);
					var zindex = 0;
					try{zindex=$.cleditor.curtopindex+10;}catch(e){zindex=10;}
					$("<div id='dragfloatbg' style='opacity:0;-moz-opacity: 0;filter:alpha(opacity=0);background:#fff;top:0;left:0;position:absolute;width:100%;height:100%;z-index:"+zindex+";'></div>").appendTo('body');
					
	            });
        });
    }
    
    
    $.fn.redrag = function(frontelem,backelem,opts) {
        var ps = $.extend({
			moveDirection:'Horizontal',
            min: 0,
            max: $(document).height(),
            onResize: function() { },
            onStop: function() { }
        }, opts);
        var dragmove = {
        	dragmove: function(e) { 
                var w,h,resizeData = e.data.resizeData;
if(resizeData.zhuobiao=='lefttop'){
                w = Math.min(Math.max(e.pageX - resizeData.offLeft + resizeData.width, resizeData.min.width), ps.max.width);
                h = Math.min(Math.max(e.pageY - resizeData.offTop + resizeData.height, resizeData.min.height), ps.max.height);

}else if(resizeData.zhuobiao=='righttop'){
                w = Math.min(Math.max(resizeData.offLeft - e.pageX + resizeData.width, resizeData.min.width), ps.max.width);
                h = Math.min(Math.max(e.pageY - resizeData.offTop + resizeData.height, resizeData.min.height), ps.max.height);

}else if(resizeData.zhuobiao=='leftbottom'){
                w = Math.min(Math.max(e.pageX - resizeData.offLeft + resizeData.width, resizeData.min.width), ps.max.width);
                h = Math.min(Math.max(resizeData.offTop - e.pageY + resizeData.height, resizeData.min.height), ps.max.height);

}else if(resizeData.zhuobiao=='rightbottom'){
                w = Math.min(Math.max(resizeData.offLeft - e.pageX + resizeData.width, resizeData.min.width), ps.max.width);
                h = Math.min(Math.max(resizeData.offTop - e.pageY + resizeData.height, resizeData.min.height), ps.max.height);

}
                resizeData.target.css({
                    width: w,
                    height: h
                });
                resizeData.onResize(e);
            },
            stop: function(e) {
                e.data.resizeData.onStop(e);

                document.body.onselectstart = function() { return true; }
                e.data.resizeData.target.css('-moz-user-select', '');


                $(document).unbind('mousemove', resize.resize)
                    .unbind('mouseup', resize.stop).unbind('click', resize.stop);
            }
        }

        this.bind('mousedown',  function(s) {
                var resizeData = {
                    offLeft: s.pageX,//pageX() 属性是鼠标指针的位置，相对于文档的左边缘。属于jquery的事件
                    offTop: s.pageY,
                    onResize: ps.onResize,
                    onStop: ps.onStop,
                    min: ps.min,
                    max: ps.max
                }
                document.body.onselectstart = function() { return false; }
                $(frontelem).add(backelem).css('-moz-user-select', 'none');
                this.bind('mouseup', { resizeData: resizeData }, dragmove.stop);
                $(document).bind('mousemove', { resizeData: resizeData }, dragmove.resize)
                    .bind('click', { resizeData: resizeData }, dragmove.stop).bind('mouseup', { resizeData: resizeData }, dragmove.stop);
				//$("<div id='dragfloatbg' class='dragfloatbg'></div>").appendTo(document);
            });
    return this;
    }
})(jQuery); 

var Class = function (name, base, members, staticMembers) {
	var klass = eval('(function(){ return function(){ this.' + name + '.apply(this, arguments);}})()');
	var prototype = klass.prototype;
	if (base) {
		extend(prototype, base.prototype);
	}
	extend(prototype, members);
	extend(klass, staticMembers);
	function extend(obj, exed) {
		for (var i in exed) {
			obj[i] = exed[i];
		}
		return obj
	}
	window[name] = klass;
	if (klass[name])//构造函数
		klass[name]();
	return klass;
};
//var $ = function (o) { return typeof o == 'object' ? o : document.getElementById(o); };
function draginit(area){	
	$("div.textareacontainer",area).each(function(){
		if($(this).attr("adddrag")!=1){
			$(this).get(0).callback=savedrag;
			new Drag($(this).get(0));
			
		}
	});	
}
//此算法假设子节点只出现在back节点里面，未考虑出现在front
Class('straightDrag', null, {
	straightDrag: function (direction,dragtag,front,back,child,container) {	//相当于构造函数，被new后，可用this.startDrag调用兄弟函数，但不可调用第四个参数，即{}里的元素，因为它是高一级的
	//$(dragtag).attr("adddrag",1);//加上标记
	
		var self = this;
		this.dragicon=$("#"+dragtag).get(0);
		this.clientXY=direction;
		this.frontdivid=front;
		this.backdivid=back;
		this.childid=child;
		this.parentdivid=container;
		//document.getElementById("textarea_"+this.dragicon.id);
		var addsize=0;
		if(this.childid) {
			if(this.clientXY=="clientY")
			addsize=parseInt($("#"+this.backdivid).attr("clientHeight"))-parseInt($("#"+this.childid).attr("clientHeight"))+10;
			else addsize=parseInt($("#"+this.backdivid).attr("clientWidth"))-parseInt($("#"+this.childid).attr("clientWidth"))+10;
		
		}
		this.__oStartOffset__ = { x: 0, y: 0 };
		this.__oMinwidthandMinheight__ = { x: addsize+600, y: addsize+24 };
		var ev = '';
		this.fnOnMouseUp = function (ev) {
			ev = window.event || ev;
			self.stopDrag(ev);
		};
		this.fnOnMouseMove = function (ev) {
			ev = window.event || ev;                  
			self.doDrag(ev);
		};
		this.dragicon.onmousedown = function (ev) { 
			ev = window.event || ev;
			self.startDrag(ev);
			return false;
		};
	},
	startDrag: function (e) {
		this.__oStartOffset__.y = e.clientY;
		this.__oStartOffset__.x = e.clientX;

		doane(e);
		document.body.onselectstart = function() { return false; }
		$("#"+this.frontdivid).add("#"+this.backdivid).css('-moz-user-select', 'none');
		if (this.dragicon.setCapture) {
			
			this.dragicon.onmouseup = this.fnOnMouseUp;
			this.dragicon.onmousemove = this.fnOnMouseMove;
			document.onmouseup = this.fnOnMouseUp;
			this.dragicon.setCapture();
		}
		else {
			document.addEventListener("mouseup", this.fnOnMouseUp, true);
			document.addEventListener("mousemove", this.fnOnMouseMove, true);
			window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
			//以下三个语句为新加，普通的不需要
			if(document.menufra && document.menufra.document){
			document.menufra.document.addEventListener("mouseup", this.fnOnMouseUp, true);
			document.menufra.document.addEventListener("mousemove", this.fnOnMouseMove, true);
			document.menufra.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
			}
		}

	},
	stopDrag: function (e) {
		if (this.dragicon.releaseCapture) {
			
			this.dragicon.onmouseup = null;
			this.dragicon.onmousemove = null;
			document.onmouseup = null;
			this.dragicon.releaseCapture();
		}
		else {
			document.removeEventListener("mouseup", this.fnOnMouseUp, true);
			document.removeEventListener("mousemove", this.fnOnMouseMove, true);
			window.releaseEvents(Event.MOUSE_MOVE | Event.MOUSE_UP);
			//以下三个语句为新加，普通的不需要
			if(document.menufra && document.menufra.document){
			document.menufra.document.removeEventListener("mouseup", this.fnOnMouseUp, true);
			document.menufra.document.removeEventListener("mousemove", this.fnOnMouseMove, true);
			document.menufra.releaseEvents(Event.MOUSE_MOVE | Event.MOUSE_UP);
		}}



		doane(e);
		document.body.onselectstart = function() { return true; }
                $("#"+this.frontdivid).add("#"+this.backdivid).css('-moz-user-select', '');
		
		
	},
	doDrag: function (e) {	doane(e);	
		if(this.clientXY=="clientY"){
			var y = e.clientY;
			this.addheight=y - this.__oStartOffset__.y;
			this.tianjiaheight=(parseInt($("#"+this.frontdivid).attr("clientHeight"))+this.addheight);
			this.jianshaoheight=(parseInt($("#"+this.backdivid).attr("clientHeight"))-this.addheight);
			
			
			if(this.tianjiaheight>2 &&this.jianshaoheight>this.__oMinwidthandMinheight__.y ){
				$("#"+this.frontdivid).css("height",this.tianjiaheight+"px");		
				$("#"+this.backdivid).css("height",this.jianshaoheight+"px");
				this.__oStartOffset__.y=e.clientY;
				if($("#"+this.childid).length>0) {
					$("#"+this.childid).css("height",(parseInt($("#"+this.childid).attr("clientHeight"))-this.addheight)+"px");
				}
			}
		}else{
			var x = e.clientX;
			this.addwidth=x - this.__oStartOffset__.x;

			this.tianjiawidth=(parseInt($("#"+this.frontdivid).attr("clientWidth"))+this.addwidth);
			this.jianshaowidth=(parseInt($("#"+this.backdivid).attr("clientWidth"))-this.addwidth);

			if(this.tianjiawidth>160 &&this.jianshaowidth>this.__oMinwidthandMinheight__.x ){
				$("#"+this.frontdivid).css("width",this.tianjiawidth+"px");
				$("#"+this.backdivid).css("width",this.jianshaowidth+"px");
				if($("#"+this.childid).length>0) $("#"+this.childid).css("width",(parseInt($("#"+this.childid).attr("clientWidth"))-this.addwidth)+"px");
				this.__oStartOffset__.x=e.clientX;
			}
		}
		
		


	/*
	$("div#newinputbox").attr("clientHeight",$("div#newinputbox").attr("clientHeight")+addheight);
		$("div#editinputbox").attr("clientHeight",$("div#editinputbox").attr("clientHeight")-addheight);


		if (this.targetwidth < this.__oMinwidthandMinheight__.x) {
			this.targetwidth = this.__oMinwidthandMinheight__.x;
		}
		if (this.targetwidth > maxx) {
			this.targetwidth = maxx;
		}
		*/
		
	}
}, {});

function PagingBar(config){
	this.instancename='new_coreply_pageing';
	this.contentbox;
	//当前分页标签产生的HTML代码渲染的目标对象
	this.pagebar;
	//总记录数
	this.total=0;
	//一共显示多少页
	this.showpages=7;
	
	//每页记录数
	this.size=20;	
	for(var i in config){
		this[i]=config[i];
	}
	
	this.init=function(){
		this.pagebar=(typeof this.pagebar==="string")?document.getElementById(this.pagebar):this.pagebar;
		this.contentbox=(typeof this.contentbox==="string")?document.getElementById(this.contentbox):this.contentbox;
	}
	//this.init();
	//
	this.pagebar='';
	this.createHref=function(html,index,curpage){
		var href = document.createElement("A");
		href.href="javascript:void(0);";
		href.innerText=html;
		if(curpage===index){
			href.className="pageselected";
		}
		href.onclick=(function(paging,ind){
			return function(){
				//paging.current=ind;
				paging.html(ind);
				
			}
		})(this,index);
		return href;
	}
	this.createHreftxt=function(html,index,curpage){
		var a_class='';
		if(curpage===index){
			a_class=' class="pageselected" '
		}
		return '<a'+a_class+' href="javascript:void(0);" onclick="'+this.instancename+'.html(this,'+index+');">'+html+'</a>';
	}
	
	this.contentchange=function($container,start,end){
		$container.children().hide();
		$container.children("div:lt("+end+"):gt("+start+")").show();
	}
	
	
	this.html=function(butt,curpage,total){//butt要么为空，要么为按钮，如this
		this.total=total?total:this.total;
		if(!curpage)curpage=1;
		this.pagebar='';
		if(butt){
			this.pagebar=$(butt).closest(".itemtoolbar").get(0);
			this.pagebar.innerHTML="";
			var start=(curpage-1)*this.size;
			var end=start+this.size-1;
			this.contentchange($(butt).closest(".fieldlable1").next(),start,end);
		}
		
		var totalPage =Math.ceil(this.total/this.size);
		if(curpage<1)curpage=1;
		if(curpage>totalPage)curpage=totalPage;
		//var text=document.createTextNode("共"+this.total+"条 每页"+this.size+"条 共"+totalPage+"页");
		//text.appendData(" 当前第"+curpage+"页"+((curpage-1)*this.size+1)+"-"+(curpage*this.size)+" 条 ");
		//bartext="共"+this.total+"条 每页"+this.size+"条 共"+totalPage+"页"+" 当前第"+curpage+"页"+((curpage-1)*this.size+1)+"-"+(curpage*this.size)+" 条 ";
		
		var bartext=curpage+"/"+totalPage+"页，共"+this.total+"条 ";
		if(totalPage>1){
			if(curpage>1)
			{
				bartext+=this.createHreftxt("首页",1,curpage)+this.createHreftxt("上一页",curpage-1,curpage);
			}else{bartext+="首页 上一页" ;
			}
			var m=(this.showpages%2===0?this.showpages/2:(this.showpages-1)/2+1);
			var start;
			var end;
			if((curpage<= m) || totalPage-curpage <= m)
			{	
				var t=totalPage<this.showpages?0:totalPage-this.showpages;
				var start =curpage<=m?0:t;
				var end = start+this.showpages>=totalPage?totalPage:start+this.showpages;
			}else{
				var start =curpage-m;
				var end = start+this.showpages;
			}
			for(var i=start+1;i<=end;i++){
					bartext+=this.createHreftxt(i,i,curpage);
				}
			
			if(curpage<totalPage){
				bartext+=this.createHreftxt("下一页",curpage+1,curpage)+ this.createHreftxt("尾页",totalPage,curpage);
			}else{
				bartext+="下一页 尾页";
			}
		}
		if(this.pagebar){$(this.pagebar).append(bartext);
			//this.pagebar.appendChild(document.createTextNode(bartext));
			return true;
		}
		else return bartext;
	}

}


//好的pagebar样式 http://flaviusmatis.github.io/simplePagination.js/#page-1  http://www.tripwiremagazine.com/2012/10/jquery-pagination-plugins.html


