(function() {
    $.fn.dramaSlider = function($opts) {
        var $defaults = {
            navA: '',
            navB: '',
            direction: 'horizontal',
            speed: 500,
            opacity: 0.5
        },
           $set = $.fn.extend($defaults, $opts),
           $page = 1; // 起始頁數
        if ($set.navLeft == '' || $set.navRight == '') {
            alert('請設定按鈕');
            return false;
        }
        
        return this.each(function() {
            var $li = ($set.direction == 'horizontal') ? $(this).find('li:first').outerWidth(true) : $(this).find('li:first').outerHeight(true), // 單一li寬度
                $ul = $(this).find('ul:first'), // 移動區塊
                $liAmount = $(this).find('li').size(), // li數量
                $liPerPage = ($set.direction == 'horizontal') ? Math.round($(this).outerWidth() / $li) : Math.round($(this).outerHeight() / $li), // 每頁數量
                $totalPage = Math.ceil($liAmount / $liPerPage), // 總頁數
                $liLeft = $liAmount % $liPerPage == 0 ? $liLeft = $liPerPage : $liAmount % $liPerPage, // 遞補數量
                $btnA = $($set.navA).css('opacity', $set.opacity), // 按鈕1
                $btnB = $($set.navB); // 按鈕2

            switch ($set.direction) {
                case 'horizontal':
                    $(this).find('ul').width(9999);
                    break;
                case 'vertical':
                    $(this).find('ul').height(9999);
                    break;
            }
            
            // 往右或上按鈕
            $btnB.click(function() {
                if ($liAmount > $liPerPage) {
                    if ($page != $totalPage) {
                        if (($page + 1) == $totalPage) {
                            if ($page == 1) $btnA.fadeTo('normal', 1);
                            switch ($set.direction) {
                                case 'horizontal':
                                    $ul.animate({left: '+=' + ($li * $liLeft * -1)}, $set.speed);
                                    $(this).fadeTo('normal', $set.opacity);
                                    $page++;
                                    break;
                                case 'vertical':
                                    $ul.animate({top: '+=' + ($li * $liLeft * -1)}, $set.speed);
                                    $(this).fadeTo('normal', $set.opacity);
                                    $page++;
                                    break;
                            }
                        } else {
                            if ($page == 1) $btnA.fadeTo('normal', 1);
                            switch ($set.direction) {
                                case 'horizontal':
                                    $ul.animate({left: '+=' + ($li * $liPerPage * -1)}, $set.speed);
                                    $page++;
                                    break;
                                case 'vertical':
                                    $ul.animate({top: '+=' + ($li * $liPerPage * -1)}, $set.speed);
                                    $page++;
                                    break;
                            }
                        }
                    }
                }
                return false;
            });
            
            $btnA.click(function() {
                if ($page != 1) {
                    if ($page == $totalPage) {
                        if ($page == $totalPage) $btnB.fadeTo('normal', 1);
                        if ($page - 1 == 1) $(this).fadeTo('normal', $set.opacity);
                        switch ($set.direction) {
                                case 'horizontal':
                                    $ul.animate({left: '+=' + ($li * $liLeft)}, $set.speed);
                                    $page--;
                                    break;
                                case 'vertical':
                                    $ul.animate({top: '+=' + ($li * $liLeft)}, $set.speed);
                                    $page--;
                                    break;
                            }
                    } else {
                        if ($page == $totalPage) $btnB.fadeTo('normal', 1);
                        if ($page - 1 == 1) $(this).fadeTo('normal', $set.opacity);
                        switch ($set.direction) {
                                case 'horizontal':
                                    $ul.animate({left: '+=' + ($li * $liPerPage)}, $set.speed);
                                    $page--;
                                    break;
                                case 'vertical':
                                    $ul.animate({top: '+=' + ($li * $liPerPage)}, $set.speed);
                                    $page--;
                                    break;
                        }
                    }
                }
                return false;
            });
        });
    }
})(jQuery);
