
var message_count, person_count, timer, current_banner_id, current_banner_name, next_btn;
var people = new Array();
var person_index = 0;

$(document).ready(function(){

	$.ajax({
	    url: '/wp-content/themes/csg_web/banner.xml',
	    success: loadBanners,
	    dataType: 'xml'
	});
	
	$('.message').fadeOut();
	$('.pic_area img').fadeOut();
	$('.carousel .inner_box small').fadeOut();

});

function loadBanners(responseXML, statusText){
	
	$(responseXML).find('person').each(function() {
		people.push( $(this) );
		jQuery.preLoadImages( $(this).find('image').text() );
	});
	person_count = $(responseXML).find('person').length;
	
	$(responseXML).find('message').each(function() {
		$('.panel').append(
			'<div id="carousel_' + $(this).attr('id') + '" class="message ' + $(this).find('strip_class').text() + '" style="display:none;"><div class="left"><div class="right"><div class="carousel_text"></div></div></div></div>'
		);
		
		$('#carousel_' + $(this).attr('id') + ' .carousel_text').append(
			'<h1>' + $(this).find('heading').text() + '</h1>' +
			'<a href="' + $(this).find('link').text() + '" class="see_link">' + $(this).find('action').text() + '</a>'
		);
		
		$('.carousel .inner_box ul').append(
			'<li class="banner_btn"><a href="#' + $(this).attr('id') + '" banner_id="' + $(this).attr('id') + '"><img src="/wp-content/themes/csg_web/css/blank.gif" class="' + $(this).find('button').text() + '" /></a></li>'
		);
		
	});
	message_count = $(responseXML).find('message').length;
	
	
	$(".color_box li a").click(function() {
		clearTimeout(timer);
		timer = 0;
		loadBanner( $(this).attr('banner_id') );
	});
	
	people = shuffle(people);
	loadBanner('fix');
	
}


function loadBanner(banner_id){

	current_banner_id = banner_id;
	
	var new_person = getRandomPerson();
	current_banner_name = new_person.find('name').text() + ', <em>' + new_person.find('title').text() + '</em>';
	
	// IE has an image caching issue, so add a date if IE
	if ($.browser.msie) {
		var new_image = 'http://www.csgpath.com' +
		new_person.find('image').text() +
		"?" +
		new Date().getTime();
	}else{
		var new_image = 'http://www.csgpath.com' +
		new_person.find('image').text();
	}
	
	$('<img src="" />').attr('src', new_image ).load(function(){
    	
		// Fade/Hide It all Out
		$('.message').fadeOut();
		$('.carousel .inner_box small').fadeOut('slow', function() { 
			$(this).html( current_banner_name );
			$('.carousel .inner_box small').fadeIn();
		});	
		if( $.browser.msie ) {
			if( $.browser.version < 9 ){
				$('.pic_area img').hide();
			}
		}
		$('.pic_area img').fadeOut();
	
		// Fade/Hide It all Back In
		$('#carousel_'+current_banner_id).fadeIn('slow');
		$('.pic_area').html( $(this) );
		$('.pic_area img').addClass('png').addClass('active').fadeIn('slow');
		
		//Change Active Button
		$("li.banner_btn a").removeClass('active');
		$('a[href*="#' + current_banner_id + '"]').addClass('active');
		
		// Clear and Set Timeout
		clearTimeout(timer);
		timer = 0;
		timer = setTimeout(function(){
			
			if( $("li.banner_btn a.active").parent().index() == (message_count-1) ){
				var next_btn = $("li.banner_btn a.active").parent().parent().first();
			}else{
				var next_btn = $("li.banner_btn a.active").parent().next();
			}
			loadBanner( next_btn.find('a').attr('banner_id') );
			
		}, 7500);
		
    });
	
}

function getRandomPerson(){
	
	var person = people[person_index];
	
	if( person_index == (person_count-1) ){
		people = shuffle(people);
		person_index = 0;
	}else{
		person_index++;
	}
	
	return person;
	
}

shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.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)


$(window).focus(function() {
	clearTimeout(timer);
	timer = 0;
	timer = setTimeout(function(){
			
		if( $("li.banner_btn a.active").parent().index() == (message_count-1) ){
			var next_btn = $("li.banner_btn a.active").parent().parent().first();
		}else{
			var next_btn = $("li.banner_btn a.active").parent().next();
		}
		loadBanner( next_btn.find('a').attr('banner_id') );
			
	}, 7500);
});

$(window).blur(function() {
    clearTimeout(timer);
	timer = 0;
});



