/**
 * ボタンを押すだけで公開する
 */
function start_public(obj, contents_id){
    var button = obj;
    var popup = "#start_public_popup";
    
    //競合するボタンを消す
    $(popup).remove();
    
    //ツールチップ
    var tooltip = document.createElement('div');
    tooltip.id = 'start_public_popup';
    
    //データをPOSTするURL
    var url = '/?m=pc&a=do_start_public_check';
    //データをPOSTする
    $.post(url, {"target_ptc_content_id":contents_id}, function(data, status){
        
        if(status == 'success' && data == 'public_ok'){
            //メッセージエリア
            var message = '<p id="onShow_confirm_message' + contents_id + '">公開しますか？ </p>';
            //ボタン設定
            var ok_button     = '<button id="public_ok'     + contents_id + '" class="inputOnShow" onclick="start_public_complete(' + contents_id + ');return false;">はい</button>';
            var cancel_button = '<button id="public_cancel' + contents_id + '" class="inputOnShow" onclick="stop_public_complete(' + contents_id + ');return false;">いいえ</button>';
            
            $(tooltip).append(message);
            $(tooltip).append(cancel_button);
            $(tooltip).append(ok_button);
            $(tooltip).addClass('box_onShow');
            $(tooltip).css({'position': 'absolute', 'top':$(button).offset().top + 20, 'left':$(button).offset().left - 20});
            $(document.body).append(tooltip);
            
            //表示する
            $(tooltip).stop(true, true).animate({opacity: "show"}, 200,function(){
                $.timer(800, function(timer) {});
            });
        }
    }, 'text');
}

/*
 * 公開開始完了フェイズ
 */
function start_public_complete(contents_id){
    //データをPOSTするURL
    var url = '/?m=pc&a=do_start_public';
    
    //データをPOSTする
    $.post(url, {"target_ptc_content_id":contents_id}, function(data, status){
        
        if(status == 'success'){
            if(data == 'public_ok'){
                var html = '公開しました。';
            }else{
                var html = 'エラー:公開できませんでした';
            }
        }else{
            var html = 'エラー:公開できませんでした';
        }
        
        $("#public_ok"                + contents_id).remove();
        $("#public_cancel"            + contents_id).remove();
        $("#onShow_confirm_message"   + contents_id).remove();

        var message = '<p class="box_onShow">'+ html +'</p>';
        var popup = "#start_public_popup";
        var cancel_button = '<img id="public_cancel' + contents_id + '" src="/img/common_parts/icon_delete.gif" style="cursor: pointer; position: absolute; top: 5px; right: 5px;" onclick="stop_public_complete(' + contents_id + ');return false;">';

        $(popup).append(message);
        
        if(data == 'public_ok'){
            //不要なボタンを削除する
            $("#start_public_button" + contents_id).remove();
            
            //表示する
            $(popup).stop(true, true).animate({opacity: "show"}, 200,function(){
                $.timer(800, function(timer) {
                    $(popup).animate({opacity: "hide"}, "slow", function(){
                        $(popup).remove();
                    });
                    timer.stop();
                });
            });
        }else{
            //表示する
            $(popup).append(cancel_button);
            $(popup).stop(true, true).animate({opacity: "show"}, 200,function(){
                $.timer(800, function(timer) {});
            });
        }
    }, 'text');
}

function stop_public_complete(contents_id){
    var popup = "#start_public_popup";
    //表示する
    $(popup).stop(true, true).animate({opacity: "show"}, 200,function(){
        $.timer(500, function(timer) {
            $(popup).animate({opacity: "hide"}, "fast", function(){
                $(popup).remove();
            });
            timer.stop();
        });
    });
}

function onButtonMousedown(target){
    $(target).css('margin-top', '6px');
    $(target).css('margin-bottom', '0px');
}
function onButtonMouseup(target){
    $(target).css('margin-top', '5px');
    $(target).css('margin-bottom', '1px');
}

$(document).ready(function(){
    $("a.btn_public_start").mousedown(function() {
        onButtonMousedown(this);
    });
    $("a.btn_public_start").mouseup(function() {
        onButtonMouseup(this);
    });
    $("a.btn_public_start").mouseout(function() {
        onButtonMouseup(this);
    });
});