﻿Array.prototype.findIndex = function(value){
    var ctr = "";
    for (var i=0; i < this.length; i++) {
        // use === to check for Matches. ie., identical (===), ;
        if (this[i] === value) {
            return i;
        }
    }
    return ctr;
};

function changeInfobarText(item) {
    //Setup the infobar text
    var img = jQuery(item).find("img").get(0);

    jQuery(".infobar .title").text(jQuery(img).attr("alt"));
    jQuery(".infobar .dates").text(jQuery(img).attr("title"));
}

function changePopupLink(item) {
    var img = jQuery(item).find("img").get(0);
    
    jQuery(".popup a").attr("href", jQuery(item).attr("href"));
    jQuery(".popup a").text(jQuery(img).attr("alt"));
}

jQuery(document).ready(function() {
    //Enable the First image
    jQuery(".feature-img").first().addClass("selected").fadeIn("1000");

    changeInfobarText(jQuery(".feature-img").first().get(0));

    jQuery(".infobar").fadeIn(500)

    jQuery(".whats-on-widget").hover(function() {
        //Disable the infobar, while thumbnails are displayed
        jQuery(".infobar").css("display", "none");
        jQuery(".thumbnails").animate({
            bottom:"10px"
        }, 100)
    }, function() {
        jQuery(".thumbnails").animate({
            bottom:"-44px"
        }, 100);
        //Re-enable the infobar
        jQuery(".infobar").fadeIn(500);
    });

    jQuery(".thumbnails img").bind("click", function() {
        var thumbs = jQuery(".thumbnails img").get();

        var current = thumbs.findIndex(this);

        jQuery(".feature-img").each(function() {
            if (jQuery(this).hasClass("selected")) {
                jQuery(this).fadeOut("500");
            }
        });

        var selected = jQuery(".feature-img").get(current);
        jQuery(selected).addClass("selected");
        jQuery(selected).fadeIn("500");

        changeInfobarText(selected);
    });
});
