﻿google.load("jquery", "1.6.3");

google.setOnLoadCallback(function() {
    $(document).ready(function() {
        init();
    });
});

function init() {
    rigWysiwyg();
    rigEditor();

    //rigYahooRTE();
    checkCookie();
    //rigApplyButton();
    rigLogOnButton();
    rigUnBlackout();
    markCurrentSection();
    rigFaq();
    rigTables();

    initRotator();
}

function markCurrentSection() {
    var url = document.location + "";
    //Main section
    var currentSection = url.split("/")[3];
    if (currentSection != "" && currentSection != "#") {
        $("." + currentSection).addClass("current");
    } else {
        $(".home").addClass("current");
    }

    //Sub section
    $("#subNav li ul:not(.alwaysOpen)").css("display", "none");
    currentSection = url.split("/")[4];
    if (currentSection != "") {
        $("." + currentSection).addClass("current");
    }
    $("#subNav li li.current").parent().parent().addClass("current");
    $("#subNav li.current ul").css("display", "block");
    $("#subNav li li.current").css("display", "block"); 
}

function rigWysiwyg() {
    var wysiwyg = $(".wysiwyg");

    if (wysiwyg.length > 0) {
        $(wysiwyg).rte({
                css: ['/content/css/screen.css'],
                controls_rte: rte_toolbar,
                controls_html: html_toolbar
        });
    }
}

function rigApplyButton() {
    var applyButton = $("#applyButton");

    if (applyButton.length > 0) {
        $(applyButton).click(function() { showApply(); });
    }

    $("#applicationLinks a").click(function() { unBlackout(); });
}

function rigLogOnButton() {
    var logOnButton = $("#logOnButton");
    if (logOnButton.length > 0) {
        $(logOnButton).click(function() { showLogOn(); });
    }

    $("#logOnLinks a").click(function() { unBlackout(); });
}

function rigUnBlackout() {
    var blackout = $("#blackout");
    if (blackout.length > 0) {
        $(blackout).click(function() { unBlackout(); });
    }
}

function showApply() {
    blackout();
    //$("#applicationLinks").fadeTo(0, 0);
    $("#applicationLinks").css("display", "block");
    //$("#applicationLinks").fadeTo(300, 1);
}

function showLogOn() {
    blackout();
    //$("#logOnLinks").fadeTo(0, 0);
    $("#logOnLinks").css("display", "block");
    //$("#logOnLinks").fadeTo(200, 1);
}

function blackout() {
    $("#markets").css("visibility", "hidden");
    var blackout = $("#blackout");
    $("#blackout").fadeTo(0, 0);
    $("#blackout").css("display", "block");
    $("#blackout").fadeTo(200, .8);
}

function unBlackout() {
    if ($.cookie("galvanRisk") != null) {
        var blackout = $("#blackout");
        $("#blackout").fadeTo(200, 0, function() { $("#blackout").css("display", "none"); });
        $("#applicationLinks").css("display", "none");
        $("#logOnLinks").css("display", "none");
        $("#riskWarning").css("display", "none");
        $("#markets").css("visibility", "visible");
    }
}

function rigFaq() {
    var faq = $(".faq");

    if (faq.length > 0) {
        $(".faq dd").hide();

        $("dt").click(function() { toggleAnswer(this) });
    }
}

function toggleAnswer(what) {

    if ($(what).hasClass("open")) {
        $(what).removeClass("open")
        $(what).next("dd").hide(100);
    } else {
        $(what).next("dd").show(100);
        $(what).addClass("open")
    }
}

function rigTables()
{
    $("tr:nth-child(odd)").addClass("odd");
}

function rigYahooRTE() {
    var myEditor = new YAHOO.widget.Editor('Body', {
        height: '300px',
        width: '500px',
        dompath: true, //Turns on the bar at the bottom
        animate: true //Animates the opening, closing and moving of Editor windows
    });
    myEditor.render();
}

function rigEditor() {
    $("input:radio[name=isPDF]").click(function() { checkMode(); })
    checkMode();
}

function checkMode() {
    var value = $("input:radio[name=isPDF]:checked").val();
    if (value == "true") {
        $("#PDFUpload").show(300);
        $("#HTMLContent").hide(300);
    }
    else {
        $("#PDFUpload").hide(300);
        $("#HTMLContent").show(300);
    }
}

function checkCookie() {
    if ($.cookie("galvanRisk") == null) {
        if ($("#Forename").length <= 0) {
            showRiskWarning();
        }
        //For testing checkCookie()
        //var date = new Date();
        //date.setTime(date.getTime() + (30000));
    }
    else {
        unBlackout();
    }
    rigRiskWarningButton();
}

function showRiskWarning() {
    blackout();
    $("#riskWarning").css("display", "block");
}

function rigRiskWarningButton() {
    $("#riskWarning .riskWarningButton a").click(function() {
    //Create cookie to not display overlay for 24hrs
        //var date = new Date();
        //date.setTime(date.getTime() + (30000));
        $.cookie("galvanRisk", "risk accepted", { expires: 1 });
        unBlackout();
    });
}

function initRotator() {
    if ($("#rotator").length > 0) {
        $("#rotator").coinslider({
            spw: 5, // squares per width
            sph: 5, // squares per height
            width: 598,
            height: 300,
            delay: 3000, // delay between images in ms
            sDelay: 30, // delay beetwen squares in ms
            navigation: false,
            effect: 'rain',
            links: false,
            hoverPause: false
        });
    }
}
