﻿var element
var onElement = false;

$(window).load(function () {
    $('.wrap').hover(function () {
        $('.scrollable').pauseAnimation();
        onElement = true;
    }, function () {
        $('.scrollable').resumeAnimation();
        onElement = false;
    });

    $('.item').live("mouseenter", function () {
        $(this).find(".overlay").css("display", "block").hide().delay(100).fadeIn(200);
    });
    $('.item').live("mouseleave", function () {
        $(this).find(".overlay").stop(true, true).fadeOut(200);
    });

    if (onElement == false) {
        setTimeout("moveLeft()", 9000);
        // alert("hello");
    }
});

function moveLeft() {
    if (onElement == false) {
        // what's the first item?
        element = $('.scrollable div.item:nth-child(1)');

        var width = $(element).outerWidth();

        // verschuiven
        $('.scrollable').startAnimation(
            { "marginLeft": "-" + width},
            1500,
            function () { replace();}
        );
    }
    else {
        setTimeout("moveLeft()", 9000);
    }
}

function replace() {
    // verplaatsen
    $('.scrollable').css({ "marginLeft": "0" });
    
    // verwijderen
    if (navigator.appVersion.indexOf("MSIE") != -1) {
        $(element).find("h2").each(function () {
            var text = ""
            $(this).find("cufontext").each(function () {
                text = text + $(this).text();
            });
            $(this).html(text);
        });
        $(element).find("span").each(function () {
            var text = ""
            $(this).find("cufontext").each(function () {
                text = text + $(this).text();
            });
            $(this).html(text);
        });
    }


    var $copyElement = $(element).clone();
    var voorstellingId = $copyElement.attr("voorstellingid");
    $(element).remove();

    var count = 0

    $(".headliners .item").each(function () {
        if ($(this).attr("voorstellingid") == voorstellingId) { 
            count += 1
        }
    });

    if (count == 0) {
        $copyElement.appendTo('.scrollable');
        
        Cufon.replace('.headliners .item:last span', { fontFamily: 'Klavika L', hover: true });
        Cufon.refresh('.headliners .item:last span');
        Cufon.replace('.headliners .item:last h2', { fontFamily: 'Klavika Bd', hover: true });
        Cufon.refresh('.headliners .item:last h2');

        $('.headliners .item:last').hover, (
          function () {
              $(this).find(".overlay").css("display", "block").hide().delay(100).fadeIn(200);
          },
          function () {
              $(this).find(".overlay").stop(true, true).fadeOut(200);
          }
        );
    }

    setTimeout("moveLeft()", 9000);

}
