﻿function htmlEncode(value) {
    return $('<div/>').text(value).html();
}

function htmlDecode(value) {
    return $('<div/>').html(value).text();
}


function showHomeImage(index) {
    $("#homeImages .headline").fadeOut(
    function () {
        $("#homeImages .bullets .bullet").removeClass('active');
        $("#homeImages .imgs img:visible").fadeOut(
        function () {
            $("#homeImages .bullets .bullet").eq(index).addClass('active');
            $("#homeImages .headline a").attr('href', $("#homeImages .imgs a").eq(index).attr('href'));
            $("#homeImages .headline a").html($("#homeImages .imgs img").eq(index).attr('alt'));
            $("#homeImages .headline .extra").html($("#homeImages .headlines div").eq(index).html());
            $("#homeImages .headline").fadeIn('slow');
            $("#homeImages .imgs img:eq(" + index + ")").fadeIn('slow',
            function () {
                if (!window.homeImagesHovering) {
                    startHomeImagesTimer();
                }
            });
        });
    });
}

function fastShowHomeImage(index) {
    var currentIndex = $("#homeImages .imgs img").index($("#homeImages .imgs img:visible"));
    if (currentIndex == index) {
        return;
    }

    $("#homeImages .bullets .bullet").removeClass('active');
    $("#homeImages .bullets .bullet").eq(index).addClass('active');
    $("#homeImages .imgs img:visible").hide();
    $("#homeImages .headline a").attr('href', $("#homeImages .imgs a").eq(index).attr('href'));
    $("#homeImages .headline a").html($("#homeImages .imgs img").eq(index).attr('alt'));
    $("#homeImages .headline .extra").html($("#homeImages .headlines div").eq(index).html());
    $("#homeImages .headline .extra a").click(logEvent);
    $("#homeImages .imgs img:eq(" + index + ")").fadeIn();
}

function startHomeImagesTimer() {
    stopHomeImagesTimer();

    var count = $("#homeImages .imgs img").length;
    var index = $("#homeImages .imgs img").index($("#homeImages .imgs img:visible"));
    var nextIndex = (index + 1) % count;

    window.homeImagesTimer = window.setTimeout("showHomeImage(" + nextIndex + ")", 4000);
}

function stopHomeImagesTimer() {
    if (window.homeImagesTimer) {
        window.clearTimeout(window.homeImagesTimer);
    }
}


function startHomeImages() {
    $(document).ready(function () {
        var count = $("#homeImages .imgs img").length;
        if (count < 2) return;

        $("#homeImages .imgs img:gt(0)").hide();
        $("#homeImages .bullets").hide();

        $("#homeImages .imgs img").each(function () {
            $("#homeImages .bullets").append("<div class='bullet'>&nbsp;</div>");
        });
        $("#homeImages .bullets").append("<div class='clear'></div>");

        $("#homeImages .bullets .bullet").each(function (index) {
            $(this).mouseenter(function () { fastShowHomeImage(index) });
        });
        $("#homeImages .bullets .bullet:eq(0)").addClass('active');

        $("#homeImages .bullets").fadeIn();

        startHomeImagesTimer();
        $("#homeImages").hover(
        function () { window.homeImagesHovering = true; stopHomeImagesTimer(); },
        function () { window.homeImagesHovering = false; startHomeImagesTimer(); }
    );
    });
}

function logEvent() {
    if (!$(this).attr('onclick')) {
        var url = $(this).attr('href').replace(/https?:\/\//, '');
        _gat._getTrackerByName()._trackEvent('Link', 'Carousel', url);
    }
}

$(function () {
    $('#homeImages a').click(logEvent);
});
