﻿    var aRotQ = 1;
    var aRotT = 0;
    var aRotR = true;
    function aRotTimer() {
        if (aRotR) {
            if (aRotQ == 6) { aRotQ = 1; }
            if (aRotT == 0) {
                aRotSet(document.getElementById("aRot_" + aRotQ));
                aRotT = 5;
            }
            else {
                aRotT--;
            }
        }
        setTimeout("aRotTimer();", 500);
    }
    function aRotSet(obj) {
            $("#aRot div._aRoter img").css("opacity", 1);
            $("#aRot_0").css("opacity", 0);
            $("#aRot_0").html($(obj).html());
            aRotQ = parseInt(obj.id.charAt(obj.id.length - 1)) + 1;
            $(obj).children("img").fadeTo(200, 0.2);
            $("#aRot_0 img").attr("src", $("#aRot_0 img").attr("src").replace("/img_105/", "/img_525/"));
            $("#aRot_0 div.e").hide();
            $("#aRot_0").fadeTo(400, 1);
            $("#aRot_0").hoverIntent(function () {
                $("#aRot_0 div.e").slideDown(250);
            }, function () {
                $("#aRot_0 div.e").slideUp(150);
            });
    }

//START
    $(document).ready(function () {
        $("a[target='colorbox']").colorbox({ iframe: true, height: 100, width: 400 });
        var aid = new String(window.location);
        aid = aid.substring(aid.lastIndexOf("/") + 1);
        aid = aid.substring(0, aid.length - 5);
        $(".baktroppen-vote a.up").click(function () { $.get("/Vote.ashx?vote=1&artikkel=" + aid, function (msg) { if (msg == "1") { var n = parseInt($(".baktroppen-vote span").html().match(/(\d+) liker,/gi)[0][0]) + 1; $(".baktroppen-vote span").html($(".baktroppen-vote span").html().replace(/\d+ liker,/gi, n + " liker,")); } else { alert("Du har allerede stemt på denne artikkelen."); } }); return false; });
        $(".baktroppen-vote a.down").click(function () { $.get("/Vote.ashx?vote=0&artikkel=" + aid, function (msg) { if (msg == "1") { var n = parseInt($(".baktroppen-vote span").html().match(/(\d+) liker ikke/gi)[0][0]) + 1; $(".baktroppen-vote span").html($(".baktroppen-vote span").html().replace(/\d+ liker ikke/gi, n + " liker ikke")); } else { alert("Du har allerede stemt på denne artikkelen."); } }); return false; });

        //aRot
        $("#aRot div._aRoter").hoverIntent(function () { aRotSet(this); }, function () { });
        $("#aRot").hover(function () { aRotR = false; }, function () { aRotR = true; }).each(function () { aRotTimer(); });

        //YouTube-linker
        $("a[href*=youtube.com/watch?v=]").addClass("youtubeLink").each(function () {
            var yid = $(this).attr("href");
            yid = yid.substring(yid.indexOf("=") + 1);
            $(this).colorbox({ href: "http://www.youtube.com/v/" + yid + '?autoplay=1&fs=1&autohide=1', iframe: true, innerWidth: 640, innerHeight: 360, title: $(this).text() });
        });
        //flickshow
        var flickshows = 0;
        $("div.flickshow").each(function () {
            var flashvars = {};
            flickshows++;
            $(this).attr("id", "flickshow_" + flickshows);
            flashvars.galleryURL = "/gallery.ashx?x=" + escape($(this).attr("class"));
            var params = {};
            params.allowfullscreen = true;
            params.allowscriptaccess = "always";
            params.bgcolor = "FFFFFF";
            swfobject.embedSWF("/simpleviewer.swf", "flickshow_" + flickshows, "630", "550", "9.0.124", false, flashvars, params);
        });
        //FANEMENYER
        $("div.dropdownbar").each(function () {
            $(this).css("height", "0em");
            $(this).parent("li").hoverIntent(function () {
                $(this).children("div.dropdownbar").stop(true, false).animate({ "height": "1.85em" });
            }, function () {
                $(this).children("div.dropdownbar").stop(true, false).animate({ "height": "0em" });
            })
        });

        $("textarea.autoresize").autoResize({ animate: false, limit: 10000 }).trigger('keyup.dynSiz');

        $("#a2_besk").keyup(function () { $("#a2_beskC").html($("#a2_besk").val().length); }).keyup();

        $("input#a2_tittel").bind('keyup change', function () { $("input#a2_aSel").val(urlChars($(this).val())); });
        $("input.limitUrl").bind('keyup change', function () { $(this).val(urlChars($(this).val())); });

        $("div.a2_edit").each(function () {
            $(this).children("input,select,textarea").change(function () { hasChanged = true; });
            $(window).bind("beforeunload", function (event) {
                if (hasChanged) return "Du har ikkje lagra endringane på denne sida.";
            });
        })

        $("#skrivelyst").click(function () {
            var sll = $("#skrivelyst").position().left + 100;
            $('body').addClippy({
                initialPosition: { left: sll, top: 60 },
                relocateOnHover: true,
                hintChangeTimeOut: 4000,
                initialText: 'Det virker som om du har lyst til å skrive for Baktroppen. Kan jeg hjelpe deg med det?',
                hints: ["Vi tar for øyeblikket ikke imot eksterne bidrag. Kan du tenke deg å skrive for noen andre?", "Takk for interessen. Prøv igjen senere. Vil du prøve igjen nå?", "In your face! Vil du bli kvitt meg?", "Haha! Baktroppens redaksjon er på ferie på ubestemt tid og tar ikke imot eksterne bidrag. Vil du legge igjen en beskjed?", "Les din tekst klart og tydelig for meg nå, så skal jeg si hva jeg synes."]
            });
            return false;
        });

        //TICKER
        setTimeout("afterMeny()", 300);

        //KOMMENTARER
        loadKoms();

    });
var ddmzi = 100;

var hasChanged = false;

function nn(i) { if (i <= 9) { return "0" + i; } else { return i; } }

function afterMeny() {
    $("#bt-ticker").each(function () {
        var loc = new String(window.location);
        $("#" + $(this).attr("class") + " li").each(function () {
            if (loc.indexOf($(this).children("a").attr("href").replace("http://", "")) < 0) { $(this).clone().appendTo("#bt-ticker"); }
        });
        $("#bt-ticker").shuffle();
        $().newsTicker({ newsList: "#bt-ticker", controls: false, stopOnHover: false, loopDelay: 4000 });
        //setTimeout("tickerChange(1, \"" + $(this).attr("class") + "\");", 100);
    });
}

function urlChars(inp) { return inp.toLowerCase().replace(/æ/g, 'e').replace(/\s+/g, '-').replace(/ø/g, 'o').replace(/å/g, 'a').replace(/[^a-z0-9\-]/gi, '').replace(/\-+/g, '-'); }

function loadKoms() {
    $("#kommentarer").load("/kommentarer.ashx?aid=" + $("#kommentarer").attr("class"), function () {
        //YouTube-linker i kommentarer
        $("#kommentarer a[href*=youtube.com/watch?v=]").addClass("youtubeLink").each(function () {
            var yid = $(this).attr("href");
            yid = yid.substring(yid.indexOf("=") + 1);
            $(this).colorbox({ href: "http://www.youtube.com/v/" + yid, iframe: true, innerWidth: 560, innerHeight: 345, title: $(this).text() });
        });
        $("#k__send").click(function () {
            var mailreg = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/gi;
            if ($("#k__navn").val() != "") {
                if (mailreg.test($("#k__epost").val()) || mailreg.test($("#k__epost").val())) {
                    if ($("#k__k").val() != "") {
                        $(this).after("Et øyeblikk..."); $(this).remove();
                        $.ajax({ url: "/kommentarer.ashx", data: {
                            aid: $("#k__aid").val(),
                            n: $("#k__navn").val(),
                            e: $("#k__epost").val(),
                            k: $("#k__k").val()
                        }, success: function (a, b, c) { setTimeout("loadKoms()", 100); }
                        });
                    } else { alert("Du vil vel skrive inn en kommentar først?"); $("#k__k").focus(); }
                } else { alert("Du må fylle inn en gyldig e-postadresse."); $("#k__epost").focus(); }
            } else { alert("Du må fylle inn navnet ditt."); $("#k__navn").focus(); }
            return false;
        });
    });
}

$.fn.insertAtCaret = function (myValue) {
    return this.each(function () {
        //IE support
        if (document.selection) {
            this.focus();
            sel = document.selection.createRange();
            sel.text = myValue;
            this.focus();
        }
        //MOZILLA/NETSCAPE support
        else if (this.selectionStart || this.selectionStart == '0') {
            var startPos = this.selectionStart;
            var endPos = this.selectionEnd;
            var scrollTop = this.scrollTop;
            this.value = this.value.substring(0, startPos)
                                      + myValue
                              + this.value.substring(endPos, this.value.length);
            this.focus();
            this.selectionStart = startPos + myValue.length;
            this.selectionEnd = startPos + myValue.length;
            this.scrollTop = scrollTop;
        } else {
            this.value += myValue;
            this.focus();
        }
    });

};
