/* Make sure jquery can work together with prototype */
jQuery.noConflict();


/* Dropdowns */
jQuery(document).ready(function() {

    jQuery("div.dropdown").click(function(e) {
        var ddl = jQuery(this);
        if (this.className.indexOf("dropdown-closed") > -1) {
            ddl.addClass("dropdown-open");
            ddl.removeClass("dropdown-closed");
        }
        else {
            ddl.removeClass("dropdown-open");
            ddl.addClass("dropdown-closed");
        }
    });

    jQuery("div.dropdown").mouseout(function(e) {
        // Close the popup if the mouse is no longer over the dropdown
        if (jQuery(e.toElement).parents("div.dropdown").length == 0) {
            var ddl = jQuery(e.fromElement).parents("div.dropdown");
            ddl.removeClass("dropdown-open");
            ddl.addClass("dropdown-closed");
        }
    });
    
    jQuery("div.dropdown li").click(function(e) {
        var ddl = jQuery(this).parents("div.dropdown");
        var el = ddl.children("div")[0];
        if (this.innerText) {
            el.innerText = this.innerText;
        }
        else if (this.textContent) {
            el.textContent = this.textContent;
        }
        ddl.removeClass("dropdown-open");
        ddl.addClass("dropdown-closed");

        e.preventDefault();
        e.stopPropagation();

        /*
        onchange: set hidden form variable etc.
        */

        return false;
    });
});

/* Popups */
var g_sBackgroundLayerId = "Overlay";
var g_sPopupContainerLayerId = "PopupContainer";
var g_sPopupLoadingLayerId = "PopupLoading";
var g_sPopupLayerId = "Popup";
var g_sPopupContentId = "PopupContent";
var g_sPopupBasePath = "/wps/themes/html/EPN_MarketingPortal/popups/"; // Don't forget the trailing slash, if applicable

var g_oLayoutRoot;
// found on: http://mar.anomy.net/entry/2006/11/23/00.06.40/
var is_ie/*@cc_on = {
  version : parseFloat(navigator.appVersion.match(/MSIE (.+?);/)[1])
}@*/;

function showPopup(popupContentId) {
    // http://www.demay-fr.net/blog/index.php/2007/04/24/57-get-scroll-position-with-all-browser-included-ie
    var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
    var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
    var el;

    // Set the size of the Overlay container
    el = document.getElementById(g_sBackgroundLayerId);
    // for IE6 or lower, you cannot set a value of 100% for the width/height style properties; set the width and height manually
    if (is_ie && is_ie.version <= 6) {
        if (typeof(g_oLayoutRoot) == "undefined") {
            g_oLayoutRoot = document.compatMode == "CSS1Compat" ? document.documentElement : document.body;
        }
        el.style.width = g_oLayoutRoot.clientWidth + "px";
        //el.style.height = g_oLayoutRoot.clientHeight + "px";
    }
    // Set the height of the Overlay equal to the height of the page
    el.style.height = document.body.offsetHeight + "px";
    el.style.display = "block";

    // Set the location of the Popup container
    el = document.getElementById(g_sPopupContainerLayerId);
    el.style.top = scrollTop + "px";
    el.style.left = scrollLeft + "px";
    el.style.display = "block";

    // Show the Popup layer
    el = document.getElementById(g_sPopupLayerId);
    el.style.display = "block";
    // Show Loader div
    jQuery("#" + g_sPopupContentId).hide();
    jQuery("#" + g_sPopupLoadingLayerId).show();
    // Load content
    jQuery("#" + g_sPopupContentId).loadIfModified(
        g_sPopupBasePath + popupContentId + ".htm",
        function() {
            jQuery("#" + g_sPopupLoadingLayerId).hide();
            jQuery("#" + g_sPopupContentId).show();
        }
    );
}

function hidePopup() {
    // Hide the Popup
    document.getElementById(g_sPopupLayerId).style.display = "none";
    // Hide the Popup container
    document.getElementById(g_sPopupContainerLayerId).style.display = "none";
    // Hide the Overlay
    document.getElementById(g_sBackgroundLayerId).style.display = "none";
}

/* Catalog */
function toggleCatalogInfo(elementId) {
    var el = document.getElementById("info_" + elementId);
    var btn = document.getElementById("btn_" + elementId);
    var imgsml = document.getElementById("img_sml_" + elementId);
    var imglrg = document.getElementById("img_lrg_" + elementId);
    var cmdbox = document.getElementById("cmdbox_" + elementId);

    var collapsed = (btn.className == "detailinfo_collapsed");
    btn.className = (collapsed) ? "detailinfo_expanded" : "detailinfo_collapsed";
    el.style.display = (collapsed) ? "block" : "none";
    imgsml.style.display = (collapsed) ? "none" : "block";
    imglrg.style.display = (collapsed) ? "block" : "none";
    cmdbox.className = (collapsed) ? "catalog-commandbox-mini-expanded" : "catalog-commandbox-mini-normal";
}

/* Spinner controls */
jQuery(document).ready(function() {
    var els = jQuery("input.spinner");
    for (var i = 0; i < els.length; i++) {
        TextFieldControls.AddControl(els[i], UPDOWN_CONTROL, 0, 100, 1);
    }
});

/* Accordion */
jQuery(document).ready(function() {
    if (jQuery("dl.accordion").length == 0) {
        return;
    }
    jQuery("dl.accordion").Accordion({
	    header: "dt",
	    alwaysOpen: true, 
	    showSpeed: "fast", 
	    hideSpeed: "fast"
    }).activate();
});

/* Treeview for Sitemap */
jQuery(document).ready(function() {
    jQuery("ul.sitemap").Treeview({
        speed: "fast",
        collapsed: false
    });
    // Close only top nodes
    //jQuery("ul.sitemap").find('li:eq(0)').siblings().find("div.hitarea:eq(0)").click();
});

/* Rounded borders */
jQuery(document).ready(function() {
    RUZEE.Borders.add({
        "div.error" : {
            borderType: "simple",
            cornerRadius:4
        }
    });

    RUZEE.Borders.render();
});

/* Key events in ISBN fields */
jQuery(document).ready(function() {
    var tags = "input.isbn-ean|input.isbn-group|input.isbn-publisher|input.isbn-item".split("|");
    //|input.isbn-checksum
    for (var i = 0; i < tags.length; i++) {
        var els = jQuery(tags[i]);
        for (var j = 0; j < els.length; j++) {
            jQuery(els[j]).keypress( function(e) {
                if(48 <= e.keyCode && e.keyCode <= 57) {
                    if(this.maxLength == this.value.length) {
                        jQuery(this).next("input").focus();
                    }
                }
                else {
                    return false;
                }
            });
        }
    }
});