/*
 * jQuery Cycle Plugin for light-weight slideshows
 * Examples and documentation at: http://malsup.com/jquery/cycle/
 * Copyright (c) 2007 M. Alsup
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * @author:   M. Alsup
 * @version:  1.6 (8/14/2007)
 * @requires: jQuery v1.1.3.1 or later
 *
 * Based on the work of:
 *  1) Matt Oakes (http://portfolio.gizone.co.uk/applications/slideshow/)
 *  2) Torsten Baldes (http://medienfreunde.com/lab/innerfade/)
 *  3) Benjamin Sterling (http://www.benjaminsterling.com/experiments/jqShuffle/)
 */
(function($) {

$.fn.cycle = function(options) {
    return this.each(function() {
        if (options && options.constructor == String) {
            if (options == 'stop') {
                if (this.cycleTimeout) clearTimeout(this.cycleTimeout);
                this.cycleTimeout = 0;
                return;
            }
            options = { fx: options };
        }
        
        var $cont = $(this), $slides = $cont.children(), els = $slides.get();
        donum("1",els.length);
        if (els.length < 2) {
			$('div.header').css('display','none');	
		}

        var opts = $.extend({}, $.fn.cycle.defaults, options || {}, $.meta ? $cont.data() : {});
        if (opts.autostop) 
            opts.countdown = els.length;
            
        opts.before = opts.before ? [opts.before] : [];
        opts.after = opts.after ? [opts.after] : [];

        // allow shorthand overrides of width, height and timeout
        var cls = this.className;
        var w = parseInt((cls.match(/w:(\d+)/)||[])[1]) || opts.width;
        var h = parseInt((cls.match(/h:(\d+)/)||[])[1]) || opts.height;
        opts.timeout = parseInt((cls.match(/t:(\d+)/)||[])[1]) || opts.timeout;

        if ($cont.css('position') == 'static') 
            $cont.css('position', 'relative');
        if (w) 
            $cont.width(w);
        if (h && h != 'auto') 
            $cont.height(h);

        $slides.each(function(i){$(this).css('z-index', els.length-i);}).css('position','absolute').css('display', 'none');
        $(els[0]).css('display', 'block');
        if (opts.fit && w) 
            $slides.width(w);
        if (opts.fit && h && h != 'auto') 
            $slides.height(h);
        if (opts.pause) 
            $cont.hover(function(){opts.paused=1;}, function(){opts.paused=0;});
        
        // run transition init fn
        var init = $.fn.cycle.transitions[opts.fx];
        if ($.isFunction(init))
            init($cont, $slides, opts);
        if (opts.cssBefore)
            $slides.not(':eq(0)').css(opts.cssBefore);

        opts.cssBefore = opts.cssBefore || {};
        opts.animIn = opts.animIn || {};
        opts.animOut = opts.animOut || {};

        opts.nextSlide = opts.random ? (Math.floor(Math.random() * (els.length-1)))+1 : 1;
        opts.currSlide = 0;
        
        if (opts.click && !opts.next)
            opts.next = opts.click;
        if (opts.next)
            $(opts.next).bind('click', function(){return advance(els, opts,1)});
        if (opts.prev)
            $(opts.prev).bind('click', function(){return advance(els, opts,-1)});
    });
};

function go(els, opts, manual) {
    var p = els[0].parentNode, curr = els[opts.currSlide], next = els[opts.nextSlide];
    //alert(els[0].parentNode);
	if (p.cycleTimeout === 0 && !manual) 
        return;
        
    if (opts.before.length)
        $.each(opts.before, function(i,o) { o.apply(next, [curr, next, opts]); });
    var after = (opts.after.length == 0) ? null : function() {
        $.each(opts.after, function(i,o) { o.apply(next, [curr, next, opts]); });
    };

    if (manual || !opts.paused) {
        if (opts.autostop && (--opts.countdown == 0)) return;
        if (opts.nextSlide != opts.currSlide) {
            if (opts.fxFn)
                opts.fxFn(curr, next, opts, after);
            else if ($.isFunction($.fn.cycle[opts.fx]))
                $.fn.cycle[opts.fx](curr, next, opts, after);
            else
                $.fn.cycle.custom(curr, next, opts, after);
        }

			// sequence
            var roll = (opts.nextSlide + 1) == els.length;
            opts.nextSlide = roll ? 0 : opts.nextSlide+1;
			if (opts.nextSlide == 0) {
				$('#nextlink').css("color","red").css('display','none');
				$('#nextnum').css("color","red").css('display','none');
				$('#nextnumnolink').html($('#nextnum').html());
				$(opts.next).unbind('click', function(){return false});
				$(opts.next).bind('click', function(){return false});
			}
			else {
				$('#nextlink').css('display','inline');
				$('#nextnum').css('display','inline');
				$('#nextnumnolink').css("color","white");
				$('#nextnumnolink').remove();
			}
            opts.currSlide = roll ? els.length-1 : opts.nextSlide-1;
		//alert(opts.pager);
    }
};

// advance slide forward or back
function advance(els, opts, val) {

    var p = els[0].parentNode, timeout = p.cycleTimeout;
	
    opts.nextSlide = opts.currSlide + val;
	//alert(opts.nextSlide);
 
    if (opts.nextSlide < 0) {
        //opts.nextSlide = els.length - 1;
		$(opts.prev).unbind('click');
		jQuery(document).trigger('close.facebox');
		return false;
	}
    else if (opts.nextSlide >= els.length) {
        opts.nextSlide = 0;
		$(opts.next).unbind('click');
		jQuery(document).trigger('close.facebox');
		return false;
	}
        
    donum(opts.nextSlide + 1, els.length);
    
    go(els, opts, 1);
    return false;
};

function donum(next,prev)
{
	$('#num').html(" / <span id='nextnumnolink'></span>");
	$('#prev').html("< " + zeroPad(next,2));
	$('#next').html('<span id="nextnum">' + zeroPad(prev,2) + '</span><span id="nextlink">' + " >" + '</span>');
};

function buildPager(els, opts) {
    var $p = $(opts.pager);
    $.each(els, function(i,o) {
        var $a = $('<a href="#">'+(i+1)+'</a>').appendTo($p).bind('click',function() {
            opts.nextSlide = i;
            go(els, opts, 1);
            return false;
        });
        if (i == 0) 
            $a.addClass('activeSlide');
    });
};

function zeroPad(num,count)
{
	var numZeropad = num + '';
	while(numZeropad.length < count) {
		numZeropad = "0" + numZeropad;
	}
	return numZeropad;
}

$.fn.cycle.custom = function(curr, next, opts, cb) {
    var $l = $(curr);
	$l.css('display', 'none');
    var fn = function() {$(next).css(opts.cssBefore);};
    $l.animate(opts.animOut, "0", opts.easeOut, function() {
        if (opts.cssAfter) $l.css(opts.cssAfter);
    });
    fn();
};

$.fn.cycle.version = '1.6';

$.fn.cycle.transitions = {
    fade: function($cont, $slides, opts) {
		opts.after.push(function() { $(this).css('display', 'none'); }),
        opts.before.push(function() { $(this).css('display', 'block'); }),
        //opts.animIn    = { display: 'block' };
        opts.animOut   = { display: 'none' };
        opts.cssAfter  = { display: 'none' }
    }
    
};

// override these globally if you like (they are all optional)
$.fn.cycle.defaults = {
    fx:         'fade', // one of: fade, shuffle, zoom, slideX, slideY, scrollUp/Down/Left/Right
    timeout:     4000,  // milliseconds between slide transitions (0 to disable auto advance)
    speed:       10,  // speed of the transition (any valid fx speed value)
    speedIn:     10,  // speed of the 'in' transition
    speedOut:    10,  // speed of the 'out' transition
    click:       null,  // @deprecated; please use the 'next' option
    next:        null,  // id of element to use as click trigger for next slide
    prev:        null,  // id of element to use as click trigger for previous slide
    pager:       null,  // id of element to use as pager container
    before:      null,  // transition callback (scope set to element to be shown)
    after:       null,  // transition callback (scope set to element that was shown)
    easing:      null,  // easing method for both in and out transitions
    easeIn:      null,  // easing for "in" transition
    easeOut:     null,  // easing for "out" transition
    shuffle:     null,  // coords for shuffle animation, ex: { top:15, left: 200 }
    animIn:      null,  // properties that define how the slide animates in
    animOut:     null,  // properties that define how the slide animates out
    cssBefore:   null,  // properties that define the initial state of the slide before transitioning in
    cssAfter:    null,  // properties that defined the state of the slide after transitioning out
    fxFn:        null,  // function used to control the transition
    height:     'auto', // container height
    sync:        0,     // true if in/out transitions should occur simultaneously
    random:      0,     // true for random, false for sequence (not applicable to shuffle fx)
    fit:         0,     // force slides to fit container
    pause:       0,     // true to enable "pause on hover"
    autostop:    0,     // true to end slideshow after X transitions (where X == slide count)
    delay:       0      // additional delay (in ms) for first transition (hint: can be negative)
};

})(jQuery);


    
