 var BGRotator = (function ($) {
	var currentAnchor = 0;
	var anchors = null;
	var anchorMap = Array();
	var rotate = true;
	interval = null;
	var startRotating = function () {
		rotate = true;
		interval = setInterval(_rotate,15000); //rotate every 15 seconds
	};
	var stopRotating = function () {
		rotate = false;
		clearInterval(interval);
	}
	var _rotate = function () {
			var newIndex = (currentAnchor + 1) % (anchors.length);
			changeBG(anchors[newIndex]);
			currentAnchor = newIndex;
	};
	var changeBG = function (anchor) {
		//anchor is the dom element of the anchor containing the img with the href we want to hear
	    var imgLink = $("img", anchor).attr("src");
        // change the body background css
        $("body").css("background", "url('" + imgLink + "')");
	};
	return {
		Init: function (wrapperID) {
			anchors = $("#"+wrapperID + " a");
			var counter = 0;
			anchors.each(function() {
				anchorMap[this] = counter;
				counter++;
			});
			startRotating();
		},
		StopRotating : function () {
			stopRotating();
		},
		ChangeBG : function (element) {
			changeBG(element);
		},
		Rotate : function() {
			_rotate();
		},
		StartRotating : function() {
			startRotating();
		}
	};
 })(jQuery);
 
jQuery(document).ready(function() {
   // Init the bg change UI (which is within document ready)
    jQuery(document).ready(function() {
        jQuery("#BGSelector a").click(function() {
			BGRotator.ChangeBG(this);
			BGRotator.StopRotating();
			return false;
        });
    });
	BGRotator.Init("BGSelector");
 });
