﻿
var intValId;
var intervalLength = 5000;
var className = 'rotator';
var activeClassName = 'active';

$(document).ready(function() {

    populateElement('#ea', 'enter your email');

    StartRotator();

    $('a.light').click(function() { pageTracker._trackEvent('Home Page Rotator', 'Click Through', this + ''); });

});

StartRotator = function() {

    var rotEnts = $('div.rotator');

    // Add the StopRotator function to anchors
    $('a.rotatorselect').click(function() { StopRotator(this); return false; });

    if ('' != GetCurrentId())
        intValId = setInterval("RotateNext();", intervalLength);

}

StopRotator = function(theLink) {
    theLink = theLink + ''; // Convert this to a link
    // Clear the Interval to stop the rotating
    clearInterval(intValId);

    // Deactivate Active Rotator
    DeActivateCurrent();

    // Get the number of the clicked rotator
    var currentId = theLink.substr(theLink.indexOf('#')+1, 1);

    // Activate the new one
    ActivateNew(currentId);

    // Google Analytics
    pageTracker._trackEvent('Home Page Rotator', 'Stop To View', 'ID ' + currentId);
}

RotateNext = function() {

    var CurId = GetCurrentId();

    if ('' != CurId) {
        DeActivateCurrent();
        ActivateNew(GetNextId(CurId));
    }

};

GetCurrentId = function() {

    var rotEnts = $('div.' + className);
    
    if (rotEnts.length > 0) {
        return $('div.' + activeClassName)[0].id.replace('r', '')
    }
    else {
        return '';
    }

};

GetNextId = function(currentId) {
    var rotEnts = $('div.' + className);
    return (parseInt(currentId, 0) < rotEnts.length) ? (parseInt(currentId, 0) + 1) : currentId = '1';
};

DeActivateCurrent = function() {
    var currentId = GetCurrentId(activeClassName);
    $('#r' + currentId).removeClass(activeClassName);                       // Hide the currently active sets
    $('#p' + currentId).removeClass(activeClassName);
};

ActivateNew = function(IdToActivate) {
    $('#r' + IdToActivate).addClass(activeClassName);
    $('#p' + IdToActivate).addClass(activeClassName);
};

populateElement = function(selector, defvalue) {
    $(selector).each(function() {
        if ($.trim(this.value) == "") {
            this.value = defvalue;
        }
    });

    $(selector).focus(function() {
        if (this.value == defvalue) {
            this.value = "";
        }
    });

    $(selector).blur(function() {
        if ($.trim(this.value) == "") {
            this.value = defvalue;
        }
    });
};
