/* 
	Simple gallery plugin
	
	Mike Harding, Cactuslab
	
	Based on 'innerfade' plugin by Torsten Baldes
	http://medienfreunde.com
*/

(function($) {
	
	var currentIndex = 0;
	
    $.fn.clgallery = function(options) {
        return this.each(function() {   
            $.clgallery(this, options);
        });
    };

	$.clgallery = function(container, options) {
		var settings = {
			'speed': 			'normal',
			'timeout': 			2000,
			'containerheight': 	'auto',
			'runningclass': 	'clgallery',
			'autoplay': 		false,
			'nextlabel': 		"Next",
			'prevlabel': 		"Prev",
			'showhidenav': 		true,
			'children':         null
		};
		
		if (options) {
			$.extend(settings, options);
		}
		
		if (settings.children === null){
			var elements = $(container).children();
		} else {
			var elements = $(container).children(settings.children);
		}
			
		if (elements.length > 1) {
            $(container).wrap('<div class="clgallery_container"></div>').css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
			
			// Add pagination
			var gallery = $(container).parent();
			gallery.append('<div class="clgallery_nav">' +
				'<div class="clgallery_pagination"><div class="clgallery_pages"><ul></ul></div></div>' +
				'<a class="clgallery_prev">'+settings.prevlabel+'</a><a class="clgallery_next">'+settings.nextlabel+'</a>' + 
				'</div>');

			var pagination = gallery.find('.clgallery_pagination ul');

			for (var i = 0; i < elements.length; i++) {
				$(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
				
				var newItem = $('<li>');
				$("<a>", {
					'href': $(elements[i]).find('img').attr('src'),
					'data-id': i+1,
					text: i+1,
					click: function(e){
						e.preventDefault();
						$.clgallery.next(elements, settings, $(this).attr('data-id')-1, $(this));
					}
				}).appendTo(newItem);
				
				newItem.appendTo(pagination);
			};
			
			updatePrevNext(elements, settings, 0, $(container));

			gallery.find('.clgallery_pagination li:first').addClass('clgallery_active');
			
			gallery.find('a.clgallery_prev, a.clgallery_next').click(function(e){
				e.preventDefault();
				$.clgallery.handleNextPrev(elements, settings, $(this));
			});

			// gallery.find('a.clgallery_next').click(function(e){
			// 	e.preventDefault();
			// 	$.clgallery.handleNextPrev(elements, settings, $(this));
			// });

			// if(settings.autoplay === true){
			// 	setTimeout(function() {
			// 		$.clgallery.next(elements, settings, 1);
			// 	}, settings.timeout);
			// }

			$(elements[0]).show();
			
			if(settings.containerheight == 'auto'){
				gallery.find('ul.clgallery').height($(elements[0]).height());
			}
			
			// 
			if(settings.showhidenav == true){
				gallerynav = $('div.clgallery_nav');
				gallery.hover(
					function(){
						gallerynav.stop().animate({ opacity: 1 });
					},
					function(){
						gallerynav.stop().animate({ opacity: 0 });
					}
				);
			}
		}
    };

	$.clgallery.next = function(elements, settings, next, object) {
		
		updatePrevNext(elements, settings, next, object);
		var container = $(object).closest('.clgallery_container');
		
		var pagination = container.find('.clgallery_pagination ul li');
		pagination.removeClass('clgallery_active');
		pagination.eq(next).addClass('clgallery_active');
		
		if(settings.containerheight == 'auto'){
			container.find('ul.clgallery').animate({ height: $(elements[next]).height() });
		}
		
		$(elements[currentIndex]).fadeOut(settings.speed);
		$(elements[next]).fadeIn(settings.speed, function() {
			removeFilter($(this)[0]);
		});
        
		currentIndex = next;
		// console.log('index is now '+currentIndex);
		
		// if ((next + 1) < elements.length) {
		// 	next = next + 1;
		// 	currentIndex = currentIndex - 1;
		// } else {
		// 	next = 0;
		// 	currentIndex = elements.length - 1;
		// }

		// setTimeout((function() {
		// 	$.clgallery.next(elements, settings, next);
		// }), settings.timeout);
	};
	
	$.clgallery.handleNextPrev = function(elements, settings, object){

		var linktype = 'next';

		if($(object).hasClass('clgallery_prev')){
			linktype = 'prev';
		}

		if(linktype=='next' && (currentIndex+1 < elements.length)){
			$.clgallery.next(elements, settings, currentIndex+1, $(object));
		} else if(linktype=='prev' && (currentIndex > 0)){
			$.clgallery.next(elements, settings, currentIndex-1, $(object));
		}
	}	

})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}

function updatePrevNext(elements, settings, next, object){
	if(object){
		var parent = $(object).closest('.clgallery_container');
		var prevlink = parent.find('a.clgallery_prev');
		var nextlink = parent.find('a.clgallery_next');
		
		if (next+1 == elements.length) {
			nextlink.addClass('clgallery_inactive');
			prevlink.removeClass('clgallery_inactive');
			prevlink.attr('href',$(elements[next-1]).find('img').attr('src'));
		} else if(next == 0) {
			prevlink.addClass('clgallery_inactive');
			nextlink.removeClass('clgallery_inactive');
			nextlink.attr('href',$(elements[next]).find('img').attr('src'));
		} else {
			prevlink.removeClass('clgallery_inactive');
			nextlink.removeClass('clgallery_inactive');
			nextlink.attr('href',$(elements[next+1]).find('img').attr('src'));
			prevlink.attr('href',$(elements[next-1]).find('img').attr('src'));
		}
	}
}