var timeOuts = new Array();  
(function($) {
	var msie = navigator.userAgent.indexOf('MSIE') > -1;

	if ($('#faderBox').length >= 0) {
		timeOuts["Fader" + pCount] = setTimeout(function(){
			$('#faderBox').fadeOut(2000,function(){
				$('#faderBox').remove();
				if (!msie) {
					// If FF
					$('.flashcontent-homepage').css('z-index', '0');
				} else {
					// If IE
					$('.flashcontent-homepage').css('z-index', '-1');
				}
			});
		},4000);
	}
})(jQuery);

var fadeTime = 1000;
var imageCount = 6;
var loadPic;
var newZindex = imageCount + 1;
var pCount = 0;
var prevCount = 0;
var previousImage;
var previousBadge;
var prevZindex;
var storyTime = 6000; 
var reloadTime = 4000;
var x;
var i;

$(document).ready(function(){
	
	if (!$('#faderBox').length > 0) {
		$('.flashcontent-homepage').css('z-index', '0');
	}
	
	$('.img-container').css('position', 'absolute');
	
	var numRand = Math.floor(Math.random()*4)+1; 
	$('div#storyImage').html('<img src="images/frontpage/text-story-' + numRand + '.png" class="text-panel" /><img src="images/frontpage/story-' + numRand + '.jpg" class="image-panel" />');
	$('div#pic0').fadeIn(fadeTime);
	timeOuts["Initial" + pCount] = setTimeout(function() {
		switch(numRand) {
		case 1:
			 $('#storyImage .image-panel').css('margin-left', '-110px');
			 $('#storyImage .image-panel').css('margin-top', '-250px');
			 $('#storyImage .image-panel').animate({top: '-=100'}, storyTime, 'linear');
		  break;
		case 2:
			 $('#storyImage .image-panel').css('margin-top', '-180px');
			 $('#storyImage .image-panel').animate({top: '-=120'}, storyTime, 'linear');
		  break;
		case 3:
			 $('#storyImage .image-panel').css('margin-left', '-100px');
			 $('#storyImage .image-panel').animate({left: '+=100', top: '-=135'}, storyTime, 'linear');
		  break;
		case 4:
			 $('#storyImage .image-panel').css('margin-left', '-100px');
			 $('#storyImage .image-panel').css('margin-top', '-200px');
			 $('#storyImage .image-panel').animate({left: '+=100', top: '+=100'}, storyTime, 'linear');
		  break;
		}
		$('div#storyImage').fadeIn(fadeTime, function() {
			$('div#pic0').css('display', 'none');
		});
		previousImage = 'storyImage';
		pCount = pCount + 1;
		rotateImages();
    }, storyTime);
	
	for (i=1; i <= imageCount; i=i+1) {
		$('div#pic' + i).css('z-index', 300+i);
	}
});

$.fn.resize = function(max_size) { 
	m = Math.ceil;
	if (max_size == undefined) {
		max_size = 125;
	}
	h=w= max_size;
	$(this).each(function() {
		image_h = $(this).height();
		image_w = $(this).width();
		
		if (image_h > image_w) {
			w = m(image_w / image_h * max_size);
		} else {
			h = m(image_h / image_w * max_size);
		}
		
		$(this).animate({
			height: h,
			width: w
		}, 200, 'linear');

		var parentDiv = $(this).parents("div:first");
		
		$(parentDiv).animate({
			top: '-=5',
			left: '-=12'
		}, 200, 'linear');
		
	})
};

function resetImage(imageToResize) { 
	if (imageToResize != 'badge-2') {
		badge_w = '102'
		badge_h = '45'
	} else {
		badge_w = '121'
		badge_h = '50'
	}

	$("#" + imageToResize + " img").animate({
		height: badge_h,
		width: badge_w
	}, 200, 'linear');
	
	var parentDiv = $("#" + imageToResize + " img").parents("div:first");
	
	$(parentDiv).animate({
		top: '+=5',
		left: '+=12'
	}, 200, 'linear');
};

function badgeRollover() {
	$("#badge-1 img").resize(125);
	resetImage("badge-6");
	
	previousBadge = "badge-1";
	
	$(".badge").mouseover(function () {
    	x = setTimeout("");
    	for(key in timeOuts ){
    		clearTimeout(timeOuts[key]); 
    	}
		var thisBadge = $(this).attr('id');
		pCount = thisBadge.charAt(6);

		if (thisBadge != previousBadge) {
			if (thisBadge != 'badge-2') {
				$("#" + thisBadge + " img").resize(125);
			} else {
				$("#" + thisBadge + " img").resize(145);
			}
		}
		
		if (prevCount != pCount) {
			loadPic = 'pic' + pCount;
			$('div#' + loadPic).css('display', 'none');
			$('div#' + loadPic).css('z-index', parseInt(newZindex) + 300);
			timeOuts["Rollover" + pCount] = setTimeout(function() {
				$('div#' + loadPic).fadeIn(fadeTime, function() {
					for (i=1; i <= imageCount; i=i+1) {
						if (i != pCount) {
						}
					}
				});
		    }, 100);
			newZindex = newZindex + 1;
			prevCount = pCount;
		}

		if (thisBadge != previousBadge) {
			resetImage(previousBadge);
		}

		previousBadge = thisBadge;
	});
}

function rotateImages() {
	loadPic = 'pic' + pCount;
	timeOuts["Rotate" + pCount] = setTimeout(function() {
		if (pCount <= imageCount) {
			switch(pCount) {
			case 1:
				 $('#pic1 .image-panel').animate({
				   top: '-=100'
				 }, 6000, 'linear');
			  break;
			case 2:
				 $('#pic2 .image-panel').animate({
					left: '-=150',
					top: '+=150'
				 }, 6000, 'linear');
			  break;
			case 3:
				 $('#pic3 .image-panel').animate({
				   left: '-=100',
				   top: '-=100'
				 }, 6000, 'linear');
			  break;
			case 4:
				 $('#pic4 .image-panel').animate({
				   left: '-=100',
				   top: '+=150'
				 }, 6000, 'linear');
			  break;
			case 5:
				 $('#pic5 .image-panel').animate({
				   top: '-=140'
				 }, 6000, 'linear');
			  break;
			case 6:
				 $('#pic6 .image-panel').animate({
				   top: '-=150'
				 }, 6000, 'linear');
			  break;
			}
    		$('div#' + loadPic).fadeIn(fadeTime, function() {
        		unloadImages();
    		});
    		if (pCount != '2') {
    			$("#badge-" + pCount + " img").resize(125);
    		} else {
    			$("#badge-" + pCount + " img").resize(145);
    		}
    		resetImage("badge-" + (pCount-1));
	    } else {
	    	pCount = 1;
	    	loadPic = 'pic' + pCount;
			$('div#' + loadPic).css('display', 'none');
			$('div#' + loadPic).css('z-index', parseInt(newZindex) + 300);
	    	$('div#' + loadPic + ' .image-panel').css('margin', 0);
	    	$('div#' + loadPic + ' .image-panel').css('top', -150); 
    		$('div#' + loadPic).fadeIn(fadeTime, function() {
    			$('div#' + previousImage).css('display', 'none');
    	    	$('.image-panel').css('margin', 0);
    	    	$('div#pic2 .image-panel').css('top', -120); 
    	    	$('div#pic3 .image-panel').css('top', -170); 
    	    	$('div#pic4 .image-panel').css('top', -120); 
    	    	$('div#pic5 .image-panel').css('top', -140);
    	    	$('div#pic6 .image-panel').css('top', -160);
    			previousImage = 'pic' + pCount;
    			prevCount = pCount;
    	    	badgeRollover();
    		});
	    }
    }, reloadTime);
}

function unloadImages() {
	$('div#' + previousImage).css('display', 'none');
	previousImage = 'pic' + pCount;
	prevCount = pCount;
	pCount++;
	rotateImages();
}
