/*
 * 
 */
/*
 * 
 */

	 
	var curImg;


	
	function _next ( tag ) {
		var next;

		if($(tag).css("opacity") < 1)
			return;
				
		//$(tag).css("opacity", "0.0");			
		$(tag).animate({opacity: 0.0}, 300);
		$(tag).css("z-index", "0");
				
		if($(tag).attr("src") == $(tag).parent().find("img:last").attr("src")){
				next = $(tag).parent().find("img:first");
		}else{
				next = $(tag).next("img");
		}
				
		$(next).css("z-index", "99");
		//$(next).css("opacity", "1.0");
		$(next).animate({opacity: 1.0}, 300);
		curImg = next;
	}
	
	function _prev ( tag ) {
		var prev;
		
		if($(tag).css("opacity") < 1)
			return;
				
		//$(tag).css("opacity", "0.0");			
		$(tag).animate({opacity: 0.0}, 300);
		$(tag).css("z-index", "0");
				
		if($(tag).attr("src") == $(tag).parent().find("img:first").attr("src")){
				prev = $(tag).parent().find("img:last");
		}else{
				prev = $(tag).prev("img");
		}
				
		$(prev).css("z-index", "99");
		//$(prev).css("opacity", "1.0");
		$(prev).animate({opacity: 1.0}, 300);
		curImg = prev;
	}

	
		
    $(document).ready(function(){

			var gallery = "baeder";

			$('.galleryImage').each(function() {
					$(this).css("opacity", "0.0");
					$(this).css("z-index", "0");
					$(this).css("display", "none");
			});
			
			curImg = $(document).find("img:first");
		
			$(curImg).css("opacity", "1.0");
			$(curImg).css("z-index", "99");
		
			$('.galleryImage').click(function() {
				_next(curImg);
				
			});			
			$('#galleryRight').click(function() {
				_next(curImg);
				
			});		
			$('#galleryLeft').click(function() {
				_prev(curImg);
				
			});
			
		  	$('.gallery').each(function(index) {

				var gal = $(this).attr("gallery");

				if(gal == gallery){
					$(this).find("img").css("display", "block");
					$(this).find("form").css("display", "block");
					$(this).find("img:first").css("opacity", "1.0");
					$(this).find("img:first").css("z-index", "99");
					curImg = $(this).find("img:first");
					gallery = gal;
				}else{
					$(this).find("img").css("display", "none");	
					$(this).find("form").css("display", "none");
					$(this).find("img").css("z-index", "0");
					$(this).find("img").css("opacity", "0.0");
				}
			});
	  
			$('.galleryLink').click(function() {
				var set_gal = $(this).attr("gallery");
				
				if(gallery == set_gal)
					return;
				
				$('.gallery').each(function(index) {
					var gal = $(this).attr("gallery");
					if(gal == set_gal){
						$(this).find("img").css("display", "block");
						$(this).find("form").css("display", "block");
						$(this).find("img:first").css("opacity", "1.0");
						$(this).find("img:first").css("z-index", "99");
						curImg = $(this).find("img:first");
					}else{
						$(this).find("img").css("display", "none");	
						$(this).find("form").css("display", "none");
						$(this).find("img").css("opacity", "0.0");
						$(this).find("img").css("z-index", "0");
					}
				  });
				
				gallery = set_gal;

			});
			
		
    });
	
