/**
 * voice.js
 */

Array.prototype.shuffle = function() {
    var i = this.length;
    while(i){
        var j = Math.floor(Math.random()*i);
        var t = this[--i];
        this[i] = this[j];
        this[j] = t;
    }
    return this;
};

(function () {
    var voice = {
        init: function() {
            this.path = /^\/products\/(.*\/)?(.*)\.html/.exec(location.pathname);
            this.path = '/products/' + this.path[1] + 'voice/' + this.path[2] + '.json';
            this.setNumber();
            this.n = 1, this.currentPage = 1;
        },
        load: function() {
            var that = this;
            jQuery.getJSON(this.path, function(json) {
                that.data = json.voice.shuffle();
                //that.data = json.voice;
                jQuery('#data_length').html('（全' + that.data.length + '件）');
                that.totalPages = Math.ceil(that.data.length / 6);
                that.setPage();
                that.show();
            });
            //jQuery('.button_box img').bind('click', $.bind(this, this.next));
            jQuery('#next_voice').hover(
                                function() { jQuery(this).css('cursor', 'pointer') },
                                function() { jQuery(this).css('cursor', 'default') }
                            ).click(jQuery.bind(this, this.next));
            jQuery('#prev_voice').hover(
                                function() { jQuery(this).css('cursor', 'pointer') },
                                function() { jQuery(this).css('cursor', 'default') }
                            ).click(jQuery.bind(this, this.prev));
        },
        show: function(n) {
            this.html = [];
            jQuery('#voice_table').css('opacity', 0);
            this.showData = this.data.slice(this.n - 1, this.n + 5);
            this.html.push('<table id="first_table"><tbody><tr>');
            var that = this;
            jQuery.each(this.showData, function(i, value) {
                if (i % 2 === 0 && i !== 0) {
                    that.html.push('</tr><tr><td colspan="3" class="voice_separator_h"></td></tr><tr>');
                }
                that.html.push('<td><em>' + value.name + '</em><p>' + value.voice + '</p></td>');
                if (i % 2 === 0) {
                    that.html.push('<td class="voice_separator_v"></td>');
                }
            });
            if (!jQuery(this.html[this.html.length - 1]).html()) {
                this.html.push('<td></td>');
            }
            this.html.push('</tr><tr><td colspan="3" class="voice_separator_h"></td></tr></tbody></table>');
            this.htmlStr = this.html.join('');
            jQuery('#voice_table').empty().append(this.htmlStr).animate({opacity: 1}, 1000);
        },
        next: function() {
            this.n = this.n + 6;
            if (this.n > this.data.length) {
                this.n = this.n - 6;
            } else {
                this.setPage('next');
                this.show();
            }
            //jQuery('#res').html(this.n);
        },
        prev: function() {
            if (this.n !== 1) {
                this.n = this.n - 6;
                this.setPage('prev');
                this.show();
            }
            //jQuery('#res').html(this.n);
        },
        setNumber: function() {
            this.h4 = jQuery('#voice_box > h4')[0];
            this.h4Width = jQuery(this.h4).width();
            jQuery('#data_length').css('left', parseInt(jQuery(this.h4).css('left')) + this.h4Width + 5 + 'px');
        },
        setPage: function(sw) {
            if (sw === 'next') { ++this.currentPage };
            if (sw === 'prev') { --this.currentPage };
            jQuery('#pages').html(this.currentPage + ' / ' + this.totalPages + ' ページ');
            if (this.currentPage === this.totalPages) {
                jQuery('#next_voice').css('backgroundPosition', '-47px -23px');
            } else {
                jQuery('#next_voice').css('backgroundPosition', '-47px 0px');
            };
            if (this.currentPage === 1) {
                jQuery('#prev_voice').css('backgroundPosition', '0px -23px');
            } else {
                jQuery('#prev_voice').css('backgroundPosition', '0px 0px');
            }
        }
    };
    voice.init();
    jQuery(window).load(function(){
        voice.load();
    });
})();

/**
 * @author trixta
 */
(function($){
    $.bind = function(object, method){
        var args = Array.prototype.slice.call(arguments, 2);
        return function() {
            var args2 = [this].concat(args, $.makeArray( arguments ));
            return method.apply(object, args2);
        };
    };
})(jQuery);
