﻿/*
 * jQuery SlidePic plugin 1.0.1
 *
 * Copyright (c) 2011 Dylan Ho
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.grnet.com.tw
 *
 * Revision: $Id: jquery.slidepic.js 2011-07-28 02:00:27Z Dylan.Ho $
 */

;(function($) {
    $.fn.slidepic = function(cfg) {
        /* 初始值 */
        var _defCfg = {
            autoSpeed:  30,
            arrowSpeed: 10,
            scrollBox:  'div.scroll',
            arrowRel:   'arrow',
            divMarg:    5,
            showBig:    ''
        };
     
        /* 固定參數 */
        var _staticCfg = {
            scrollWidth: 0,
            autoStart:   true,
            timeID1:     null,
            timeID2:     null
        };
     
        /* 更新設定 */
        var _cfg = $.extend(_defCfg, cfg),
            _cfg = $.extend(_cfg, _staticCfg);
     
        /* 功能執行 */
        var _handler = function() {
            /* 物件暫存 */
            var container  = this,
                obj        = $jQuery('#' +this.id),
                blockWidth = parseInt(obj.css('width').substring(0, obj.css('width').indexOf('px'))),
                scrollBox  = $jQuery(_cfg.scrollBox, container),
                picArrow   = $('img[rel="' +_cfg.arrowRel+ '"]', container);
         
            /* 計算區塊寬 */
            $('img[rel!="' +_cfg.arrowRel+ '"]', container).each(function() {
                $jQuery(this).parent().css({'float':'left', 'width':$jQuery(this).attr('width'), 'margin-right':_cfg.divMarg+ 'px'});
                _cfg.scrollWidth += parseInt($jQuery(this).width()) + _cfg.divMarg;
            });
            scrollBox.css('width', _cfg.scrollWidth);
         
            /* 執行確認 */
            if (_cfg.scrollWidth <= blockWidth)
            {
                /* 停止自動播放 */
                _cfg.autoStart = false;
             
                /* 移除箭頭 */
                picArrow.remove();
            }
            else
            {
                /* 箭頭設定 */
                layer = parseInt(scrollBox.css('z-index')) + 1;
                picArrow.css({'position':'absolute', 'z-index':layer, 'cursor':'pointer'})
                .eq(1)
                .css('right', '0px');
             
                /* 內容設定 */
                scrollBox.css({'width': _cfg.scrollWidth*2+ 'px', 'left':'0px'}).append(scrollBox.html());
            }
         
            /* 主程式 */
            slide_process1 = function(box, width) {
                /* 取得位置 */
                Yloc = parseInt(box.css('left').substring(0, box.css('left').indexOf('px')));
             
                /* 區塊移動 */
                box.css('left', (Yloc <= 0 - width) ? 0 : Yloc - 1);
            };
            slide_process2 = function(box, width, to) {
                /* 取得位置 */
                Yloc = parseInt(box.css('left').substring(0, box.css('left').indexOf('px')));
             
                /* 計算位移 */
                piexl = (navigator.userAgent.search("MSIE") != -1) ? 3 : 1;
                if (to == 0)      Yloc = (Yloc >= 0) ? Yloc - width : Yloc + piexl;
                else if (to == 1) Yloc = (Yloc <= 0 - width) ? 0 : Yloc - piexl;
             
                /* 區塊移動 */
                box.css('left', Yloc+ 'px');
            };
         
            /* 箭頭控制 */
            picArrow.mousedown(function() {
                /* 清除位移 */
                clearTimeout(_cfg.timeID1);
                clearTimeout(_cfg.timeID2);
             
                /* 執行位移 */
                _scrollTo = picArrow.index($jQuery(this));
                _cfg.timeID2 = setInterval(function() {slide_process2(scrollBox, _cfg.scrollWidth, _scrollTo);}, _cfg.arrowSpeed);
            })
            .mouseup(function() {
                /* 清除位移 */
                clearTimeout(_cfg.timeID1);
                clearTimeout(_cfg.timeID2);
             
                /* 執行位移 */
                _cfg.timeID1 = setInterval(function() {slide_process1(scrollBox, _cfg.scrollWidth);}, _cfg.autoSpeed);
            });
         
            /* 圖片控制 */
            $('img[rel!="' +_cfg.arrowRel+ '"]', container).hover(function() {
                /* 清除位移 */
                clearTimeout(_cfg.timeID1);
                clearTimeout(_cfg.timeID2);
             
                /* 滑鼠在圖片上效果開始 */
                //$jQuery(this).css('opacity', 1);
                /* 滑鼠在圖片上效果結束 */
            },
            function() {
                /* 執行位移 */
                if (_cfg.autoStart) _cfg.timeID1 = setInterval(function() {slide_process1(scrollBox, _cfg.scrollWidth);}, _cfg.autoSpeed);
             
                /* 滑鼠離開圖片效果開始 */
                //$jQuery(this).css('opacity', .5);
                /* 滑鼠離開圖片效果結束 */
            })
            .click(function() {
                /* 區塊確認 */
                if (_cfg.showBig == '') return;
             
                /* 圖片設置 */
                $jQuery(_cfg.showBig).find('img').attr('src', $jQuery(this).attr('img_src'));
            })
            .css('cursor', 'pointer');
         
            /* 執行位移 */
            if (_cfg.autoStart) _cfg.timeID1 = setInterval(function() {slide_process1(scrollBox, _cfg.scrollWidth);}, _cfg.autoSpeed);
        };
     
        return this.each(_handler);
    };
})(jQuery);
