$(document).ready(function(){
	
	if ( $('#centralThumbImgs > div').length>0 ) {
	
		$('div#mainContainer').width($('#centralThumbImgs > div').length * 160); 
		
		// $('div#mainContainer').css({ "margin-left":"-"+(Math.round($('div#mainContainer').width()/2))+"px" });  
		
		// margin-top:-296px; margin-left:-480px;
		
		$('#centralThumbImgs > div').hover(
			
			function(){
				// $(this).animate({ "top":"+=10", "left":"+=10" }, 300, 'swing'); 
				// $('div.catHeaderBG', this).animate({ "opacity":"1.0", "filter":"alpha(opacity=100)" }, 300, 'swing'); 
				$('div.catHeaderBG', this).fadeTo(300, 1); 
			}, 
			function(){
				// $(this).animate({ "top":"-=10", "left":"-=10" }, 300, 'swing'); 
				// $('div.catHeaderBG', this).animate({ "opacity":"0.4", "filter":"alpha(opacity=40)" }, 300, 'swing'); 
				$('div.catHeaderBG', this).fadeTo(300, 0.7); 
			}
			
		);
		
		
		$('#centralThumbImgs > div').click(function(){
			
			catID = $(this).attr('id').split('_')[1]; 
			location.href = "photoGallery.php?cat=" + catID; 
			
		}); 
	
	}
	
	if ( $('#navDiv > a').length>0 ) {
	
		$('#navDiv > a').width( Math.round(960 / $('#navDiv > a').length - 2) ); 
	
		$('#navDiv > a').each(function(){
			
			if ( location.href.indexOf($(this).attr('href'))>-1 ) {
				$(this).addClass('activeCategory'); 
				return; 
			}
			
		});
	
	}
	
	if ( $('.scrollable').length>0 ) {
		
		window.imgSrcsToLoad = new Array(); 
		window.currSection = null; 
		window.loadingImages = new Array(); 
		window.mainImgMaxHeight = 423; 
		window.mainImgMaxWidth = 892; 
		loadImageSections(); 
		
	}
	
	if ( $('#googleMap').length>0 ) 
	initGoogleMap(); 
	
}); 



function initGoogleMap () {
	
	geocoder = new google.maps.Geocoder(); 
	pwAddress = "10/49 Butterfield Street, Herston, QLD"; 
	
	geocoder.geocode( { 'address': pwAddress }, function(results, status) {
		
      if (status == google.maps.GeocoderStatus.OK) {
	      
	    var myOptions = {
	      zoom: 15,
	      center: results[0].geometry.location,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
   	 	};
	      
	   	var map = new google.maps.Map(document.getElementById("googleMap"), myOptions);
	      
	   	var marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location
        }); 
        
      } 
      
    }); 
        
}


function loadImageSections () {
	
	loadPhotos = false; 
	
	for ( ph in mainPhotos ) {
		if ( mainPhotos[ph].length>0 ) {
			imgSrcsToLoad = mainPhotos[ph]; 
			currSection = ph; 
			loadPhotos = true; 
			break; 
		}
	}
	
	if ( loadPhotos ) {
	
		loadingImages = new Array(); 
		i = 0; 
		
		for ( imgSrc in imgSrcsToLoad ) { 
			
			//loadingImages[i] = new Image(); 
			//loadingImages[i].loaded = false; 
			//$(loadingImages[i]).load( function(){ displayLoadedImg(this); } ); 
			//loadingImages[i].id = "mainImg_" + imgSrcsToLoad[imgSrc].num; 
			
			$('#mainImgDiv_'+imgSrcsToLoad[imgSrc].num+' > img.mainDisplayImg').each(
				function () {
					this.loaded = false; 
					$(this).load( function(){ displayLoadedImg(this); } ); 
					$(this).attr({ 
						"id" : "mainImg_" + imgSrcsToLoad[imgSrc].num, 
						"src" : 'scripts/galleryThumbs.php?src=../' + imgSrcsToLoad[imgSrc].imgSrc + '&h=' + mainImgMaxHeight + '&w=' + mainImgMaxWidth
					}); 
				}
			); 
			
			//loadingImages[i].src = 'scripts/galleryThumbs.php?src=../' + imgSrcsToLoad[imgSrc].imgSrc + '&h=' + mainImgMaxHeight + '&w=' + mainImgMaxWidth; 
			
			loadingImages[i] = $('#mainImgDiv_'+imgSrcsToLoad[imgSrc].num+' > img.mainDisplayImg'); 
			
			i++; 
			
		}

	
	}
	
}

function displayLoadedImg ( img ) { 
	
	imgNum = $(img).attr('id').split('_')[1]; 
	
	img.loaded = true; 
	
	$('#mainImgDiv_'+imgNum+' > img.loaderImg').remove(); 
	
	$(img).css({ "margin-left":"0px", "margin-top":"0px" }); 
		
	if ( $(img).width() < mainImgMaxWidth ) {
		$(img).css("margin-left", ( Math.round(((mainImgMaxWidth-$(img).width())/2)) )+"px" ); 
	}
		
	if ( $(img).height() < mainImgMaxHeight ) {
		$(img).css("margin-top", ( Math.round(((mainImgMaxHeight-$(img).height())/2)) )+"px" );
	} 
		
	$(img).css({ "visibility":"visible" }); 
	
	if ( parseInt(imgNum)==1 ) mainScrollAPI.play(); 
	
	testIfAllItemsLoaded(); 
	
}



function testIfAllItemsLoaded () {
	
	if ( loadingImages.length>0 ) {
		
		allItemsLoaded = true; 
		
		for ( i in loadingImages ) { 
			
			loadingImages[i].each(function(){
				
				if ( !this.loaded ) {
					allItemsLoaded = false; 
					return; 
				}
				
			}); 
			
			if ( !allItemsLoaded ) break; 
			
		}
		
		if ( allItemsLoaded ) {
			
			if ( currSection ) {
				mainPhotos[currSection] = new Array(); 
				loadingImages = new Array(); 
				currSection = null; 
			}
			
			loadImageSections(); 
			
		}
		
	}
		
}

