/*
 * 	Easy Slider 1.9 - jQuery plugin
 *	1.7 written by Alen Grakalic 
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *	1.8 modified by Kyle Florence
 *	1.9 modified by Kevin McDonald
 *	
 *	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
 *
 *
 *	Autosize and Autosize Animation Options added 03/02/2011
 *
 *
 *
 */

(function($)
{
	$.fn.easySlider = function(options)
	{
		// default configuration properties
		var defaults = {
			prevNext:		true,
			prevId: 		'previous_slide',
			prevText: 		'previous',
			prevImage:		'',
			nextId: 		'next_slide',
			nextText: 		'next',
			nextImage:		'',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',
			controlsFade:	false,
			insertAfter:	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:		'numberControls',
			ease:			'swing',
			autoresize:		false, // glue - additional option for re-sizing height of content container
			scrollAll:		false, // glue - if numeric do you want to scroll through all intermitent slides to get to next one (if more than 1 slide between start point and chosen slide)
			paddingInPod:   true
		};
		
        var options = $.extend(defaults, options);

        this.each(function() {
			var obj = $(this);

            // Fix for nested list items
            var ul = obj.children("ul");
            var li = ul.children("li");

            var s = li.length;
            var w = obj.width();
            var h = obj.height();

            var t = 0;
            var ts = s-1;
            var clickable = true;

			var currentT = 0;

            // Set obj overflow to hidden
            obj.css("overflow","hidden");

            // Set width/height of list items based on width/height of obj
            li.each(function() {
               if(options.vertical) $(this).height(h);
                else $(this).width(w);
            });

			// Sets a padding-left on the slider to sit inside a pod box
			// glue - needed to remove as takes precedence over css file styles
			// Webteam - have put an if statement around it to allow existing sliders to work
			if(options.paddingInPod){
            ul.css('padding-left','10px');
            }
			
            // Float items to the left
            li.css('float', 'left');

            // Set width/height of ul
			// glue - unsure of logic here, seems to be miss calculating, surely
			// ul.height(s*h) for vertical?
            if(options.vertical) ul.height(s*w);
            else ul.width(s*h);

			// glue - if auto resize true then resize the container onload
			// to ensure it's the right height to start
			if(options.autoresize) {
				autoresize(obj, options.speed)
			}

            // Clone elements for continuous scrolling
            if(options.continuous)
            {
                if(options.vertical)
                {
                    ul.prepend(li.filter(":last-child").clone().css("margin-top","-"+ h +"px"));
                    ul.append(li.filter(":nth-child(2)").clone());
                    ul.height((s+1)*h);
                } else {
                    ul.prepend(li.filter(":last-child").clone().css("margin-left","-"+ w +"px"));
                    ul.append(li.filter(":nth-child(2)").clone());
                    ul.width((s+1)*w);
                }
            };

            if(options.controlsShow){
                var html = options.controlsBefore;
				if(options.numeric){
					html += '<div class="numberControlsHolder clearfix">';
					if(options.prevNext && options.firstShow && options.lastShow){
						html += '<span class="'+ options.firstId +'"><a href="#">'+ options.firstText +'</a></span>';
						html += '<span class="'+ options.prevId +'"><a href="#">'+ options.prevText +'</a></span>';
						html += '<ol class="'+ options.numericId +'"></ol>';
						html += '<span class="'+ options.nextId +'"><a href="#">'+ options.nextText +'</a></span>';
						html += '<span class="'+ options.lastId +'"><a href="#">'+ options.lastText +'</a></span>';
						}
					else if (options.prevNext){
						html += '<span class="'+ options.prevId +'"><a href="#">'+ options.prevText +'</a></span>';
						html += '<ol class="'+ options.numericId +'"></ol>';
						html += '<span class="'+ options.nextId +'"><a href="#">'+ options.nextText +'</a></span>';
					}
					html += '</div>';
					// glue - commented out, due to style required by slider
					//html += '<br class="cb />';
                }
				
                if(options.firstShow && !options.numeric) {
                    html += '<span class="'+ options.firstId +'"><a href="#">'+ options.firstText +'</a></span>';
                }
                if(options.prevNext && !options.numeric){
					html += '<div class="controlHolder">';
                    if (options.prevImage == '' && options.nextImage == ''){
						html += '<span class="'+ options.prevId +'"><a href="#">'+ options.prevText +'</a></span>';
						html += '<span class="'+ options.nextId +'"><a href="#">'+ options.nextText +'</a></span>';
						}
					else{
				html += '<span class="'+ options.prevId +'"><a href="#">'+'<img src="' + options.prevImage + '" alt="Previous" />' +'</a></span>';
				html += '<span class="'+ options.nextId +'"><a href="#">'+'<img src="' + options.nextImage + '" alt="Next" />' +'</a></span>';
					}
					html += '</div>';
					html += '<br class="cb" />';
                }
                if(options.lastShow && !options.numeric) {
                    html += '<span class="'+ options.lastId +'"><a href="#">'+ options.lastText +'</a></span>';
                }
                html += options.controlsAfter;

                if (options.insertAfter) $(obj).after(html);
                else $(obj).before(html);
            };

            if(options.numeric){
                for(var i=0;i<s;i++){
                    $(document.createElement("li"))
                        .attr('class',options.numericId + (i+1))
                        .html('<a rel="'+ i +'" href="#"><span>'+ (i+1) +'</span></a>')
                        .appendTo($("."+ options.numericId))
                        .click(function(){
                            animate($("a",$(this)).attr('rel'),true);
                            return false;
                        });
                };
            }

            if(options.prevNext)
            {
                $("a","."+options.nextId).click(function(){
                    animate("next",true); return false;
                });
                $("a","."+options.prevId).click(function(){
                    animate("prev",true); return false;
                });
                $("a","."+options.firstId).click(function(){
                    animate("first",true); return false;
                });
                $("a","."+options.lastId).click(function(){
                    animate("last",true); return false;
                });
            };

            function setCurrent(i)
            {
                i = parseInt(i)+1;
                $("li", "." + options.numericId).removeClass("current");
                $("li." + options.numericId + i).addClass("current");
            };

            function adjust()
            {
                if(t>ts) t=0;
                if(t<0) t=ts;
                if(!options.vertical) {
                    ul.css("margin-left",(t*w*-1));
                } else {
                    ul.css("margin-top",(t*h*-1));
                }
                clickable = true;
                if(options.numeric) setCurrent(t);
            };

			//glue - re-size height of content container if required
			function autoresize(obj, speed) {
				obj.animate(
					{ height: Math.floor(parseInt($(obj.children('ul').children('li').get(t)).outerHeight()))},
						{
							queue:false,
							duration:speed
						});
			};

            function animate(dir,clicked)
            {
                if (clickable)
                {
					clickable = false;
                    var ot = t;
					currentT = 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 = parseInt(dir);
                            break;
                    };

                    var diff = Math.abs(ot-t);
                    var speed = diff*options.speed;

					if(!options.vertical) {

						p = (t*w*-1);

						// glue - if the slide being navigated to is further than 1 slide away just
						// jump to it (rather than animating through all the others to get tot it)
						if (  (((t - currentT) > 1) || ((t - currentT) < -1)) && options.numeric && !options.scrollAll     ) {
							ul.css('marginLeft',p);
							adjust();
							if(options.autoresize) {
								autoresize(obj, options.speed);
							}
						}
						else {
							ul.animate(
								{ marginLeft: p },
								{
									queue:false,
									duration:speed,
									easing:options.ease,
									// glue - call autoresize if required
									complete: function() {
										if(options.autoresize) {
											autoresize(obj, options.speed);
										}
										adjust();
									}
								}
							);
						}





                    } else {
                        p = (t*h*-1);
						// glue - if the slide being navigated to is further than 1 slide away just
						// jump to it (rather than animating through all the others to get tot it)
						if (  (((t - currentT) > 1) || ((t - currentT) < -1)) && options.numeric && !options.scrollAll     ) {
							ul.css('marginTop',p);
							adjust();
							if(options.autoresize) {
								autoresize(obj, options.speed);
							}
						}
						else {
							ul.animate(
								{ marginTop: p },
								{
									queue:false,
									duration:speed,
									easing:options.ease,
									complete:adjust
								}
							);
						}
                    };

                    if(!options.continuous && options.controlsFade)
                    {
                        if(t==0){
                            $("a","."+options.prevId).fadeOut('slow');
                            $("a","."+options.firstId).fadeOut('slow');
                        } else if(t==ts){
                            $("a","."+options.nextId).fadeOut('slow');
                            $("a","."+options.lastId).fadeOut('slow');
                        } else {
                            $("a","."+options.prevId).fadeIn('slow');
                            $("a","."+options.firstId).fadeIn('slow');
                            $("a","."+options.nextId).fadeIn('slow');
                            $("a","."+options.lastId).fadeIn('slow');
                        };
                    };

                    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();
            };

        });

    };
})(jQuery);
