﻿function displayMainImage(elmt, url, id) {
    if ($(elmt).parent().attr('class').indexOf('selected') == -1) {
        $('#' + id).stop(true, true);
        $('#' + id).fadeOut(300, function() {
            var element = document.getElementById(id);
            element.src = url;
            $('#' + id).fadeIn(300);
        });

        $(elmt).parent().siblings().removeClass('selected');
        $(elmt).parent().addClass('selected');
    }
}

function loadCarousel(carouselID, isVertical, scrollQty, itemList, isFeaturesCarousel, targetImageID, imageWidth, imageHeight, fallbackDimension) {
    jQuery('#' + carouselID).jcarousel({ vertical: isVertical, scroll: scrollQty,
        size: itemList.length,
        itemLoadCallback: { onBeforeAnimation: carousel_itemLoadCallback },
        itemList: itemList,
        isFeaturesCarousel: isFeaturesCarousel,
        targetImageID: targetImageID,
        listImageWidth: imageWidth,
        listImageHeight: imageHeight,
        itemFallbackDimension: fallbackDimension,
        carouselItemsLoaded: false,
        carouselID: carouselID
    });
}

function carousel_itemLoadCallback(carousel, state) {
    for (var i = carousel.first; i <= carousel.last; i++) {
        if (carousel.has(i)) {
            continue;
        }

        if (i > carousel.size) {
            break;
        }

        var item = jQuery(carousel_getItemHTML(carousel.options.itemList[i - 1], carousel.options.isFeaturesCarousel, carousel.options.targetImageID,carousel.options.listImageWidth,carousel.options.listImageHeight)).get(0);
        carousel.add(i, item);
    }

    $('#' + carousel.options.carouselID).parent().css('background', '#ffffff');

    if(!carousel.options.carouselItemsLoaded){
        $('#' + carousel.options.carouselID).children(":first").addClass('selected');
        carousel.options.carouselItemsLoaded = true;
    }
    
};

function carousel_getItemHTML(item, isFeaturesCarousel, targetImage, listImageWidth, listImageHeight) {
    if (isFeaturesCarousel) 
    {
        return '<div style="padding-left: 25px;padding-right: 25px;" class="slidetext"><span class="slidetitle">' + item.title + '</span><br /><br />' + item.copytext + '<br /><img style="padding-top:10px;width:' + listImageWidth + 'px;height:' + listImageHeight + 'px;border:none;" src="' + item.url + '" alt="" /></div>';
    }
    else 
    {
        return '<a href="javascript:;" onmousedown=displayMainImage(this,"' + item.image + '","' + targetImage + '") title="' + item.title + '"><img style=";width:' + listImageWidth + 'px;height:' + listImageHeight + 'px;border:none;" src="' + item.url + '" alt="' + item.title + '" /></a>';
    }
};

function preload(arrayOfImages) {

    jQuery.each(arrayOfImages, function() {
        var newImage2 = new Image();
        newImage2.src = this.image;
        var newImage1 = new Image();
        newImage1.src = this.url;
    });

}

function preloadFeatures(arrayOfImages) {
    $(arrayOfImages).each(function() {
        var newImage1 = new Image();
        newImage1.src = this.url;
    });
}



