var header_interval_id = 0;
var header_pause = false;

(function($) {
	var cache = [];
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}
})(jQuery)

$(document).ready(function() {
	initHeader();

	jQuery.preLoadImages("images/button_download_hover.png", "images/button_browse_hover.png", "images/bg_main_nav_hover.png", "http://www.bu.edu/dbin/ece-clotho/images/sponsor_logos/logo_autodesk_hover.png","http://www.bu.edu/dbin/ece-clotho/images/sponsor_logos/logo_quintara_hover.png","http://www.bu.edu/dbin/ece-clotho/images/sponsor_logos/logo_synberc_hover.png","http://www.bu.edu/dbin/ece-clotho/images/sponsor_logos/logo_qb3_hover.png");
});

function initHeader() {
	$('#header').each(function(i) {
		$(this).data('index', 0);
		$(this).find('#header_tray .orb').click(function(e) {
			var sh = $(this).parents('#header');
			sh.find('#header_reel').stop(1,1);
			sh.find('#header_tray .orb_active').removeClass('orb_active');
			$(this).addClass('orb_active');
			goToHeader(sh, sh.find('#header_tray .orb').index(this));
			header_pause = true;
		});
		$(this).find('#header_tray .left_arrow').click(function(e) {
			var sh = $(this).parents('#header');
			sh.find('#header_reel').stop(1,1);
			sh.find('#header_tray .orb_active').removeClass('orb_active');
			var index = (sh.data('index') == 0) ? sh.find('#header_tray .orb').length - 1 : sh.data('index') - 1;
			sh.find('#header_tray .orb:eq('+index+')').addClass('orb_active');
			goToHeader(sh, index);
			header_pause = true;
		});
		$(this).find('#header_tray .right_arrow').click(function(e) {
			var sh = $(this).parents('#header');
			sh.find('#header_reel').stop(1,1);
			sh.find('#header_tray .orb_active').removeClass('orb_active');
			var index = (sh.data('index') == sh.find('#header_tray .orb').length - 1) ? 0 : sh.data('index') + 1;
			sh.find('#header_tray .orb:eq('+index+')').addClass('orb_active');
			goToHeader(sh, index);
			header_pause = true;
		});
		var sh = $(this);
		setTimeout(function() {
			header_interval_id = setInterval(function() { if(!header_pause) { sh.find('#header_tray .orb').removeClass('orb_active'); var index = (sh.data('index') == sh.find('#header_tray .orb').length - 1) ? 0 : sh.data('index') + 1; sh.find('#header_tray .orb:eq('+index+')').addClass('orb_active'); goToHeader(sh, index); } }, 5000);
		}, 30000);
	});
}
function goToHeader(obj, index) {
	if(obj.data('index') < index && (index != obj.find('#header_tray .orb').length - 1 || obj.data('index') != 0) || 
	  obj.data('index') == obj.find('#header_tray .orb').length - 1 && index == 0) {
		obj.find('#header_container > img:eq('+index+')').clone().appendTo(obj.find('#header_reel'));
		obj.find('#header_reel').animate({ left: -obj.find('#header_reel img:eq(0)').width() }, { duration : 500, queue: false, complete: function() {
			$(this).css({ left : 0 }).find('img:eq(0)').remove();
		}});
	} else {
		obj.find('#header_container > img:eq('+index+')').clone().prependTo(obj.find('#header_reel'));
		obj.find('#header_reel').css({ left: -obj.find('#header_reel img:eq(0)').width() }).animate({ left : 0 }, { duration : 500, queue: false, complete: function() {
			$(this).find('img:eq(1)').remove();
		}});
	}
	obj.data('index', index);
}
