/*
Plugin Name: Juno Carousel / Slider
Plugin URI: http://junowebdesign.com
Version: 1.0
Author: Binh Nguyen (Juno)
Author URI: http://junowebdesign.com

 how to use
 
 <div id="carousel">
    <div class="prev"><span>Previous</span></div>
    <div class="viewport">
        <ul>
            <li></li>
        </ul>
    </div>
    <div class="next"><span>Next</span></div>
 </div>
 
 $('carousel').jcarousel();
 
 //auto slider
  $('carousel').jcarousel({
    interval: true
  });
  
//auto slider with infinite loop
$('carousel').jcarousel({
    interval: true,
    loop: true
  });
  
- next release with navigation!
*/

(function($){
    //jcarousel
    $.fn.jcarousel = function(options){
        var defaults = {
            nextBtn: '.button-next',
            prevBtn: '.button-back',
            animation: false,
            axis : 'x',
            scrollInt: 1,
            interval: false,
            intervaltime: 3000,
            duration: 1000,
            loop: false
        };
        var opts = $.extend(defaults, options);
        var slider = $(this);
        //var viewport = slider.find('ul').parent();
        var viewport = slider.find('.viewport');
        var em = slider.find('ul > li');
        var emSize, emOver, emStep, jCurrent, _timer, _forward, _pause, firstStart = true;
        var axis = opts.axis == "x";
        var jCurrent = 1;
        return this.each(function(){
            setup();
        });
        function setup(){
            emSize = axis ? (em.first().outerWidth(true)) : (em.first().outerHeight(true));
            emOver = Math.ceil(((axis ? viewport.outerWidth() : viewport.outerHeight()) / (emSize * opts.scrollInt)) -1);
	    emStep = Math.max(1, Math.ceil(em.length / opts.scrollInt) - emOver);
            jCurrent = Math.min(emStep, Math.max(1, 1)) -2;
            viewport.children('ul').css(axis ? 'width' : 'height', (emSize * em.length));
            jScroll(1);
            setEvents();
            log('emSize',this,emSize);
			log('emOver',this,emOver);
            log('emStep',this,emStep);
        }
        
        function setEvents(){
            $(opts.prevBtn).click(function(){
                if(opts.loop){
                    if(jCurrent ==  0){
                        jCurrent = emStep;
                    }
                }
                jScroll(-1); return false;                
            });
	    $(opts.nextBtn).click(function(){
               
                if(opts.loop){
                    if(jCurrent == em.length - 2){
                        jCurrent = -1;
                    }
                }
                jScroll(1); return false;
                
            });
            if(opts.interval){
                slider.hover(function(){clearTimeout(_timer); _pause = true},function(){_pause = false; setTimer();});
            }
        }
        
        
        function setTimer(){
            if(opts.interval && !_pause){
                clearTimeout(_timer);
                _timer = setTimeout(function(){
                    jCurrent = !opts.interval && (jCurrent +1 == emStep) ? -1 : jCurrent;
                    _forward = jCurrent +1 == emStep ? false : jCurrent == 0 ? true : _forward;
                    jScroll((opts.interval ? (_forward ? 1 : -1) : 1));
                    //log('interval',this,(opts.interval ? (_forward ? 1 : -1) : 1));
                }, opts.intervaltime);
            }
        }
        
        function jScroll(iDirection){
            if(jCurrent + iDirection > -1 && jCurrent + iDirection < emStep){
                jCurrent += iDirection;
                //log('jCurrent',this,jCurrent);
                var _opacity = opts.animation ? "0.5" : "1";                
                if(axis){
                    viewport.children('ul').animate({
                        left: -(jCurrent * (emSize * opts.scrollInt)),
                        opacity: firstStart ? _opacity : _opacity
                    }, {
                        queue: false,
                        duration: opts.duration,
                        complete: function() {
                            $(this).animate({
                                opacity: '1'
                            })
                        }
                    });
                }else{
                    viewport.children('ul').animate({
                        top: -(jCurrent * (emSize * opts.scrollInt)),
                        opacity: firstStart ? _opacity : _opacity
                    }, {
                        queue: false,
                        duration: opts.duration,
                        complete: function() {
                            $(this).animate({
                                opacity: '1'
                            })
                        }
                    });
                }
                firstStart = false;
                setTimer();
            }            
        }
    }    
})(jQuery);

window.log = function(){
  log.history = log.history || [];
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};

// catch all document.write() calls
(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);


