/* jQpopup - jQuery Popup Box v0.2.1
 *  jQpopup is distributed under the terms of the MIT license
 *  For more information visit http://jqframework.com/jqpopup
 *  Copyright (C) 2009  jqframework.com
 * Do not remove this copyright message
 */
$.jQpopup={
	minHeight:100,
	minWidth:200,
	imagePath:"images/",
	popupwrapper:null,
	box:function(object_id){
		var popupwrapper='<div id="'+object_id+'_p" class="jqpopup">';
		popupwrapper+='<div class="jqpopup_header"><div id="'+object_id+'_ph"></div>';
		popupwrapper+="<div>";
		popupwrapper+='<div id="'+object_id+'_pl" ></div>';
		popupwrapper+='<div id="'+object_id+'_px" class="jqpopup_cross"></div>';
		popupwrapper+="</div>";
		popupwrapper+="</div>";
		popupwrapper+='<div id="'+object_id+'_pm" class="jqpopup_message"></div>';
		popupwrapper+='<div id="'+object_id+'_pc" class="jqpopup_content"></div>';
		popupwrapper+='<div id="'+object_id+'_pf" class="jqpopup_footer">오늘하루 다시 열지 않기  <img src="/template/user/images/close_btn_today.gif" id="'+object_id+'_tc" /></div>';
		popupwrapper+='<div id="'+object_id+'_ps"></div>';
		popupwrapper+="</div>";
		return popupwrapper
	},
	toTopEvent:function(e){
		var rid=(this.id).substr(0,this.id.length-1);
		var z=900;
		$(".jqpopup").each(function(index){
			$(this).css("zIndex",z);
			z++
		});
		$("#"+rid).css("zIndex",999);
		/*
		var rid=(this.id).substr(0,this.id.length-1);
		var zMax=0;
		$(".jqpopup").each(function(index){
			if($(this).css("zIndex")>zMax) { zMax=$(this).css("zIndex"); }
		});
		var val=parseInt(zMax)+1;
		$("#"+rid).css("zIndex",val);
		*/
	},
	toTop:function(id){
		var str=id.substr(id.length-2);
		var rid=id;
		if(str!="_p"){ rid+="_p"; }
		var z = 900;
		$(".jqpopup").each(function(index){
			$(this).css("zIndex",z);
			z++;
		});
		$("#"+rid).css("zIndex",999);
		/*
		var str=id.substr(id.length-2);
		var rid=id;
		if(str!="_p"){ rid+="_p"; }
		var zMax=0;
		$(".jqpopup").each(function(index){
			if($(this).css("zIndex")>zMax){ zMax=$(this).css("zIndex"); }
		});
		var val=parseInt(zMax)+1;
		$("#"+rid).css("zIndex",val);
		*/
	},
	toCenter:function(id){
		var top=parseInt($(window).scrollTop());
		var left=parseInt($(window).scrollLeft());
		var rid=id+"_p";
		var pos=$("#"+rid).offset();
		var box_x=parseInt($("#"+rid).width());
		var box_y=parseInt($("#"+rid).height());
		var center_x=parseInt($(window).width())/2-box_x/2+left;
		var center_y=parseInt($(window).height())/2-box_y/2+top;
		$("#"+rid).css({left:center_x,top:center_y});
	},
	open:function(popup_config,object_id) {
		if($("#"+object_id).html()!=""){
			var content=$.jQpopup.box(object_id);
			$("body").append(content);
			$("#"+object_id+"_p").bgiframe();
			//$("#"+object_id+"_p").jqDrag(".jqpopup_header").jqResize(".jqpopup_resize");
			$("#"+object_id+"_p").jqDrag(".jqpopup_header");
			$("#"+object_id+"_px").unbind("click");
			$("#"+object_id+"_px").bind("click",function() { $("#"+object_id).jqpopup_close(); });
			//$("#"+object_id+"_pl").unbind("click");
			//$("#"+object_id+"_pl").bind("click",function(){$("#"+object_id).jqpopup_toCenter(this.id); });
			$("#"+object_id+"_tc").unbind("click");
			$("#"+object_id+"_tc").bind("click",function(){
				$.cookie(object_id+"_layerPopup", 'ok', { expires: 1});
				$("#"+object_id).jqpopup_close();
			});
			//var pos=$("#"+button_id).offset();
			//var popup_x = parseInt(pos.left)+(parseInt($("#"+button_id).width()/2));
			//var popup_y=parseInt(pos.top)+parseInt($("#"+button_id).height());

/*
type_pos
popup_x
popup_y
type_size
popup_w
popup_h
*/
			if(popup_config.type_pos == 'auto') {
				$.jQpopup.toCenter(object_id);
			}
			else {
				var popup_x = popup_config.popup_x;
				var popup_y = popup_config.popup_y;
				$("#"+object_id+"_p").css({left:popup_x,top:popup_y});
			}


			var popup_content=$("#"+object_id).clone(true);
			var title=$("#"+object_id).attr("title");
			$("#"+object_id+"_ph").html(title);
			$("#"+object_id+"_pc").html(popup_content.show());
			$("#"+object_id+"_p").show();
			$("#"+object_id+"_ph").mousedown($.jQpopup.toTopEvent);
			$("#"+object_id).empty();

			if(popup_config.type_size == 'auto') {
				if($("#"+object_id+"_p").height()<$.jQpopup.minHeight){ $("#"+object_id+"_p").css("height",$.jQpopup.minHeight+14); }
				else{ $("#"+object_id+"_p").css("height",$("#"+object_id+"_p").height()+14); }
				if($("#"+object_id+"_p").width()<$.jQpopup.minWidth) { $("#"+object_id+"_p").css("width",$.jQpopup.minWidth); }
			}
			else {
				$("#"+object_id+"_p").css("width",popup_config.popup_w);
				$("#"+object_id+"_p").css("height",popup_config.popup_h+14);
			}

			$("#"+object_id+"_p").jqpopup_toTop();
		}
	},
	close:function(object_id){
		var content=$("#"+object_id+"_pc").clone(true);
		$("#"+object_id).html(content.show());
		$("#"+object_id+"_pc").empty();
		$("#"+object_id+"_p").hide();
	}
};

$.fn.extend({
	jqpopup_close:function(){ return this.each(function(){$.jQpopup.close(this.id); }); },
	jqpopup_open:function(popup_config){ return this.each(function(){$.jQpopup.open(popup_config,this.id);}); },
	jqpopup_toCenter:function(){ return this.each(function(){$.jQpopup.toCenter(this.id); }); },
	jqpopup_toTop:function(){ return this.each(function(){$.jQpopup.toTop(this.id); }); }
});
