/**
 * wSlide 0.1 - http://www.webinventif.fr/wslide-plugin/
 * 
 * Rendez vos sites glissant !
 *
 * Copyright (c) 2008 Julien Chauvin (webinventif.fr)
 * Licensed under the Creative Commons License:
 * http://creativecommons.org/licenses/by/3.0/
 *
 * Date: 2008-01-27
 *
 * MODIFIED BY Matt Bain - Sole Graphics
 * Allow pagination, next and prev links, and new animation types
 * Date: 2009-04-14
 *
 */
(function($) {
		
		$.soleslide = function(data) {}
		
		
		$.soleslide.settings = {
				width: 150,
				height: 150,
				start: 1,
				columns: 1,
				effect: 'swing',
				fade: false,
				horiz: false,
				duration: 1500,
				tightFit: '',
				title: false,
				links: true,
				linkNext: false,
				linkPrev: false,
				onCurrent: function() {}
		}
		
		var $ss = $.soleslide.settings;
		var $cp = $ss.start;
		var $np = 0;
		var $pp = 0;
			
		$.soleslide.helpers = {
			next: function() {
				$np = (Math.round(Number($cp) + 1) <= panes.length) ? Math.round(Number($cp) + 1) : 1;
				this.slide($np);				
			},
			prev: function() {
				$pp = (($cp - 1) > 0) ? $cp - 1 : panes.length;
				this.slide($pp);
			},
			titleHide: function() {
				if (!$ss.title) return;
				$('#' + $ss.title).html('&nbsp;');
			},
			titleShow: function(value) {
				if (!$ss.title) return;
				
				$('#' + $ss.title).html(value);
			},
			current: function(num) {
				// Update the current position
				$cp = num;
								
				if (typeof $ss.onCurrent == 'function') {
					$ss.onCurrent($cp);	
				}
				
				// Update current nav tab
				$('a[rel*="soleslide"]').removeClass("wactive");
				$('a[key="' + num + '"]').addClass("wactive");
			},
			getPos: function(num) {
				// split out the positioning of the list element
				var c = $('#'+ objid +'-'+ num);
				var title = c.attr('title');
				var pos = c.attr('rel').split(':');
				
				return [-pos[0],-pos[1],title];
			},
			slide: function(num) {
				
				// Set current links
				this.current(num);
				
				// Get target positioning
				var pos = this.getPos(num);
				var posL = pos[0];
				var posT = pos[1];
				var title = pos[2];

				// Perfrom the animation
				if (typeof $ss.fade == 'boolean') {
					if ($ss.fade){
						obj.animate({ opacity: 0 }, $ss.duration/2, effects, function(){
							$(this).css('top',posT +'px').css('left',posL +'px');$(this).animate({ opacity: 1 }, $ss.duration/2, effects)
						});
					} else {
						obj.animate({ top: posT +'px',left: posL +'px' }, $ss.duration, effects, 
							function() {$(this).animate({ opacity: 1 }, 500)}
						);
					}
				} else {
					// Fade Out, Slide, Fade In
					obj.animate({opacity:.5},150,effects,function() { 
						$.soleslide.helpers.titleHide();
						$(this).animate({ top: posT+'px',left: posL+'px' }, $ss.duration, effects, function() {
							$(this).animate({ opacity: 1 }, 300, effects, $.soleslide.helpers.titleShow(title));
						});
					});
				}
	
			}
		}
		
		$.fn.soleslide = function(settings) {

			if (settings) $.extend($ss, settings);
			
			this.each(function(cnt) {
				
				obj = $(this);
				
				// Some General Settings
				objid = obj.attr('id');
				if(objid == 'undefined') {
					objid = 'soleSlide' + cnt;
				}
				
				// Effects
				effects = $ss.effect;
				if(jQuery.easing.easeInQuad == undefined && (effects!='swing' || effects!='normal')){
					effects = 'swing';
				}
				
				// Create the wrapper and wrapper
				obj.wrap('<div class="soleSlideWrapper" id="' + objid +'Wrapper"></div>'); /// WAS CAUSING BUGS IN IE with Wrap???
				//var wrapper = $('body');
				var wrapper = $('#' + objid +'Wrapper');
				
				// Iterate over the list elements
				panes = wrapper.find('ul li');
				
				// Do some math for sizing
				columns = ($ss.horiz) ? columns =  Number(panes.length+1) : $ss.columns;
				numLines = Math.ceil(Number(panes.length)/columns);
				
				// CSS Styles for the ul element
				obj.css('margin','0');
				obj.css('padding','0');
				
				// CSS for the wrapper div
				wrapper.css('overflow','hidden');
				wrapper.css('position','relative');
				wrapper.css('width',$ss.width + 'px');
				wrapper.css('height',$ss.height + 'px');
				wrapper.css('text-align','left');
				
				// Set the original list CSS
				obj.css('position','absolute');
				obj.css('margin','0')
				obj.css('padding','0')
				obj.css('width',Number((columns+0)*$ss.width)+'px')
				obj.css('height',Number(numLines*$ss.height)+'px');
				
				// CSS list element 'panes'
				panes.css('overflow','hidden');
				panes.css('display','block');
				panes.css('float','left');
				panes.css('width',$ss.width + 'px');
				panes.css('height',$ss.height + 'px');
				
				// If there's right padding on the last element,kill the padding
				if (typeof $ss.tightFit == 'string') {
					panes.find( $ss.tightFit + ':last-child').css('margin-right','0').css('padding-right','0');	
				}
				
				
				// Setup paging links
				paging = '';
				panes.each(function (i) {
					var offset = obj.offset();
					var myoffset = $(this).offset();
					$(this).attr('id',objid+'-'+Number(i+1))
					$(this).attr('rel', Number(myoffset.left-offset.left)+':'+Number(myoffset.top-offset.top));
					paging += ' <a href="javascript:;void(0);" rel="soleslide" key="' + Number(i+1) + '">'+Number(i+1)+'</a>';
				});
				
				if (typeof $ss.links == 'boolean') {
					if($ss.links) {
						wrapper.after('<div class="wslideMenu" id="'+objid+'Menu">'+ paging +'</div>');
					}
				} else if (typeof $ss.links == 'string') {
					if ($('#'+$ss.links).length) {
						$('#'+$ss.links).html(paging);
					} else {
						wrapper.after('<div id="#'+$ss.links+'">'+ paging +'</div>');
					}
				}
				
				// Iterate over each link and give click function
				$('a[rel*="soleslide"]').click(function() {
						var key = $(this).attr('key');		
						$.soleslide.helpers.slide(key);
				});
				
				
				// Previous & Next Buttons
				if (typeof $ss.linkNext == 'string') {					
					$('#' + $ss.linkNext).click(function() {  $.soleslide.helpers.next();  });
				}
				
				if (typeof $ss.linkPrev == 'string') {
					$('#' + $ss.linkPrev).click(function() { $.soleslide.helpers.prev(); });
				}			
				
				// Start at the first position
				$cp = ($ss.start <= 0) ? 1 : $ss.start;
				$.soleslide.helpers.current($cp);
				
				if ($cp > 1) {
					$.soleslide.helpers.slide($cp);
				}
			
		
		});  /** End Each **/
		
		
		
		
		
		
    }
})(jQuery);
