/**
 * jQuery Widget : Popup
 */
(function($){

var Popup = {
	
	_currentTarget:"",
	_animating:false,
	_opened:false,
	
	options: {
		position:'dynamic',
		anchor: "center center",
		targetAnchor: "center center",
		target:window,
		
		offset: "0 0",
		
		show: "fade",
		show_duration: 400,
		hide: "fade",
		hide_duration: 400,
		
		modal:false,
		modalClickable:true,
		modal_show: "fade",
		modal_show_duration: 400,
		modal_hide: "fade",
		modal_hide_duration: 400,
		
		moveToBody:true
	},
	
	_create: function() {
		var self = this;
		this.element.addClass("cp-popup").hide();
		
		if(this.options.moveToBody) this.element.appendTo("body");
	},
	
	destroy: function() {
		this.element.removeClass("cp-popup");
		$.Widget.prototype.destroy.call( this );
	},

	position: function(){
		//if(this.options.position=="fixed") return;
		
		var offset = this.options.offset;
		
		this.element.position({
			of: this.options.target,
			my: this.options.anchor,
			at: this.options.targetAnchor,
			offset: offset
		});
	},

	open: function(options){
		$.extend(this.options,options);
		$(window).unbind('resize').unbind('scroll');
		this.element.find(".btn-close").unbind('click');
		
		if(this.options.position!="fixed"){
			$(window).resize(function(){
				self.position();
				/*$(document).click(function(){
					self.position();
					$(document).unbind('click');
				});*/
				setTimeout(function(){self.position()},10)
			}).scroll(function(){
				self.position();
			});
		}
		
		this.element.find(".btn-close").click(function(){
			self.close();
		});
		
		if(this._animating) return;
		if(this._opened) {
			this.close();
			return;
		}
		var self = this;
		
		//merge options with existing widget options
		$.extend(this.options,options);
		
		if(this.options.target==this._currentTarget){
			this.close();
			return;
		}
		
		this._animating = true;
		this.element.hide().stop(true,true).show(this.options.show,this.options.show_duration,function(){
			self._animating = false;
		});
		
		this._currentTarget = this.options.target;
		this._opened = true;
		this.position();
		if(this.options.modal){
			if(!$("#cp-popup-modalbg").length) $(document.body).append("<div id='cp-popup-modalbg'></div>");
			
			$("#cp-popup-modalbg").hide()
			.show(this.options.modal_show,this.options.modal_show_duration)
			
			if(this.options.modalClickable)
			$("#cp-popup-modalbg").click(function(){ self.close(); });
		}
		
	},

	close: function(){
		var self = this;
		
		
		this._animating = true;
		this.element.stop(true,true).hide(this.options.hide,this.options.hide_duration,function(){
			self._animating = false;
		});
		
		this._currentTarget = "";
		this._opened = false;
		if(this.options.modal){
			$("#cp-popup-modalbg").hide(this.options.modal_hide,this.options.modal_hide_duration)
			.unbind("click");
		}
	},
	
	_setOption: function( key, value ) {
		$.Widget.prototype._setOption.apply( this, arguments );
		switch ( key ) {
			case "target":
				//this.open();
				break;
		}
	}
}

//Expose the widget with the widget system
$.widget('nj.cp_popup', Popup);

//Expost the widget with the sublass system
//$.ui.widget.subclass('nj.cp_popup', Popup);

})(jQuery)
