// Create a namespace for our utilities
var UTIL = UTIL || {};
UTIL.popup = UTIL.popup || {};

/**
 * Open popup window
 * 
 * Opens a popup window using as little as a URL. An optional params object can 
 * be passed.
 * 
 * @param {Object} href
 * @param {Object} params
 * @return {WindowObjectReference}
 */
UTIL.popup.open = function (href, params)
{
    // Defaults (don't leave it to the browser)
    var defaultParams = {
        "width":       "800",   // Window width
        "height":      "600",   // 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":    "yes",    // Show location/address bar?
        "resizeable":  "yes",   // Make the window resizable?
        "menubar":     "yes",    // Show the menu bar?
        "toolbar":     "yes",    // Show the tool (Back button etc.) bar?
        "scrollbars":  "yes",   // Show scrollbars?
        "status":      "yes"     // Show the status bar?
    };
    
    var windowName = params["windowName"] || "new_window";
    
    var i, useParams = "";
    
    // Override defaults with custom values while we construct the params string
    for (i in defaultParams)
    {
        useParams += (useParams === "") ? "" : ",";
        useParams += i + "=";
        useParams += params[i] || defaultParams[i];
    }
    
    return window.open(href, windowName, useParams);
};

/**
 * Using jQuery, we search the document for all links that have a CSS class of â€śpopupâ€?. 
 * For each one we find, we add an onClick handler that disables the browserâ€™s default onClick behavior 
 * for links and then opens up a popup window using the links href attribute. 
*/

$(function(){ // Run this code when the document's done loading

    // Apply this code to each link with class="popup"
    $("a.popup").each(function (i){
		
		// Add an onClick behavior to this link 
        $(this).click(function(event) {
            
			// Prevent the browser's default onClick handler
            event.preventDefault();
    
            // Grab parameters using jQuery's data() method
            var params = $(this).data("popup") || {};
        
            // Use the target attribute as the window name
            if ($(this).attr("target"))
            {
                params.windowName = $(this).attr("target");
            }
        
            // Pop up the window
            var windowObject = UTIL.popup.open(this.href, params);
			
            // Save the window object for other code to use
            $(this).data("windowObject", windowObject);
        });
    });
});

/* Added function to check css in div for clickable section - RHS Components */

var initClickable = function () {
	var divs = document.getElementsByTagName('div');
	for (var i = 0; i < divs.length; i++) {
		if (divs[i].className.indexOf('clickable') != -1) {
			if (window.event) { window.event.cancelBubble = true; }
			divs[i].onclick = function () {
				var links = this.getElementsByTagName('a');
				if (links.length > 0 && links[0]) {
					if (links[0].target == '_blank') {
						var newWindow = window.open(links[0]);
						newWindow.focus();
						return false;
					}
					else {
						window.location = links[0];
						return true;
					}
				}
				
				return false;
			}
		}
	}
	return false;
}

$(document).ready(function() {
    // Call here or attach to window.onload
    initClickable();
});