$(document).ready(function(){
	
	$('#latest-news ul').cycle({ 
		fx:      'fade', 
		speed:    600, 
		timeout:  7000 
	});
	
	$('#bg').bgResize({
		imagewidth:1680,
		imageheight:1007
	});
	
	//Initialize Photo Wall
	initConglomerate();
	
	//Run Photo Wall
	setInterval('runConglomerate()', 2500);
	
	//Fade in elements
	setTimeout('$("#home-content").animate({opacity:1})', 200);
	setTimeout('$("#about-facts").animate({opacity:1})', 500);
	setTimeout('$("#home-featured").animate({opacity:1})', 800);
	setTimeout('$("#latest-news").animate({opacity:1})', 1100);
	setTimeout('$("#small-blue-circle").animate({opacity:.8})', 1400);
	
	$(window).resize(function(){
		
		//Resize Photo Wall
		resizeWall();		
				  
	});
						   
});

function resizeWall(){

	//WINDOW DIMENSIONS
	var winHeight = $(window).height();
	var winWidth = $(window).width();
	
	//DETERMINE AMOUNT OF ROWS AND COLUMNS
	var columns = Math.ceil(winWidth/140);
	var rows = Math.ceil(winHeight/180) + 1;
	
	//DETERMINE NUMBER OF ITEMS TO CREATE
	var createItems = columns*rows;
	
	//DETERMINE HOW MANY ITEMS CURRENTLY EXIST
	var currentItems = $('#conglomeration li').length;
	
	//DETERMINE HOW MANY ITEMS TO REMOVE OR ADD
	var addRemove = createItems - currentItems;
	
	//SET CONTAINER DIMENSIONS
	$('#home-conglomeration').height(winHeight);
	$('#home-conglomeration').width(winWidth);
	
	//SET WALL DIMENSIONS
	$('#conglomeration').height(rows*180);
	$('#conglomeration').width(columns*140);
	
	var congLeft = winWidth/2;
	
	$('#home-conglomeration').css({'marginLeft' : -congLeft});
	
	//ADD OR REMOVE ITEMS
	if (addRemove > 0){
		$.ajax({
			type: 'POST',
			url: '/site/home-conglomeration-init/',
			data: 'listNum='+createItems+'&type=added',
			success: function(msg){
				$('#conglomeration').append(msg);
				$('#conglomeration li.added').each(function(){
					$(this).animate({opacity:1}, 500);
				});
				$('#conglomeration li.added').hover(function(){
				
					$(this).css({'z-index' : '20'});
					$(this).children('a').children('.cong-img').animate({opacity:1}, 100);
													   
				},function(){
					
					$(this).css({'z-index' : '10'});
					$(this).children('a').children('.cong-img').animate({opacity:.8}, 100);
					
				});
				
			}
		});
	}
	else if (addRemove < 0){
		addRemove = Math.abs(addRemove);
		while(addRemove > 0){
			
			$('#conglomeration li:last').remove();
			addRemove -= 1;
		}
	}
	else {
		
	}
	
}
function initConglomerate(){
	
	var winHeight = $(window).height();
	var winWidth = $(window).width();
	
	var columns = Math.ceil(winWidth/140);
	var rows = Math.ceil(winHeight/180) + 1;
	
	var createItems = columns*rows;
	
	$('#home-conglomeration').height(winHeight);
	$('#home-conglomeration').width(winWidth);
	
	$('#conglomeration').height(rows*180);
	$('#conglomeration').width(columns*140);
	
	var congLeft = winWidth/2;
	
	$('#home-conglomeration').css({'marginLeft' : -congLeft});
		
	//GET INITIAL SET OF ITEMS
	$.ajax({
		type: 'POST',
		url: '/site/home-conglomeration-init/',
		data: 'listNum='+createItems+'type=init',
		success: function(msg){
			$('#conglomeration').html(msg);
			$('#conglomeration').animate({opacity:1}, 1700);
			$('#conglomeration li').hover(function(){
					
				$(this).css({'z-index' : '20'});
				$(this).children('a').children('.cong-img').animate({opacity:1}, 100);
												   
			},function(){
				
				$(this).css({'z-index' : '10'});
				$(this).children('a').children('.cong-img').animate({opacity:.8}, 100);
				
			});
			
		}
	});
	
}

function runConglomerate(){
	
	var totalItems = $('#conglomeration li').length;
	var notFilled = $('#conglomeration li:not(.filled)').length;
	
	if(notFilled == 0){
		
		var randomIndex = Math.floor(Math.random()*totalItems);
		var listItem = $('#conglomeration li:eq('+randomIndex+')');
		
	}
	else{
		
		var randomIndex = Math.floor(Math.random()*notFilled);
		var listItem = $('#conglomeration li:not(.filled):eq('+randomIndex+')');
		
	}

	$.get("/site/home-conglomeration-ajax", function(data){
		
		listItem.removeClass().addClass('loading');
		listItem.html(data);
		listItem.children('.cong-img').animate({
			opacity: .8							
		}, 500);
		listItem.removeClass().addClass('filled');
		
	});

}