/// <reference path="jquery-1.3.1-vsdoc.js" />
/// <reference path="swfobject-vsdoc.js" />
/// <reference path="sifr-vsdoc.js" />

$(document).ready(function() {

    /*
    * FLASH DETECTION  
    * Uses jQuery for adding elements. Displays a modal dialog box over a semi-transparent background if 
    * flash version too old. 
    * 
    * Todo: Perhaps store in cookies that user has already pressed "ok" to avoid showing message on every load.
    */
    if (!swfobject.hasFlashPlayerVersion("9.0.0")) {
        var message = "<b>Flash Player 9 required</b><br />This site requires flash player 9.0.0 or newer. Please <a href='http://www.adobe.com/products/flashplayer/'>download here</a><br/><br/><input type=\"button\" style=\"float:right\" onclick=\"JavaScript:$('#__flashversion').remove();\" value=\"OK\" />";
        var bgcolor = "#FFFFFF";    // color of background
        var msg_width = "200";      // pixels
        var msg_height = "80";      // pixels
        var opacity = 80;           // background per cent visible

        $("body").append("<div id='__flashversion'></div>");
        var flash = $("#__flashversion");
        flash.append("<div id='__flashversion_bg' style=\"position:absolute;background:" + bgcolor + ";-khtml-opacity:." + opacity + "; -moz-opacity:." + opacity + "; -ms-filter:'alpha(opacity=" + opacity + ")'; filter:alpha(opacity=" + opacity + "); opacity:." + opacity + ";left:0;top:0;width:100%;height:100%;\"></div>");
        flash.append("<div style='position:absolute;margin-left:-" + (msg_width / 2) + "px;margin-top:-" + (msg_height / 2) + "px;background:" + bgcolor + ";width:" + msg_width + "px;height:" + msg_height + "px;left:50%;top:50%;padding:20px;text-align:left;border:1px solid #aaaaaa;'>" + message + "</div>");
    }

    // attach flash version of top menu
    //var att = { data: "swf/preloader.swf", width: "899", height: "119" };
    var att = { data: "swf/MenuTop.swf", width: "899", height: "119" };
    //var par = { wmode: "transparent", flashvars: "url=" + escape("swf/MenuTop.swf?css=MenuTop" + color + ".css" + "&menu=" + menu) + "&pre_width=100%25&pre_height=1&pre_bg=0xFFFFFF&pre_fg=0x4d4d4d" };
    var par = { wmode: "transparent", flashvars: "css=MenuTop" + color + ".css" + "&menu=" + menu };
    var id = "menu-top";
    var myObject = swfobject.createSWF(att, par, id);

    // attach flash version of font-newsbox
    //var att = { data: "swf/preloader.swf", width: "900", height: "359" };
    var att = { data: "swf/StartNews.swf", width: "900", height: "359" };
    //var par = { wmode: "transparent", flashvars: "url=" + escape("swf/StartNews.swf?css=StartNews" + color + ".css") + "&pre_width=100%25&pre_height=1&pre_bg=0xFFFFFF&pre_fg=0x4d4d4d" };
    var par = { wmode: "transparent", flashvars: "css=StartNews" + color + ".css", allowFullScreen: true };
    var id = "start-news";
    var myObject = swfobject.createSWF(att, par, id);

    // attach flash version of bottom-scroller via preloader
    //var att = { data: "swf/preloader.swf", width: "100%", height: "257" };
    var att = { data: "swf/BottomScroller.swf", width: "100%", height: "257" };
    //var par = { wmode: "transparent", flashvars: "url=" + escape("swf/BottomScroller.swf?css=BottomScroller" + color + ".css&menu=" + menu) + "&pre_width=100%25&pre_height=1&pre_bg=0xFFFFFF&pre_fg=0x4d4d4d" };
    var par = { wmode: "transparent", flashvars: "css=BottomScroller" + color + ".css&menu=" + menu };
    var id = "bottom-scroller";
    var myObject = swfobject.createSWF(att, par, id);

    // attach flash version of bottom-scroller-boats via preloader
    //var att = { data: "swf/preloader.swf", width: "100%", height: "257" };
    var att = { data: "swf/BottomScrollerBoats.swf", width: "100%", height: "257" };
    //var par = { wmode: "transparent", flashvars: "url=" + escape("swf/BottomScrollerBoats.swf?css=BottomScrollerBoats" + color + ".css&menu=" + menu) + "&pre_width=100%25&pre_height=1&pre_bg=0xFFFFFF&pre_fg=0x4d4d4d" };
    var par = { wmode: "transparent", flashvars: "css=BottomScrollerBoats" + color + ".css&menu=" + menu };
    var id = "bottom-scroller-boats";
    var myObject = swfobject.createSWF(att, par, id);

    // attach flash version of MenuLeft via preloader
    //var att = { data: "swf/preloader.swf", width: "320", height: "445" };
    var att = { data: "swf/MenuLeft.swf", width: "320", height: "445" };
    //var par = { wmode: "transparent", flashvars: "url=" + escape("swf/MenuLeft.swf?css=MenuLeft" + color + ".css&menu=" + menu) + "&pre_width=330&pre_height=1&pre_bg=0xFFFFFF&pre_fg=0x4d4d4d" };
    var par = { wmode: "transparent", flashvars: "css=MenuLeft" + color + ".css&menu=" + menu };
    var id = "menu-left";
    var myObject = swfobject.createSWF(att, par, id);

    // attach flash version of LandingPage via preloader
    //var att = { data: "swf/preloader.swf", width: "585", height: "317" };
    var att = { data: "swf/LandingPage.swf", width: "585", height: "317" };
    //var par = { wmode: "transparent", flashvars: "url=" + escape("swf/LandingPage.swf?css=LandingPage" + color + ".css&menu=" + menu) + "&pre_width=591&pre_height=317&pre_bg=0xd9e6f0&pre_fg=0x4d4d4d" };
    var par = { wmode: "transparent", flashvars: "css=LandingPage" + color + ".css&menu=" + menu };
    var id = "landing-page";
    var myObject = swfobject.createSWF(att, par, id);

    // Hover events for doc list ------------------------------------------------------------------------------------------------

    $(".doc-list-item").hover(function() {
        $(this).addClass("doc-list-item-hover");
    }, function() {
        $(this).removeClass("doc-list-item-hover");
    });

    $(".anim-list-item").hover(function() {
        $(this).addClass("anim-list-item-hover");
    }, function() {
        $(this).removeClass("anim-list-item-hover");
    });

    $(".boat-list-item").hover(function() {
        $(this).addClass("boat-list-item-hover");
        $(this).find(".boat-list-item-overlay").css("left", -3000);
    }, function() {
        $(this).removeClass("boat-list-item-hover");
        $(this).find(".boat-list-item-overlay").css("left", 0);
    });

    // Download pdf link in doc-boat (or any doc) get styled in a special way
    // which color scheme is active?
    if (color == "BW") {
        $(".doc1-body a[href$='.pdf']").each(function() {
            $(this).attr("target", "_blank");
            $(this).wrap("<div class='doc-boat-download-outer doc-boat-download'></div>");
            //($(this).addClass("doc-boat-download");
        });
    }
    else {
        $(".doc1-body a[href$='.pdf']").each(function() {
            $(this).attr("target", "_blank"); 
            $(this).wrap("<div class='doc-boat-download-outer-color doc-boat-download-color'></div>");
            //$(this).addClass("doc-boat-download-color");
        });
    }

    // Gallery button in boat (or any doc) get styled in a special way
    // which color scheme is active?
    if (color == "BW") {
    	$(".img-gallery-button").addClass("doc-gallery-bw").text("");
    }
    else {
    	$(".img-gallery-button").addClass("doc-gallery-color").text("");
    }

    // Download pdf link for publication list also get styled specially
    // which color scheme is active?
    if (color == "BW") {
        $(".doc-list-publication a[href$='.pdf']").each(function() {
            $(this).attr("target", "_blank"); 
            $(this).wrap("<div class='doc-publication-download'></div>");
        });
    }
    else {
        $(".doc-list-publication a[href$='.pdf']").each(function() {
            $(this).attr("target", "_blank"); 
            $(this).wrap("<div class='doc-publication-download-color'></div>");
        });
    }

    // Video player
    $(".doc1-body a[href$='.flv']").each(function(i) {
        $(this).attr("id", "video-a-" + i);
        var att = { data: "swf/VideoPlayer.swf", width: "597", height: "380" };
        var par = { wmode: "transparent", flashvars: "url=" + $(this).attr("href"), allowFullScreen: true };
        var myObject = swfobject.createSWF(att, par, "video-a-" + i);
    });


    // Window event handling ---------------------------------------------------------------------------------------------------- 

    // add resize event on window - to position elements according to Mister Senior Art Director Tom Olsens disgressions.
    $(window).resize(function() { repos($(this)) });
    $("#bottom-scroller").css("position", "absolute");
    $("#bottom-scroller").css("left", "0");
    $("#bottom-scroller-boats").css("position", "absolute");
    $("#bottom-scroller-boats").css("left", "0");
    $("#credit").css("position", "absolute");
    $("#start-news").css("position", "relative");
    $("#start-news").css("margin-top", "0");
    $("#start-news").css("left", "34");

    // when sifr gets run, sometimes the replaced flash text is higher than the original, and then we need to resize the background too
    /*
    var dlay = setInterval(function(){
    if($(".sIFR-replaced").length>=sIFR.replacements.length){
    // resize when all sIFR elements have loaded
    repos($(window))
    // stop polling
    clearInterval(dlay);
    };
    },100);
    */

});

// SIFR Flash Text ----------------------------------------------------------------------------------------------------------

    // replace different tags with flash version (using the fantastic sifr 3)
    var mrsEavesItalic = { src: 'swf/mrseavesitalic.swf' };
    var mrsEavesRoman = { src: 'swf/mrseavesroman.swf' };
    var fffGlorious = { src: 'swf/fffglorious.swf' };
    //sIFR.useDomLoaded = false;
    sIFR.domains = ['*'];
    sIFR.activate(mrsEavesItalic, mrsEavesRoman, fffGlorious);

    // which color scheme is active?
    if (color == "BW") {

        // replace h1 tags
        sIFR.replace(mrsEavesRoman, {
            selector: 'h1',
            css: '.sIFR-root { color: #FFFFFF; font-size:18; letter-spacing:1; leading:3; text-transform:uppercase; }',
            transparent: true,
            antiAliasType: 'advanced',
            ratios: [8, 1.3, 9, 1.22, 10, 1.25, 15, 1.22, 19, 1.18, 27, 1.17, 28, 1.16, 37, 1.15, 48, 1.14, 49, 1.13, 55, 1.14, 84, 1.13, 85, 1.12, 86, 1.13, 90, 1.12, 91, 1.13, 1.12]
        });


        // replace h2 tags
        sIFR.replace(mrsEavesItalic, {
            selector: 'h2',
            css: [
                    '.sIFR-root { color: #727070; font-size:20; letter-spacing:1; leading:3 }',
                    'a { text-decoration: none; }',
                    'a:link { color: #727070; }',
                    'a:hover { color: #727070; }'
                ],
            transparent: true,
            antiAliasType: 'advanced',
            ratios: [8, 1.3, 9, 1.22, 10, 1.25, 15, 1.22, 19, 1.18, 27, 1.17, 28, 1.16, 37, 1.15, 48, 1.14, 49, 1.13, 55, 1.14, 84, 1.13, 85, 1.12, 86, 1.13, 90, 1.12, 91, 1.13, 1.12]
        });        

        // replace h3 tags
        sIFR.replace(mrsEavesRoman, {
            selector: 'h3',
            css: [
                        '.sIFR-root { color: #FFFFFF; font-size:11; letter-spacing:1; leading:3; text-transform:uppercase }',
                        'a { text-decoration: none; }',
                        'a:link { color: #FFFFFF; }',
                        'a:hover { color: #FFFFFF; }'
                    ],
            transparent: true,
            antiAliasType: 'advanced'
        });


    }
    else {

        // replace h1 tags
        sIFR.replace(mrsEavesRoman, {
            selector: 'h1',
            css: '.sIFR-root { color: #000000; font-size:18; letter-spacing:1; leading:3; text-transform:uppercase; }',
            transparent: true,
            antiAliasType: 'advanced',
            ratios: [8, 1.3, 9, 1.22, 10, 1.25, 15, 1.22, 19, 1.18, 27, 1.17, 28, 1.16, 37, 1.15, 48, 1.14, 49, 1.13, 55, 1.14, 84, 1.13, 85, 1.12, 86, 1.13, 90, 1.12, 91, 1.13, 1.12]
        });

        // replace h2 tags
        sIFR.replace(mrsEavesItalic, {
            selector: 'h2',
            css: [
                    '.sIFR-root { color: #727070; font-size:20; letter-spacing:1; leading:3 }',
                    'a { text-decoration: none; }',
                    'a:link { color: #727070; }',
                    'a:hover { color: #727070; }'
                ],            
            transparent: true,
            antiAliasType: 'advanced'
        });


        // replace h3 tags
        sIFR.replace(mrsEavesRoman, {
            selector: 'h3',
            css: [
                        '.sIFR-root { color: #000000; font-size:11; letter-spacing:1; leading:3; text-transform:uppercase }',
                        'a { text-decoration: none; }',
                        'a:link { color: #000000; }',
                        'a:hover { color: #000000; }'
                    ],
            transparent: true,
            antiAliasType: 'advanced'
        });

    }

    // replace h4 tags for boat document
    sIFR.replace(mrsEavesRoman, {
        selector: 'h4',
        css: [
                '.sIFR-root { color: #fcfcfc; font-size:11; letter-spacing:1; leading:3; text-transform:uppercase }'
            ],
        transparent: true,
        quality: 'high',
        antiAliasType: 'advanced'
    });

    if (color == "BW") {
        // FFF Glorious for download link
        sIFR.replace(fffGlorious, {
            selector: '.doc-boat-download',
            css: [
                    '.sIFR-root { color: #FFFFFF; font-size:8; text-transform:uppercase; margin-left:2; cursor:pointer}',
                    'a { text-decoration: none; color: #FFFFFF }',
                    'a:link { color: #FFFFFF; }',
                    'a:hover { color: #FFFFFF; }'
                ],
            transparent: true,
            antiAliasType: 'none',
            pixelFont: true
        });
    }
    else {
        // FFF Glorious for download link
        sIFR.replace(fffGlorious, {
            selector: '.doc-boat-download-color',
            css: [
                '.sIFR-root { color: #000000; font-size:8; text-transform:uppercase; margin-left:2; cursor:pointer}',
                'a { text-decoration: none; color: #FFFFFF }',
                'a:link { color: #000000; }',
                'a:hover { color: #000000; }'
            ],
            transparent: true,
            antiAliasType: 'none',
            pixelFont: true
        });
    }

    // FFF Glorious for download link on publication list
    sIFR.replace(fffGlorious, {
        selector: '.doc-publication-download-color',
        css: [
                '.sIFR-root { color: #000000; font-size:8; text-transform:uppercase; margin-left:55; cursor:pointer}',
                'a { text-decoration: none; }',
                'a:link { color: #000000; }',
                'a:hover { color: #000000; }'
            ],
        transparent: true,
        antiAliasType: 'none',
        pixelFont: true
    });    

// repositions boxes on the webside according to size of screen. 
function repos(w) {
    // Get height of window
    var wh = w.height();
    var ih = $("#inner-frame").height();
    var bsh = $("#bottom-scroller").height();
    var bsbh = $("#bottom-scroller-boats").height();
    var mth = $("#menu-top").height();
    var snh = $("#start-news").height();

    var ch = $("#content").height() + 83;
    if (ch < $("#menu-left").height()+20) ch = $("#menu-left").height()+20;
    ch += mth;

    var bh = wh;

    var totalh = bsh + ch;

    if (w.height() > totalh) totalh = w.height();
    if (totalh > wh) wh = totalh;
    
    // position bottom scroller
    if ($("#bottom-scroller")) {
        if (wh > 760) {
            $("#bottom-scroller").css("top", wh - bsh);
        }
        else {
            $("#bottom-scroller").css("top", 760 - bsh);
        }
    }
    
    // position bottom scroller for boats
    if ($("#bottom-scroller-boats")) {
        if (wh > 760) {
            $("#bottom-scroller-boats").css("top", wh - bsbh);
        }
        else {
            $("#bottom-scroller-boats").css("top", 760 - bsbh);
        }
    }    
    
    // position credit if exists
    if ($("#credit")){
        var credh = $("#credit").height();
        $("#credit").css("top", totalh - credh);
        $("#credit").css("left", (w.width() / 2) - ($("#credit").width() / 2));

    }

    // position start-news
    if ($("#start-news")) {
        if (wh > 760) {
            var height = wh - bsh - mth;
            $("#start-news").css("top", (height / 2) - ((snh - 22) / 2));
        }
        else {
            var height = 760 - bsh - mth;
            $("#start-news").css("top", (height / 2) - ((snh - 22) / 2));
        }
    }

    // resize body (so background fills all way down)

    if ($("#content")) {
        if (bh < totalh) bh = totalh;
    }
    $("body").height(totalh);
}

// size leftmenu
function sizeLeftMenu(height) {
    //alert(height);
    //$("#menu-left").animate( { "height": height }, "slow", "easein");
    if (height < 400) height = 400;
    $("#menu-left").attr("height", height);
    repos($(window));
}