/*
Supersized - Fullscreen Slideshow jQuery Plugin
Version 3.0
By Sam Dunn (www.buildinternet.com // www.onemightyroar.com)
Version: supersized.3.0.js
Website: www.buildinternet.com/project/supersized
*/

(function($){
	var curr = 0;
	
	//Resize image on ready or resize
	$.fn.supersized = function() {
		
		
		$.inAnimation = false;
		$.paused = false;
		
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
		$.currentSlide = options.start_slide - 1;
				
		/******Set up initial images -- Add class doesnt work*****/
		//Set previous image
		var imageLink = (options.slides[options.slides.length - 1].url) ? "href='" + options.slides[options.slides.length - 1].url + "'" : "";
		$("<img/>").attr("src", options.slides[options.slides.length - 1].image).appendTo("#supersized").wrap("<a " + imageLink + "></a>");//Doesnt account for start slide
		
		//Set current image
		imageLink = (options.slides[$.currentSlide].url) ? "href='" + options.slides[$.currentSlide].url + "'" : "";
		$("<img/>").attr("src", options.slides[$.currentSlide].image).appendTo("#supersized").wrap("<a class=\"activeslide\" " + imageLink + "></a>");
		
		//Set next image
		imageLink = (options.slides[$.currentSlide + 1].url) ? "href='" + options.slides[$.currentSlide + 1].url + "'" : "";
		$("<img/>").attr("src", options.slides[$.currentSlide + 1].image).appendTo("#supersized").wrap("<a " + imageLink + "></a>");
		
		if (options.thumbnail_navigation == 1){
		
			/*****Set up thumbnails****/
			//Load previous thumbnail
			$.currentSlide - 1 < 0  ? prevThumb = options.slides.length - 1 : prevThumb = $.currentSlide - 1;
			$('#prevthumb').html($("<img/>").attr("src", options.slides[prevThumb].image));
			
			//Load next thumbnail
			$.currentSlide == options.slides.length - 1 ? nextThumb = 0 : nextThumb = $.currentSlide + 1;
			$('#nextthumb').html($("<img/>").attr("src", options.slides[nextThumb].image));
		
		}
		
		
		
		
		
		$(window).bind("load", function(){
			thumb_hilite($.currentSlide);

			$('#loading').hide();
			$('#supersized').fadeIn('fast');
			
			$('#controls-wrapper').show();
			
			$('#supersized').resizenow();
			
			if (options.slide_captions == 1) $('#slidecaption').html(options.slides[$.currentSlide].title);//*********Pull caption from array
			if (options.navigation == 0) $('#navigation').hide();
			if (options.thumbnail_navigation == 0){ $('#nextthumb').hide(); $('#prevthumb').hide(); }

			
			//Slideshow
			if (options.slideshow == 1){
				if (options.slide_counter == 1){ //Initiate slide counter if active
					$('#slidecounter .slidenumber').html(options.start_slide);
	    			$('#slidecounter .totalslides').html(options.slides.length); //*******Pull total from length of array
	    		}
				slideshow_interval = setInterval(nextslide2, options.slide_interval);
				
				
				//************** Thumbnail Click handling 
				$('.thumb').click(function() {
					//if($.inAnimation) return false;
					clearInterval(slideshow_interval);
					$('#pauseplay').attr("src", "images/ss-play.png"); 
					ti = $(this).attr('rel');
					jump_to_pic(ti);
					//if(!($.paused)) slideshow_interval = setInterval(nextslide, options.slide_interval);
					return false;
				});
				
				
				//stop slideshow if moving carousel
				$('#caru-back,#caru-fwd').bind('click.ss',function() {
				    clearInterval(slideshow_interval);
					$('#pauseplay').attr("src", "images/ss-play.png"); 
				});
				
				
				thumb_prep();
				//**************************************************************







				
				
				if (options.thumbnail_navigation == 1){
					//Thumbnail Navigation
				/*
					$('#nextthumb').click(function() {
				    	if($.inAnimation) return false;
					    clearInterval(slideshow_interval);
					    nextslide();
					    if(!($.paused)) slideshow_interval = setInterval(nextslide, options.slide_interval);
					    return false;
				    });
				    $('#prevthumb').click(function() {
				    	if($.inAnimation) return false;
				        clearInterval(slideshow_interval);
				        prevslide();
				       	if(!($.paused)) slideshow_interval = setInterval(nextslide, options.slide_interval);
				        return false;
				    });
					*/


				}
				
				
				if (options.navigation == 1){ //Skip if no navigation
					$('#navigation a').click(function(){  
   						$(this).blur();  
   						return false;  
   					});
   					 	
					//Slide Navigation
				    $('#nextslide').click(function() {
				    	if($.inAnimation) return false;
					    clearInterval(slideshow_interval);
						$('#pauseplay').attr("src", "images/ss-play.png"); 
					    nextslide(0);
					    //if(!($.paused)) slideshow_interval = setInterval(nextslide, options.slide_interval);
					    return false;
				    });
				    $('#prevslide').click(function() {
				    	if($.inAnimation) return false;
				        clearInterval(slideshow_interval);
				        prevslide(0);
						$('#pauseplay').attr("src", "images/ss-play.png"); 
				        //if(!($.paused)) slideshow_interval = setInterval(nextslide, options.slide_interval);
				        return false;
				    });
				   
				   
				   
				   
				   
				  
				    //Play/Pause Button
				    $('#pauseplay').click(function() {
				    	//if($.inAnimation) return false;
				    	var src = ($(this).attr("src") === "images/ss-play.png") ? "images/ss-pause.png" : "images/ss-play.png";
      					if (src == "images/ss-pause.png"){
      						$(this).attr("src", "images/ss-play.png");
      						$.paused = false;
							nextslide(1);
					        slideshow_interval = setInterval(nextslide2, options.slide_interval);  
				        }else{
				        	$(this).attr("src", "images/ss-pause.png");
				        	clearInterval(slideshow_interval);
				        	$.paused = true;
				        }
      					$(this).attr("src", src);
					    return false;
				    });
				
					
					
				


				function css_opac_hiCB(){
					$(this).css({ opacity: 1});
				}

				function css_opac_low(e){
					e.css({ opacity: 0.6});
				}

				function css_opac_lowCB(){
					css_opac_low($(this));
				}

		
				css_opac_low($('#pauseplay'));
				css_opac_low($('#nextslide'));
				css_opac_low($('#prevslide'));
				css_opac_low($('#caru-fwd'));
				css_opac_low($('#caru-back'));

				$('#pauseplay,#nextslide,#prevslide,#caru-fwd,#caru-back').hover(css_opac_hiCB, css_opac_lowCB);
				
				
				/*
				 $('#pauseplay').hover(css_opac_hiCB, css_opac_lowCB);
				 $('#nextslide').hover(css_opac_hiCB, css_opac_lowCB);
				$('#prevslide').hover(css_opac_hiCB, css_opac_lowCB);
				$('#caru-fwd').hover(css_opac_hiCB, css_opac_lowCB);
				$('#caru-back').hover(css_opac_hiCB, css_opac_lowCB);
				*/
					
				}
			}
		});
				
	
				
				
			
		$(document).ready(function() {
			//$('#supersized').resizenow(); 
		});
		
		//keyboard nav
		$(document.documentElement).keyup(function (event) {
			// handle cursor keys
			if (event.which == 37) {
			  $('#prevslide').click();
			} else if (event.keyCode == 39) {
			  $('#nextslide').click();
			} else if (event.keyCode == 32) {
			  $('#pauseplay').click();
			}
		});
		
		
		/*
		$(window).bind("resize", function(){
    		$('#supersized').resizenow(); 
		});
		*/
		
		$('#supersized').hide();
		//$('#controls-wrapper').hide();
	};
	
	//Adjust image size
	$.fn.resizenow = function() {
		
		var t = $(this);
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
	  	return t.each(function() {
	  		
			//Define image ratio
			var ratio = options.startheight/options.startwidth;
			
			
			//Gather browser and current image size
			var imagewidth = t.width();
			var imageheight = t.height();
			var browserwidth = $(window).width();
			var browserheight = $(window).height();
			var offset;
	
			var yyy = t.children().height();
			var xxx = t.children().width();
	
//			var ratio = imageheight/imagewidth;
//console.log('ratio: '+ratio);
/*console.log('imagewidth: '+yyy);*/
/*console.log('imageheight: '+xxx);*/

			if(options.fullscreen){
				//Resize image to proper ratio
				if ((browserheight/browserwidth) > ratio){
					t.height(browserheight);
					t.width(browserheight / ratio);
					t.children().height(browserheight);
					t.children().width(browserheight / ratio);
				} else {
					t.width(browserwidth);
					t.height(browserwidth * ratio);
					t.children().width(browserwidth);
					t.children().height(browserwidth * ratio);
				}
				if (options.vertical_center == 1){
					t.css('left', (browserwidth - t.width())/2);
					t.css('top', (browserheight - t.height())/2);
					//t.children().css('left', (browserwidth - t.width())/2);
					//t.children().css('top', (browserheight - t.height())/2);
				}
			} else {
				w = t.find('img').width();
				h = t.find('img').height();
				
				i = t.find('img');
				i.each(function(){
					j =i.attr('src');
					k = i.naturalWidth;
					//console.log(j +" : "+k);
					
				});
				
				
				/*console.log('w: '+w);
				console.log('h: '+h);
				*///t.width(imagewidth);
				//t.height(imageheight);
//					t.height(browserheight);
	//				t.width(browserheight / ratio);
var x = options.startwidth;
var y = options.startheight;

				t.width(x);
				t.height(y);

				t.children().width(x);
				t.children().height(y);

				
				t.center_both();
				//t.children().center_both();
				
			}
		
			return false;
		});
	};
	
	
	function nextslide2(){	//dummy function to avoid transition1 getting weird values -- only called from setInterval
		nextslide();
	}
	
	
	//Slideshow Next Slide
	function nextslide(transition1) {
		
		thumb_prep();
		/*if($.inAnimation) return false;
		else $.inAnimation = true;
		*/
	    var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
		
		var currentslide = $('#supersized .activeslide');
	    currentslide.removeClass('activeslide');
		
	    if ( currentslide.length == 0 ) currentslide = $('#supersized a:last'); //*******Check if end of array?
			
	    var nextslide =  currentslide.next().length ? currentslide.next() : $('#supersized a:first'); //*******Array
	    var prevslide =  nextslide.prev().length ? nextslide.prev() : $('#supersized a:last'); //*******Array
		
		$('.prevslide').removeClass('prevslide');
		prevslide.addClass('prevslide');
		
		//Get the slide number of new slide
		$.currentSlide + 1 == options.slides.length ? $.currentSlide = 0 : $.currentSlide++;
		
		/**** Image Loading ****/
		//Load next image
		loadSlide=false;
		$.currentSlide == options.slides.length - 1 ? loadSlide = 0 : loadSlide = $.currentSlide + 1;
		imageLink = (options.slides[loadSlide].url) ? "href='" + options.slides[loadSlide].url + "'" : "";
		$("<img/>").attr("src", options.slides[loadSlide].image).appendTo("#supersized").wrap("<a " + imageLink + "></a>");
		
		if (options.thumbnail_navigation == 1){
		//Load previous thumbnail
		$.currentSlide - 1 < 0  ? prevThumb = options.slides.length - 1 : prevThumb = $.currentSlide - 1;
		$('#prevthumb').html($("<img/>").attr("src", options.slides[prevThumb].image));
		
		//Load next thumbnail
		nextThumb = loadSlide;
		$('#nextthumb').html($("<img/>").attr("src", options.slides[nextThumb].image));
		}
		
		currentslide.prev().remove(); //Remove Old Image
		
		/**** End Image Loading ****/
		
		
		//hilite thumb
		thumb_hilite($.currentSlide);
		
		
	    nextslide.hide().addClass('activeslide')

		var transition2 = typeof transition1 === 'undefined' ? options.transition : transition1 ;
		if(transition2 > 1 || transition2 < 0){		//because for some retarded reason "transition1" is set to random values!!! wtf?!?!?
			transition2 = 1;
		}

		if (transition2 == 0){
			nextslide.show(); $.inAnimation = false;
		}
		if (transition2 == 1){
			nextslide.fadeIn(750, function(){$.inAnimation = false;});
		}
		
	    //$('#supersized').resizenow();
	}
	
	//Slideshow Previous Slide
	function prevslide(transition1) {
		
		
		thumb_prep();
		//if($.inAnimation) return false;
		//else $.inAnimation = true;
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
	    
	    var currentslide = $('#supersized .activeslide');
	    currentslide.removeClass('activeslide');
		
	    if ( currentslide.length == 0 ) currentslide = $('#supersized a:first');
			
	    var nextslide =  currentslide.prev().length ? currentslide.prev() : $('#supersized a:last'); //****** If equal to total length of array
	    var prevslide =  nextslide.next().length ? nextslide.next() : $('#supersized a:first');
				
		//Get current slide number
		$.currentSlide == 0 ?  $.currentSlide = options.slides.length - 1 : $.currentSlide-- ;
		
		/**** Image Loading ****/
		//Load next image
		loadSlide=false;
		$.currentSlide - 1 < 0  ? loadSlide = options.slides.length - 1 : loadSlide = $.currentSlide - 1;
		imageLink = (options.slides[loadSlide].url) ? "href='" + options.slides[loadSlide].url + "'" : "";
		$("<img/>").attr("src", options.slides[loadSlide].image).prependTo("#supersized").wrap("<a " + imageLink + "></a>");
		
		if (options.thumbnail_navigation == 1){
		//Load previous thumbnail
		prevThumb = loadSlide;
		$('#prevthumb').html($("<img/>").attr("src", options.slides[prevThumb].image));
		
		//Load next thumbnail
		$.currentSlide == options.slides.length - 1 ? nextThumb = 0 : nextThumb = $.currentSlide + 1;
		$('#nextthumb').html($("<img/>").attr("src", options.slides[nextThumb].image));
		}
		
		currentslide.next().remove(); //Remove Old Image
		
		/**** End Image Loading ****/
		
		//hilite thumb
		thumb_hilite($.currentSlide);
		
		//Display slide counter
		if (options.slide_counter == 1){
		    $('#slidecounter .slidenumber').html($.currentSlide + 1);
		}
		
		$('.prevslide').removeClass('prevslide');
		prevslide.addClass('prevslide');
		
		//Captions
	    if (options.slide_captions == 1){
	    	(options.slides[$.currentSlide].title) ? $('#slidecaption').html(options.slides[$.currentSlide].title) : $('#slidecaption').html('') ; //*******Grab next slide's title from array
	    }
		
	    nextslide.hide().addClass('activeslide')
		
			var transition2 = typeof transition1 === 'undefined' ? options.transition : transition1 ;
		if(transition2 > 1 || transition2 < 0){		//because for some retarded reason "transition1" is set to random values for no reason!!! wtf?!?!?
			transition2 = 1;
		}
	    	if (transition2 == 0){
	    		nextslide.show(); $.inAnimation = false;
	    	}
	    	if (transition2 == 1){
	    		nextslide.fadeIn(750, function(){$.inAnimation = false;});
	    	}
	    	
	    	//$('#supersized').resizenow();//Fix for resize mid-transition
	}
	
	//******************************************************** custom function for going to specific image
	
	function jump_to_pic(ti) {		// ti = thumb index
		ti = parseInt(ti);

		thumb_prep();
		
		//hilite thumb
		thumb_hilite(ti);
		
		if($.inAnimation) return false;
		else $.inAnimation = true;
	    var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
		
		var currentslide = $('#supersized .activeslide');
	    currentslide.removeClass('activeslide');
	    
		if ( currentslide.length == 0 ) currentslide = $('#supersized a:last'); //*******Check if end of array?
			
	    //var nextslide =  currentslide.next().length ? currentslide.next() : $('#supersized a:first'); //*******Array
	    //var prevslide =  nextslide.prev().length ? nextslide.prev() : $('#supersized a:last'); //*******Array
		
		//$('.prevslide').removeClass('prevslide');
		//prevslide.addClass('prevslide');
		
		//Get the slide number of new slide
		//$.currentSlide + 1 == options.slides.length ? $.currentSlide = 0 : $.currentSlide++;
		$.currentSlide = ti;
		
		/**** Image Loading ****/
		//Load next image
		currentslide.prev().remove(); //Remove Old Image
		currentslide.next().remove();
		loadSlide=false;
		//$.currentSlide == options.slides.length - 1 ? loadSlide = 0 : loadSlide = $.currentSlide + 1;
		loadSlide = $.currentSlide;
		imageLink = (options.slides[loadSlide].url) ? "href='" + options.slides[loadSlide].url + "'" : "";
		$("<img/>").attr("src", options.slides[loadSlide].image).appendTo("#supersized").wrap("<a " + imageLink + "></a>");
		
		
		$.currentSlide == options.slides.length - 1 ? loadSlide = 0 : loadSlide = $.currentSlide + 1;
		//loadSlide = $.currentSlide +1;
		//console.log('loadSlide: '+loadSlide);
		imageLink = (options.slides[loadSlide].url) ? "href='" + options.slides[loadSlide].url + "'" : "";
		$("<img/>").attr("src", options.slides[loadSlide].image).appendTo("#supersized").wrap("<a " + imageLink + "></a>");

		/*		
		if (options.thumbnail_navigation == 1){
		//Load previous thumbnail
		$.currentSlide - 1 < 0  ? prevThumb = options.slides.length - 1 : prevThumb = $.currentSlide - 1;
		$('#prevthumb').html($("<img/>").attr("src", options.slides[prevThumb].image));
		
		//Load next thumbnail
		nextThumb = loadSlide;
		$('#nextthumb').html($("<img/>").attr("src", options.slides[nextThumb].image));
		}
		*/
		
		
		
		/**** End Image Loading ****/
		var nextslide =  currentslide.next() ;
		
	    nextslide.hide().addClass('activeslide')
		nextslide.show(); $.inAnimation = false;	    	
			
			/*
			if (options.transition == 0){
	    		nextslide.show(); $.inAnimation = false;
	    	}
	    	if (options.transition == 1){
	    		nextslide.fadeIn(750, function(){$.inAnimation = false;});
	    	}
			*/
	    //$('#supersized').resizenow();
	}
	//********************************************************
	
	
	//hilite thumbnail
	function thumb_hilite(ti){
		var thu = $("#caru img[rel='"+ti+"']");
		thu.fadeTo(300, 1);
		thu.unbind('mouseenter mouseleave');
		
		
		//slide carousel to keep thumbnail in view
		var op = $.fn.jCarouselLite.options2;
		if(typeof op !== 'undefined') {
			viz = op.visible;
			if(ti < curr || ti > (curr+viz-1)){	//curr = 1st thumb currently visible
				var go = parseInt(ti/viz)*viz;
				$("#caru").caru_slideto(go);
			}
		}
	}
	
	//generic hilite functions
	function thumb_hilite_on(){
		$(this).stop().fadeTo(300, 1);
	}

	function thumb_hilite_off(){
		$(this).stop().fadeTo(500, 0.5);
	}

	//************** Thumbnail HOVER handling
	function thumb_prep(){
		$('#caru .thumb').css({ opacity: .5 });		
		$('#caru .thumb').hover(thumb_hilite_on, thumb_hilite_off);	
	}

	
	
	$.fn.supersized.defaults = { 
			startwidth: 4,  
			startheight: 3,
			vertical_center: 1,
			slideshow: 1,
			navigation:1,
			thumbnail_navigation: 0,
			transition: 1, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
			pause_hover: 0,
			slide_counter: 1,
			slide_captions: 1,
			slide_interval: 5000,
			start_slide: 1,
			fullscreen:1

	};
	
	
	
	
	
	
	
	
	
	
	//**********************************************************************************************
	//jCarouselLite
	//**********************************************************************************************
	
	
	
	
	

	
$.fn.jCarouselLite = function(o) {
    o = $.extend({
        btnPrev: null,
        btnNext: null,
        btnGo: null,
        mouseWheel: false,
        auto: null,

        speed: 200,
        easing: null,

        vertical: false,
        circular: true,
        visible: 3,
        start: 0,
        scroll: 1,

        beforeStart: null,
        afterEnd: null
    }, o || {});
	
	$.fn.jCarouselLite.options2 = o;
	
	
    return this.each(function() {                           // Returns the element collection. Chainable.

        var running = false, animCss=o.vertical?"top":"left", sizeCss=o.vertical?"height":"width";
        var div = $(this), ul = $("ul", div), tLi = $("li", ul), tl = tLi.size(), v = o.visible;

        if(o.circular) {
            ul.prepend(tLi.slice(tl-v-1+1).clone())
              .append(tLi.slice(0,v).clone());
            o.start += v;
        }

        var li = $("li", ul), itemLength = li.size();
		curr = o.start;
		div.css("visibility", "visible");

        li.css({overflow: "hidden", float: o.vertical ? "none" : "left"});
        ul.css({margin: "0", padding: "0", position: "relative", "list-style-type": "none", "z-index": "1"});
        div.css({overflow: "hidden", position: "relative", "z-index": "2", left: "0px"});

        var liSize = o.vertical ? height(li) : width(li);   // Full li size(incl margin)-Used for animation
        var ulSize = liSize * itemLength;                   // size of full ul(total length, not just for the visible items)
        var divSize = liSize * v;                           // size of entire div(total length for just the visible items)

        li.css({width: li.width(), height: li.height()});
        ul.css(sizeCss, ulSize+"px").css(animCss, -(curr*liSize));

        div.css(sizeCss, divSize+"px");                     // Width of the DIV. length of visible images

        if(o.btnPrev)
            $(o.btnPrev).click(function() {
				return go(curr-o.scroll);
            });

        if(o.btnNext)
            $(o.btnNext).click(function() {
				return go(curr+o.scroll);
            });

        if(o.btnGo)
            $.each(o.btnGo, function(i, val) {
                $(val).click(function() {
                    return go(o.circular ? o.visible+i : i);
                });
            });

        if(o.mouseWheel && div.mousewheel)
            div.mousewheel(function(e, d) {
                return d>0 ? go(curr-o.scroll) : go(curr+o.scroll);
            });

        if(o.auto)
            setInterval(function() {
                go(curr+o.scroll);
            }, o.auto+o.speed);

        function vis() {
            return li.slice(curr).slice(0,v);
        };

        function go(to) {
            if(!running) {

                if(o.beforeStart)
                    o.beforeStart.call(this, vis());

                if(o.circular) {            // If circular we are in first or last, then goto the other end
                    if(to<=o.start-v-1) {           // If first, then goto last
                        ul.css(animCss, -((itemLength-(v*2))*liSize)+"px");
                        // If "scroll" > 1, then the "to" might not be equal to the condition; it can be lesser depending on the number of elements.
                        curr = to==o.start-v-1 ? itemLength-(v*2)-1 : itemLength-(v*2)-o.scroll;
                    } else if(to>=itemLength-v+1) { // If last, then goto first
                        ul.css(animCss, -( (v) * liSize ) + "px" );
                        // If "scroll" > 1, then the "to" might not be equal to the condition; it can be greater depending on the number of elements.
                        curr = to==itemLength-v+1 ? v+1 : v+o.scroll;
                    } else curr = to;
                } else {                    // If non-circular and to points to first or last, we just return.
                    //if(to<0 || to>itemLength-v) return;
                    //else curr = to;
					if(to<0){ 
						curr = 0;
					} else if(to>itemLength-v) {
						curr = itemLength-v;
					} else curr = to;
					
                }                           // If neither overrides it, the curr will still be "to" and we can proceed.

                running = true;

                ul.animate(
                    animCss == "left" ? { left: -(curr*liSize) } : { top: -(curr*liSize) } , o.speed, o.easing,
                    function() {
                        if(o.afterEnd)
                            o.afterEnd.call(this, vis());
                        running = false;
                    }
                );
                // Disable buttons when the carousel reaches the last/first, and enable when not
                if(!o.circular) {
					$(o.btnPrev + "," + o.btnNext).css({'visibility':'visible'});
                    $( (curr == 0 && o.btnPrev)
                        ||
					   (curr+v+1 > itemLength && o.btnNext)
				         ||
                       []
                     
					 ).css({'visibility':'hidden'});
				}
            }
			//console.log('curr-go2: '+curr);
            return false;
        };
    });
};



$.fn.caru_slideto = function(to)  {
	var o = $.fn.jCarouselLite.options2;
	
	
		var running = false, animCss=o.vertical?"top":"left", sizeCss=o.vertical?"height":"width";
        var div = $(this), ul = $("ul", div), tLi = $("li", ul), tl = tLi.size(), v = o.visible;

        if(o.circular) {
            ul.prepend(tLi.slice(tl-v-1+1).clone())
              .append(tLi.slice(0,v).clone());
            o.start += v;
        }

        var li = $("li", ul), itemLength = li.size()//, curr = o.start;
        div.css("visibility", "visible");

        li.css({overflow: "hidden", float: o.vertical ? "none" : "left"});
        ul.css({margin: "0", padding: "0", position: "relative", "list-style-type": "none", "z-index": "1"});
        div.css({overflow: "hidden", position: "relative", "z-index": "2", left: "0px"});

        var liSize = o.vertical ? height(li) : width(li);   // Full li size(incl margin)-Used for animation
        var ulSize = liSize * itemLength;                   // size of full ul(total length, not just for the visible items)
        var divSize = liSize * v;                           // size of entire div(total length for just the visible items)

        li.css({width: li.width(), height: li.height()});
        ul.css(sizeCss, ulSize+"px").css(animCss, -(curr*liSize));

        div.css(sizeCss, divSize+"px");                     // Width of the DIV. length of visible images
	
	
		if(o.beforeStart)
			o.beforeStart.call(this, vis());

		if(o.circular) {            // If circular we are in first or last, then goto the other end
			if(to<=o.start-v-1) {           // If first, then goto last
				ul.css(animCss, -((itemLength-(v*2))*liSize)+"px");
				// If "scroll" > 1, then the "to" might not be equal to the condition; it can be lesser depending on the number of elements.
				curr = to==o.start-v-1 ? itemLength-(v*2)-1 : itemLength-(v*2)-o.scroll;
			} else if(to>=itemLength-v+1) { // If last, then goto first
				ul.css(animCss, -( (v) * liSize ) + "px" );
				// If "scroll" > 1, then the "to" might not be equal to the condition; it can be greater depending on the number of elements.
				curr = to==itemLength-v+1 ? v+1 : v+o.scroll;
			} else curr = to;
		} else {                    // If non-circular and to points to first or last, we just return.
				if(to<0){ 
					curr = 0;
				} else if(to>itemLength-v) {
					curr = itemLength-v;
				} else {	
					curr = to;
				}
					
		}                           // If neither overrides it, the curr will still be "to" and we can proceed.

		running = true;


		ul.animate(
			animCss == "left" ? { left: -(curr*liSize) } : { top: -(curr*liSize) } , o.speed, o.easing,
			function() {
				if(o.afterEnd)
					o.afterEnd.call(this, vis());
				running = false;
			}
		);
		// Disable buttons when the carousel reaches the last/first, and enable when not
		 if(!o.circular) {
					$(o.btnPrev + "," + o.btnNext).css({'visibility':'visible'});
                    $( (curr == 0 && o.btnPrev)
                        ||
					   (curr+v+1 > itemLength && o.btnNext)
				         ||
                       []
                     
					 ).css({'visibility':'hidden'});
		}
};













function css(el, prop) {
    return parseInt($.css(el[0], prop)) || 0;
};
function width(el) {
    return  el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
};
function height(el) {
    return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
};

	
	
	
	
	
	
	
	
	
	
	
	
	
})(jQuery);


