
var ajaxGallery = {};

ajaxGallery.settings = {
		//default values, override per design
		galleryFilesFolder : ".",
		imageFolder : "images/",
		thumbsFolder : "thumbs/",
		thumbsNailPrefix : "thumb_",
		thumbsNailDimensions : 200
}

ajaxGallery.constructGallery = function (divId, gallery){
	
	$("#"+divId)
		.empty()
		.append($("<div></div>")
			.addClass("AG_galleryViewport")
			.append($("<div></div>")
					.addClass("imageHolder")
			)
			.append($("<a></a>")
					.addClass("left")
			)
			.append($("<a></a>")
					.addClass("right")
			)
		)
		.append($("<div></div>")
			.addClass("AG_galleryThumbnails")
		)
		.append($("<div></div>")
			.addClass("AG_galleryCategories")
			.append($("<div></div>")
				.addClass("AG_galleryCategoriesLinks")
			)
			.append($("<div></div>")
				.addClass("AG_galleryCategoriesOpener")
			)
		)
		
	$.ajax({
		url: gallery,
		data: ajaxGallery.settings,
		context: document.body,
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert(XMLHttpRequest+ textStatus+ errorThrown);
		},
		success: function(data) {
			$('.AG_galleryThumbnails').html($(data).find('#AG_galleryThumbsScroller').html());
			ajaxGallery.thumbFunctionality();
			$('.AG_galleryCategoriesLinks')
				.html($(data).find('#AG_galleryCategories').html())
				.find("a:first")
				.addClass("active");
			ajaxGallery.setImage( $('.AG_galleryThumbnails').find("a").attr("href") );
			ajaxGallery.mouseFunctions();
			$('.AG_galleryCategoriesOpener')
				.append($("<a href='#'>&nbsp;</a>"))
				.click(function(e){
					e.preventDefault();
					ajaxGallery.toggleCategories();
				})
		}
	});
}
ajaxGallery.toggleCategories = function(){
	if($('.AG_galleryCategoriesLinks').innerWidth() > 0){
		$('.AG_galleryCategoriesLinks')
			.animate({
				"width":"220px"
			},"fast", "swing")
	}else{
		$('.AG_galleryCategoriesLinks')
			.animate({
				"width":"220px"
			},"fast", "swing")
	}
};


ajaxGallery.getSequenceImage = function(imageURL, offset){
	var sendBack = false;
	var index = 0;
	$(".AG_galleryThumbnails img")
		.each(function(i){
			if($(this).parent().attr("href") == imageURL){
				index = i + offset;
			}
		});
	if(index >= 0 && index < $(".AG_galleryThumbnails img").size()){
		$(".AG_galleryThumbnails img")
			.each(function(i){
				if(i == index){
					sendBack = $(this).parent().attr("href")
				}
			});
	};
	
	return sendBack;
};

ajaxGallery.setImage = function(imageURL){
		$('.AG_galleryViewport img').fadeOut("fast");
		$('.AG_galleryViewport .imageHolder')
			.empty()
			.append($('<img />')
				.attr("src", imageURL)
				.load(function(){
					$(this).fadeIn("slow");
				})
			);
	
	
	var previous = ajaxGallery.getSequenceImage(imageURL, -1);
	var next = ajaxGallery.getSequenceImage(imageURL, 1);
	
	if(previous){
		$('.AG_galleryViewport .left')
			.removeClass("inactive")
			.addClass("active")
			.attr("href", previous)
			.unbind()
			.click(function(e){
				e.preventDefault();
				ajaxGallery.setImage($(this).attr("href"));
			});
	}else{
		$('.AG_galleryViewport .left')
			.removeClass("active")
			.addClass("inactive")
			.attr("href","#")
			.unbind();
	}
	if(next){
		$('.AG_galleryViewport .right')
			.removeClass("inactive")
			.addClass("active")
			.attr("href",next)
			.unbind()
			.click(function(e){
				e.preventDefault();
				ajaxGallery.setImage($(this).attr("href"));
			});
	}else{
		$('.AG_galleryViewport .right')
			.removeClass("active")
			.addClass("inactive")
			.attr("href","#")
			.unbind();
	}
	
	$(".AG_galleryThumbnails img")
	.each(function(i){
		$(this).removeClass("current");
		if($(this).parent().attr("href") == imageURL){
			$(this).addClass("current");
			if($(this).is(":visible")){
				
			}else{
				var photoClusterIndex = Math.floor($(".AG_galleryThumbnails img").index(this)/5);
				var visibleClusterIndex = $(".AG_galleryThumbnails .AG_thumbCluster:visible").index();
				if(photoClusterIndex > visibleClusterIndex){
					ajaxGallery.toggleThumbNavigation(false, false);
					$('.AG_thumbCluster:visible')
						.animate({'margin-left': -568},"fast","swing",function(){$(this).hide();})
					$('.AG_thumbCluster')
						.each(function(){
							if($(this).index() == photoClusterIndex){
							$(this)
								.show()
								.css('margin-left' , 568 + 'px')
								.animate({'margin-left': '0px'},"fast","swing", ajaxGallery.checkThumbs);
							}
						})
				}else{
					ajaxGallery.toggleThumbNavigation(false, false);
					$('.AG_thumbCluster:visible')
						.animate({'margin-left': 568 },"fast","swing",function(){$(this).hide()})
					$('.AG_thumbCluster')
						.each(function(){
							if($(this).index() == photoClusterIndex){
							$(this)
								.show()
								.css('margin-left' , -568 + 'px')
								.animate({'margin-left': '0px'},"fast","swing", ajaxGallery.checkThumbs);
							}
						})
				}
			}
		}
	});
}

ajaxGallery.nextThumbs = function(event){
	event.preventDefault();
	ajaxGallery.toggleThumbNavigation(false, false);
	$('.AG_thumbCluster:visible')
		.animate({'margin-left': -$(this).parent().innerWidth() + 'px'},"fast","swing",function(){$(this).hide();})
		.next('.AG_thumbCluster')
		.show()
		.css('margin-left' , $(this).parent().innerWidth() + 'px')
		.animate({'margin-left': '0px'},"fast","swing", ajaxGallery.checkThumbs);
};

ajaxGallery.previousThumbs = function(event){
	event.preventDefault();
	ajaxGallery.toggleThumbNavigation(false, false);
	$('.AG_thumbCluster:visible')
		.animate({'margin-left': $(this).parent().innerWidth() + 'px'},"fast","swing",function(){$(this).hide()})
		.prev('.AG_thumbCluster')
		.show()
		.css('margin-left' , -$(this).parent().innerWidth() + 'px')
		.animate({'margin-left': '0px'},"fast","swing", ajaxGallery.checkThumbs)
}

ajaxGallery.thumbFunctionality = function(){
	var amount = 0;
	$(".AG_thumbCluster")
		.each(function(){amount++;})
		.hide()
		.find('img')
		.load(function(){
			$(this).fadeIn();
		})
		.end()
		.first()
		.show();
	if(amount>1){
		$('.AG_galleryThumbnails')
			.append($('<a href="#">&nbsp;</a>')
				.addClass('AG_nextThumbnail AG_navigation')
			)
			.append($('<a href="#">&nbsp;</a>')
				.addClass('AG_previousThumbnail AG_navigation')
			)
		ajaxGallery.toggleThumbNavigation(false, amount>1);
	}
}

ajaxGallery.checkThumbs = function(){
	var reachedEnd = $('.AG_thumbCluster:last').css("display") == "none";
	var reachedStart = $('.AG_thumbCluster:first').css("display") == "none";
	ajaxGallery.toggleThumbNavigation(reachedStart, reachedEnd);
}

ajaxGallery.toggleThumbNavigation = function(previous, next){
	if(previous){
		 $(".AG_previousThumbnail")
			.unbind()
			.click(ajaxGallery.previousThumbs)
			.addClass("AG_active")
			.removeClass("AG_inactive")
	}else{ 
		$(".AG_previousThumbnail")
			.unbind()
			.click(function(e){e.preventDefault()})
			.addClass("AG_inactive")
			.removeClass("AG_active")
			
	}
	if(next){ 
		$(".AG_nextThumbnail")
			.unbind()
			.click(ajaxGallery.nextThumbs) 
			.addClass("AG_active")
			.removeClass("AG_inactive")
	}else{ 
		$(".AG_nextThumbnail")
			.unbind()
			.click(function(e){e.preventDefault()})
			.addClass("AG_inactive")
			.removeClass("AG_active")
	}
}

ajaxGallery.setCategory = function(category){
	$('.AG_galleryViewport a, .AG_galleryThumbnails a, .AG_galleryCategories a')
		.unbind()
		.click(function(event){
			event.preventDefault();
		});
	
	$.ajax({
		url: 'gallery.php?category='+category,
		data: ajaxGallery.settings,
		context: document.body,
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert(XMLHttpRequest+ textStatus+ errorThrown);
		},
		success: function(data) {
			/*document.write($(data)
						.find('#AG_galleryThumbsScroller')
						.html());*/
			$('.AG_galleryThumbnails').fadeOut("fast", function(){
				$('.AG_galleryThumbnails')
					.empty()
					.html($(data)
						.find('#AG_galleryThumbsScroller')
						.html()
					)
					.fadeIn();
				ajaxGallery.thumbFunctionality();
				
				ajaxGallery.setImage($('.AG_galleryThumbnails')
					.find("a")
					.attr("href"));
				ajaxGallery.mouseFunctions();
			})
		}
	});
	ajaxGallery.toggleCategories();
}

ajaxGallery.mouseFunctions = function(){

	$('.AG_thumbCluster a').click(function(event){
		event.preventDefault();
		ajaxGallery.setImage($(this).attr("href"));
	})
	$('.AG_galleryCategoriesLinks a').click(function(event){
		event.preventDefault();
		ajaxGallery.setCategory($(this).html());
		$('.AG_galleryCategoriesLinks a').removeClass("active").addClass("inActive");
		$(this).removeClass("inActive").addClass("active");
	})
	
}
