/*
 *   SmallSilder JQuery plugin
 *   $http://lab.cnscene.com/smallslider/$
 *   $Document : jquery.smallslider.js$
 *   $Created on : 2009-7-3, 11:56:24$
 *   $Last Update: 2010-3-15, 14:40:16$
 *   $Author : Sinrow$
 *   $E-Mail: sinrow@yeah.net$
 *   $Description
 *	JQuery plugin for switch images, Please Open me with UTF-8 encoding
 *	Created under Ubuntu 9.10 OS , if you open it with notepad , some black square will appears !
 *	這是一個源碼開放程式，本人特意加上注釋以方便大家修改，你可以自行修改，再發佈，歡迎和我交流。
 *	本程式的第一次靈感來源於網上流傳的 Slider插件，修改了插件的不足之後，幾經改寫，
 *	直到0.4版本，代碼全部翻新重新，希望能有較高的執行效率.$
 *
 *   $Version : 0.4$
 */
(function($) {
	
	$.smallslider=function(elm, options){

	// this 為當前的smallslider物件，為了區別，使用 _this 替換
	var _this = this;
	      _this.elm = elm ;	    // elm 為當前的 DOM物件 ，即使用class="smallslider" 的那個div物件。
	      _this.$elm = $(elm);  // $elm 為elm物件的jquery形式 
	      _this.opts=$.extend({},$.smallslider.defaults, options);	      
              _this.sliderTimer= null ;

	    // 初始化對象
	    _this.init = function()
	    {   
		    _this.$ul = _this.$elm.find('>ul') ;  // 為子元素ul
		    _this.$lis = _this.$elm.find('li') ;  // 為所有ul下子元素li 陣列
		    _this.$ims = _this.$elm.find('img') ;  // 為所有li下子元素img 陣列
		    _this.itemNums = _this.$lis.length ;
		    _this.width = _this.$elm.width();
		    _this.height = _this.$elm.height();
		    _this.current = 0 ;			// 當前的index索引
		    
		    if(_this.itemNums > 1)
		    {
			if(_this.opts.switchEffect=='ease')
			{
			    _this.$ul.css({
				position:'absolute',
				left:0,
				top: 0
			    });
			    if(_this.opts.switchPath=='left')
			    {
				var width = _this.itemNums *  _this.width;
				_this.$lis.css({
				    'float' : 'left'
				});
				_this.$ul.css({
				    'width' : width
				});
			    }
			    else if(_this.opts.switchPath=='up')
			    {
				var height = _this.itemNums *  _this.height;
				_this.$ul.css({
				    'height' : height
				});
			    }
			}
			else if(_this.opts.switchEffect=='fadeOut')
			{
			    _this.$ul.css({
				position:'relative'
			    });
			    _this.$lis.css({
				position:'absolute',
				zIndex:1
			    }).eq(0).css({
				zIndex:2
			    });
			}
			if(_this.opts.showButtons)
			{
			    _this.createButtons();	// 創建按鈕。
			}
			if(_this.opts.showText)
			{
			    _this.createText();	// 創建文字顯示。
			}
			if(_this.opts.autoStart)
			{
			    _this.startSlider(1);
			}
			if(_this.opts.onImageStop)
			{
			    _this.onImage();
			}
		    }
	    };
	    _this.createButtons = function()
	    {
		    var buttons='';
		    for(var i=1; i <= _this.itemNums ; i++ ){
			buttons += '<span>'+i+'</span>';
		    }
		    buttons ='<div class="smallslider-btns">' + buttons + '</div>';
		    var left=0,right=0,top =0,bottom=0;
		    var style_btns={};
		    switch(_this.opts.buttonPosition){
			case 'leftTop':
			    left = _this.opts.buttonOffsetX;
			    top  = _this.opts.buttonOffsetY;
			    style_btns={left: left + 'px' , top: top+'px'};
			    break;
			case 'rightTop':
			    right = _this.opts.buttonOffsetX;
			    top  = _this.opts.buttonOffsetY;
			    //style_btns={right: right + 'px' , top: top+'px'};
				var T = 210;
				style_btns={paddingleft: T + 'px' };
			    break;
			case 'rightBottom':
			    right = _this.opts.buttonOffsetX;
			    bottom = _this.opts.buttonOffsetY;
			    style_btns={right: right + 'px' ,bottom: bottom+'px'};
				
				
			    break;
			case 'leftBottom':
			    left = _this.opts.buttonOffsetX;
			    bottom = _this.opts.buttonOffsetY;
			    style_btns={left: left + 'px' ,bottom: bottom+'px'};
			    break;
		    }
						
		    $(buttons).css(style_btns).appendTo('#event_top_box');
			
		    _this.$btns = $('#event_top_box').find('span');
		    $('#event_top_box').find('span:not(:first)').css({marginLeft: _this.opts.buttonSpace+'px'});
		    _this.$btns.removeClass('current-btn');
		    _this.$btns.eq(0).addClass('current-btn');
		    if(_this.opts.switchMode=='click'){
			_this.$btns.click(function(){
			    var ix = _this.$btns.index($(this));
			    _this.slideTo(ix); // 表示需要切換到哪一張
			});
		    }else if(_this.opts.switchMode=='hover'){
			 _this.$btns.hover(function(){
			    var ix = _this.$btns.index($(this));
			    _this.slideTo(ix);
			});
		    }
	    };

	    // 創建標題標籤
	_this.createText = function(){
	    var style_tex={}; 
	    switch(_this.opts.buttonPosition){
		case 'leftTop':
		    style_tex={left:8,	top:0,textAlign:'right',width:'350px'};
		    _this.textPosition = 'top';
		    break;
		case 'rightTop':
		    style_tex={left:8,	top:320,textAlign:'left',width:'350px',height:'50px'};
		    
			if(_this.opts.textPosition){
				_this.textPosition = 'bottom';
			} else {
				_this.textPosition = 'top';
			}
		    break;
		case 'rightBottom':
		    style_tex={left:8,bottom:0,textAlign:'left',width:'350px'};
		    _this.textPosition = 'bottom';
		    break;
		case 'leftBottom':
		    style_tex={left:8,bottom:0,textAlign:'right',width:'350px'};
		    _this.textPosition = 'bottom';
		    break;
	    }
		//alert(_this.opts.textPosition);
	    if(_this.opts.textPosition){
		switch(_this.opts.textPosition)
		{
			case 'top':
			    style_tex.left = 8 ;  style_tex.top = 0;
			    break;
			case 'bottom':
			    style_tex.left = 8 ; style_tex.bottom=300 ;
			    break;
		}
		_this.textPosition = _this.opts.textPosition ;
	    }
	    if(_this.opts.textAlign)
	    {
		    style_tex.textAlign =_this.opts.textAlign;
	    }
	    $('<div id="txt_lay" class="smallslider-tex smallslider-lay"></div>').css(style_tex).css({
		opacity:0.59
	    }).appendTo(_this.$elm);

	    var tex0= _this.$ims.eq(0).attr('alt');
	    if(_this.opts.textLink){
		tex0 = '<a href="'+_this.$ims.eq(0).parent('a').attr('href')+'">'+ tex0+'</a>';
	    }
		
	    $('<h3 id="h3_lay" class="smallslider-tex"></h3>').css(style_tex).html(tex0).appendTo(_this.$elm);
	    _this.$h3 = _this.$elm.find('h3');
	    _this.$lay = _this.$elm.find('div.smallslider-lay');
	    _this.$tex = _this.$elm.find('.smallslider-tex');
	};
        _this.onImage =function(){
            _this.$ims.hover(function(){
                _this.stopSlider();
            }, function(){
                //_this.slideTo(_this.current+1);
				_this.slideTo(_this.current);
            });
        };

        _this.slideTo = function (index){
			if (index==1) {close_big();}
            _this.stopSlider(); // 先清掉以前的setTimeout;
            if(index > _this.itemNums -1) index = 0;
	    if(index < 0 ) index = _this.itemNums -1 ;
	    // 切換表示當前元素
            _this.$lis.removeClass('current-li').eq(index).addClass('current-li');
	    if(_this.opts.showButtons)
	    {
		_this.$btns.removeClass('current-btn');
		_this.$btns.eq(index).addClass('current-btn');
	    }
	    _this.slideText(index);
	    var chAttr = '';
	    var iC = 0;
	    switch(_this.opts.switchPath)
	    {	
		case 'left':
		    chAttr = 'left';
		    iC =_this.width ;
		    break;
		case 'up':
		 default :
		    chAttr = 'top';
		    iC = _this.height ;
		    break;
	    }
	    var iCx = -1 * index * iC; // Top或Left 變化量
	    var switchEase = _this.opts.switchEase  ;
            switch( _this.opts.switchEffect){
                case 'fadeOut':
		    _this.$lis.stop(true,false);
                    _this.$lis.css({zIndex:1,opacity:1}).hide();
		    _this.$lis.eq(_this.current).css({zIndex:3}).show();
		    _this.$lis.eq(index).css({zIndex:2}).show();
                    if(_this.current != index)
                    {
                    	_this.$lis.eq(_this.current).fadeOut(_this.opts.switchTime,function(){
				_this.$lis.css({zIndex:1});
				_this.$lis.eq(index).css({zIndex:3,opacity:1}).show();
                   	});
                    }
                    break;
		case 'ease':
		    _this.$ul.stop(true,false);
		    if(chAttr=='top')
			_this.$ul.animate({top : iCx}, {duration: _this.opts.switchTime, easing: switchEase, complete: function(){
			    }
			});
		    else if(chAttr=='left')
			_this.$ul.animate({left : iCx}, {duration: _this.opts.switchTime, easing: switchEase ,complete:function(){
			}});
		    break;
                case 'none':
                default :
                    _this.$lis.eq(_this.current).hide();
                    _this.$lis.eq(index).show();
                    break;
            }
	    _this.current = index ;
            _this.startSlider(index+1);
        };

	// 切換文字
	_this.slideText = function(index)
	{
	    if(_this.opts.showText)
	    {
		    var tex = _this.$ims.eq(index).attr('alt');
		    if(_this.opts.textLink){
			tex = '<a href="'+_this.$ims.eq(index).parent('a').attr('href')+'">'+ tex+'</a>';
		    }
		    _this.$h3.html(tex);
		    if(_this.opts.textSwitch>0){
			var t_path = _this.$h3.height();
			var t_ani1 ={}, t_ani2 ={};
			if(_this.textPosition=='top'){
				t_ani1 = {top : -1*t_path};
				t_ani2 = {top : 0}; 
			 }
			else if(_this.textPosition=='bottom'){
				t_ani1 = {bottom : -1*t_path};
				t_ani2 = {bottom : 0};
			 }
			 if(_this.opts.textSwitch==1) {
				 _this.$h3.stop(true, false).animate(t_ani1, {duration: 200, easing: 'easeOutQuad'}).animate(t_ani2, {duration: 200, easing: 'easeOutQuad'});
			 }else if(_this.opts.textSwitch==2){
				 _this.$tex.stop(true, false).animate(t_ani1, {duration: 200, easing: 'easeOutQuad'}).animate(t_ani2, {duration: 200, easing: 'easeOutQuad'});
				//_this.$lay.animate(t_ani1, {duration: 200, easing: 'easeOutQuad'}).animate(t_ani2, {duration: 200, easing: 'easeOutQuad'});
			 }
		    }
	    }
	};
        
	// 開始切換
        _this.startSlider = function(index){
            // 由第幾個序號開始 初始為1
            var st =setTimeout(function(){
                _this.slideTo(index);
            },_this.opts.time);
	    _this.sliderTimer = st ;
        };
	// 停止切換
        _this.stopSlider = function(){
	    //if(_this.opts.switchEffect=='fadeOut') _this.$lis.stop();
	  //  else if(_this.opts.switchEffect=='ease') _this.$ul.stop();
            if(_this.sliderTimer) {
		clearTimeout(_this.sliderTimer);
	    }		
            _this.sliderTimer = null;
        };

	_this.init();
    };

    $.smallslider.defaults={
            time:10000,              // 切換時間間隔，單位毫秒，1秒=1000毫秒
            autoStart:true,         // 是否自動開始播放
	    onImageStop : true , // 滑鼠放在圖片上時，是否停止播放
            switchMode:'click',     // 圖片切換的方式，click為單擊切換，hover為滑鼠移動到按鈕上時切換
            switchEffect:'fadeOut', // 切換特效,fadeOut, ease, none,
	    switchPath: 'left' , // 切換的方向，可選值為：up ， left ，即向上，向左
	    switchEase : 'easeOutQuart' , // 可選值列表如下
            switchTime: 1000,         // 切換時間，單位毫秒，1秒=1000毫秒
            buttonPosition: 'rightTop', // 按鈕位置表示，共有四個值：leftTop，leftBottom, rightTop, rightBottom
            buttonOffsetX:10,             // 水準方向上的按鈕偏移位置，指向中心部移動多少，這裏是數值，不加px
            buttonOffsetY:2,              // 豎直方向上的按鈕偏移位置，指向中心部移動多少，這裏是數值，不加px
            buttonSpace:4,           // 按鈕之間的間隔 單位為圖元，但不要加px
	    showText: false,          // 是否顯示標題，如果不顯示，則只顯示按鈕
	    showButtons : true,	    // 是否顯示按鈕，默認顯示
	    textLink : false,          // 是否給顯示的標題加上鏈結，如果為false，則，只顯示標題，標題不可單擊，鏈結的位址自動和當前播放到的圖片位址一致
	    textSwitch : 0 ,	// 標題是否運動顯示，如果為0則不動，1 標題動，2 標題和背景一起動。
	    textPosition: 'bottom',		// 標題欄的位置，默認為空，即和按鈕的位置一致，取值 top ,  bottom
	    textAlign: 'center'		// 如果留空，則會默認和按鈕位置的相反方向排列，取值：left, center, right
    };

    $.fn.smallslider = function(options){
	    // 遍曆由$.smallslider類創建生成的smallslider物件。
	    return this.each(function(i){
				(new $.smallslider(this, options));
	   });
    };
    
})(jQuery);

$.smallslider.switchEases = ["easeInQuad", "easeOutQuad", "easeInOutQuad", "easeInCubic", "easeOutCubic",
 "easeInOutCubic", "easeInQuart", "easeOutQuart", "easeInOutQuart", "easeInQuint", "easeOutQuint", "easeInOutQuint",
 "easeInSine", "easeOutSine", "easeInOutSine", "easeInExpo", "easeOutExpo", "easeInOutExpo", "easeInCirc", "easeOutCirc", "easeInOutCirc", "easeInElastic",
 "easeOutElastic", "easeInOutElastic", "easeInBack", "easeOutBack", "easeInOutBack",
 "easeInBounce", "easeOutBounce", "easeInOutBounce"];

// 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;
	}
});
