function getURLParam(url, paramName) {
    paramName = paramName.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + paramName + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(url);
    if (results) {
        return results[1];
    }
    return null;
}

var highlight = {
    loadingPanel: null,
    layouts: null,
    flashConfig: {},
    triggers: [],
    initialize: function () {
        var propertyHighlightTriggerElements = yuiDom.getElementsByClassName('propertyHighlight', 'a');
        propertyHighlightTriggerElements.forEach(function(triggerElement) {
            var propertyId = getURLParam(triggerElement.href, "propertyID");
            var debugParam = getURLParam(triggerElement.href, "debug");
            if (propertyId) {
                triggerElement.config = {
                    propertyId: propertyId,
                    addMaskEvent: false,
                    debugParam: debugParam
                }
                yuiEvent.addListener(triggerElement, "click", highlight.getHighlightBridge);
                highlight.triggers.push(triggerElement);
            }
        });
    },
    createHighlight: function(trigger) {
        var propertyHighlightPanel = document.createElement("div");
        yuiDom.addClass(propertyHighlightPanel, "propertyHighlightPanel");

        var highlightContainer = document.createElement("div");
        yuiDom.addClass(highlightContainer, "highlightContainer");
        propertyHighlightPanel.appendChild(highlightContainer);

        var iFrame = document.createElement("iframe");
        var dateIdentifier = new Date();
        var frameIdName = "frame_" + trigger.config.propertyId + "_" + (dateIdentifier.getHours() + "" + dateIdentifier.getMinutes() + "" + dateIdentifier.getSeconds());
        iFrame.setAttribute("name", frameIdName);
        iFrame.setAttribute("id", frameIdName);
        iFrame.frameBorder = "0";
        iFrame.style.border = "none";
        iFrame.scrolling = "no";
        iFrame.width = "790";
        iFrame.height = "540";
        yuiDom.addClass(iFrame, "propertyHighlightIFrame")
        var debugParam = "";
        if (trigger.config.debugParam) {
            debugParam = "&debug=" + trigger.config.debugParam;
        }
        iFrame.src = "/luxury/property/highlight/index.html?propertyID=" + trigger.config.propertyId + debugParam;
        highlightContainer.appendChild(iFrame);
        if (window.frames[iFrame.name]) {
            window.frames[iFrame.name].location = "/luxury/property/highlight/index.html?propertyID=" + trigger.config.propertyId;
        }
        propertyHighlightPanel.id = "panel_" + trigger.config.propertyId;
        document.body.appendChild(propertyHighlightPanel);
        return {
            main: propertyHighlightPanel,
            highlightContianer: highlightContainer,
            frame: iFrame
        };
    },
    flashGetHighlightBridge: function(propertyId) {
        if (!highlight.flashConfig[propertyId]) {
            highlight.flashConfig[propertyId] = {config:{
                propertyId: propertyId
            }};
        }
        highlight.getHighlight(highlight.flashConfig[propertyId]);
    },
    getHighlightBridge: function(e) {
        highlight.getHighlight(this);
        if (e) {
            if (typeof e.preventDefault != 'undefined') {
                e.preventDefault(); // W3C
            }
            else {
                e.returnValue = false; // IE
            }
        }
        return false;
    },
    getHighlight: function (trigger) {
        if (!trigger.config.panel) {
            trigger.config.panelConfig = this.createHighlight(trigger);
            var panel = new YAHOO.widget.Panel(trigger.config.panelConfig.main.id,
            {
                close:true,
                visible:false,
                modal:true,
                constraintoviewport:true,
                fixedcenter: true,
                effect: {effect:YAHOO.widget.ContainerEffect.FADE, duration: 1}
            }
                );
            panel.render(document.body);
            trigger.config.panel = panel;

        }
        trigger.config.panel.show();
        if (!trigger.config.addMaskEvent) {
            yuiEvent.addListener(yuiDom.get("panel_" + trigger.config.propertyId + "_mask"), "click", highlight.closeHighlightBridge, trigger);
            trigger.config.addMaskEvent = true;
        }
    },
    closeHighlightBridge: function(e, trigger) {
        trigger.config.panel.hide();
        if (e) {
            if (typeof e.preventDefault != 'undefined') {
                e.preventDefault(); // W3C
            }
            else {
                e.returnValue = false; // IE
            }
        }
        return false;
    }
}
yuiEvent.onDOMReady(highlight.initialize);