var clock,c,delay=10000;
var initial_campaign;

switchCampaign = function(c) {
	var body = document.body;
	var active_index;
	
	// if we have any campaigns on the page
	if ($('#campaigns').siblings()) {
		var tmp = "";
		var campaigns = [];
		var children = $('#campaigns').children();
		
		// generating the archive of campaigns IDs
		for (var i = 0; i < children.length; i++) {
			if (children[i].className != 'undefined') {
				campaigns[i] = children[i].className;
			} 
			else {Console.log("Error: the campaign item has no classes!")}
		}
		
		// if the first element of the array is not already the same as the body's one
		if (campaigns[0] != initial_campaign) {
			// resort the array and make it the first element
			for (var i = 0; i < campaigns.length; i++) {
				if (campaigns[i] == initial_campaign) {
					campaigns.splice(i, 1);
					campaigns.splice(0, 0, initial_campaign);
					break;
				}
			}
		}
		
		// getting the current campaign position
		active_index = $.inArray(body.className, campaigns);
		
		// looping through the campaigns array
		if (active_index < campaigns.length - 1 && active_index != -1) {
			body.className = campaigns[active_index + 1];
		}
		else {
			body.className = campaigns[0];
		}
		
		window.clearTimeout(clock);
		delayswitch();
	}
	else {Console.log("Error: no campaigns found!")}
}


// delaying the switch
delayswitch = function() {clock = window.setTimeout(switchCampaign, delay);}


// adding event listener
$(document).ready(function() {
	// stopping the rotation on hover
  $('#campaigns, #indicator li').mouseenter(
    function() {window.clearTimeout(clock);}
  );
  $('#campaigns,#indicator li').mouseleave(
    function() {window.clearTimeout(clock);delayswitch();}
  );
	// rotating on click
	$('#indicator a').click(function(e) {
		document.body.className = this.parentNode.className;
		window.clearTimeout(clock);
		e.preventDefault();
	});
	
	// getting the initial campaign value
	initial_campaign = document.body.className;
	// starting the rotation with the delay
	delayswitch();
});

