/*
 * 	Easy Slider 1.7 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	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 $("#slider").easySlider();
 *	
 * 	<div id="slider">
 *		<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',
            prevText: 'Previous',
            nextId: 'nextBtn',
            nextText: 'Next',
            controlsShow: true,
            controlsBefore: '',
            controlsAfter: '',
            controlsFade: true,
            firstId: 'firstBtn',
            firstText: 'First',
            firstShow: false,
            lastId: 'lastBtn',
            lastText: 'Last',
            lastShow: false,
            vertical: false,
            speed: 800,
            auto: false,
            pause: 2000,
            continuous: false,
            numeric: false,
            numericId: 'numericcontrols',
            numbericShowPrevBttn: false,
            numbericShowNextBttn: false,
            buttonsAfter: false,
            slidingMode: 'Page',
            divSlidingSpeed: 10,
            animationMode: "Slid"
        };

        var options = $.extend(defaults, options);

        this.each(function() {
            var obj = $(this);
			var h = $("li", obj).height();
			obj.height(h);
			obj.css("overflow", "hidden");
			var w = $("li", obj).width();
			obj.width(w);
			var ts = 0;
			var t = 0;
			var maxDivPosition = 0;
			var currentDivPosition = 0;
			var clickable = true;
			
            if(options.animationMode == "Fade")
            {
				$("ul", obj).css('position', 'relative');
				var myCounter = 0
				$("li", obj).each(function(){
					$(this).css('position', 'absolute');
					if(myCounter != 0)
						$(this).hide();
					myCounter++;
				})
            }
			var s = $("li", obj).length;
			ts = s - 1;

            if(options.slidingMode == "Page" && options.animationMode != "Fade")
            {
				//return false;
				$("ul", obj).css('width', s * w);
			
				if (options.continuous) {
					
					$("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left", "-" + w + "px"));
					$("ul", obj).append($("ul li:nth-child(2)", obj).clone());
					$("ul", obj).css('width', (s + 1) * w);
				};
				if (!options.vertical && options.animationMode != "Fade") $("li", obj).css('float', 'left');
            }
            else if(options.slidingMode == "Div")
            {
				if (!options.vertical) 
				{
					$("li", obj).each(function(){
						$(this).css('float', 'left');
					})
				}
				var w = 0;
				$("li", obj).each(function(){
					//alert($(this).width())
					w += $(this).width();
				});
				$("ul", obj).css('width', w);
            }
            
            
            maxDivPosition = $("ul", obj).width();


            if (options.controlsShow) {
                var html = "";

                if (!options.numeric){
                    var prevBttnHtml = "";
                    var nextBttnHtml = "";
                    if (options.firstShow) {
                        prevBttnHtml += '<span id="' + options.firstId + '"><a href=\"javascript:void(0);\">' + options.firstText + '</a></span>';
                    }
                    prevBttnHtml += ' <span id="' + options.prevId + '"><a href=\"javascript:void(0);\">' + options.prevText + '</a></span>';
                    
                    if(!options.buttonsAfter)
						$(obj).before(prevBttnHtml);
					else
						$(obj).after(prevBttnHtml);

                    nextBttnHtml += ' <span id="' + options.nextId + '"><a href=\"javascript:void(0);\">' + options.nextText + '</a></span>';
                    if (options.lastShow) {
                        nextBttnHtml += ' <span id="' + options.lastId + '"><a href=\"javascript:void(0);\">' + options.lastText + '</a></span>';
                    }
                    $(obj).after(nextBttnHtml);
                }
                else {
					
                
                    html = options.controlsBefore;
                    html += '<ol id="' + options.numericId + '"></ol>';
                    html += options.controlsAfter;
                    $(obj).after(html);
                } 

            };

            if (options.numeric ) {
				if(options.numbericShowPrevBttn)
				{
                    $(document.createElement("li"))
						.attr('id', options.numericId + "_prevbuttn")
						.html('<a rel="'+(s - 1)+'" href=\"javascript:void(0);\">&lt;</a>')
						.appendTo($("#" + options.numericId))
						.click(function() {
							var index = $("a", $(this)).attr('rel')
							//alert(index)
						    animate(index, true);
						});
				}
				
                for (var i = 0; i < s; i++) {
                    $(document.createElement("li"))
						.attr('id', options.numericId + (i + 1))
						.html('<a rel=' + i + ' href=\"javascript:void(0);\">' + (i + 1) + '</a>')
						.appendTo($("#" + options.numericId))
						.click(function() {
						    animate($("a", $(this)).attr('rel'), true);
						});
                };
                
				if(options.numbericShowNextBttn)
				{
                    $(document.createElement("li"))
						.attr('id', options.numericId + "_nextbuttn")
						.html('<a rel="1" href=\"javascript:void(0);\">&gt;</a>')
						.appendTo($("#" + options.numericId))
						.click(function() {
						    animate($("a", $(this)).attr('rel'), true);
						});
				}
            } else{
				if(options.slidingMode == "Page")
				{
					$("a", "#" + options.nextId).click(function() {
						animate("next", true);
					});
					$("a", "#" + options.prevId).click(function() {
						animate("prev", true);
					});
				}
				if(options.slidingMode == "Div")
				{
					$("a", "#" + options.nextId).mousedown(function() {
						options.auto = true;
						animate("next", true);
					});
					$("a", "#" + options.prevId).mousedown(function() {
						options.auto = true;
						animate("prev", true);
					});
					$("a", "#" + options.nextId).mouseup(function() {
						options.auto = false;
						clearTimeout(timeout);

					});
					$("a", "#" + options.prevId).mouseup(function() {
						options.auto = false;
						clearTimeout(timeout);
					});
				}
				$("a", "#" + options.firstId).click(function() {
					animate("first", true);
				});
				$("a", "#" + options.lastId).click(function() {
					animate("last", true);
				});
            };

            function setCurrent(i) {
                i = parseInt(i) + 1;
                $("li", "#" + options.numericId).removeClass("current");
                $("li#" + options.numericId + i).addClass("current");
            };

            function adjust() {
				if(options.slidingMode == "Page" && options.animationMode != "Fade")
				{
					if (t > ts) t = 0;
					if (t < 0) t = ts;
					if (!options.vertical) {
						$("ul", obj).css("margin-left", (t * w * -1));
					} else {
						$("ul", obj).css("margin-left", (t * h * -1));
					}
					if (options.numeric) setCurrent(t);
                }
                else if(options.animationMode == "Fade")
                {
					if (t > ts) t = 0;
					if (t < 0) t = ts;
					if (options.numeric) setCurrent(t);
                }
                clickable = true;
            };

            function animate(dir, clicked) {
                if (clickable) {
                    clickable = false;
					if(options.animationMode == "Slid")
					{
						if(options.slidingMode == "Page")
						{
							var ot = t;
							switch (dir) {
								case "next":
									t = (ot >= ts) ? (options.continuous ? t + 1 : ts) : t + 1;
									break;
								case "prev":
									t = (t <= 0) ? (options.continuous ? t - 1 : 0) : t - 1;
									break;
								case "first":
									t = 0;
									break;
								case "last":
									t = ts;
									break;
								default:
									t = dir;
									break;
							};
							
							if(options.numbericShowPrevBttn)
							{
								$(obj).parent().find("#" + options.numericId + "_prevbuttn a").attr("rel", t == 0? ts : (parseInt(t) - 1) )
							}
							
							if(options.numbericShowNextBttn)
							{
								$(obj).parent().find("#" + options.numericId + "_nextbuttn a").attr("rel", t == ts? 0 : (parseInt(t) + 1) )
							}
							
							var diff = Math.abs(ot - t);
							var speed = diff * options.speed;
							if (!options.vertical) {
								p = (t * w * -1);
								$("ul", obj).animate(
									{ marginLeft: p },
									{ queue: false, duration: speed, complete: adjust }
								);
							} else {
								p = (t * h * -1);
								$("ul", obj).animate(
									{ marginTop: p },
									{ queue: false, duration: speed, complete: adjust }
								);
							};


							if (!options.continuous && options.controlsFade) {
								if (t == ts) {
									$("a", "#" + options.nextId).hide();
									$("a", "#" + options.lastId).hide();
								} else {
									$("a", "#" + options.nextId).show();
									$("a", "#" + options.lastId).show();
								};
								if (t == 0) {
									$("a", "#" + options.prevId).hide();
									$("a", "#" + options.firstId).hide();
								} else {
									$("a", "#" + options.prevId).show();
									$("a", "#" + options.firstId).show();
								};
							};
							if (clicked) clearTimeout(timeout);
							if (options.auto && dir == "next" && !clicked) {
								;
								timeout = setTimeout(function() {
									animate("next", false);
								}, diff * options.speed + options.pause);
							};
						}
						else if(options.slidingMode == "Div")
						{
							var oldPosition = currentDivPosition;
							var positionTemp = 0;
							switch (dir) {
								case "next":
									currentDivPosition = (currentDivPosition >= maxDivPosition) ?  maxDivPosition : currentDivPosition + options.divSlidingSpeed;
									break;
								case "prev":
									currentDivPosition = (currentDivPosition <= 0) ? 0 : currentDivPosition - options.divSlidingSpeed;
									break;
								case "first":
									currentDivPosition = 0;
									break;
								case "last":
									currentDivPosition = maxDivPosition;
									break;
								default:
									currentDivPosition = dir;
									break;
							};
							var diff = Math.abs(oldPosition - currentDivPosition);
							var speed = diff * options.speed / 10000;
							if (!options.vertical) {
								p = (currentDivPosition * -1);
								$("ul", obj).animate(
									{ marginLeft: p },
									{ queue: false, duration: speed, complete: adjust }
								);
							} else {
								p = (t * h * -1);
								$("ul", obj).animate(
									{ marginTop: p },
									{ queue: false, duration: speed, complete: adjust }
								);
							};

							if (options.controlsFade) {
								//alert(Math.abs(currentDivPosition) + obj.width() + " " + maxDivPosition)
								if ((Math.abs(currentDivPosition) + obj.width()) >= maxDivPosition) {
									$("a", "#" + options.nextId).hide();
									$("a", "#" + options.lastId).hide();
									clearTimeout(timeout);
									options.auto = false;
								} else {
									$("a", "#" + options.nextId).show();
									$("a", "#" + options.lastId).show();
								};
								if (currentDivPosition == 0) {
									$("a", "#" + options.prevId).hide();
									$("a", "#" + options.firstId).hide();
									clearTimeout(timeout);
									options.auto = false;
								} else {
									$("a", "#" + options.prevId).show();
									$("a", "#" + options.firstId).show();
								};
							};
	                    	
                    		if (options.auto) {
								timeout = setTimeout(function() {
									animate(dir, false);
								}, 25);
							};

						}
						
						if(options.slidingMode == "Div" && options.auto)
						{
							timeout = setTimeout(function() {
								animate(dir, false);
							}, 25);
						}
					}
					else if(options.animationMode == "Fade")
					{
						var ot = t;
						switch (dir) {
							case "next":
								t = (ot >= ts) ? (options.continuous ? t + 1 : ts) : t + 1;
								break;
							case "prev":
								t = (t <= 0) ? (options.continuous ? t - 1 : 0) : t - 1;
								break;
							case "first":
								t = 0;
								break;
							case "last":
								t = ts;
								break;
							default:
								t = dir;
								break;
						};
						
						if(options.numbericShowPrevBttn)
						{
							$(obj).parent().find("#" + options.numericId + "_prevbuttn a").attr("rel", t == 0? ts : (parseInt(t) - 1) )
						}
						
						if(options.numbericShowNextBttn)
						{
							$(obj).parent().find("#" + options.numericId + "_nextbuttn a").attr("rel", t == ts? 0 : (parseInt(t) + 1) )
						}
						
						var diff = Math.abs(ot - t);
						var speed = diff * options.speed;
						//alert(ot + " " + t)
						if(t > ts) t = 0
						$($("li", obj)[ot]).fadeOut(500)
						$($("li", obj)[t]).fadeIn(500)
						adjust()
						
						if (clicked) clearTimeout(timeout);
						if (options.auto && dir == "next" && !clicked) {
							;
							timeout = setTimeout(function() {
								animate("next", false);
							}, diff * options.speed + options.pause);
						};					
					}
                };

            };
            // init
            var timeout;
            
            if (options.auto) {
                ;
                timeout = setTimeout(function() {
                    animate("next", false);
                }, options.pause);
            };

            if (options.numeric) setCurrent(0);

            if (!options.continuous && options.controlsFade) {
                $("a", "#" + options.prevId).hide();
                $("a", "#" + options.firstId).hide();
            };
            if (options.slidingMode == "Page" && ts == 0) {
                $("a", "#" + options.nextId).hide();
                $("a", "#" + options.lastId).hide();
            }

			if(options.slidingMode == "Div" && $.browser.msie && ($.browser.version=="6.0" || $.browser.version=="7.0"))
			{
				var w = 0;
				$("li", obj).each(function(){
					w += $(this).width();
				});
				$("ul", obj).css('width', w);
            
				maxDivPosition = $("ul", obj).width();
			}
			
        });

    };

})(jQuery);




