var j$ = jQuery.noConflict();

var productSlideDuration = 3500;
var currentProductIndex = 1;
var nextProductIndex = 0;
var productTimer = null;
var fadeDuration = 1500;
var featureRotateTimeout = 30000;

j$(document).ready(function () {

    if (ftrRotTimeout != null)
        featureRotateTimeout = ftrRotTimeout; // this value is written to the page in aspx page

    if (prdRotTimeout != null)
        productSlideDuration = prdRotTimeout; // this value is written to the page in aspx page

    /* feartures */
    if (j$('#features ul').length) {

        // rotateFeature();
        var featureTimer = window.setTimeout('rotateFeature()', featureRotateTimeout);
    }

    /* Products */
    if (j$('#ProductRotator').length) {
        // create an index list placeholder
        var indexHolder = j$('<div id="productIndex"></div>');

        // create an index list of products
        j$('#ProductRotator ul li').each(function (i) {

            // hide the list item
            if (i > 0) {
                j$(this).hide();
                j$(this).removeClass('hide');
                j$(this).removeClass('show');

                // hide the image, price, links, etc
                var img = j$(this).find('img');
                var a = j$(this).find('a');
                var p = j$(this).find('p');
                j$(img).hide();
                j$(a).hide();
                j$(p).hide();
            }

            var el = j$('<label />');
            j$(el).text((i + 1));

            // add mouse over and mouseout events
            j$(el).bind('mouseover', function () {
                window.clearTimeout(productTimer);
                productTimer = null;
                nextProduct(i);
            });

            j$(el).bind('mouseout', function () {
                if (!productTimer)
                    productTimer = window.setTimeout('nextProduct()', productSlideDuration);
            });

            j$(indexHolder).append(el);
        });

        j$('#ProductRotator').append(indexHolder);

        // 'activate' the fist index item
        j$('#productIndex label:nth-child(1)').addClass('active');

        // show the first product
        if (j$('#ProductRotator ul li').length > 1)
            productTimer = window.setTimeout('nextProduct()', productSlideDuration);
    }

});

function rotateFeature() {
    var current = j$('#features ul li.show');

    if (j$(current).next().length > 0) {
        j$(current).removeClass('show');
        // j$(current).addClass('hide');
        j$(current).hide();
        j$(current).next().addClass('show');
        j$(current).next().removeClass('hide');
        j$(current).next().fadeIn(300);
    } else {
        j$(current).removeClass('show');
        // j$(current).addClass('hide');
        j$(current).hide();
        j$(current).parent().children(":first").addClass('show');
        j$(current).parent().children(":first").removeClass('hide');
        j$(current).parent().children(":first").fadeIn(300);
    }

    var featureTimer = window.setTimeout('rotateFeature()', featureRotateTimeout);
}

// Next Product
function nextProduct(index) {

    var fade = 100;

    if (index == currentProductIndex - 1)
        return;

    if (index > -1) {
        nextProductIndex = index + 1;
        fade = 200;
    } else {
        fade = fadeDuration;
        nextProductIndex = currentProductIndex + 1;
        if (nextProductIndex > j$('#ProductRotator ul li').length)
            nextProductIndex = 1;
    };

    var currentItem = j$('#ProductRotator').find('li:nth-child(' + currentProductIndex + ')');
    var nextItem = j$('#ProductRotator').find('li:nth-child(' + nextProductIndex + ')');

    // hide the current image, price, links, etc
    var imgC = j$(currentItem).find('img');
    var aC = j$(currentItem).find('a');
    var pC = j$(currentItem).find('p');    
    j$(imgC).hide();
    j$(aC).hide();
    j$(pC).hide();
    j$(currentItem).hide();

    // show the next image, price, links, etc
    var imgN = j$(nextItem).find('img');
    var aN = j$(nextItem).find('a');
    var pN = j$(nextItem).find('p');
    j$(imgN).fadeIn();
    j$(aN).show();
    j$(pN).show();
    j$(nextItem).show();

    // update the index
    j$('#productIndex').find('label:nth-child(' + currentProductIndex + ')').removeClass('active');
    j$('#productIndex').find('label:nth-child(' + nextProductIndex + ')').addClass('active');

    currentProductIndex = nextProductIndex;

    if (!index) {
        // if (!productTimer)
        productTimer = window.setTimeout('nextProduct()', productSlideDuration);
    } else {
        window.clearTimeout(productTimer);
        productTimer = null;
    }
}




