﻿$(document).ready(function () {

    // Add pdf icons to pdf links
    $(".attachments a").addClass("download"); //default icon, might be overridden later    
    $(".attachments a[href$='.pdf']").addClass("pdf");
    $(".attachments a[href$='.mp3'], .attachments a[href$='.wav']").addClass("audio");
    $(".attachments a[href$='.doc'], .attachments a[href$='.rtf'], .attachments a[href$='.docx']").addClass("word");
    $(".attachments a[href$='.xls'], .attachments a[href$='.xlsx'], .attachments a[href$='.csv']").addClass("excel");
    $(".attachments a[href$='.jpg'], .attachments a[href$='.png'], .attachments a[href$='.gif'], .attachments a[href$='.tif'], .attachments a[href$='.tiff'], .attachments a[href$='.bmp']").addClass("picture");
    $(".attachments a[href$='.avi'], .attachments a[href$='.flv'], .attachments a[href$='.wmv'], .attachments a[href$='.mov'], .attachments a[href$='.divx'], .attachments a[href$='.mpeg']").addClass("video");
    $(".attachments a[href$='.zip'], .attachments a[href$='.rar']").addClass("zip");

    $(".recipelink").click(function () {
        var id = $(this).attr('href').substr(1);
        var content = $("#recipe" + id).html();

        TINY.box.show(content, 0, 693, 0, 1);

        return false;
    });

    $("#tinybox .popupClose").live("click", function () {
        TINY.box.hide();
        return false;
    });

    $(".klover1").click(function () {
        var content = $("#popupKlover").html();
        TINY.box.show(content, 0, 693, 0, 1);

        if (pageTracker != undefined)
            pageTracker._trackEvent('Popup', 'Åpnet Kløver generell', document.location.pathname);

        return false;
    });

    $(".klover3").click(function () {
        var content = $("#popupKloverBakeriet").html();
        TINY.box.show(content, 0, 693, 0, 1);

        if (pageTracker != undefined)
            pageTracker._trackEvent('Popup', 'Åpnet Kløver bakeriet', document.location.pathname);

        return false;
    });

    $(".klover2").click(function () {
        var content = $("#popupKloverKjott").html();
        TINY.box.show(content, 0, 693, 0, 1);

        if (pageTracker != undefined)
            pageTracker._trackEvent('Popup', 'Åpnet Kløver kjøtt', document.location.pathname);

        return false;
    });

    $(".firstprice").click(function () {
        var content = $("#popupFirstprice").html();
        TINY.box.show(content, 0, 693, 0, 1);

        if (pageTracker != undefined)
            pageTracker._trackEvent('Popup', 'Åpnet firstprice', document.location.pathname);
        Adform.Tracking.Track(29765, 121214, ['']);

        return false;
    });

    $(".tilbudsavis").click(function () {
        var widht = Math.round($(window).width() * 0.9);
        var height = Math.round($(window).height() * 0.9);

        var html = '<a href="#" class="popupClose" style="top: -25px; right: 0"><img src="/App_Themes/bunnpris/images/btn_close.png" alt="x" /></a>'
                    + '<div style="line-height:0;"><object type="application/x-shockwave-flash" data="/tilbudsavis/bunnpris.swf" width="' + widht + '" height="' + height + '">'
	                    + '<param name="movie" value="/tilbudsavis/bunnpris.swf">'
                    + '</object></div>';

        TINY.box.show(html, 0, widht, height, 1);

        if (pageTracker != undefined)
            pageTracker._trackEvent('Popup', 'Åpnet tilbudsavis', document.location.pathname);
        Adform.Tracking.Track(29765,121211,['']);

        return false;

    });

    $(".historie").click(function () {
        var width = 650;
        var height = 406;
        var m = "/movie/bunnprisfilmen.swf";

        var html = '<a href="#" class="popupClose" style="top: -25px; right: 0"><img src="/App_Themes/bunnpris/images/btn_close.png" alt="x" /></a>'
            + '<div style="line-height:0;"><object height="' + height + '" width="' + width + '">'
            + '<param name="movie" value="' + m + '">'
            + '<param name="allowFullScreen" value="true"><param name="allowscriptaccess" value="always">'
            + '<embed src="' + m + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" height="' + height + '" width="' + width + '">'
            + '</object></div>';

        TINY.box.show(html, 0, width, height, 1);

        if (pageTracker != undefined)
            pageTracker._trackEvent('Popup', 'Åpnet historiefilm', document.location.pathname);

        Adform.Tracking.Track(29765, 121208, ['']);

        return false;

    });

    $(".footerBanner").click(function () {
        if (pageTracker != undefined)
            pageTracker._trackEvent('Bannerklikk', $(this).attr('id'), document.location.pathname);
    });

    $("#col2 #rotator2").click(function () {
        if (pageTracker != undefined)
            pageTracker._trackEvent('Bannerklikk', 'Forsidebanner', document.location.pathname);
    });

    // esc - close popup
    $(document).keydown(function (e) {
        if (e.which == 27) {  // escape, close box
            try {
                TINY.box.hide();
            } catch (e) { }
            hideFb();
        }
    });


    // Facebook flip
    $(document).ready(function () {
        $("#fb-tab").mouseenter(function () {
            showFb();
            return false;
        });
        $("#fb-tab").click(function () {
            toggleFb();
            return false;
        });
        $("body").click(function () {
            hideFb();
        });
        $("#fb").click(function (e) {
            e.stopPropagation();
        });
    });

    var showFb = function () {

        if ($("#fb").css('right') != '-312px')
            return;

        $("#fb").animate({
            right: 0
        }, 300);

        if (pageTracker != undefined)
            pageTracker._trackEvent('FaceBook', 'Open flip', document.location.pathname);
    };
    var hideFb = function () {

        if ($("#fb").css('right') == '-312px')
            return;

        $("#fb").animate({
            right: '-312px'
        }, 300);

        pageTracker._trackEvent('FaceBook', 'Close flip', document.location.pathname);
    };

    function toggleFb() {
        if ($("#fb").css('right') != '-312px')
            hideFb();
        else
            showFb();
    }

    // tracking banner clicks
    $("#bannerTopRight").click(function(){
        pageTracker._trackEvent('Banner topp-høyre', 'Click', document.location.pathname);
        Adform.Tracking.Track(29765,121209,['']);
    });
    $("#bannerBottomMiddle").click(function(){
        pageTracker._trackEvent('Banner bunn-midten', 'Click', document.location.pathname);
        Adform.Tracking.Track(29765,121207,['']);
    });
    $("#bannerBottomLeft").click(function(){
        pageTracker._trackEvent('Banner bunn-venstre', 'Click', document.location.pathname);
        Adform.Tracking.Track(29765,121210,['']);
    });
    $("#bannerBottomRigt").click(function(){
        pageTracker._trackEvent('Banner bunn-høyre', 'Click', document.location.pathname);
        Adform.Tracking.Track(29765,121212,['']);
    });
    $("#rotator2").click(function(){
        pageTracker._trackEvent('Banner stor forside', 'Click', document.location.pathname);
        Adform.Tracking.Track(29765,121213,['']);
    });
});

function popupImage(url) {
    TINY.box.show("<img src=\"" + url + "\" alt=\" \" />", 0, 595, 842, 1);

}


