/*
 * 	Easy Slider - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#images").easySlider();
 *	
 * 	<div id="images">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {
			prevId: 		    'prevBtn',
			prevIdOff: 		    'prevBtnOff',
			prevText: 		    'Previous',
			nextId: 		    'nextBtn',
			nextIdOff: 		    'nextBtnOff',
			nextText: 		    'Next',
			redDotTwoOne:       'redDotTwoOne',
			redDotTwoTwo:       'redDotTwoTwo',
			redDotThreeOne:     'redDotThree',
			redDotThreeTwo:     'redDotThreeTwo',
			redDotThreeThree:   'redDotThreeThree',
			redDotFourOne:      'redDotFourOne',
			redDotFourTwo:      'redDotFourTwo',
			redDotFourThree:    'redDotFourThree',
			redDotFourFour:     'redDotFourFour',
			orientation:	'', //  'vertical' is optional;
			speed: 			800
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this); 				
			var s = $("li", obj).length; // number of li's
			var w = obj.width(); 
			var h = obj.height(); 
			var ts = s-1;
			var t = 0;
			var pageCounter = 1;
			var vertical = (options.orientation == 'vertical');
			$("ul", obj).css('width',s*w);			
			if(!vertical) $("li", obj).css('float','left');
			$(obj).after('<span id="'+ options.redDotTwoOne +'"><a href=\"javascript:void(0);\" style="cursor: default;"><img src="/images/store/red_dot_2_1.png"></a></span>');	
			$(obj).after('<span id="'+ options.redDotTwoTwo +'"><a href=\"javascript:void(0);\" style="cursor: default;"><img src="/images/store/red_dot_2_2.png"></a></span>');	
			$(obj).after('<span id="'+ options.redDotThreeOne +'"><a href=\"javascript:void(0);\" style="cursor: default;"><img src="/images/store/red_dot_1.png"></a></span>');	
			$(obj).after('<span id="'+ options.redDotThreeTwo +'"><a href=\"javascript:void(0);\" style="cursor: default;"><img src="/images/store/red_dot_2.png"></a></span>');	
			$(obj).after('<span id="'+ options.redDotThreeThree +'"><a href=\"javascript:void(0);\" style="cursor: default;"><img src="/images/store/red_dot_3.png"></a></span>');	
			$(obj).after('<span id="'+ options.redDotFourOne +'"><a href=\"javascript:void(0);\" style="cursor: default;"><img src="/images/store/red_dot_4_1.png"></a></span>');
			$(obj).after('<span id="'+ options.redDotFourTwo +'"><a href=\"javascript:void(0);\" style="cursor: default;"><img src="/images/store/red_dot_4_2.png"></a></span>');
			$(obj).after('<span id="'+ options.redDotFourThree +'"><a href=\"javascript:void(0);\" style="cursor: default;"><img src="/images/store/red_dot_4_3.png"></a></span>');	
			$(obj).after('<span id="'+ options.redDotFourFour +'"><a href=\"javascript:void(0);\" style="cursor: default;"><img src="/images/store/red_dot_4_4.png"></a></span>');	

			$("a","#"+options.prevId).hide();
			$("a","#"+options.nextId).hide();
			
			$("a","#"+options.prevIdOff).hide();
			$("a","#"+options.nextIdOff).hide();
			
			$("a","#"+options.redDotTwoOne).hide();
			$("a","#"+options.redDotTwoTwo).hide();
			
			$("a","#"+options.redDotThreeOne).hide();
			$("a","#"+options.redDotThreeTwo).hide();
			$("a","#"+options.redDotThreeThree).hide();
			
			$("a","#"+options.redDotFourOne).hide();
			$("a","#"+options.redDotFourTwo).hide();
			$("a","#"+options.redDotFourThree).hide();
			$("a","#"+options.redDotFourFour).hide();
			
			$("a","#"+options.nextId).click(function(){		
				animate("next");	
				pageCounter=pageCounter+1;
				if (t>=ts) {
				    $(this).hide();
				    $("a","#"+options.nextIdOff).show();
				};
				if(s==2) {
                    $("a","#"+options.redDotTwoOne).hide();
				    $("a","#"+options.redDotTwoTwo).show();
                } 
                else if(s==3){
                    if (pageCounter==2){
				        $("a","#"+options.redDotThreeOne).hide();
				        $("a","#"+options.redDotThreeTwo).show();
                        $("a","#"+options.redDotThreeThree).hide();
				    }
				    else if (pageCounter==3){
				        $("a","#"+options.redDotThreeOne).hide();
				        $("a","#"+options.redDotThreeTwo).hide();
                        $("a","#"+options.redDotThreeThree).show();
				    };
                }
                else if(s==4){
                     if (pageCounter==2){
				        $("a","#"+options.redDotFourOne).hide();
				        $("a","#"+options.redDotFourTwo).show();
                        $("a","#"+options.redDotFourThree).hide();
                        $("a","#"+options.redDotFourFour).hide();
				    }
				    else if (pageCounter==3){
				        $("a","#"+options.redDotFourOne).hide();
				        $("a","#"+options.redDotFourTwo).hide();
                        $("a","#"+options.redDotFourThree).show();
                        $("a","#"+options.redDotFourFour).hide();
				    }
				    else if (pageCounter==4){
				        $("a","#"+options.redDotFourOne).hide();
				        $("a","#"+options.redDotFourTwo).hide();
                        $("a","#"+options.redDotFourThree).hide();
                        $("a","#"+options.redDotFourFour).show();
				    };
                };
				
				$("a","#"+options.prevIdOff).hide();
				$("a","#"+options.prevId).show();
				
			});
			$("a","#"+options.prevId).click(function(){		
				animate("prev");	
				pageCounter=pageCounter-1;
				if (t<=0) {
				    $(this).hide();
				    $("a","#"+options.prevIdOff).show();
				}
				if(s==2) {
                    $("a","#"+options.redDotTwoOne).show();
				    $("a","#"+options.redDotTwoTwo).hide();
                } 
                else if(s==3){
                    if (pageCounter==2){
				        $("a","#"+options.redDotThreeOne).hide();
				        $("a","#"+options.redDotThreeTwo).show();
                        $("a","#"+options.redDotThreeThree).hide();
				    }
				    else if (pageCounter==1){
				        $("a","#"+options.redDotThreeOne).show();
				        $("a","#"+options.redDotThreeTwo).hide();
                        $("a","#"+options.redDotThreeThree).hide();
				    };
                }
                else if(s==4){
                    if (pageCounter==2){
				        $("a","#"+options.redDotFourOne).hide();
				        $("a","#"+options.redDotFourTwo).show();
                        $("a","#"+options.redDotFourThree).hide();
                        $("a","#"+options.redDotFourFour).hide();
				    }
				    else if (pageCounter==3){
				        $("a","#"+options.redDotFourOne).hide();
				        $("a","#"+options.redDotFourTwo).hide();
                        $("a","#"+options.redDotFourThree).show();
                        $("a","#"+options.redDotFourFour).hide();
				    }
				    else if (pageCounter==1){
				        $("a","#"+options.redDotFourOne).show();
				        $("a","#"+options.redDotFourTwo).hide();
                        $("a","#"+options.redDotFourThree).hide();
                        $("a","#"+options.redDotFourFour).hide();
				    };
                };
				
				$("a","#"+options.nextIdOff).hide();
				$("a","#"+options.nextId).show();
			});	
			function animate(dir){
				if(dir == "next"){
					t = (t>=ts) ? ts : t+1;	
				} else {
					t = (t<=0) ? 0 : t-1;
				};								
				if(!vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						options.speed
					);				
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			};
			if(s>1) $("a","#"+options.nextId).show();
			
			if(s>1) $("a","#"+options.prevIdOff).show();
			
            if(s==2) {
                $("a","#"+options.redDotTwoOne).show();
            } 
            else if(s==3){
                $("a","#"+options.redDotThreeOne).show();
            }
            else if(s==4){
                $("a","#"+options.redDotFourOne).show();
            };
		});
	  
	};

})(jQuery);
