/// <reference path="jquery-1.2.6.min-vsdoc.js" />

var popupStatus = 0;
var movieplaying;

function removeAllRequiredStuff() {
    $("span.requiredspan").remove();
    $("input.clearrequired").unbind('click', removeRequiredStuff);
}

function removeRequired(bool) {
    $(this).next(".requiredspan").remove();
    if (bool) {
        $(this).change();
    }
}

function applyRequiredValidators() {
    $(".required").one('blur', true, removeRequired).one('change', false, removeRequired).after("<span class='requiredspan'>*</span>");

    $("input.clearrequired").one('click', removeAllRequiredStuff);
}

$(document).ready(function() {

    $(".jQ_expand_menu_levels").click(function() {
        $(this).toggleClass("expand");
        $(this).toggleClass("collapse");
        $("#jQ_menu_levels").toggle();
    });

    $("li.film > a").click(function() {
        centerPopup(this);
        loadPopUp(this);
        return false;
    });

    $("a.popupMovieClose").click(function() {
        disablePopup();
    });

    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            disablePopup();
        }
    });

    $("#backgroundPopup").click(function() {
        disablePopup();
    });

    $("#utilitymenu .searchfield").keypress(function(e) {
        if (e.keyCode == 13) {
            $("#utilitymenu .searchbutton").click();
            return false;
        }
    });

    $("#rightcolumn > .banner").each(function() {
        var divheight = $(this).children("div:first").height() + 20;
        var imgheight = $(this).children("img:first").height() + 5;

        var height;

        if (divheight > imgheight) {
            height = divheight;
        }
        else {
            height = imgheight;
        }

        if ($(this).height() < height) {
            $(this).height(height);
        }
    });

    /* -- COLLAPSABLE PANELS -- */
    //hide collapsable content
    $("div.jqExpandableContent").hide();
    //remove the "open"-class, which is used for fallback and
    //collapse/expand toggle
    $("ul.jqExpandableList .jqTrigger").removeClass("open").click(function() {
        $(this).parent().find(".jqExpandableContent").slideToggle("fast");
        $(this).toggleClass("open");
        return false;
    });

});

function loadPopUp(element) {
    if (popupStatus == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $(element).siblings(".popupMovie").fadeIn("slow");

        movieplaying = getFlashMovieObject(element.id + 'Movie');

        if (movieplaying) {
            setTimeout(function() {
                movieplaying.Play();
            }, 2000);
        }

        popupStatus = 1;
    }
}

function centerPopup(element) {

    var movieHeight = $(element).siblings(".popupMovie").height();
    var movieWidth = $(element).siblings(".popupMovie").width();

    var dimensions = { width: 0, height: 0 };

    if (document.documentElement) {
        dimensions.height = document.documentElement.offsetHeight;
        dimensions.width = document.documentElement.offsetWidth;
    }
    else if (window.innerWidth && window.innerHeight) {
        dimensions.height = window.innerHeight;
        dimensions.width = window.innerWidth;
    }

    $(element).siblings(".popupMovie").css({
        "position": "absolute",
        "top": (dimensions.height - movieHeight) / 2,
        "left": (dimensions.width - movieWidth) / 2
    });
}

function OpenPopupWindow(url) {
    var w = 480, h = 340;

    if (document.all) {
        /* the following is only available after onLoad */
        w = document.body.clientWidth;
        h = document.body.clientHeight;
    }
    else if (document.layers) {
        w = window.innerWidth;
        h = window.innerHeight;
    }

    var popW = 650, popH = 500;
    var leftPos = (w - popW) / 2, topPos = (h - popH) / 2;

    window.open(url, 'popup', 'width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos + ',scrollbars=yes');
} 

function disablePopup() {
    if (popupStatus == 1) {
        popupStatus = 0;
        $("#backgroundPopup").fadeOut("slow");
        $("div.popupMovie").fadeOut("slow");

        if (movieplaying) {
            setTimeout(function() {
                movieplaying.Rewind();
            }, 2000);
        }
    }
}

function printArea(id) {
    var printArea = document.getElementById(id);

    if (printArea) {
        var sStart = "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"/templates/NMD/Styles/NMD/style.css\"><link rel=\"stylesheet\" type=\"text/css\" href=\"/templates/NMD/Styles/NMD/print.css\"></head><body onload=\"if(window.print)window.print();\"> "+
        " <div id=\"content\"><div class=\"main\"><img class='printImage' src='/Templates/NMD/Images/logo.gif' alt='Vitusapotek' />";
        
        sStop = " <div class='printFooter'> Farmas&oslash;yter i Vitusapotek jobber kontinuerlig for &aring; oppdatere og kvalitetssikre nettstedet. Vi kan allikevel ikke garantere at vi alltid har helt fullstendige og oppdaterte opplysninger innenfor alle omr&aring;der. Dersom du mener &aring; finne mangler h:&oslash;rer vi gjerne fra deg p&aring; redaksjon@vitusapotek.no. "+
        " <br /><br />Gode r&aring;d f&aring;r du p&aring; apoteket - finn ditt n&aelig;rmeste apotek p&aring; www.vitusapotek.no </div></div></div></body></html>";

        var s = sStart + printArea.innerHTML;

        var w = window.open('about:blank', 'printWin', 'width=580,height=500,scrollbars=yes,toolbar=no');
        wdoc = w.document;
        wdoc.open();
        wdoc.write(s);
        wdoc.writeln(sStop);
        wdoc.close();
    }
}

function getFlashMovieObject(movieName) {
    if (window.document[movieName]) {
        return window.document[movieName];
    }
    if (navigator.appName.indexOf("Microsoft Internet") == -1) {
        if (document.embeds && document.embeds[movieName])
            return document.embeds[movieName];
    }
    else
    {
        return document.getElementById(movieName);
    }
}

function GetSted(url, pn, ps) {
    if ($("#" + pn).val().length == 4) {
        document.getElementById(ps).readOnly = true;               
        $.get(url + "?nr=" + $("#" + pn).val(), function(data) {
        if (data == '') {
                $("#" + ps).val('Ukjent poststed');
            }
            else {
                $("#" + ps).val(data);
            }
            document.getElementById(ps).readOnly = false;               
        });
    }
}

function showMoreInfo(e, id) {
    var posx = 0;
    var posy = 0;

    if (!e) var e = window.event;
    if (e.pageX || e.pageY)
    {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY)
    {
        posx = e.clientX + document.body.scrollLeft;
        posy = e.clientY + document.body.scrollTop;
    }

    var infoCtrl = document.getElementById(id);
    infoCtrl.style.display = 'block';
    infoCtrl.style.left = parseInt(posx + 15)  + 'px';
    infoCtrl.style.top = parseInt(posy + 25) + 'px';
    infoCtrl.style.zIndex = 9999;
}

function hideMoreInfo(e, id) {
    var infoCtrl = document.getElementById(id);
    infoCtrl.style.display = 'none';
}