if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
        "use strict";
        if (this === void 0 || this === null) {
            throw new TypeError();
        }
        var t = Object(this);
        var len = t.length >>> 0;
        if (len === 0) {
            return -1;
        }
        var n = 0;
        if (arguments.length > 0) {
            n = Number(arguments[1]);
            if (n !== n) { // shortcut for verifying if it's NaN
                n = 0;
            } else if (n !== 0 && n !== Infinity && n !== -Infinity) {
                n = (n > 0 || -1) * Math.floor(Math.abs(n));
            }
        }
        if (n >= len) {
            return -1;
        }
        var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
        for (; k < len; k++) {
            if (k in t && t[k] === searchElement) {
                return k;
            }
        }
        return -1;
    }
}

$(document).ready(function() {
	var slides = new Array('#yso-content', '#uf-content', '#fb-content');
	var wrapperClass = new Array('yso-header-promo-wrapper', 'uf-header-promo-wrapper', 'fb-header-promo-wrapper');
	var markers = new Array('#marker-fb', '#marker-uf', '#marker-yso');
	var current = 0;
	var animating = 0;
	var timeout;

	function changeSlides(from, to) {
		animating = 1;
		$('#header-promo-wrapper').animate({opacity: 0}, 200, function() {
			$('#header-promo-wrapper').removeClass().addClass(wrapperClass[to]);
			$(slides[from]).hide();
			$(markers[from]).attr('src', $(markers[from]).attr('src').replace('marker-on.png', 'marker-off.png'));
			$(slides[to]).show();
			$(markers[to]).attr('src', $(markers[to]).attr('src').replace('marker-off.png', 'marker-on.png'));
			
			$('#header-promo-wrapper').animate({opacity: 1}, 200, function() {
				current = to;
				animating = 0;
				timeout = setTimeout(function() {
					changeSlides(to, (to + 1) % 3);
				}, 7000); // 7 seconds between slides
			});
		});
	}
	
	$('.markers img').click(function(event) {
		if (!animating) {
			clearTimeout(timeout);
			to = markers.indexOf('#' + $(this).attr('id'));
			changeSlides(current, to);
		}
	});

	timeout = setTimeout(function() {
		changeSlides(current, (current + 1) % 3);
	}, 4000);  // 4 seconds on initial load slide
});
