var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline=0;
var headlines = new Array(); // an array of jQuery objects

// New jquery functions
jQuery.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}

$(document).ready( function() {
	$("img").each( function() { 
		$.preloadImages($(this).get(0).src);
	});

	headline_count = $("dd.headline").size();

	if(headline_count > 0) {
		for (var i = 0; i < headline_count; i++) {
			headlines[i] = $("dd.headline:eq("+i+")");
		}

		headlines[current_headline].show().animate({top: 5},"slow");

		headline_interval = setInterval(headline_rotate,7000);

		$('#scrollup').hover(function() {
			clearInterval(headline_interval);
		}, function() {
			headline_interval = setInterval(headline_rotate,7000);
		//headline_rotate();
		});
	}

	$(".color").each( function () {
		$(this).css('padding', 15);

		var blW = $(this).width();
		var blH = $(this).innerHeight();

		var background = 'a28363';
		
		switch($(this).attr('class')) {
			case 'color brown':
				var background = 'a28363';
				break;
			case 'color darkbrown':
				var background = '663401';
				break;
			case 'color orange':
				var background = 'cb8058';
				break;
			case 'color darkorange':
				var background = 'de791e';
				break;
			case 'color green':
				var background = 'bac792';
				break;
			case 'color darkgreen':
				var background = '00652e';
				break;
			case 'color blue':
				var background = '5b88b2';
				break;
			case 'color darkblue':
				var background = '021f5f';
				break;
			case 'color purple':
				var background = '9091ab';
				break;
			case 'color darkpurple':
				var background = '340167';
				break;
		}

		ua = navigator.userAgent;
		var isMSIE = (navigator.appName == "Microsoft Internet Explorer");
		var isMSIE6 = isMSIE && (ua.indexOf('MSIE 6') != -1) && (ua.indexOf('MSIE 7') == -1);
	
		if(isMSIE6)
			$(this).css('background', 'url(/images/phpThumb.php?new='+background+'&fltr[]=ric|15|15&f=gif&q=80&zc=1&w='+blW+'&h='+blH+') no-repeat');
		else
			$(this).css('background', 'url(/images/phpThumb.php?new='+background+'&fltr[]=ric|15|15&f=png&q=80&zc=1&w='+blW+'&h='+blH+') no-repeat');
	});
});

function headline_rotate() {
	current_headline = (old_headline + 1) % headline_count;
	if(current_headline == 0) {}
	
	headlines[current_headline].hide();
	headlines[old_headline].fadeOut( function() { 
		headlines[old_headline].css('top', '200px');
	
		headlines[current_headline].fadeIn().show().animate({top: 5},"slow");
		old_headline = current_headline;
	});

}
