/*
 * This Document is Two Part: Jquery Easing Extension, then the JS Banner Plugin.
 * This is so that the easing extension doesn't have to be included in the html
 *
*/

/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * t: current time, b: begInnIng value, c: change In value, d: duration
*/
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; s=p/4; }
		else s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; s=p/4; }
		else s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; s=p/4; }
		else s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});

/*-------------------------------------------------
 * JS Banner
 * Author: Matt Fiocca - www.golden-tech.com
 * Version:1.0
 */

(function(jQuery){

    jQuery.js_banner = function(prefs)
    {
        // overridable preferences
        var config = {
            container: '.js_banner',
            controller: '.js_banner_controller',
			dots: '.js_dots',
            leftArrow: '.js_banner_arrow_l',
            rightArrow: '.js_banner_arrow_r',
            pause: '.js_banner_pause',
            pausePlayText:'<div class="play_button">&rsaquo;</div>',
            pausePauseText:'| |',
            onPause: function(button){},
            onPlay: function(button){},
            effect: 'slide',
            direction: 'x',
            onTrigger: function(item){},
            easing: 'easeOutQuad',
            useDots:false,
            dotColor:'#ffffff',
            speed: 1,
            timer: 0,
            active: 0,
            liTop:0,
            liLeft:0,
            fadeTriggers:true
        };

        var item_prefix = 'js_banner_item_';
        var effects = ['slide','fade'];
        var current_item = 0;
        var timer_object = 0;
        var item_count = 0;
        var item_padding_top = 0;
        var item_padding_right = 0;
        var item_padding_bottom = 0;
        var item_padding_left = 0;
        var item_width = 0;
        var item_height = 0;
        var overwrite = 0;
        var banner_paused = 0;
        var started = 0;

        // override the configs with the params

        if(typeof prefs == 'object')
        {
            for(var pref_key in prefs)
            {
                for(var config_key in config)
                {
                    if(pref_key == config_key)
                    {
                        // preprocessing on preferences

                        overwrite = 0;

                        if(pref_key=='effect')
                        {
                            if(in_array(prefs[pref_key], effects))
                            {
                                overwrite = 1;
                            }
                        }else{
                            overwrite = 1;
                        }

                        if(overwrite == 1)
                        {
                            config[config_key] = prefs[pref_key];
                        }
                    }
                }
            }
        }

        // Convert seconds to milliseconds for the effects

        config.speed = (config.speed * 1000);
        config.timer = (config.timer * 1000);

        // Get item count

        item_count = jQuery('ul' + config.container + ' li').size();

        // Prepare element styles

        jQuery('ul' + config.container).css({
            'position': 'relative',
            'overflow': 'hidden',
            'list-style':'none'
        });

        jQuery('ul' + config.container + ' > li').css({
            'float':'left'
        });

        jQuery('div' + config.dots + ' > a').css({
            'display':'block',
            'float':'left'
        });

        jQuery('div' + config.leftArrow + ', ' + 'div' + config.rightArrow).css({
            'cursor':'pointer'
        });
		/*
        jQuery('div' + config.pause).css({
            'position':'absolute',
            'cursor':'pointer'
        });
		*/
        // Get padding for actual width / height, strip off 'px'

        item_padding_top = jQuery('ul' + config.container + ' > li').eq(0).css('padding-top');
        item_padding_top = (item_padding_top.match(/px/) ? item_padding_top.replace(/px/, '') : item_padding_top);

        item_padding_right = jQuery('ul' + config.container + ' > li').eq(0).css('padding-right');
        item_padding_right = (item_padding_right.match(/px/) ? item_padding_right.replace(/px/, '') : item_padding_right);

        item_padding_bottom = jQuery('ul' + config.container + ' > li').eq(0).css('padding-bottom');
        item_padding_bottom = (item_padding_bottom.match(/px/) ? item_padding_bottom.replace(/px/, '') : item_padding_bottom);

        item_padding_left = jQuery('ul' + config.container + ' > li').eq(0).css('padding-left');
        item_padding_left = (item_padding_left.match(/px/) ? item_padding_left.replace(/px/, '') : item_padding_left);

        // calculate actual width and height, strip off 'px'

        item_width = jQuery('ul' + config.container + ' > li').eq(0).css('width');
        item_width = (item_width.match(/px/) ? item_width.replace(/px/, '') : item_width);
        item_width = parseInt(item_width) + parseInt(item_padding_left) + parseInt(item_padding_right);

        item_height = jQuery('ul' + config.container + ' > li').eq(0).css('height');
        item_height = (item_height.match(/px/) ? item_height.replace(/px/, '') : item_height);
        item_height = parseInt(item_height) + parseInt(item_padding_top) + parseInt(item_padding_bottom);

        // Setup each banner item
		
        jQuery.each(jQuery('ul' + config.container + ' > li'), function(li_key, item)
        {
            jQuery(this).addClass(item_prefix + li_key);
			
            switch(config.effect)
            {
                case 'slide':
                    switch(config.direction)
                    {
                        case 'x':
                            jQuery(this).css({
                            	'display':'block',
                                'position':'absolute',
                                'top':config.liTop,
                                'left':(item_width * li_key) + 'px'
                            });
                            break;

                        case 'y':
                            jQuery(this).css({
                            	'display':'block',
                                'position':'absolute',
                                'top':(item_height * li_key) + 'px',
                                'left':config.liLeft
                            });
                            break;
                    }
                    break;

                case 'fade':
                    jQuery(this).css({
                    	'display':'block',
                        'position':'absolute',
                        'top':config.liTop,
                        'left':config.liLeft
                    }).prependTo(jQuery('ul' + config.container));
                    break;
            }
			
			if(config.useDots)
            {
				
                jQuery('div' + config.dots ).append('<a href="javascript:void(0);">&bull;</a>');
            }
			

        });
		

        // Setup the nav controller
		/*
        if(config.useDots)
        {
            jQuery('div' + config.dots).css({
                'text-align':'center',
                'color':config.dotColor,
                'padding':'5px 0 5px 0'
            });
        }
		*/
		
        jQuery.each(jQuery('div' + config.dots + ' > a'), function(a_key, item)
        {
			/*
            // For IE, allows fading of inline elements
            jQuery(this).css({
                'display':'inline-block'
            });

            // If we're using dots
            if(config.useDots)
            {
                jQuery(this).css({
                    'font-size':'48px',
                    'text-decoration':'none',
                    'padding':'3px 5px 0 5px',
                    'color':config.dotColor
                });
            }
			*/
            // Setup trigger
            jQuery(this).click(function()
            {
                if(typeof config.onTrigger == 'function')
                {
                    config.onTrigger(this);
                }
                current_item = a_key;
                transition();

                if(banner_paused == 0)
                {
                    resetTimer();
                }

            });
        });
		// Setup any next and previous controllers

        jQuery('div' + config.leftArrow).click(function()
        {
            previous();
        });

        jQuery('div' + config.rightArrow).click(function()
        {
            next();
        });

        if(item_count < 2)
        {
            jQuery('div' + config.leftArrow + ', div' + config.rightArrow).hide();
        }

        // Setup pause button

        jQuery('div' + config.pause)
        .html((config.timer > 0 && banner_paused == 0 ? config.pausePauseText : config.pausePlayText))
        .css({
            //'text-align':'center'
        });

        jQuery('div' + config.pause).click(function()
        {
            if(banner_paused == 0)
            {
                stopTimer();
                banner_paused = 1;
                jQuery(this).html(config.pausePlayText);
                config.onPause(this);
            }
            else if(banner_paused == 1)
            {
                resetTimer();
                banner_paused = 0;
                jQuery(this).html(config.pausePauseText);
                config.onPlay(this);
                transition();
            }
        });

        // Set the first item

        current_item = config.active;
        transition();

        // Start the timer, if requested

        resetTimer();

        function transition()
        {
            if( item_count > 1 )
            {
                jQuery('div' + config.dots + ' > a').removeClass('active');
                jQuery('div' + config.dots + ' > a:eq(' + current_item + ')').addClass('active');
                jQuery('ul' + config.container + ' > li').stop();

                if(config.fadeTriggers)
                {
                    jQuery('div' + config.dots + ' > a:not(:eq(' + current_item + '))').fadeTo(0, .3);
                    jQuery('div' + config.dots + ' > a:eq(' + current_item + ')').fadeTo(0, 1);
                }

                switch(config.effect)
                {
                    case 'slide':
                        jQuery.each(jQuery('ul' + config.container + ' > li'), function(li_key, item)
                        {
                            switch(config.direction)
                            {
                                case 'x':
                                    jQuery(this).animate({'left':((li_key * item_width) - (current_item * item_width)) + 'px'},config.speed,config.easing, function(){});
                                    break;

                                case 'y':
                                    jQuery(this).animate({'top':((li_key * item_height) - (current_item * item_height)) + 'px'},config.speed,config.easing, function(){});
                                    break;
                            }
                        });
                        break;

                    case 'fade':
                        if(started == 1)
                        {
                            jQuery('ul' + config.container + ' > li.' + item_prefix + current_item)
                            .hide()
                            .appendTo('ul' + config.container)
                            .fadeTo(config.speed, 1);
                        }
                        break;
                }

                started = 1;
                current_item = (current_item == (item_count - 1)) ? 0 : current_item + 1;
            }
        }

        function previous()
        {
            if(typeof config.onTrigger == 'function')
            {
                config.onTrigger(this);
            }

            if((parseInt(current_item)-1) < 0)
            {
                current_item = (parseInt(item_count) - 2);
            }
            else if((parseInt(current_item)-1) == 0)
            {
                current_item = (parseInt(item_count) - 1);
            }else{
                current_item = (parseInt(current_item) == 1 ? 0 : parseInt(current_item) - 2);
            }

            transition();
            resetTimer();
        }

        function next()
        {
            if(typeof config.onTrigger == 'function')
            {
                config.onTrigger(this);
            }

            if(parseInt(current_item) == parseInt(item_count))
            {
                current_item = 0;
            }

            transition();
            resetTimer();
        }

        function stopTimer()
        {
            clearInterval(timer_object);
        }

        function resetTimer()
        {
            stopTimer();
            if(typeof config.timer == 'number' && config.timer > 0)
            {
                timer_object = setInterval(function(){transition();}, config.timer);
            }
        }

        function in_array(needle, haystack)
        {
            for(var i in haystack)
            {
                if(haystack[i] == needle)
                {
                    return true;
                }
            }
            return false;
        }
    };

})(jQuery);
