var activeCountdown = 0;
var countdownDates = [new Date(2011, 9 - 1, 8), new Date(2011, 9 - 1, 16), new Date(2011, 9 - 1, 21), new Date(2011, 9 - 1, 27),
    new Date(2012, 2 - 1, 9), new Date(2012, 2 - 1, 17), new Date(2012, 2 - 1, 22), new Date(2012, 2 - 1, 28),
    new Date(2012, 9 - 1, 13), new Date(2012, 9 - 1, 14), new Date(2012, 9 - 1, 21), new Date(2012, 9 - 1, 28),
    new Date(2013, 2 - 1, 7), new Date(2013, 2 - 1, 16), new Date(2013, 2 - 1, 21), new Date(2013, 2 - 1, 27),
    new Date(2013, 9 - 1, 12), new Date(2013, 9 - 1, 13), new Date(2013, 9 - 1, 20), new Date(2013, 9 - 1, 27)
];

if (typeof jQuery == 'undefined') {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'http://code.jquery.com/jquery-latest.min.js';
    document.body.appendChild(script);
    setTimeout("checkJQuery();", 10);
} else {
    hofWdgtStarup();
}

function checkJQuery() {
    if (typeof jQuery == 'undefined') {
        setTimeout("checkJQuery();", 10);
    } else {
        if (!jQuery().countdown) {
            $.getScript('http://hofprod.fastnet.co.uk/creativeAssets/fashionweek_countdown_timer/assets/scripts/jquery.countdown.js', function () {
                hofWdgtStarup();
            });
        } else {
            hofWdgtStarup();
        }
    }
}

function hofWdgtStarup() {
    $(".hfEvt").css({ display: 'block' });
    $(".hfEvt").hide();
    //check if the first couple is past
    for (var i = 0; i < countdownDates.length; i++) {
        var nowDate = new Date();
        if (nowDate > countdownDates[i]) {
            activeCountdown++;
        } else {
            // this should be the first event that needs to be displayed
            activeCountdown = i;
            $("#hfEvt" + i).show();
            break;
        }
    }
    $("#hfTimr").countdown({ until: countdownDates[activeCountdown],
        layout: '<span style="color:#d1d1d1; font-family:Arial, Helvetica, sans-serif; font-weight:bold; font-size: 37px; height: 45px; text-align:center; position:absolute; top: 136px; left: 12px; width: 70px; line-height: 45px;">{dn}</span>' +
            '<span style="color:#d1d1d1; font-family:Arial, Helvetica, sans-serif; font-weight:bold; font-size: 37px; height: 45px; text-align:center; position:absolute; top: 136px; left: 90px; width: 47px; line-height: 45px;">{hnn}</span>' +
            '<span style="color:#d1d1d1; font-family:Arial, Helvetica, sans-serif; font-weight:bold; font-size: 37px; height: 45px; text-align:center; position:absolute; top: 136px; left: 145px; width: 47px; line-height: 45px;">{mnn}</span>' +
            '<span style="color:#d1d1d1; font-family:Arial, Helvetica, sans-serif; font-weight:bold; font-size: 37px; height: 45px; text-align:center; position:absolute; top: 136px; left: 200px; width: 47px; line-height: 45px;">{snn}</span>'
    });
    $("#hfEvntNxt").click(function () {
        showNextPrv(true);
        return false;
    });
    $("#hfEvntPrv").click(function () {
        showNextPrv(false);
        return false;
    });
    $("#hfAdd2site").click(function () {
        var txtArea = $("#hofcodearea");
        if (txtArea.is(":visible")) {
            txtArea.hide();
        } else {
            txtArea.val("");    //clear before doing htmll();
            txtArea.val("<div id=\"hofwdgtwrap\">" + $('#hofwdgtwrap').html().replace("hasCountdown", "") + "</div>");
            txtArea.show();
        }

        return false;
    });
    $("#hfFacts span").hide();
    var randNum = Math.floor(Math.random() * $("#hfFacts span").length);
    $("#hfFacts span").eq(randNum).show();
    $("#hfFacts").show();
}

function updateCountdown() {
    $("#hfTimr").countdown('change', { until: countdownDates[activeCountdown] });
}

function showNextPrv(goForward) {
    if (goForward) {
        if (activeCountdown + 1 > (countdownDates.length - 1)) {
            activeCountdown = 0;
        } else {
            activeCountdown++;
        }
    } else {
        if (activeCountdown - 1 < 0) {
            activeCountdown = countdownDates.length - 1;
        } else {
            activeCountdown--;
        }
    }
    $(".hfEvt").hide(); //animate({ width: 'hide' });
    $("#hfEvt" + activeCountdown).animate({ width: 'show' });

    updateCountdown();
}

