// nokia-main.js Main Javascript for Nokia Interactive Advertising
// Copyright (c) Sharkbyte Studios 2008

// To avoid conflicts, use the 'jq' variable instead of the $ variable
var jq = jQuery.noConflict();
var nokiasm = { src: '/++resource++nia.core.images/nsm.swf' };
var nokiasmb = { src: '/++resource++nia.core.images/nsmb.swf' };
sIFR.activate(nokiasm, nokiasmb);

// If cssQuery is not defined (loaded earlier), redefine it in terms of jQuery
// For everything but corner cases, this is good enough
if (typeof cssQuery == 'undefined') {
    function cssQuery(s, f) { return jq.makeArray(jq(s, f)) };
};

// Focus on error or first element in a form with class="enableAutoFocus"
jq(function() {
    if (jq("form div.error :input:first").focus().length) return;
    jq("form.enableAutoFocus :input:not(.formTabs):visible:first").focus();
});


this.addMinimiseButton = function() {
    var closerlink = jq("<a href='#'><img src='/++resource++nia.core.images/menu-minimiser.gif' alt='-' width='30' height='29'/></a>").click(function (ev) {
        ev.preventDefault();
        jq("#nav").animate({
            right:-131
        }, 500, function() {
            jq(".menu-maximiser").animate({
                right:0
            }, 250, function() {
              setMenuCookie('min');
            });
        });
    });
    var minimiser = jq("<div class='menu-minimiser'></div>").html(closerlink);   
    jq("#navw > ul").append(minimiser); 
}

this.addMaximiseButton = function() {
    var maximiserlink = jq("<a href='#'><img src='/++resource++nia.core.images/menu-closed.gif' alt='+' width='38' height='211'/></a>").click(function (ev) {
        ev.preventDefault();
        jq(".menu-maximiser").animate({
            right:-38
        }, 250, function() {
            jq("#nav").animate({
                right:0
            }, 500, function() {
                setMenuCookie('max');
            });
        });
    });
    var maximiser = jq("<div class='menu-maximiser'></div>").html(maximiserlink);
    jq("#nava").append(maximiser); 
}

this.setMenuCookie = function(value) {
    document.cookie = "menu=" + escape(value);
}

this.getMenuCookie = function() {
    var results = document.cookie.match ( '(^|;) ?menu=([^;]*)(;|$)' );
    if (results) {
        return (unescape(results[2]));
    } else {
        return 'max'
    }
}

this.applyMenuSettings = function() {
    cookie_value = getMenuCookie();
    if (cookie_value == 'min') {
        jq("#nav").css({'right':'-131px'})    
        jq(".menu-maximiser").css({'right':'0px'})
    }
}

this.expandContent = function() {
    jq(".extra-content").slideDown("slow");
    jq(".content-expander").fadeOut();
}

this.addExpanderButton = function() {
    var expanderlink = jq("<a href='#'><img src='/++resource++nia.core.images/cmx.gif' alt='+' width='118' height='40'/></a>").click(function (ev) {
        ev.preventDefault();
        jq(".extra-content").slideDown("slow");
        jq(".content-expander").fadeOut();
    });
    var expander = jq("<div class='content-expander'></div>").html(expanderlink);
    jq("#content").append(expander); 
}

this.addContractorButton = function() {
    var contractorlink = jq("<a href='#'><img src='/++resource++nia.core.images/cmn.gif' alt='+' width='118' height='40'/></a>").click(function (ev) {
        ev.preventDefault();
        jq(".extra-content").slideUp("slow");
        jq(".content-expander").fadeIn();
    });
    var contractor = jq("<div class='content-contractor'></div>").html(contractorlink);
    jq("#content").append(contractor); 
}

this.initNewsletter = function() {
    jq("#newsl").find("input[type='image']").click(function (ev) {
        ev.preventDefault();
        var email = jq(".news-email").attr('value');
        var region = jq(".news-region :selected").attr('value');
        jq.get("newsletter_macros", { email: email, region: region, submitted: 'true' },
            function(data){
                jq("#newsl").html(data);
                initNewsletter();
            });
        });
}

this.addNewsletter = function() {
    var newsletterlink = jq("<li><a><img src='/++resource++nia.core.images/m-newsletter.gif' alt='Newsletter'/></a><ul><div class='subnav'><div class='subnavw'><div id='newsl'></div></div></div></ul></li>");
    jq("#navw > ul > li:eq(3)").after(newsletterlink);
}

this.fixHeadings = function () {
    jq("#content h1").each(function() {
        var mytext = jq(this).text();
        var newheading = jq("<h1></h1>").text(mytext);
        jq(this).after(newheading);
        jq(this).remove();
    })
    jq("#content h2").each(function() {
        var mytext = jq(this).text();
        var newheading = jq("<h2></h2>").text(mytext);
        jq(this).after(newheading);
        jq(this).remove();
    })
    //sifr
    sIFR.replace(nokiasmb, {
        selector: 'h1',
        css: ['.sIFR-root {color: #dc0963; font-size: 20px}'],
        wmode: 'transparent',
        onRelease: expandContent,
        ratios: [6, 0.9, 8, 0.98, 9, 1.14, 10, 1.22, 11, 1.24, 12, 1.3, 14, 1.35, 16, 1.41, 18, 1.45, 21, 1.5, 22, 1.53, 25, 1.55, 27, 1.58, 30, 1.59, 32, 1.62, 36, 1.63, 38, 1.65, 42, 1.66, 43, 1.68, 44, 1.67, 48, 1.68, 50, 1.69, 55, 1.7, 60, 1.71, 67, 1.72, 74, 1.73, 85, 1.74, 99, 1.75, 118, 1.76, 119, 1.77, 120, 1.76, 1.77]
    });
    sIFR.replace(nokiasm, {
        selector: 'h2',
        css: ['.sIFR-root {color: #dc0963; font-size: 16px}'],
        wmode: 'transparent',
        ratios: [6, 0.88, 7, 0.93, 8, 1.02, 9, 1.07, 11, 1.15, 12, 1.26, 14, 1.28, 16, 1.35, 17, 1.39, 19, 1.42, 21, 1.46, 24, 1.48, 25, 1.51, 28, 1.52, 32, 1.56, 34, 1.58, 37, 1.59, 41, 1.6, 42, 1.61, 46, 1.62, 50, 1.63, 54, 1.64, 58, 1.65, 64, 1.66, 72, 1.67, 80, 1.68, 94, 1.69, 95, 1.7, 96, 1.69, 107, 1.7, 108, 1.71, 111, 1.7, 112, 1.71, 113, 1.7, 1.71]

    });
}

jq(document).ready(function() {
    
    fixHeadings();
    
    // add the newsletter
    //addNewsletter()

    // add the minimise button
    addMinimiseButton()
        
    // add the maximise button
    addMaximiseButton()
        
    // apply the navigation settings
    applyMenuSettings()
    
    // remove the hover class from any menu that is active    
    jq(".navigation li a img").each(function() {
        if (jq(this).attr("src").match(/-on\.(.+)$/i)) {
            jq(this).removeClass("hover");
        }
    });
    
    jq("#navw > ul > li:eq(3) > ul").remove();
    
    jq("#navw > ul > li:eq(4) > ul").remove();
    
    // add the expanderand
    // hide the extra content
    if ( jq(".extra-content").length > 0 ) {
        if( jq(".default-expand").length > 0) {
            addExpanderButton()
            addContractorButton()  
            jq(".content-expander").hide();   
        } else {
            jq(".extra-content").hide();
            addExpanderButton()
            addContractorButton()      
        }
    }
    
    jq("#nav li").hover(
        function() {
            jq(this).addClass("hover").find("ul").fadeIn();
            jq(this).find("img:first").each(function() {
                s = jq(this).attr("src").replace(/-on\.gif/i, ".gif").replace(/\.gif/i, "-on.gif");
                jq(this).attr("src", s)
            })
        },
        function() {
            jq(this).removeClass("hover").find("ul").hide();
            jq(this).find("img:first").each(function() {
                s = jq(this).attr("src").replace(/-on\.gif/i, ".gif");
                jq(this).attr("src", s)
            })
        }
    )

    //var onewsl = jq("#onewsl");
    //jq("#newsl").html(onewsl.html());   
    //initNewsletter();
    
    jq(".pfg-form input[type='submit']").after("<input type='image' class='pfg-submit' src='/++resource++nia.core.images/button-submit.gif' />").hide();

    jq("a[href$='brands-love-mobile'],a[href$='sponsor-our-team']").each(function() {
      jq(this).parent().prev().addClass('last');
      jq(this).parent().remove();
    });

});
