﻿/*
* jQuery Tiny Pub/Sub - v0.6 - 1/10/2011
* http://benalman.com/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function ($) { var a = $("<b/>"); $.subscribe = function (b, c) { function d() { return c.apply(this, Array.prototype.slice.call(arguments, 1)) } d.guid = c.guid = c.guid || ($.guid ? $.guid++ : $.event.guid++); a.bind(b, d) }; $.unsubscribe = function () { a.unbind.apply(a, arguments) }; $.publish = function () { a.trigger.apply(a, arguments) } })(jQuery);

var pgcarousel = (function () {
    /**
    * We use the initCallback callback
    * to assign functionality to the controls
    */
    function pgcarousel_initCallback(carousel) {
        // stop on init by default
        carousel.stopAuto();

        $('a.carousel-control').bind('click', function () {
            carousel.scroll(jQuery.jcarousel.intval(jQuery(this).data("imageid")));
            return false;
        });

        $('#carousel-next').bind('click', function () {
            carousel.next();
            return false;
        });

        $('#carousel-prev').bind('click', function () {
            carousel.prev();
            return false;
        });
    };

    var pub = {
        init: function () {
            $("#carousel").jcarousel({
                visible: 1,
                scroll: 1,
                size: 4,
                auto: 8,
                itemFallbackDimension: 950,
                animation: "fast",
                wrap: "circular",
                initCallback: pgcarousel_initCallback,
                setupCallback: function (carousel) {
                    // start on random img
                    //carousel.scroll(Math.floor(Math.random() * 4) + 1);
                    setTimeout('$.publish("carouselReady");', 1000); // uten timeout sa feiler dette i IE
                },
                itemVisibleInCallback: {
                    onAfterAnimation: function (carousel, li) {
                        if ($.browser.version == '7.0') return;
                        var imageId = $(li).data("imageid");
                        var target = "#image" + imageId + "-desc";
                        $(target).removeClass("hidden");
                        var target = "#image" + imageId + "-bullet";
                        var newImageSrc = $(target).attr("src").replace("hidden", "visible");
                        $(target).attr("src", newImageSrc);
                    }
                },
                itemVisibleOutCallback: {
                    onAfterAnimation: function (carousel, li) {
                        if ($.browser.version == '7.0') return;
                        var imageId = $(li).data("imageid");
                        var target = "#image" + imageId + "-desc";
                        $(target).addClass("hidden");
                        var target = "#image" + imageId + "-bullet";
                        var newImageSrc = $(target).attr("src").replace("visible", "hidden");
                        $(target).attr("src", newImageSrc);
                    }
                }
            });
        },

        stop: function () {
            var carouselData = $("#carousel").data("jcarousel");
            if (carouselData === undefined) return;
            carouselData.stopAuto();
        },

        start: function () {
            var carouselData = $("#carousel").data("jcarousel");
            if (carouselData === undefined) return;
            carouselData.startAuto();
        }
    }

    return pub;
})();

function handleViewportSize() {
    var winwidth = $(window).width();

    if (winwidth > 1600) {
        $("#ledningervenstre, #ledningerhoyre").hide();
    } else if ($("#ledningervenstre:hidden").size()) {
        $("#ledningervenstre, #ledningerhoyre").show();
    }

    $("#ledningerhoyre").css("left", (winwidth - $("#ledningerhoyre").width()));
}

$(function () {
    handleViewportSize();

    $(window).bind("resize", function () {
        handleViewportSize();
    });

    // resize logic for the show/hide carousel button
    $('#showhidecarousellink').click(function () {
        if ($('#carousel').css("height") == "150px") {
            //slide down and start carouesl
            $('#carousel').animate({
                height: '384px'
            }, function () {
                $("#showhidecarousellink").html("Skjul bilder")
            });
            $('#imagecarousel').animate({
                height: '450px'
            }, function () {
                pgcarousel.start();
            });
        } else {
            //slide up and stop carousel
            $('#carousel').animate({
                height: '150px'
            }, function () {
                $("#showhidecarousellink").html("Vis bilder")
            });
            $('#imagecarousel').animate({
                height: '215px'
            }, function () {
                pgcarousel.stop();
            });
        }
    });

    // manually set html5 placeholder attr on ASP.net login view
    $("input.username").attr("placeholder", "Brukernavn");
    $("input.password").attr("placeholder", "Passord");
    if (!supports_input_placeholder()) {
        // fallback to jquery.placeholder for browsers which lack HTML5 placeholder support
        $("input[placeholder]").placeholder();
    }

    // hack for the asp.net loginview form
    $("input.password").keydown(function (e) {
        if (e.keyCode == 13) {
            eval($("a.loginlink").attr("href"));
        }
    });

    // image carousel
    if ($.browser.version == '7.0') $("#carouselcontrols").hide();
    pgcarousel.init();

    // round corners in boxes on the front page
    $(".box").corner("dog br");
    // add CSS3 rounding for the last elements in the dropdown menus
    $("#topnav ul li ul").each(function (i, ul) {
        $(ul).find("li:last a").addClass("roundbr");
    });
});

function supports_input_placeholder() {
    var i = document.createElement('input');
    return 'placeholder' in i;
}


// dropdown menus
var jsddm = {
    timeout: 500,
    closetimer: 0,
    ddmenuitem: 0
}

function jsddm_open() {
    jsddm_canceltimer();
    jsddm_close();
    jsddm.ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}

function jsddm_close()
{ if (jsddm.ddmenuitem) jsddm.ddmenuitem.css('visibility', 'hidden'); }

function jsddm_timer()
{ jsddm.closetimer = window.setTimeout(jsddm_close, jsddm.timeout); }

function jsddm_canceltimer() {
    if (jsddm.closetimer) {
        window.clearTimeout(jsddm.closetimer);
        jsddm.closetimer = null;
    }
}

$(function () {
    $('#jsddm > li').bind('mouseover', jsddm_open)
    $('#jsddm > li').bind('mouseout', jsddm_timer)
});

document.onclick = jsddm_close;

