function RegisterNamespace (name){
	eval(name + " = new Object();");
}
function RegisterEnum(name){
	eval(name + " = new " + name + "();");
}
function RegisterGlobal(name){
	eval(name + " = new " + name + "();");	
}

RegisterNamespace("pJSfw2009");
pJSfw2009.DropDownPanel = function (target, opener, options){
	this._target = target || null;
	this._opener = opener || null;
	this._tdimensions = null;
	this._olocation = null;
	this._toffset = {top : 0, left : 0}	
	this._shown = false;
	this._calculatedPosition = false;
	this._animate = false;
	this._fixedTarget = true;
	this._refixTarget = false;
	this._autohide = true;
	this._alignment = pJSfw2009.Alignment.RIGHT;
	this._showTriggerEvent = pJSfw2009.Events.CLICK;
	this._hideTriggerEvent = pJSfw2009.Events.BLUR;
	this._options = options || {};
	this._init();
	
}
pJSfw2009.DropDownPanel.prototype = {
	_init : function (){	
		this._setOptions();	
		if(this._target){
			if(typeof pJSfw2009.panels == "undefined")
				pJSfw2009.panels = new Object();
			pJSfw2009.panels[this._target.attr("id")] = this;
			//this._target.corner("bottom 5px").corner("top 5px");
			this._tdimensions = {width: this._target.outerWidth(), height: this._target.outerHeight()};
			this._odimensions = {top : 0, left : 0}
			//console.log(this._target);
			this._target.hide();
			if(this._fixedTarget){	
				this._target.css({
					position : "absolute"
				});	
			}
			if(this._opener){
				this._addOpenerHandlers();
				this._olocation = this._opener.offset();
				this._odimensions = {width: this._opener.outerWidth(), height: this._opener.outerHeight()};
			}
		}
	},
	_setOptions : function (){
		for (prop in this._options){
			this["_" + prop] = this._options[prop];
		}
	},
	_openerOnClick : function (event){
		if(this.isShown()){
			this.hidePanel();
		}else{
			this.showPanel();
		}
		if (!event) var event = window.event;
		event.cancelBubble = true;
		if (event.stopPropagation) event.stopPropagation();
	},
	_openerOnBlur : function (event){
		if(this.isShown){
			this.hidePanel();
		}
	},			
	_bodyOnClick : function(event){
		//if(this.isEnabled()){
			if(this.isShown()){
				this.hidePanel();
			}
		//}
	},
	_calculatePosition: function(){
		if(this._olocation){
			if(this._alignment == pJSfw2009.Alignment.RIGHT){
				var x_pos = this._olocation.left + this._toffset.left;
			}else{
				var x_pos = this._olocation.left + this._toffset.left - (this._tdimensions.width - this._odimensions.width);
			}
			this._target.css({
				top : (this._olocation.top + this._toffset.top + this._odimensions.height ) + "px",
				left: x_pos + "px"
			});			
		}
	},
	showPanel : function (){
		if(pJSfw2009.showedPanel)
			pJSfw2009.panels[pJSfw2009.showedPanel].hidePanel();
		if(this._calculatedPosition){
			this._calculatePosition();
		}
		if(this._animate)
			this._target.slideDown("fast");
		else
			this._target.show();
		this._shown = true;
		pJSfw2009.showedPanel = this._target.attr("id");
	},
	hidePanel : function (){		
		if(this._animate)
			this._target.slideUp("fast");
		else
			this._target.hide();
		//this._target.hide();
		this._shown = false;
	},
	_addOpenerHandlers : function (){
		this._opener.bind(this._showTriggerEvent, Function.createDelegate(this, this._openerOnClick));
		//this._opener.bind(this._hideTriggerEvent, Function.createDelegate(this, this._openerOnBlur));
		$(document.body).bind(pJSfw2009.Events.CLICK, Function.createDelegate(this, this._bodyOnClick));
	},
	_removeOpenerHandlers : function (){
		this._opener.unbind(this._showTriggerEvent,this._targetOnClick);
		//this._opener.unbind(this._hideTriggerEvent, this._targetOnBlur);
		$(document.body).unbind(pJSfw2009.Events.CLICK, this._bodyOnClick);
	},		
		
	isShown	: function(){
		return this._shown;
	},
		
	setShowTriggerEvent : function (tevent){
		this._showTriggerEvent = tevent;
	},
	getShowTriggerEvent : function (){
		return this._showTriggerEvent;
	},
		
	setCalculatedPosition : function (value){
		this._calculatedPosition = value;
	},
	getCalculatedPosition : function (){
		return this._calculatedPosition;
	},
		
	setFixedTarget : function (value){
		this._fixedTarget = value;
	},
	getFixedTarget : function (){
		return this._fixedTarget;
	},
		
	setRefixedTarget : function (value){
		this._refixedTarget = value;
	},
	getRefixedTarget : function (){
		return this._refixedTarget;
	},
		
	setAlignment : function (value){
		this._alignment = value;
	},
	getAlignment : function (){
		return this._alignment;
	},
		
	setAnimate : function (value){
		this._animate = value;
	},
	getAnimate : function (){
		return this._animate;
	},
		
	setAutoHide : function (value){
		$(document.body).unbind(pJSfw2009.Events.CLICK, this._bodyOnClick);
		if(value)
			$(document.body).bind(pJSfw2009.Events.CLICK, Function.createDelegate(this, this._bodyOnClick));	
		this._autohide = value;
	},
	getAutoHide : function (){
		return this._autohide;
	}
	
}

pJSfw2009.Events = function (){}
pJSfw2009.Events.prototype = {
	CLICK : "click",
	OVER   : "mouseover",
	OUT     : "mouseout",
	FOCUS  : "focus",
	BLUR    : "blur"
}
RegisterGlobal("pJSfw2009.Events");

pJSfw2009.Alignment = function (){}
pJSfw2009.Alignment.prototype = {
	RIGHT : "right",
	LEFT   : "left",
	TOP     : "top",
	BOTTOM  : "bottom"
}
RegisterGlobal("pJSfw2009.Alignment");

//Function Extension
Function.__typeName = 'Function';
Function.__class = true;

Function.createDelegate = function Function$createDelegate(instance, method) {
    return function() {
        return method.apply(instance, arguments);
    }
}


