// 投稿文字数制限値
var MAX_CHAR = 100;
// 投票済フラグ
var voted = [];
// 投稿ウィンドウオープン済フラグ
var iopen = false;
// 〆切時刻
var deadline = Date.parse('Aug, 16 2010 17:00:00');

jQuery.extend(jQuery.easing,
{
    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;
    }
});


$(document).ready(function() {
    // 投稿済フラグ
    var posted = false;
    
    // ナイスピンチ！
    $('a.vote').click(function() {
        // 〆切前？
        if (deadline < (new Date()).getTime()) {
            return false;
        }
        var grand_parent = $(this).parent().parent();
        var tweet_id = $('div.tweet_id', grand_parent).html();
        var div = grand_parent.parent().parent().parent();
        var target = $('div.numNp', div);
        target.slideToggle('fast', function() {
            // 重複ナイス排除
            if (in_array(tweet_id, voted)) {
                target.slideToggle('fast');
                return false;
            }
            $.ajax({
                type: 'POST',
                url: 'func.php',
                data: {
                    method: 'point',
                    tweet_id: tweet_id
                },
                dataType: 'json',
                success: function(res) {
                    if (res.status == 'ok') {
                        var current_count = parseInt(target.html());
                        target.html(current_count + 1);
                        voted.push(tweet_id);
                    } else {
                        // 加算失敗
                    }
                    target.slideToggle('fast');
                },
                error: function(xhr, status, exception) {
                    // 通信失敗等
                    target.slideToggle('fast');
                }
            });
        });
        return false;
    });
    
    // 投稿ウィンドウオープン
    $('#open_window').click(function(e) {
        if (!iopen) {
            $('#post_window > iframe').attr('src', iframe_src).load(function() {
                $('#post_window').css({
                    top:  e.pageY + 10 + 'px',
                    left: e.pageX - 400 + 'px'
                }).fadeIn('fast');
                return false;
            });
            iopen = true;
        } else {
            $('#post_window').css({
                top:  e.pageY + 10 + 'px',
                left: e.pageX - 400 + 'px'
            }).fadeIn('fast');
        }
        return false;
    });
    
    // 投稿ウィンドウクローズ
    $('#post_window img').click(function() {
        $('#post_window').fadeOut('fast');
    });
    
    // 投稿ウィンドウ移動
    $('#post_window div.taskbar').mousedown(function(e) {
        var f_window = $(this).parent();
        f_window
            .data('clickPointX', e.pageX - f_window.offset().left)
            .data('clickPointY', e.pageY - f_window.offset().top);
        $(document).mousemove(function(e) {
            f_window.css({
                top:  e.pageY - f_window.data('clickPointY') + 'px',
                left: e.pageX - f_window.data('clickPointX') + 'px'
            });
        });
    }).mouseup(function() {
        $(document).unbind('mousemove');
    });
    
    // 検索
    $('#innerSearch > form').submit(function() {
        var mode = ($('input[name="mode"]:checked', this).val() == '1') ? 'id' : 'name';
        var val = $('input[name="val"]', this).val();
        if (!val) return false;
        location.href = 'search.php?' + mode + '=' + val;
        return false;
    });
    
    // ランキング表示
    $('#ranking-select').change(function() {
        var date = $(this).val();
        var div = $(this).parent().parent();
        var ul = $('ul', div);
        ul.fadeTo('fast', 0, function() {
            $.ajax({
                type: 'GET',
                url: 'func.php',
                data: {
                    method: 'ranking',
                    date: date
                },
                dataType: 'json',
                success: function(res) {
                    if (res) {
                        var icon, name, dt, dd;
                        $('li', ul).each(function(i) {
                            if (res[i].image_url) {
                                icon = '<a href="./unit.php?id=' + res[i].id + '"><img src="' + res[i].image_url + '" alt="icon" width="48" height="48" /></a>';
                                name = '@<a href="./unit.php?id=' + res[i].id + '">' + res[i].user_id + '</a>';
                            } else {
                                icon = '<a href="./unit.php?id=' + res[i].id + '"><img src="img/img_pinchnow.gif" alt="icon" width="48" height="48" /></a>';
                                name = '<a href="./unit.php?id=' + res[i].id + '">' + res[i].name + '</a>';
                            }
                            dt = $('dt', this);
                            $('div.pic', dt).html(icon);
                            $('div.name', dt).html(name);
                            $('div.time', dt).html(res[i].created_at);
                            $('div.numNp', dt).html(res[i].point);
                            dd = $('dd', this);
                            $('div.comment', dd).html(res[i].content);
                        });
                        ul.fadeTo('fast', 1);
                    } else {
                        ul.fadeTo('fast', 1);
                        alert('ランキングが取得できませんでした。');
                    }
                }
            });
        });
    });
    
    // ページトップに戻る
    $('#gotoTop > a').click(function() {
        $($.browser.safari ? 'body' : 'html').animate({scrollTop:0}, 400, 'easeInOutCubic');
        return false;
    });
});


function in_array(needle, haystack) {
    for (var i = 0, imax = haystack.length; i < imax; i++) {
        if (haystack[i] === needle) return true;
    }
    return false;
}
