/* Everything to exec on page load */
Event.observe(window, "load", function() {

    var nodes =
   // Image rollover supprt
    nodes = $$("#menuBar a img");
    for (i = 0; nodes != null && i < nodes.length; i++) {
        Event.observe(nodes[i], "mouseover", function() {
            this.src = this.src.replace(/.gif/, "-over.gif");
        });
        Event.observe(nodes[i], "mouseout", function() {
            this.src = this.src.replace(/-over.gif/, ".gif");
        });
    }
});


/*
  Please scope all variables into this object RF
  - or - your own object. Keep top level namspace "clean"
*/
var RF = {
    languageId : 'en',
    locale : 'en-au',
    currentPopover : null,

    isMouseOver : function(element, event) {
        var cos = Element.cumulativeOffset(element);
        return (event.pageX - cos[0]) > 0 &&
               (event.pageY - cos[1]) > 0 &&
               (event.pageX - cos[0]) < Element.getWidth(element) &&
               (event.pageY - cos[1]) < Element.getHeight(element)
    },

    /*
      Spawn a popup. returns false to cancel the click.
      Usage:
      DEFAULT
        <a href="wacky.html"
           onclick="return RF.doPopup(this.href)">
          Foo
        </a>
      Set width/height
        <a href="wacky.html"
           onclick="return RF.doPopup(this.href, 200, 300)">
          Foo
        </a>

      Custom style - not resizeable
        <a href="wacky.html"
           onclick="return RF.doPopup(this.href, 200, 300, 'titlebar,scrollbars=yes,resizable=no')">
          Foo
        </a>

      Custom style - and text
        <a href="wacky.html"
           onclick="return RF.doPopup(this.href, 200, 300, 'titlebar,scrollbars=yes,resizable=no', 'doh')">
          Foo
        </a>
    */
    doPopup: function(a_href, a_width, a_height, a_windowStyle, a_customText) {
        if (a_windowStyle == undefined) {
            a_windowStyle = "titlebar,scrollbars=yes,resizable=yes";
        }

        if (a_width == undefined || a_width <= 0) {
            a_width = 400;
        }
        if (a_height == undefined || a_height <= 0) {
            a_height = 400;
        }

        a_windowStyle += (",left=" + ((screen.availWidth - a_width) / 2));
        a_windowStyle += (",width=" + a_width);
        a_windowStyle += (",top=" + (((screen.availHeight - a_height) / 2 ) - 30));
        a_windowStyle += (",height=" + a_height);

        var w;
        if (a_customText != undefined) {
            w = window.open('', "_blank", a_windowStyle);
            try {
                w.document.close();
                w.document.open();
                w.document.writeln(a_customText);
                try {
                    w.document.close();
                } catch(e) {}
            } catch(e) {}
        } else {
            w = window.open(a_href, "_blank", a_windowStyle);
        }
        w.focus();

        return false;
    },

    insertFlash: function(id, src, width, height, requiredVersion, params) {
        if (!src) {
            alert("No source specified");
        }
        if (!width) width = 726;
        if (!height) height = 287;
        if (!requiredVersion) requiredVersion = 8;

        if (AC_FL_RunContent == 0) {
            alert("This page requires AC_RunActiveContent.js.");
        } else {
            if (DetectFlashVer(requiredVersion, 0, 0)) {
                AC_FL_RunContent(
                        'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + requiredVersion +
                                    ',0,0,0',
                        'width', width,
                        'height', height,
                        'src', src,
                        'quality', 'high',
                        'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
                        'align', 'middle',
                        'play', 'true',
                        'loop', 'true',
                        'scale', 'showall',
                        'wmode', 'window',
                        'devicefont', 'false',
                        'id', id,
                        'bgcolor', '#ffffff',
                        'name', id,
                        'menu', 'true',
                        'allowFullScreen', 'false',
                        'allowScriptAccess', 'sameDomain',
                        'movie', src,
                        'salign', ''
                        );
            }
        }

    },


    containerPopup: function(id, src) {
        var popup = new Element('iframe', {'id': id, 'name': id, 'src': src, 'allowTransparency': 'true'});
        var container = $('container');
        if (container) {
            container.insert({before: popup});
            var offsets = container.viewportOffset();
            popup.setStyle({'left': (document.viewport.getWidth() / 2) - (popup.getWidth() / 2) + 'px'});
            popup.setStyle({'top': (document.viewport.getHeight() / 2) - offsets.top - (popup.getHeight() / 2) + 'px'});
        }
        return popup;
    },

    closeContainerPopup: function(id) {
        var p = parent.document.getElementById(id);
        p.style.display = 'none';
        p.remove();
    }

};

function getFlashAsset(key) {
    alert("looking for asset " + key);
}

function printPage() {
    window.print();
}