﻿function addEvent(el, action, fn) {
    if(el.addEventListener)
        el.addEventListener(action, fn, false);
    else if(el.attachEvent)
        el.attachEvent('on'+action, fn);
    else
        throw "Unable to add event listener for '" + el.id + "'";
}

function launchwin(url, name, features)
{
    shortlistWin = window.open(url, name, features);
    shortlistWin.focus();
}

function stopAction(event)
{
    if (typeof(event.preventDefault) != 'undefined'){event.preventDefault();}else{event.returnValue = false;}
    if (typeof(event.cancelBubble) != 'undefined'){event.cancelBubble = true;}
}

function addTrackingCode(anchor, ridCode)
{				
    if (anchor.href.indexOf("?") == -1)
    {
	    if (anchor.href.indexOf("s_rid") == -1)
	    {
		    var url = " " + anchor.href + "?s_rid=" + ridCode;
		    anchor.href =  url;	
	    }
    }
    else
    {
        if (anchor.href.indexOf("s_rid") == -1)
	    {
            var url = " " + anchor.href + "&s_rid=" + ridCode;
            anchor.href =  url;	
	    }
    }
    return true;
}

// Switch the display mode of an element between
// "block" (displayed) and "none" (not rendered).
//
// @param "selector" - is required. Path to the DOM element for style changes.
// e.g. 
// <div id="mydiv">content</div>
// to toggle its display style:
//   toggleDisplay("#mydiv")
// @param "show" - is optional. If not set, then the switch will be based on
// elements' current display mode. If a value is set, then based on its 
// boolean value if
//  true => set display style to "block"
// false => set display style to "none"
function toggleDisplay(selector, show)
{
    var elements = $$(selector)
    
    if (elements)
    {
        elements.each(function(item, index) {
            var displayMode = "";

            if (typeof(show) != "boolean")
            {
                show = (item.getStyle("display") == "none");
            }
            
            displayMode = show ? "block" : "none";
            item.setStyle("display", displayMode);
        });
    }
}

// Resizes the height of the target element down to 'size'
// and show scroll bar.
// @param "size" - value must be an integer or decimal.
function resizeElementOnOverflow(targetElem, thresholdHeight, resizeToHeight)
{
    if (!targetElem)
    {
        return;
    }
    
    var screenSize = window.getSize();

    if (screenSize.y < thresholdHeight)
    {
        if (!isNaN(resizeToHeight) && (resizeToHeight < 1))
        {
            resizeToHeight = Math.floor(screenSize.y * resizeToHeight);
        }
        
        if (typeof document.body.style.maxHeight != "undefined")
        {
            targetElem.setStyle("max-height", resizeToHeight + "px");
        }
        else // IE6 and lower
        {
            if (targetElem.scrollHeight > 0 && targetElem.scrollHeight < resizeToHeight)
            {
                resizeToHeight = targetElem.scrollHeight;
            }
            
            targetElem.setStyle("height", resizeToHeight + "px");
        }

        targetElem.setStyle("overflow", "auto");
    }
}

// Based on the given CSS selector, retrieve a single (the first) element.
function getSingleElemBySelector(selector)
{
    if (!window["$$"])
    {   // mootool core is not loaded, can not proceed.
        return;
    }
    
    var singleElement = $$(selector);

    // The double dollar getter always return an array, even when
    // there is one element. So we need to take the first item.
    if (singleElement && (singleElement.length > 0))
    {   
        singleElement = singleElement[0];
    }

    return singleElement;
}

// Client script used by the Saved Search lightbox
if (!window["Domain"])
{
    Domain = {};
}

if (!Domain["SearchLightboxClient"])
{
    Domain.SearchLightboxClient = function(containerSelectorPath,
                                           selectorPath, 
                                           textboxId, 
                                           alertCheckboxId, 
                                           buttonId)
    {
        var _loaded = false;
        var _selectorPath = selectorPath;
        var _textboxId = textboxId;
        var _alertCheckboxId = alertCheckboxId;
        var _buttonId = buttonId;
        var _LIGHTBOX_RESIZE_RATIO = 0.7;
        var _LIGHTBOX_MAXHEIGHT = 800; // Lightbox maximum height is approximately 785
                                       // plus some buffer.

        this.init = function()
        {   
            // Attemt to put in scroll bars if the lightbox is too big
            // relative to the viewable area in the browser.
            resizeElementOnOverflow(getSingleElemBySelector(containerSelectorPath), 
                                    _LIGHTBOX_MAXHEIGHT, 
                                    _LIGHTBOX_RESIZE_RATIO);

            // The init() function is called whenever the modal is opened.
            // However we only need to setup the first time.
            if (_loaded) return;

            // Open the alert form if checkbox is ticked.
            $(_alertCheckboxId).addEvent("click", function(event) {
                toggleDisplay(_selectorPath, $(_alertCheckboxId).checked);
            });
            
            // Set the display mode of the alertbox based on checkbox value.
            toggleDisplay(_selectorPath, $(_alertCheckboxId).checked);
            
            // Listens to the "enter" key in the saved search textbox and fire 
            // the button to submit.
            bindButtonToTextbox(_textboxId, _buttonId);
            
            _loaded = false;
        }
    }
}

if (!Domain["HomeAlertLightboxClient"])
{
    Domain.HomeAlertLightboxClient = function(containerSelectorPath)
    {
        var _LIGHTBOX_RESIZE_RATIO = 0.7;
        var _LIGHTBOX_MAXHEIGHT = 800; // Lightbox maximum height is approximately 785
                                       // plus some buffer.
        this.init = function()
        {
            // Attemt to put in scroll bars if the lightbox is too big
            // relative to the viewable area in the browser.
            resizeElementOnOverflow(getSingleElemBySelector(containerSelectorPath), 
                                    _LIGHTBOX_MAXHEIGHT, 
                                    _LIGHTBOX_RESIZE_RATIO);
        }
    }
}
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();