/**
 * Simple yet (hopefully) universal image gallery plugin for jQuery
 * Usage: Just call .rotating_gallery() on any element that you want it to work on,
 * 			the script will do its best to add buttons if you haven't already.
 * Notes: Tested in IE6/7/8, Safari 5, Firefox 3.8/4, Chrome 5, Opera 11.
 * 			Colorbox: Requires colorbox plugin - http://colorpowered.com/colorbox/
 * Author: Andrew Lowther andy.lowther@mademedia.co.uk
 * Version: 1.1
 * Default Settings -
 * 		gallery_width: 3
 *		duration: 500
 * 		next_button: #gallery-next
 *		prev_button: #gallery-prev
 *		type: image
 * 		colorbox: false
 */

if(typeof jQuery != 'undefined') {
	(function($){
		$.fn.extend({
			rotating_gallery: function(custom_settings) {
				
				// If it's not at least jquery 1.2.6 then sad times :(
				if($.fn.jquery < '1.2.6') {return;}
				// If the actual object doesn't have length, sad times :(
				if(!$(this).length){
					return;
				}
				
				// Check for IE6
				if($.browser.msie && parseInt($.browser.version, 10) == 6) {
					return;
				}

				// Default settings
				var original_settings = {
					gallery_width: 3,
					duration: 500,
					next_button: '#gallery-next',
					prev_button: '#gallery-prev',
					child_elem: "li",
					type: 'media',
					colorbox: false
				}
				
				// Extend the original with any user settings
				var settings = $.extend(true, original_settings, custom_settings);
				
				return this.each(function() {
					// We need an object to work with
					var $t = $(this);
					var current_length = $t.children(settings.child_elem).length;
					var current_pos = 0;
					var current_css_left = 0;
					
					// Get the width, for accurate scrolling
					var item_width = parseInt($t.children(settings.child_elem).eq(0).width()) + parseInt($t.children(settings.child_elem).eq(0).css('margin-right').replace('px', '')) + parseInt($t.children(settings.child_elem).eq(0).css('margin-left').replace('px', '')) + parseInt($t.children(settings.child_elem).eq(0).css('padding-right').replace('px', '')) + parseInt($t.children(settings.child_elem).eq(0).css('padding-left').replace('px', ''));
					
					// If the length of the gallery is larger then the settings length, galleryize that stuff!
					if(current_length > settings.gallery_width) {
						
						if($t.parent().css('position') != 'relative') {
							$t.parent().css({
								"position": "relative"
							});
						}
						if($t.parent().css('overflow') != 'hidden') {
							$t.parent().css({
								"overflow": "hidden"
							});
						}
						
						// Append the parent object with a nice gallery wrapper
						$t.parent().append('<div class="rotating-' + settings.type + '-gallery-wrapper"></div>');
					
						// Clone the old gallery into the new object, then remove it
						var $n = $t.clone().appendTo('.rotating-' + settings.type + '-gallery-wrapper');
						$t.remove();
					
						// Remove any listclass classes and set the parent width.
						$n.children(settings.child_elem).removeClass('end').removeClass('last').removeClass('start').removeClass('first');	
						$n.css({width: '90000px', position: 'relative'}).addClass('rotating-' + settings.type + '-gallery');
					
						// If no next button exists, create it
						if(!$(settings.next_button).length) {
							$('.rotating-' + settings.type + '-gallery-wrapper').after('<div><a href="#next" id="' + settings.next_button.substr(1) + '"></a></div>');
						}
					
						// If no previous button exists, create it
						if(!$(settings.prev_button).length) {
							$('.rotating-' + settings.type + '-gallery-wrapper').before('<div><a href="#previous" id="' + settings.prev_button.substr(1) + '"></a></div>');
						}
					
						// If colourbox is enabled and colourbox isn't undefined, run it.			
						if(settings.colorbox && typeof colorbox != undefined) {
							var colorbox_selector = $n.parent().parent().attr('id').toString() + ' li a';
							try {
								$('#' + colorbox_selector).colorbox();
							} catch(e) {}
						}
						
						// Unbind the click event so no icky errors
						$(settings.prev_button).unbind('click');
						// Bind a new click event to our button and rotate that gallery
						$(settings.prev_button).live('click', function(){
							if(current_pos > 0) {
								current_pos--;
								$(settings.prev_button).css({display: 'block'});
								$(settings.next_button).css({display: 'block'});
								$('.rotating-' + settings.type + '-gallery').animate({
									left: current_css_left += item_width
								}, settings.duration);
							}
							if(current_pos == 0) {
								$(settings.prev_button).css({display: 'none'});
							}
							return false;
						});
						
						// Unbind the click event so no icky errors
						$(settings.next_button).unbind('click');
						// Bind a new click event to our button and rotate that gallery
						$(settings.next_button).live('click', function(){
							if(current_pos < (current_length - settings.gallery_width)) {
								current_pos++;
								$(settings.next_button).css({display: 'block'});
								$(settings.prev_button).css({display: 'block'});
								$('.rotating-' + settings.type + '-gallery').animate({
									left: current_css_left -= item_width
								}, 500);
							}
							
							if(current_pos == (current_length - settings.gallery_width)) {
								$(settings.next_button).css({display: 'none'});
							}
							return false;
						});
					
						// Automatigically set the previous button to display none, we don't need it yet
						$(settings.prev_button).css({display: 'none'});
					
					}
				});

			}
		});
	})(jQuery);
}
