/**
 * jQuery.geeSlider - Simple sliding gallery
 * Date: 31/01/2011
 *
 * @author Shannon Smith
 * @version 0.1
 * @jQuery Version: 1.4.4
 *
 ***************************
 * - Added paging functionality
 *
 * Copyright: GeeMultimedia
 **/

(function($){
		$.fn.geeSlider2 = function(options) {
				var settings = {
						delay: 5000,
						duration: 1000,
						easing: 'swing',
						slider: null,
						bookmarks: null
					};
				if (options) { 
					$.extend(settings, options);
				}

				return this.each(function() {
						function position_elements() {
							var int_div = (current_index) / hero_li_length;
							int_div = int_div < 0 ? Math.ceil(int_div) : Math.floor(int_div);

							if((-(current_index % hero_li_length) - visible_count) < 0) {
								var low_offset = -(int_div +1) * hero_outer_width * hero_li_length;
								var high_offset = -(int_div) * hero_outer_width * hero_li_length;
							}
							else {
								var low_offset = -(int_div) * hero_outer_width * hero_li_length;
								var high_offset = -(int_div -1) * hero_outer_width * hero_li_length;
							}

							var cutover;
							if((-(current_index % hero_li_length) - visible_count) < 0) {
								cutover = hero_li_length + -(current_index % hero_li_length) - visible_count;
							}
							else {
								cutover = -(current_index % hero_li_length) - visible_count
							}

							hero_li.each(function(i) {
									$(this).css({
											left: ((i <= cutover ? high_offset : low_offset) + i * hero_outer_width) + 'px'
										});
								});

							hero_ul.stop().animate({
									left: (hero_outer_width * current_index) + 'px'
								}, 400, settings.easing);
						}

						var $this = this;
						var hero_ul = $(settings.slider ? settings.slider : $this);

						var hero_li = hero_ul.find('li');
						var hero_outer_width = hero_li.outerWidth();
						var hero_outer_height = hero_li.outerHeight();

						var visible_count;

						if(hero_ul.is('div')) {
							visible_count = Math.floor(hero_ul.outerWidth() /hero_outer_width);
							hero_ul = hero_ul.find('ul').first();
						}
						else {
							hero_ul.wrap('<div style="position: relative; width: ' + hero_outer_width + 'px; height: ' + hero_outer_height + 'px; overflow: hidden;" />');
							visible_count = 1;
						}

						var current_index = 0;
						var hero_li_length = hero_li.length;

						var bookmarks = null;
						if(settings.bookmarks) {
							bookmarks = $(settings.bookmarks);

							bookmarks.filter('a[href="#next"]').click(function() {
									current_index--;

									position_elements();
									return false;
								});
							bookmarks.filter('a[href="#prev"]').click(function() {
									current_index++;

									position_elements();
									return false;
								});
						}

						hero_ul.css({
								width: hero_li.length *hero_outer_width + 'px',
								height: hero_outer_height,
								position: 'absolute',
								left: '0px'
							});
						hero_li.each(function(i) {
								$(this).css({
										position: 'absolute',
										left: (hero_outer_width * i) + 'px'
									});
							});
				});
			};
	})(jQuery);
