

//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////// | VARIABLES | ///////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////

//window.setInterval(updateScrollPos, 1000/5);
var timer,
	buildNum	= 	0,
	buildSet	= 	'a',
	tmp_array	= 	new Array(),
	TIME_DELAY	= 	7000,
	ANI_SPEED	= 	200,
	ITEM_DELAY	= 	400,
	IMG_PATH	= 	'http://devlinux.propaganda3.com/confluence/website/wp-content/themes/confluence/images/mosaic-',
	//IMG_PATH	= 	'http://www.thinkconfluence.com/wp-content/themes/confluence/images/mosaic-',
	link_array	= 	new Array(	'/confluence/website/work/health-care-senior-living/ ',
								'/confluence/website/work/athletics/',
								'/confluence/website/work/civic-institutions/',
								'/confluence/website/work/corporate/',
								'/confluence/website/work/leed/',
								'/confluence/website/work/commercial-mixed-use/',
								'/confluence/website/work/colleges-universities/ ',
								'/confluence/website/work/residential/',
								'/confluence/website/work/corridors-streetscape/',
								'/confluence/website/work/parks-open-space/',
								'/confluence/website/work/planning/',
								'/confluence/website/work/transportation/',
								'/confluence/website/work/waterfronts/',
								'/confluence/website/work/primary-secondary-schools/'
								),
	
	background_array = new Array( 'url('+IMG_PATH+'btn_1.png) 0 500px no-repeat',
							 	  'url('+IMG_PATH+'btn_2.png) 0 500px no-repeat',
							 	  'url('+IMG_PATH+'btn_3.png) 0 500px no-repeat',
							 	  'url('+IMG_PATH+'btn_4.png) 0 500px no-repeat',
							 	  'url('+IMG_PATH+'btn_5.png) 0 500px no-repeat',
							 	  'url('+IMG_PATH+'btn_6.png) 0 500px no-repeat',
							 	  'url('+IMG_PATH+'btn_7.png) 0 500px no-repeat',
							 	  'url('+IMG_PATH+'btn_8.png) 0 500px no-repeat',
							 	  'url('+IMG_PATH+'btn_9.png) 0 500px no-repeat',
							 	  'url('+IMG_PATH+'btn_10.png) 0 500px no-repeat',
							 	  'url('+IMG_PATH+'btn_11.png) 0 500px no-repeat',
							 	  'url('+IMG_PATH+'btn_12.png) 0 500px no-repeat',
							 	  'url('+IMG_PATH+'btn_13.png) 0 500px no-repeat',
							 	  'url('+IMG_PATH+'btn_14.png) 0 500px no-repeat'	
								),
	
	img_array	= new Array(IMG_PATH + '1b.jpg',
							IMG_PATH + '2b.jpg',
							IMG_PATH + '3b.jpg',
							IMG_PATH + '4b.jpg',
							IMG_PATH + '5b.jpg',
							IMG_PATH + '6b.jpg',
							IMG_PATH + '7a.jpg',
							IMG_PATH + '7b.jpg',
							IMG_PATH + '8a.jpg',
							IMG_PATH + '8b.jpg',
							IMG_PATH + '9a.jpg',
							IMG_PATH + '9b.jpg',
							IMG_PATH + '10a.jpg',
							IMG_PATH + '10b.jpg',
							IMG_PATH + '11a.jpg',
							IMG_PATH + '11b.jpg',
							IMG_PATH + '12a.jpg',
							IMG_PATH + '12b.jpg',
							IMG_PATH + '13a.jpg',
							IMG_PATH + '13b.jpg',
							IMG_PATH + '14a.jpg',
							IMG_PATH + '14b.jpg'
							);

//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////













//////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// | INIT METHODS | //////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////

function initMosiacBuild(){
	initMosaicTimer();
	addMosaicHandlers();
	addInitLinksToMosaic();
	preloadImages();
}


function initMosaicTimer(){
	timer = window.setTimeout(rebuildMosiac, TIME_DELAY);
}


function addMosaicHandlers(){
	$('.gridWrapper li').mouseover(onMosaicMouseOver); 
	$('.gridWrapper li').mouseout(onMosaicMouseOut);
}


function addInitLinksToMosaic(){
	for(var i = 0; i < 7; i++){
		$('.gridWrapper li:eq('+ i +') a')
			.attr('href', link_array[i])
			.css({'background':background_array[i]})
			.mouseover(gridItemMouseOverHandler)
			.mouseout(gridItemMouseOutHandler);
	}
}


function preloadImages(){
	var tmpIMG = new Image();
	for(var i = 0; i < img_array.length; i++){
		tmpIMG.src	= img_array[i];
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////













//////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// | EVENT HANDLERS | /////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////

function gridItemMouseOverHandler(evt){
	$(evt.target).css({'background-position':'0 0'});
}

function gridItemMouseOutHandler(evt){
	$(evt.target).css({'background-position':'0 500px'});
}

//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////













//////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// | MOSIAC BUILD METHODS | //////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////

function rebuildMosiac(){
	
	buildNum		= (buildNum == 0) ? 7 : 0;
	//buildSet		= (buildSet == 'a') ? ((buildNum == 0) ? 'b' : 'a') : ((buildNum == 0) ? 'a' : 'b');
		
	for(var i = 0; i < $('.gridWrapper li').size(); i++){
		tmp_array.push({'src':IMG_PATH + ((i+1) + buildNum) + buildSet +'.jpg',
						'href':link_array[(i + buildNum)],
						'target':i,
						'height':$('.gridWrapper li:eq('+ i +') img').height(),
						'width': $('.gridWrapper li:eq('+ i +') img').width(),
						'background':background_array[(i + buildNum)]
						});
	}
	
	displayIMG(Math.floor(Math.random() * tmp_array.length));
}

function displayIMG(index){
	
	if(tmp_array.length > 0){
		$('.gridWrapper li:eq('+ tmp_array[index].target +') a').css({'background':tmp_array[index].background});
		$('.gridWrapper li:eq('+ tmp_array[index].target +') img')
			.animate({'height':'10px', 
					'width':tmp_array[index].width, 
					'top':(tmp_array[index].height / 2)}, ANI_SPEED, function(){
																			$(this).attr('src', tmp_array[index].src)
																					.animate({'height':tmp_array[index].height, 'top':0}, ANI_SPEED);
																			$('.gridWrapper li:eq('+ tmp_array[index].target +') a').attr('href', tmp_array[index].href);
																			tmp_array.splice(index, 1);
																			//displayIMG(Math.floor(Math.random() * tmp_array.length));
																			setTimeout(function(){
																				displayIMG(Math.floor(Math.random() * tmp_array.length));					
																			},ITEM_DELAY)
		});		
	}
	else{
		if(timer != null) initMosaicTimer();
	}
	
}




// MOUSE OVER
function onMosaicMouseOver(evt){
	window.clearTimeout(timer);
	timer = null;
}



// MOUSE OUT
function onMosaicMouseOut(evt){
	if(timer == null) initMosaicTimer();
}


//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
