var galleryArray;
var currentItem = 0;
var status = 1;

function loadGallery(targetArray) {

	currentArray = targetArray;
	galleryArray = targetArray;
	
	resetugl();
	addPreloader();
	
	uglLoadImage($(galleryArray[0]));
	
	// alert( $(galleryArray[1]).attr('href') ); THIS WORKS
	
	
	
}

function stopGallery(targetArray) {

	status=0;
	
}


function addPreloader() {

	galleryArray.each(function(i) {
			
		$(this).css("background-color", "#000000");
/* 		$(this).addClass( "photographContainerVisible").removeClass("photographContainer"); */
			
	});

}

function uglLoadImage(targetDiv){


	if ( $(targetDiv).children().length > 0)
	{
	
	   
	   console.log("LOADING " + targetDiv.attr('href'));
		// $(targetDiv).fadeOut();
		// alert(currentGalleryArray);
		$(targetDiv).append("<img class='imageoncanvas' src='" + targetDiv.attr('href') + "'>");
		$(targetDiv).find('img').fadeOut();
		
		$(targetDiv).find(".imageoncanvas").load(function() {
			if($(this).parent().parent().hasClass('selectedgallery')){
				currentLoaded();
			}
			if($(this).parent().parent().hasClass('selectedaugallery')){
				currentLoaded();
			}
			$(this).parent().animate({ backgroundColor: "#f8f8f7" }, 'fast');
			console.log("LOADED- " + targetDiv.attr('href'));
			$(this).find('img').fadeIn();
			$(this).removeClass("imageoncanvas");
		});
		targetDiv.fadeIn();
	   
	   
	
	}else{
		
		
		if($(this).parent().parent().hasClass('selectedgallery')){
				currentLoaded();
			}
			if($(this).parent().parent().hasClass('selectedaugallery')){
				currentLoaded();
			}
			$(this).parent().animate({ backgroundColor: "#f8f8f7" }, 'fast');
			console.log("LOADED- " + targetDiv.attr('href'));
			$(this).find('img').fadeIn();
			$(this).removeClass("imageoncanvas");
		
	}

}

function currentLoaded() {
	
	currentItem++;
	uglLoadImage($(galleryArray[currentItem]));
	
}


function resetugl() {

	currentItem = 0;

}
