var APP = APP || {};

(function(){

/**
 * ゲージ表示処理
 * @static
 * @function
 * @param {Node|jQuery} node
 */
APP.gauge = function(node) {
    if(typeof node === 'undefined') {
        $('div.gauge div').each(function() {
            var rate = $(this).attr('data-rate');
            rate = rate + '%';
            $(this).css('width', rate);
        });
    } else {
        var rate = $(node).attr('data-rate');
        rate = rate + '%';
        $(node).css('width', rate);
    }
};

/**
 * ゲージ表示処理
 * @static
 * @function
 * @param {Node|jQuery} node
 * @param {Numer} rate
 */
APP.gaugeForFloor = function(node, rate) {
    if ( typeof node === 'undefined' ) {
        node = $('div.gauge div');
    } else {
        node = $(node).children('div');
    }

    if ( typeof rate === 'undefined' ) return;
    rate = Math.round(rate);

    $(node).each(function(){
        $(this).animate({'width' : rate + '%'}, 'slow', 'swing');
    });
};

/**
 * タブ切り替え処理
 * @static
 * @function
 * @param {String} tab
 */
APP.tab = function(tab) {
    tab = (typeof tab === 'undefined') ? '' : '[data-content="' + tab + '"]' ;

    $('ul.tab-menu > li' + tab).bind('click', function() {
        if( !$(this).hasClass('selected') ) {
            var content = $(this).attr('data-content');

            $('ul.tab-menu > li').removeClass('selected');
            $(this).addClass('selected');

            $('div.tab-contents > *').hide();
            $('div.tab-contents > *[data-content="' + content + '"]').show();
        }
    });
};

/**
 * タブ初期化
 * @static
 * @function
 * @param {String} tab
 */
APP.initTab = function(tab) {
    $('ul.tab-menu > li').removeClass('selected');
    $('ul.tab-menu > li[data-content="' + tab + '"]').addClass('selected');

    $('div.tab-contents > *').hide();
    $('div.tab-contents > *[data-content="' + tab + '"]').show();
};

/**
 * 数値をカンマで区切る
 * @static
 * @function
 * @param {String} value
 */
APP.formatToNumeric = function( value ) {
    if ( typeof value === "undefined" || value === null || value === '' ) return '';
    value = new String(value).replace(/,/g, "");
    while(value != (value = value.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
    return '<span>'+ value +'</span>';
};

/**
 * サブメニューをホバー表示
 * @static
 * @function
 * @param {jQuery} clicker クリックされた要素
 * @param {jQuery} hover ホバーする要素
 * @param {jQuery} hover_container ホバー対象要素のグループ
 * @param {boolean} direction true:top false:bottom
 * @param {integer} align 0:左揃え 1:中央揃え 2:右揃え
 */
APP.hoverSubMenu = function( clicker, hover, hover_container, direction, align ) {
    if ( !clicker || !hover || !hover_container) return;

    clicker = $(clicker);
    hover = $(hover);
    hover_container = $(hover_container);

    var top = clicker.get( 0 ).offsetTop;
    var left = clicker.get( 0 ).offsetLeft;
    var width = parseInt(hover.css('width'), 10);
    var height = parseInt(clicker.css('height'), 10);
    var MARGIN = 5;
    //var screen_w = parseInt($(window).width(), 10);

    var c_left = 0;
    if ( align === 0 ) {
        // 左揃え
        c_left = left - MARGIN;
    } else if ( align === 1 ) {
        // 中央揃え
        c_left = left + ((parseInt(clicker.css('width'), 10)) / 2) - width / 2;
    } else if ( align === 2 ) {
        // 右揃え
        c_left = left + parseInt(clicker.css('width'), 10) - width + MARGIN;
    }

    var c_top = null;
    if ( direction ) {
        c_top = top - parseInt(hover.css('height'), 10) - MARGIN;
    } else {
        c_top = top + height + MARGIN;
    }

    if ( hover.hasClass('fadein') ) {
        // 既に表示中の場合
        hover.removeClass('fadein').addClass('disp-none');
        return;
    } else {
        hover_container.removeClass('fadein').addClass('disp-none');
        hover.css({
            top : c_top,
            left : c_left
        });
        hover.addClass('fadein').removeClass('disp-none');
    }
};

/**
 * 画像を先読みする
 * @static
 * @function preloadImages
 * @params {Array} urls
 * @params {Function} callback
 * @params {int} [timeout]
 */
APP.preloadImages = function(urls, callback, timeout) {
    urls = $.unique(urls);
    timeout = timeout || 1000;
    callback = callback || function() {};
    var called = false;
    function _callback() {
        if (!called) {
            callback();
            called = true;
        }
    }

    //画像を先読み
    var image_count = urls.length;
    var len = urls.length;
    if (len <= 0) {
        callback();
        return;
    }

    var t = setTimeout(function() { _callback(); } , timeout);
    for (var i = 0; i < len; i++) {
        var url = urls[i];
        url = url.replace(/^\"(.*)\"$/,"$1");
        console.log('preload:' + url);

        $('<img>').attr('src', function() {
            return url;
        }).bind('load error', function(e) {
            if (e.type === 'error') {
                console.log('preload error', url);
            }
            image_count--;
            if (image_count <= 0) {
                //全部読み終わったら読み込み開始
                _callback();
                clearTimeout(t);
            }
        });
    }
};

/**
 * @function
 * @desc 指定form内のcheckboxを全チェック/外すをはずす
 */
APP.formCheck = function( id ){
    console.log(id);
    var $check_list = $('#' + id + ' input[type=checkbox]');
    var $check_count = $check_list.filter(':checked').length;
    if($check_count == 0){
        $('#' + id + ' input').attr('checked',true);
    }else{
        $('#' + id + ' input').attr('checked',false);
    }
};
})();
