(function ($) {
    $.fn.jqDrag = function (h, opt) {
        return i(this, h, 'd', opt);
    };
    $.fn.jqResize = function (h, opt) {
        return i(this, h, 'r', opt);
    };
    $.jqDnR = {
        dnr: {},
        e: 0,
        opt: null,
        drag: function (v) {
                if (M.k == 'd') {
                    var left = M.X + v.pageX - M.pX,
                        top = M.Y + v.pageY - M.pY;
                    if (OPT != null) {
                        if (OPT.lock == 'x') {
                            left = M.X;
                        } else if (OPT.lock == 'y') {
                            top = M.Y;
                        }

                        if (OPT.containment) {
                            var c = $(OPT.containment);

                            var offset = c.offset();
                            var _left = parseInt(offset.left),
                                _top = parseInt(offset.top);

                            if (E.offset().left < _left) {
                                left = _left;
                            }
                            if (E.offset().top < _top) {
                                top = offset.top;
                            }
                            if (left + M.W > _left + c.width()) {
                                left = _left + c.width() - M.W;
                            }
                            if (top + M.H > _top + c.height()) {
                                top = _top + c.height() - M.H;
                            }
                        }

                        if (OPT.dragging) OPT.dragging(v, E);
                    }
                    E.css({
                        left: left,
                        top: top
                    });

                } else {
                    var width = Math.max(v.pageX - M.pX + M.W, 0),
                        height = Math.max(v.pageY - M.pY + M.H, 0);

                    if (OPT != null) {
                        if (OPT.containment) {
                            var c = $(OPT.containment);

                            if (M.X + width > c.offset().left + c.width()) {
                                width = c.offset().left + c.width() - M.X;
                            }
                            if (M.Y + height > c.offset().top + c.height()) {
                                height = c.offset().top + c.height() - M.Y;
                            }
                        }

                        if (OPT.resizing) OPT.resizing(v, E);
                    }
                    E.css({
                        width: width,
                        height: height
                    });
                }
                return false;
            },
            stop: function (v) { /*E.css('opacity',M.o);*/
                if (OPT != null && OPT.stop) OPT.stop(v, E);
                $(document).unbind('mousemove', J.drag).unbind('mouseup', J.stop);
            }
    };

    var J = $.jqDnR,
        M = J.dnr,
        E = J.e,
        OPT = J.opt,
        i = function (e, h, k, opt) {
            return e.each(function () {
                h = (h) ? $(h, e) : e;

                h.bind('mousedown', {
                    e: e,
                    k: k
                }, function (v) {
                    var d = v.data,
                        p = {};
                    E = d.e;
                    OPT = opt ? opt : null;
                    if (OPT != null && OPT.drag) OPT.drag(v, E);
                    // attempt utilization of dimensions plugin to fix IE issues
                    if (E.css('position') != 'relative') {
                        try {
                            E.position(p);
                            if (d.k == 'd') E.css({
                                left: E.offset().left + 'px',
                                top: E.offset().top + 'px'
                            });
                        } catch (e) {}
                    }
                    M = {
                        X: p.left || f('left') || 0,
                        Y: p.top || f('top') || 0,
                        W: f('width') || E[0].scrollWidth || 0,
                        H: f('height') || E[0].scrollHeight || 0,
                        pX: v.pageX,
                        pY: v.pageY,
                        k: d.k /*,o:E.css('opacity');*/
                    };
                    /*E.css({opacity:0.8});*/
                    $(document).mousemove($.jqDnR.drag).mouseup($.jqDnR.stop);
                    return false;
                });

            });
        },

        f = function (k) {
            return parseInt(E.css(k)) || false;
        };
})(jQuery);




$(document).ready(function () {
    $('.p8label').each(function () {
        $(this).hover(function () {
            $(this).css({
                'opacity': '0.8',
                'filter': 'alpha(opacity=70)'
            });
        }, function () {
            $(this).css({
                'opacity': '0.4',
                'filter': 'alpha(opacity=50)'
            });
        }).jqResize($('div', this))
    });
});



var jumpto=true;
function showlabel_(obj,mouse,type,name){	
	if(mouse=='over'){
		obj.style.filter='Alpha(Opacity=80)';
		obj.style.cursor='hand';
		obj.title='点击修改';
	}else if(mouse=='out'){
		obj.style.filter='Alpha(Opacity=50)';
	}else if(mouse=='click'&&jumpto==true){
		var width = parseInt( $(obj).next().width() );
		var height = parseInt( $(obj).next().height() );
		if(width<30 || height<30){
			width = parseInt( $(obj).next().next().width() );
			height = parseInt( $(obj).next().next().height() );
		}

		width = parseInt(obj.style.width)==100 ? width : parseInt(obj.style.width);
		height = parseInt(obj.style.height)==30 ? height : parseInt(obj.style.height);

		layer_label_iframe(admin_url+'?type='+type+'&name='+name+'&div_width='+width+'&div_height='+height);
	}
}
//layer_label_iframe
function layer_label_iframe(url){
	layer.open({
	  type: 2,
	  title: '标签设置',
	  shadeClose: true,
	  shade: false,
	  maxmin: true, //开启最大化最小化按钮
	  area: ['60%', '80%'],
	  content: url,
	  end: function(){
		  	window.location.reload();
		}
	});
}
function ckjump_(type){
	if(type==1){
		jumpto=true;
	}else{
		jumpto=false;
	}
}
var layobj=null;
var potype=null;
var ifchange=null;
function change_po_(type,t,tag){
	ifchange=t;
	layobj=document.getElementById(tag);
	potype=type;
	change_ls_();
}

function change_ls_(){
 
	var obj=layobj;
	var type=potype;
	if(type=='up'){
		num=(parseInt(obj.style.height)-1);
		obj.style.height=num+'px';
	}else if(type=='left'){
		num=(parseInt(obj.style.width)-1);
		obj.style.width=num+'px';
	}else if(type=='down'){
		num=(parseInt(obj.style.height)+1);
		obj.style.height=num+'px';
	}else if(type=='right'){
		num=(parseInt(obj.style.width)+1);
		obj.style.width=num+'px';
	}

	if(ifchange==1){
		window.setTimeout('change_ls_()',40);
	}
}









