var UTIL = UTIL || {};
UTIL.popup = UTIL.popup || {};

UTIL.popup.open = function (href, params)
{
    var defaultParams = {
        "width":       "500",   // Window width
        "height":      "400",   // Window height 
        "top":         "0",     // Y offset (in pixels) from top of screen
        "left":        "0",     // X offset (in pixels) from left side of screen
        "directories": "no",    // Show directories/Links bar?
        "location":    "no",    // Show location/address bar?
        "resizable":   "yes",   // Make the window resizable?
        "menubar":     "no",    // Show the menu bar?
        "toolbar":     "no",    // Show the tool (Back button etc.) bar?
        "scrollbars":  "yes",   // Show scrollbars?
        "status":      "no"     // Show the status bar?
    };
    
    var windowName = params["windowName"] || "new_window";
    
    var i, useParams = "";
    
    for (i in defaultParams)
    {
        useParams += (useParams === "") ? "" : ",";
        useParams += i + "=";
        useParams += params[i] || defaultParams[i];
    }
    
    return window.open(href, windowName, useParams);
};

jQuery(function(){ 

	jQuery("a.popup").each(function (i){
			
        jQuery(this).click(function(event) {

			if (jQuery(this).data("windowObject")) {
				jQuery(this).data("windowObject").focus();
				return;
			}
            event.preventDefault();  
            var params = jQuery(this).data("popup") || {};
            if (jQuery(this).attr("target")) {
                params.windowName = jQuery(this).attr("target");
            }
        
            var windowObject = UTIL.popup.open(this.href, params);
			
            jQuery(this).data("windowObject", windowObject);
        });
    });
});