jQuery.fn.center = function()
{
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}


function pop(id,t,w,h){

	var popUpId = id;
	var title = t;
	var width = w;
	var height = h; 
	var mWidth=width-20;
	var url = null;
	var type = null;
	var curObj = this;
	var theme = 'white';
	
	this.setUrl = function(u){
		this.url = u;
	}
	
	this.setTheme = function(opt){
		theme = opt;
	}
	
	this.frame = function(){
		var content = null;
			if (theme == 'white') {
				content = '<div style="width:' + width + 'px;"><div style="height:30px"><div class="box-header-left"></div>';
				content += '<div id="' + popUpId + 'Title" class="box-header-middle" style="width:' + mWidth + 'px;"><span style="float:left">' + title + '</span><input type="image" style="float:right; margin-top:7px;" src="frontend/img/close_box.gif" onclick="hidePopup(\'' + popUpId + '\');" /></div>';
				content += '<div class="box-header-right"></div>';
				content += '</div><div class="box-body" id="' + popUpId + 'Content" style="padding-top:10px;"></div>';
				content += '<div style="height:10px;"><div class="box-footer-left"></div>';
				content += '<div class="box-footer-middle" style="width:' + mWidth + 'px;"></div>';
				content += '<div class="box-footer-right"></div></div></div>';
			}
			else if (theme=='black_old'){
				
				mWidth -=6;
				
				content = '<div style="width:'+width+'px;overflow:hidden;">';
	    		content +='<div style="height:20px,width:'+width+'px;">';
				content +='<div class="pink-header-left"></div>';
				content+= '<div id="'+popUpId+'Title" class="pink-header-middle" style="width:'+mWidth+'px;"><span style="margin-top:2px;float:left;font-weight:bold;color:#FFFFFF">'+title+'</span><span><a href="javascript:;" style="float:right; margin-top:0px;color:#ec268c;text-decoration:none;" onclick="hidePopup(\'' + popUpId + '\');" >close</a></span></div>';
				content+= '<div class="pink-header-right"></div>';
				content+= '</div><div style="clear:both"></div>'
				content+= '<div class="pink-body" id="'+popUpId+'Content" ></div>';
				content+= '<div style="height:13px; ">'
				content+= '<div class="pink-footer-left"></div>';
				content+= '<div class="pink-footer-middle" style="width:'+mWidth+'px;"></div>';
				content+= '<div class="pink-footer-right"></div>'
				content +='</div>';
				content +='</div>';
				
			}
			else if (theme=='black'){
				
				mWidth -=6;
				
				content = '<div style="width:'+width+'px;overflow:hidden;">';
	    		content +='<div style="height:20px,width:'+width+'px;">';
				content +='<div class="pink-header-left"></div>';
				content += '<div id="'+popUpId+'Title" class="pink-header-middle" style="width:'+mWidth+'px;"><span style="margin-top:2px;float:left;font-weight:bold;">'+title+'</span></div>';
				content += '<div class="pink-header-right"></div>';
				content += '</div><div style="clear:both"></div>'
				content += '<div class="pink-body" id="'+popUpId+'Content" ></div>';
				content += '<div style="height:13px; ">'
				content += '<div class="pink-footer-left"></div>';
				content += '<div class="pink-footer-middle" style="width:'+mWidth+'px;"></div>';
				content += '<div class="pink-footer-right"></div>'
				content +='</div>';
				content +='</div>';
				
			}
			
		return content;
	
	}
 
 
 	this.setUrl = function(u,t,p){
 	 
 		url = u; 
		type = t;
		param = p;
 		
 	}
	
	this.setUp = function(){
		
		if ($('#'+popUpId)) {
			var obj = document.createElement('div');
			obj.id = popUpId;
			
			$('#main').append(obj);
			$('#' + popUpId).addClass('popup');
			$('#' + popUpId).height(height);
			$('#' + popUpId).width(width);
			
		} 
		
		 //$('#'+popUpId).draggable();
		 $('#'+popUpId).center(); 
		 /*var coords = this.getPosition();
		 alert(coords['x']);*/
		 
	}
	
	
	this.show = function(){
		 
		 if ( !$('#'+popUpId).is(':visible') ) {
				this.setUp();
				$('#'+popUpId).html(this.frame()); 
				
				//$('#'+popUpId).show();
				//showContent(popUpId);
				
				if(url!=''){
					
					this.loadContent();
					
				}			 	
		}
	}
	
	
	this.loadContent = function(){
		if(type=='get')
		$.get(url,function(data){
			$("#"+popUpId+'Content').html(data);
            $.facebox(data);
		});
		
		else if(type=='post'){
			if(typeof param != 'object')
				eval('parm={'+param+'}')
			else	
				parm = param;
			
			$.post(url,parm,function(data){
				$("#"+popUpId+'Content').html(data);
                $.facebox(data);
			});
		}
		
	}
	
	
	this.hide = function(){
		$('#'+popUpId).remove();
	}

}

function hidePopup(id){
	$('#'+id).remove();
    $(document).trigger('close.facebox');
}


function popUp(objId,url,w,t,onShowFunc,onShowFuncParm){
	
	var win = document.getElementById(objId);
	
	// ev = event || window.event;
	//divObj.innerHTML = “Mouse X:”+ev.pageX + ” Mouse Y:”+ev.pageY;
	 
	//window.event.p 
	 
	var width = w? w:320;
	var title = t? t:'';
	var mWidth=width-20;
	
	//var winH = 330;
	var winX = ( screen.availWidth - width) / 2;
	var winY = window.pageYOffset - (screen.availHeight/2)+500; 
	
	var content = '<div style="width:'+width+'px;"><div style="height:30px"><div class="box-header-left"></div>';
			content += '<div id="'+objId+'Title" class="box-header-middle" style="width:'+mWidth+'px;"><span style="float:left">'+title+'</span><input type="image" style="float:right; margin-top:7px;" src="images/close_box.gif" onclick="popDown(\''+objId+'\')" /></div>';
			content += '<div class="box-header-right"></div>';
			content += '</div><div class="box-body" id="'+objId+'Content" style="padding-top:10px;"></div>';
			content += '<div style="height:10px;"><div class="box-footer-left"></div>';
			content += '<div class="box-footer-middle" style="width:'+mWidth+'px;"></div>';
			content += '<div class="box-footer-right"></div></div></div>';
	
	win.innerHTML=content;

	if(win.style.display!='block'){
		win.style.display='block';
		
		var handle = document.getElementById(objId+'Title');
		Drag.init(handle,win);
		var param = 'petid='+petId;
		//if(p!=null) param+='&'+p;
		
		if (onShowFunc != null && onShowFuncParm != null) {
			Updater42(url, objId + 'Content', param,onShowFunc,onShowFuncParm);
			//win.onc
		}
		else 
			Updater(url, objId + 'Content', param);
	
	}else
		win.style.display='none';
		
		win.style.top=winY;
		win.style.left=winX;
		
}

function popDown(objId){
	var win = document.getElementById(objId);
	if(win) win.style.display='none';
}

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

