// Using the equal heights jquery plug in, the largest column with the class .columns sets the height for all elements with .columns
// If you use this function and you get scroll bars, it is usually caused by an image in the markup with no set height.

$(window).load(function() {
    $(".module_content").equalHeights();
});



// Rollover function. Replaces _off with _on in image name for images with the class="rollover"

var slideShowInterval = null;

$(function() {
  $('.rollover').hover(function() {
      var currentImg = $(this).attr('src');
      $(this).attr('src', $(this).attr('src').replace('_off', '_on'));
  }, function() {
		if (!$(this).hasClass('current')) {
	      var currentImg = $(this).attr('src');
   	   $(this).attr('src', $(this).attr('src').replace('_on', '_off'));
		}
  });
  
	if ($('#feature').size() > 0) {
		$($('.feature_img')[0]).show();
		$($('.feature_content')[0]).show();
		slideShowInterval = setInterval("slideShow()", 12000);
	}
	
	//if ($('.news_headline_reveal span').size() > 0) {
//		setTimeout("revealNewsHeadline()", 1000);
//	}
});

//Preload the navigation rollover states to prevent flickering
function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;

    });
}

preload([
    '../images/icon_2_on.gif',
    '../images/icon_3_on.gif',
    '../images/icon_1_off.gif',
    '../images/icon_twitter_on.gif',
    '../images/icon_linkedin_on.gif',
    '../images/icon_rss_on.gif',
    '../images/icon_mail_off.gif'
	]);


// Homepage news headline reveal
function revealNewsHeadline() {
	$('.news_headline_reveal span').animate({width: "10px"}, 3000, 'linear', function() {
		$('.news_headline_reveal span').hide();
	});
}

// Homepage feature function area
var curr_feature = 1;
var switching = false;

function featureLink(num) {
	clearInterval(slideShowInterval); 
	setFeature(num)
}

function setFeature(num) {
	if (num > $('.feature_img').size()) num = $('.feature_img').size();
	if (num != curr_feature && !switching && num != 0) {
		switching = true;
		$('#feature_link'+curr_feature).removeClass('current');
		$('#feature_link'+curr_feature).attr('src', $('#feature_link'+curr_feature).attr('src').replace('_on', '_off'));
		$('#feature_link'+num).addClass('current');
		$('#feature_link'+num).attr('src', $('#feature_link'+num).attr('src').replace('_off', '_on'));

		$($('.feature_img')[curr_feature-1]).css('z-index', '1')
		$($('.feature_img')[num-1]).css('z-index', '99')
		$($('.feature_content')[curr_feature-1]).css('z-index', '1')
		$($('.feature_content')[num-1]).css('z-index', '99')
		$($('.feature_img')[num-1]).fadeIn(1000, function(){
			$($('.feature_img')[curr_feature-1]).hide();
			curr_feature = num;
			switching = false;
		});
		$($('.feature_content')[curr_feature-1]).customFadeOut(500, function(){
			$($('.feature_content')[num-1]).customFadeIn(500);
		});
	}
}

function slideShow() {
	num = curr_feature+1;
	if (num > 3) num = 1;
	setFeature(num);
}

(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);
