﻿var features = new Array();
var speed = 6000;
var transitionSpeed = 2000;
var showClass = 'showFeature';
var interval;
var dPrefix = '#divFeature_';
var aPrefix = '#aFeature_';

function setRotation() {
    var i;
    var id;
    var show;
    for (i = 1; i < 5; i++) {
        id = dPrefix + i;
        if ($(id).length) {
            features.push(i);
            if ($(id).hasClass(showClass)) {
                show = $(id);
                setOverlay();
            }
            $(id).hide(0);
        }
    }
    show.show(0);
    setOverlay();
}

function rotate() {
    var i;
    var cur;
    var toShow;
    for (i = 0; i < features.length; i++) {
        cur = dPrefix + features[i];
        if ($(cur).hasClass(showClass)) {
            if ((i + 1) == features.length) {
                toShow = features[0];
                flipbutton(aPrefix + 1);
            } else {
                toShow = features[i + 1];
                flipbutton(aPrefix + (i + 2));
            }
            flipbutton(aPrefix + (i + 1));
            $(cur).toggleClass(showClass);
            //$(cur).fadeOut(transitionSpeed);
            $(cur).hide("slide", { direction: "left" }, transitionSpeed);
        }
    }
    if (toShow != '') {
        cur = dPrefix + toShow;
        $(cur).toggleClass(showClass);
        //$(cur).fadeIn(transitionSpeed);
        $(cur).show("slide", { direction: "right" }, transitionSpeed);
    }
    setOverlay();
}

function flipbutton(b) { $(b).toggleClass('featureButtonSelected'); $(b).toggleClass('featureButtonUnselected'); }

function swapContent(id, bid) {
    clearInterval(interval);
    if ($(id).length && $(id).attr('id') != $('.' + showClass).attr('id')) {
        //$('.' + showClass).delay(100).fadeOut(transitionSpeed);
        $('.' + showClass).hide("slide", { direction: "left" }, transitionSpeed);
        $('.' + showClass).toggleClass(showClass);
        //$(id).fadeIn(transitionSpeed);
        $(id).show("slide", { direction: "right" }, transitionSpeed);
        $(id).toggleClass(showClass);
        flipbutton('#' + $('.featureButtonSelected').attr('id'));
        flipbutton(bid);
    }
    setOverlay();
    interval = null;
    interval = setInterval(rotate, speed);
}

function setOverlay() {
    $('.featureFrame').unbind('click').css('cursor', 'default');
    if ($('.' + showClass + ' .featureContainerTxtLink').length) {
        $('.featureFrame').bind('click', function() { window.location = $('.' + showClass + ' .featureContainerTxtLink a')[0].toString(); }, false).css('cursor', 'pointer'); ;
    }
}

$(document).ready(function() {
    setRotation();
    if (features.length > 1) {
        interval = setInterval(rotate, speed);
    }
    $(".featureContainer .feature .featureContainerTxtBG").css({ opacity: 0.75 });
});
