var image_count = 0;
var current_bg_image = 0;
var rotate_bg_time = 10000;
var bg_images = new Array();
var interval_number = null;
var first_instance = 1;


var rotateIntroImages = function() {

	$('div.linkbox').each(function(){
		$(this).removeClass('linkbox_selected');
		if($(this).attr('title') == bg_images[current_bg_image]) {
			$(this).addClass('linkbox_selected');
		}
	});

	$('body').css({
		'background-image':'url('+ bg_images[current_bg_image] +')'}
	);
	
	current_bg_image = current_bg_image + 1;
	
	if(current_bg_image > (bg_images.length - 1)) {
		current_bg_image = 0;
	}
	
}

var ri_interval_number = null;
var ri_current_image = '1';
var ri_rotate_time = 5000;

var rotateRiModelImages = function() {
	$('div#ri_sidebar img.ri_model_image').hide();
	ri_current_image = ri_current_image + 1;
	if(ri_current_image > 4) {
		ri_current_image = 1;
	}
	var next_image_id = 'div#ri_sidebar img#img'+ ri_current_image;
	$(next_image_id).show();  
}

$(window).load(function(){

	$.get(basePath + 'background_images', function(html){
		$('#background_images').append(html);

		$('div.linkbox').click(function(){
			var image_path = $(this).attr('alt');

			$('body').css({
				'background-image':'url('+ image_path +')'
			});

			clearInterval(interval_number);
		});

		$('div.linkbox').each(function(){
			var image_path = $(this).attr('title');
			bg_images[image_count] = image_path;
			image_count = image_count + 1;
		});

		interval_number = setInterval("rotateIntroImages()", rotate_bg_time);
	});


	$('a#case_studies_link').mouseover(function(){ $('div#case_studies_inner').show(); }).mouseout(function(){ $('div#case_studies_inner').hide(); });
	$('div#case_studies_inner').mouseover(function(){ $(this).show(); }).mouseout(function(){ $(this).hide(); });

	$('a#white_papers_link').mouseover(function(){ $('div#white_papers_inner').show(); }).mouseout(function(){ $('div#white_papers_inner').hide(); });
	$('div#white_papers_inner').mouseover(function(){ $(this).show(); }).mouseout(function(){ $(this).hide(); });

	$('a#ri_process_link').mouseover(function(){ $('div#ri_process_inner').show(); }).mouseout(function(){ $('div#ri_process_inner').hide(); });
	$('div#ri_process_inner').mouseover(function(){ $(this).show(); }).mouseout(function(){ $(this).hide(); });

	/**
	* RI Model Sidebar
	*/
	ri_interval_number = setInterval("rotateRiModelImages()", ri_rotate_time);

});


$(document).ready(function() {
	var default_text = ['vertical', 'encouraging', 'people', 'legacy'];

	$('area.sbarea1').click(function(){
		var img_num = $(this).attr('title');
		var img_id = 'img'+ img_num;
		var default_text_id = default_text[(img_num - 1)];
		$('div#ri_sidebar img').hide();
		$('img#'+img_id).show();
		$('div#bottom_wrap div').hide(); 
		$('div#'+ default_text_id).show();
		clearInterval(ri_interval_number);
	});

	$('area.sbarea2').mouseover(function(){ $('div#bottom_wrap div').hide(); $('div#vertical').show(); });
	$('area.sbarea3').mouseover(function(){ $('div#bottom_wrap div').hide(); $('div#horizontal').show(); });
	$('area.sbarea4').mouseover(function(){ $('div#bottom_wrap div').hide(); $('div#vertical').show(); });
	$('area.sbarea5').mouseover(function(){ $('div#bottom_wrap div').hide(); $('div#encouraging').show(); });
	$('area.sbarea6').mouseover(function(){ $('div#bottom_wrap div').hide(); $('div#focusing').show(); });
	$('area.sbarea7').mouseover(function(){ $('div#bottom_wrap div').hide(); $('div#collaborating').show(); });
	$('area.sbarea8').mouseover(function(){ $('div#bottom_wrap div').hide(); $('div#challenging').show(); });
	$('area.sbarea9').mouseover(function(){ $('div#bottom_wrap div').hide(); $('div#people').show(); });
	$('area.sbarea10').mouseover(function(){ $('div#bottom_wrap div').hide(); $('div#autonomy').show(); });
	$('area.sbarea11').mouseover(function(){ $('div#bottom_wrap div').hide(); $('div#truth').show(); });
	$('area.sbarea12').mouseover(function(){ $('div#bottom_wrap div').hide(); $('div#commitment').show(); });
	$('area.sbarea13').mouseover(function(){ $('div#bottom_wrap div').hide(); $('div#legacy').show(); });

	$('div.subbox_inner').hover(function() {
		var id = $(this).attr('id');
		var id_str = id.replace(/_inner/, "");
		$('a#'+ id_str +'_link').addClass('home_hover');
	} , function() {
		var id = $(this).attr('id');
		var id_str = id.replace(/_inner/, "");
		$('a#'+ id_str +'_link').removeClass('home_hover');
	});

	$('table#about_us_table tr td img').hover(function(){
		$(this).addClass('opachover');
	}, function(){
		$(this).removeClass('opachover');
	});

});	
