//-----------------------------------------------------------------------
// Copyright (C) Totaljobs Group Ltd. All rights reserved.
//-----------------------------------------------------------------------

Type.registerNamespace("PJB.UI.Controls");

PJB.UI.Controls.PopupAlign = {
    "Free": 0,
    "User": 1,
    "Center": 2,
    "Pointer": 3,
    "Control": 4
};

PJB.UI.Controls.PopupMode = {
    "Modeless": 0,
    "Modal": 1
};

PJB.UI.Controls.PopupControl = function(element) {
    PJB.UI.Controls.PopupControl.initializeBase(this, [element]);
    this._popupWidth = 500;
    this._popupHeight = 500;
    this._popupLeft = 0;
    this._popupTop = 0;
    this._popupLocation = true;
    this._popupMenubar = false;
    this._popupResizable = true;
    this._popupScrollbars = true;
    this._popupStatus = true;
    this._popupTitlebar = true;
    this._popupToolbar = false;
    this._popupWindow = null;
	this._navigateUrl = element.href;	// Defaults to href
	this._navigateUrlFunction = "";
	this._target = "_blank";
	this._popupMode = 0;
	this._popupAlign = 2;
	this._openFunction = "";
	this._excludeClientID = false;
	this._maintainFocusDelegate = null;
}
PJB.UI.Controls.PopupControl.prototype = {
    initialize: function() {
        PJB.UI.Controls.PopupControl.callBaseMethod(this, "initialize");
        $addHandler(this.get_element(), "click", Function.createDelegate(this, this._onClick));
        if (this.get_popupMode() === PJB.UI.Controls.PopupMode.Modal) {
			$addHandlers(document.body, {
				"click": this._onBodyFocus,
				"focus": this._onBodyFocus
			}, this);
		}
		this._maintainFocusDelegate = Function.createDelegate(this, this._maintainFocus);
    },
    
	dispose: function() {
		this.closePopup();
		$clearHandlers(this.get_element());
        if (this.get_popupMode() === PJB.UI.Controls.PopupMode.Modal) {
			$clearHandlers(document.body);
		}
        PJB.UI.Controls.PopupControl.callBaseMethod(this, "dispose");
    },
	
	_onBodyFocus: function(ev) {
		setTimeout(this._maintainFocusDelegate, 0);
	},
	
	_maintainFocus: function() {
		if (this._popupWindow && !this._popupWindow.closed) {
			this._popupWindow.focus();
		}
	},
	
	_onClick: function(ev) {
	
		ev.preventDefault();
		ev.stopPropagation();

		if (this._popupWindow && !this._popupWindow.closed) {
			this._popupWindow.focus();
			return;
		} else {
			this._popupWindow = null;
		}
		
		var clientId = this.get_element().id;
		var navigateUrl = this.get_navigateUrl();
		var width = this.get_popupWidth();
		var height = this.get_popupHeight();
		
		var features = {};
		features["height"] = height;
		features["width"] = width;
		features["left"] = Math.floor((window.screen.availWidth-width)/2);
		features["top"] = Math.floor((window.screen.availHeight-height)/2);
		features["location"] = this.get_popupLocation() ? 1 : 0;
		features["menubar"] = this.get_popupMenubar() ? 1 : 0;
		features["resizable"] = this.get_popupResizable() ? 1 : 0;
		features["scrollbars"] = this.get_popupScrollbars() ? 1 : 0;
		features["status"] = this.get_popupStatus() ? 1 : 0;
		features["titlebar"] = this.get_popupTitlebar() ? 1 : 0;
		features["toolbar"] = this.get_popupToolbar() ? 1 : 0;

		// extract fragment
		var fragment = "";
		var fragmentIndex = navigateUrl.indexOf("#");
		if (fragmentIndex !== -1) {
			fragment = navigateUrl.substring(fragmentIndex);
			navigateUrl = navigateUrl.substring(0, fragmentIndex);
		}
		
		// evaluate user function to get navigate url
		var navigateUrlFunction = this.get_navigateUrlFunction();
		if (navigateUrlFunction && navigateUrlFunction.length > 0) {
			if (typeof window[navigateUrlFunction] === "function") {
				navigateUrl = window[navigateUrlFunction].apply(this, [clientId, navigateUrl]);
			}
		}

		// call user function to open popup
		var openFunction = this.get_openFunction();
		if (openFunction && openFunction.length > 0) {
			if (typeof window[openFunction] === "function") {
				// clientId, navigateUrl, target, features;
				this._popupWindow = window[openFunction].apply(this, [clientId, navigateUrl, target, features.join(",")]);
			}
		}

		// add clientId to query string
		if (!this.get_excludeClientID()) {
			navigateUrl += (navigateUrl.indexOf("?") == -1) ? "?" : "&";
			navigateUrl += "clientId=" + clientId;
		}
		
		if (fragmentIndex !== -1) {
			// append fragment back to url
			navigateUrl += fragment;
		}

		// open the popup window if openFunction did not do so (i.e. returned null)
		this.openPopup(navigateUrl, this.get_target(), features);
	},
	
	// You can override this method to change any of the arguments before calling the base implmentation.
	// Used by PJB.UI.Controls.PopupPropertyButton and PJB.UI.Controls.PopupPropertyLink.
	openPopup: function(navigateUrl, target, features) {
		var temp = [];
		for (var key in features) {
			temp.push(key + "=" + features[key]);
		}
		if (!this._popupWindow) {
			this._popupWindow = window.open(navigateUrl, target, temp.join(","));
		}
		if (this._popupWindow) {
			this._popupWindow.focus();
		}
	},

	closePopup: function() {
		// close popup window if it is still open
		if (this._popupWindow && !this._popupWindow.closed) {
			this._popupWindow.close();
		}
		this._popupWindow = null;
	},
	
    get_navigateUrl: function() {
        return this._navigateUrl;
    },
    set_navigateUrl: function(value) {
        this._navigateUrl = value;
    },

    get_navigateUrlFunction: function() {
        return this._navigateUrlFunction;
    },
    set_navigateUrlFunction: function(value) {
        this._navigateUrlFunction = value;
    },

    get_target: function() {
        return this._target;
    },
    set_target: function(value) {
        this._target = value;
    },
    
    get_popupMode: function() {
        return this._popupMode;
    },
    set_popupMode: function(value) {
        this._popupMode = value;
    },
    
    get_popupAlign: function() {
        return this._popupAlign;
    },
    set_popupAlign: function(value) {
        this._popupAlign = value;
    },
    
    get_openFunction: function() {
        return this._openFunction;
    },
    set_openFunction: function(value) {
        this._openFunction = value;
    },
    
    get_excludeClientID: function() {
        return this._excludeClientID;
    },
    set_excludeClientID: function(value) {
        this._excludeClientID = value;
    },
    
    get_popupTitlebar: function() {
        return this._popupTitlebar;
    },
    set_popupTitlebar: function(value) {
        this._popupTitlebar = value;
    },

    get_popupLeft: function() {
        return this._popupLeft;
    },
    set_popupLeft: function(value) {
        this._popupLeft = value;
    },
    
    get_popupTop: function() {
        return this._popupTop;
    },
    set_popupTop: function(value) {
        this._popupTop = value;
    },
    
    get_popupWidth: function() {
        return this._popupWidth;
    },
    set_popupWidth: function(value) {
        this._popupWidth = value;
    },
    
    get_popupHeight: function() {
        return this._popupHeight;
    },
    set_popupHeight: function(value) {
        this._popupHeight = value;
    },
    
    get_popupLocation: function() {
        return this._popupLocation;
    },
    set_popupLocation: function(value) {
        this._popupLocation = value;
    },
    
    get_popupMenubar: function() {
        return this._popupMenubar;
    },
    set_popupMenubar: function(value) {
        this._popupMenubar = value;
    },
    
    get_popupResizable: function() {
        return this._popupResizable;
    },
    set_popupResizable: function(value) {
        this._popupResizable = value;
    },
    
    get_popupScrollbars: function() {
        return this._popupScrollbars;
    },
    set_popupScrollbars: function(value) {
        this._popupScrollbars = value;
    },
        
    get_popupStatus: function() {
        return this._popupStatus;
    },
    set_popupStatus: function(value) {
        this._popupStatus = value;
    },

    get_popupTitlebar: function() {
        return this._popupTitlebar;
    },
    set_popupTitlebar: function(value) {
        this._popupTitlebar= value;
    },

    get_popupToolbar: function() {
        return this._popupToolbar;
    },
    set_popupToolbar: function(value) {
        this._popupToolbar = value;
    }
}
PJB.UI.Controls.PopupControl.registerClass("PJB.UI.Controls.PopupControl", Sys.UI.Control);

Sys.Application.notifyScriptLoaded();

