﻿$(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;
    });

    $(".klover4").click(function () {
        var content = $("#popupKloverKylling").html();
        TINY.box.show(content, 0, 693, 0, 1);

        if (pageTracker) pageTracker._trackEvent('Popup', 'Åpnet Kløver kylling', document.location.pathname);

        return false;
    });

    $(".tilbudsavis").click(function () {
        openTilbudsavis();
        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);
            
        return false;
    });


    $(".dinnercampaign").click(function () {
        var width = 648;
        var height = 365;
        var movie = "/movie/" + $(this).attr('href').substr(1);

        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;">'
            + GetFlashVideoCode(movie, "", width, height)
            + '</div>';

        TINY.box.show(html, 0, width, height, 1);

        if (pageTracker != undefined)
            pageTracker._trackEvent('Popup', 'Åpnet middagskampanje', document.location.pathname);
            
        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);

        if (pageTracker) pageTracker._trackEvent('FaceBook', 'Close flip', document.location.pathname);
    };

    function toggleFb() {
        if ($("#fb").css('right') != '-312px')
            hideFb();
        else
            showFb();
    }

    // tracking banner clicks
    $("#rotator1").click(function () {
        if (pageTracker) pageTracker._trackEvent('Banner topp-høyre', 'Click', document.location.pathname);
    });
    $("#bannerBottomMiddle").click(function () {
        if (pageTracker) pageTracker._trackEvent('Banner bunn-midten', 'Click', document.location.pathname);
    });
    $("#bannerBottomLeft").click(function () {
        if (pageTracker) pageTracker._trackEvent('Banner bunn-venstre', 'Click', document.location.pathname);
    });
    $("#bannerBottomRigt").click(function () {
        if (pageTracker) pageTracker._trackEvent('Banner bunn-høyre', 'Click', document.location.pathname);
    });
    $("#rotator2").click(function () {
        if (pageTracker) pageTracker._trackEvent('Banner stor forside', 'Click', document.location.pathname);
    });


    $("#tinybox a#close").live('click', function () {
        TINY.box.hide();
        return false;
    });
});

function popupImage(href) {
    var alt = "";

    $("body").append($("<img src=\"/App_Themes/Bunnpris/images/loader.gif\" alt=\"\" />").attr("id", "loader").attr("style", "position: fixed; top: 50%; left: 50%; z-index: 1000"));
    var img = new Image();
    $(img).load(function () {
        var w = this.width;
        var h = this.height;

        var mw = Math.round($(window).width() * 0.9);
        var mh = Math.round($(window).height() * 0.9);

        var size = calcNewDimensions(w, h, mw, mh);
        var width = size[0];
        var height = size[1];

        $("#loader").remove();
        TINY.box.show("<a href=\"#close\" id=\"close\"><img src=\"" + href + "\" alt=\"" + alt + "\" style=\"width: " + width + "px; height: " + height + "px;\" /></a>", 0, width, height, 1);
    })
        .attr('src', href);

}

function calcNewDimensions(w, h, maxWidth, maxHeight) {
    if (w < maxWidth && h < maxHeight)
        return [w, h];

    var scaleW = maxWidth / w;
    var scaleH = maxHeight / h;

    if (scaleW < scaleH)
        return [Math.round(w * scaleW), Math.round(h * scaleW)];
    else
        return [Math.round(w * scaleH), Math.round(h * scaleH)];

};

function GetFlashVideoCode(filename, thumb, width, height) {
    var player = "/flvtools/player.swf";
    var skin = "/flvtools/skin.swf";
    var ran = String((new Date()).getTime()).replace(/\D/gi, '');

    return "<embed flashvars=\"file=" + filename + "&controlbar=none&image=" + thumb + "&skin=" + skin + "&autostart=true\" allowfullscreen=\"true\" allowscriptaccess=\"always\" id=\"container" + ran + "\" name=\"container" + ran + "\" src=\"" + player + "\" width=\"" + width + "\" height=\"" + height + "\" />";
}


function openTilbudsavis() {
    try {
        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);

    } catch (e) {
    }
}
