$(document).ready(function(){
	
	setTimeout("$('#about-info').animate({opacity:1}, 600)", 300);
	
	$('#about-slider').cycle({ 
		fx:     'scrollHorz', 
		speed:  300, 
		timeout: 0, 
		next:   '#about-next', 
		prev:   '#about-previous' 
	});
	
	$('#watch-about').click(function(){
		
		$('#about-video').fadeIn(300);
		
      	VideoJS.setup();
		
		return false;
		
	});
	
	$('#close-about-video').click(function(){
		
		if(navigator.appName != "Microsoft Internet Explorer"){
			document.getElementById('video').pause();
		}
		else{
			$("#flow-video").flowplayer().stop();
		}
	
		$('#about-video').fadeOut(300);
		
		return false;
		
	});
	
	//Initialize Photo Wall
	initConglomerate();
	setInterval('runConglomerate()', 2500);
	
	//Resize Video Container
	$('#about-video').height($(window).height()-44);	
	$('#about-video').width($(window).width());
	
	$(window).resize(function(){
		
		//Resize Photo Wall
		resizeWall();		
		
		//Resize Video Container
		$('#about-video').height($(window).height()-44);	
		$('#about-video').width($(window).width());
				  
	});
	
	if(jQuery.url.attr('anchor') == "watch"){
		$('#watch-about').click();
	}
	
});

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
	$('#cong-container').height(winHeight);
	$('#cong-container').width(winWidth);
	
	//SET WALL DIMENSIONS
	$('#conglomeration').height(rows*180);
	$('#conglomeration').width(columns*140);
	
	var congLeft = winWidth/2;
	
	$('#cong-container').css({'marginLeft' : -congLeft});
	
	//ADD OR REMOVE ITEMS
	if (addRemove > 0){
		$.ajax({
			type: 'POST',
			url: '/site/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('.cong-img').animate({opacity:1}, 100);
													   
				},function(){
					
					$(this).css({'z-index' : '10'});
					$(this).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;
	
	$('#cong-container').height(winHeight);
	$('#cong-container').width(winWidth);
	
	$('#conglomeration').height(rows*180);
	$('#conglomeration').width(columns*140);
	
	var congLeft = winWidth/2;
	
	$('#cong-container').css({'marginLeft' : -congLeft});
		
	$.ajax({
		type: 'POST',
		url: '/site/conglomeration-init/',
		data: 'listNum='+createItems+'type=init',
		success: function(msg){
			$('#conglomeration').html(msg);
			$('#conglomeration').animate({opacity:1}, 500);
			$('#conglomeration li').hover(function(){
					
				$(this).css({'z-index' : '20'});
				$(this).children('.cong-img').animate({opacity:1}, 100);
												   
			},function(){
				
				$(this).css({'z-index' : '10'});
				$(this).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/conglomeration-ajax", function(data){
		
		listItem.removeClass().addClass('loading');
		listItem.html(data);
		listItem.children('.cong-img').animate({
			opacity: .8												
		}, 500);
		listItem.removeClass().addClass('filled');
		
	});

}