var buy_id;
var buy_type;
var sec_token;
var scrollPos;
var target;

$(document).ready(function() {
    _createOverlay();
    sec_token = $('#sec_token span').attr('id');
    // Global elements
    scrollPos = _getScroll();
    // Window/Keyboard events
    $(window).scroll(function(){
        scrollPos = _getScroll();
        _centerOverlay();
    });
    var count = $('#counter span').attr('id');

    $('#charge_pp_confirm_1').click( function() {
        $('div.overlay').show();
        buy_id = 1;
        buy_type = 'confirm';
        charge_pp_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
    $('#charge_pp_confirm_2').click( function() {
        $('div.overlay').show();
        buy_id = 2;
        buy_type = 'confirm';
        charge_pp_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
    $('#charge_pp_confirm_3').click( function() {
        $('div.overlay').show();
        buy_id = 3;
        buy_type = 'confirm';
        charge_pp_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
    $('#charge_pp_confirm_4').click( function() {
        $('div.overlay').show();
        buy_id = 4;
        buy_type = 'confirm';
        charge_pp_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
    $('#charge_pp_confirm_5').click( function() {
        $('div.overlay').show();
        buy_id = 5;
        buy_type = 'confirm';
        charge_pp_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
    $('#charge_pp_confirm_6').click( function() {
        $('div.overlay').show();
        buy_id = 6;
        buy_type = 'confirm';
        charge_pp_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
    $('#crysta_item_confirm_1').click( function() {
        $('div.overlay').show();
        buy_id = 1;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
    $('#crysta_item_confirm_2').click( function() {
        $('div.overlay').show();
        buy_id = 2;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
    $('#crysta_item_confirm_3').click( function() {
        $('div.overlay').show();
        buy_id = 3;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
    $('#crysta_item_confirm_4').click( function() {
        $('div.overlay').show();
        buy_id = 4;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
    $('#crysta_item_confirm_5').click( function() {
        $('div.overlay').show();
        buy_id = 5;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
    $('#crysta_item_confirm_6').click( function() {
        $('div.overlay').show();
        buy_id = 6;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
        $('#crysta_item_confirm_7').click( function() {
        $('div.overlay').show();
        buy_id = 7;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
        $('#crysta_item_confirm_8').click( function() {
        $('div.overlay').show();
        buy_id = 8;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
        $('#crysta_item_confirm_9').click( function() {
        $('div.overlay').show();
        buy_id = 9;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
        $('#crysta_item_confirm_10').click( function() {
        $('div.overlay').show();
        buy_id = 10;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
        $('#crysta_item_confirm_11').click( function() {
        $('div.overlay').show();
        buy_id = 11;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
        $('#crysta_item_confirm_12').click( function() {
        $('div.overlay').show();
        buy_id = 12;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
        $('#crysta_item_confirm_13').click( function() {
        $('div.overlay').show();
        buy_id = 13;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
        $('#crysta_item_confirm_14').click( function() {
        $('div.overlay').show();
        buy_id = 14;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
        $('#crysta_item_confirm_15').click( function() {
        $('div.overlay').show();
        buy_id = 15;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
        $('#crysta_item_confirm_16').click( function() {
        $('div.overlay').show();
        buy_id = 16;
        buy_type = 'confirm';
        crysta_item_confirm(sec_token, buy_type, buy_id);
        return false;
    } );
        $('#crysta_item_confirm_17').click( function() {
            $('div.overlay').show();
            buy_id = 17;
            buy_type = 'confirm';
            crysta_item_confirm(sec_token, buy_type, buy_id);
            return false;
        } );
        $('#crysta_item_confirm_18').click( function() {
            $('div.overlay').show();
            buy_id = 18;
            buy_type = 'confirm';
            crysta_item_confirm(sec_token, buy_type, buy_id);
            return false;
        } );
        $('#crysta_item_confirm_19').click( function() {
            $('div.overlay').show();
            buy_id = 19;
            buy_type = 'confirm';
            crysta_item_confirm(sec_token, buy_type, buy_id);
            return false;
        } );
        $('#crysta_item_confirm_20').click( function() {
            $('div.overlay').show();
            buy_id = 20;
            buy_type = 'confirm';
            crysta_item_confirm(sec_token, buy_type, buy_id);
            return false;
        } );
        $('#crysta_item_confirm_21').click( function() {
            $('div.overlay').show();
            buy_id = 21;
            buy_type = 'confirm';
            crysta_item_confirm(sec_token, buy_type, buy_id);
            return false;
        } );
        $('#crysta_item_confirm_22').click( function() {
            $('div.overlay').show();
            buy_id = 22;
            buy_type = 'confirm';
            crysta_item_confirm(sec_token, buy_type, buy_id);
            return false;
        } );
        $('#crysta_item_confirm_23').click( function() {
            $('div.overlay').show();
            buy_id = 23;
            buy_type = 'confirm';
            crysta_item_confirm(sec_token, buy_type, buy_id);
            return false;
        } );
        $('#crysta_item_confirm_24').click( function() {
            $('div.overlay').show();
            buy_id = 24;
            buy_type = 'confirm';
            crysta_item_confirm(sec_token, buy_type, buy_id);
            return false;
        } );
    $('#charge_pp').click( function() {
        $('div.overlay').show();
        buy_type = 'execute';
        charge_pp_confirm(sec_token, buy_type, buy_id);
    } );
    $('#crysta_item').click( function() {
        $('div.overlay').show();
        buy_type = 'execute';
        crysta_item_confirm(sec_token, buy_type, buy_id);
    } );
    $('a.closeChargePPBoxButton').click(function(){
        $('div.overlay').hide();
        $('div.chargePPBox').hide(200);
    });
	$('a.closeChargePPBoxButton').click(function(){
        $('div.overlay').hide();
        $('div.buyItemBox').hide(200);
    });
    return false;
} );
function _centerOverlay(){
    if($.browser.opera) {
        windowHeight = window.innerHeight;
        windowWidth = window.innerWidth;
    }else{
        windowHeight = $(window).height();
        windowWidth = $(window).width();
    };
    $(target).css({'top': (windowHeight/2) + scrollPos['scrollTop'] - 110 - 220});
};
function _getScroll(){
    if (self.pageYOffset) {
        scrollTop = self.pageYOffset;
        scrollLeft = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
        scrollTop = document.documentElement.scrollTop;
        scrollLeft = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        scrollTop = document.body.scrollTop;
        scrollLeft = document.body.scrollLeft;
    }
    return {scrollTop:scrollTop,scrollLeft:scrollLeft};
};
function _createOverlay() {
    var overlay = document.createElement('div');
    $(overlay).addClass('overlay');
    $(overlay).css({
        'position':'absolute',
        'display' :'none',
        'top'     :0,
        'left'    :0,
        'z-index' :9999,
        'height'  :$(document).height(),
        'width'   :$(window).width()
    });
    $(overlay).click(function(){
        $(this).hide();
        $('div.chargePPBox').hide(200);
    });
    $('body').append($(overlay));
};

function charge_pp_confirm(sec_token, type, id) {
    var request_url = "/?m=pc&a=do_crysta_charge_pp";
    $.ajax( {
        url: request_url,
        type: 'POST',
        dataType: 'json',
        data: { type : type,
                sec_token : sec_token,
                charge_type : id },
        success : charge_pp_response,
        error: function(xhr, status, e) {
            charge_pp_response({'status':'ng', 'error_message':'サーバーとの通信が切れました。<br />ページを再読み込みしてください。'}, 'ng');
        }
    } );
}

function crysta_item_confirm(sec_token, type, id) {
    var request_url = "/?m=pc&a=do_sell_crysta_item";
    $.ajax( {
        url: request_url,
        type: 'POST',
        dataType: 'json',
        data: { type : type,
                sec_token : sec_token,
                charge_type : id },
        success : crysta_item_response,
        error: function(xhr, status, e) {
　　　　　　　　　　crysta_item_response({'status':'ng', 'error_message':'サーバーとの通信が切れました。<br />ページを再読み込みしてください。'}, 'ng');
        }
    } );
}

function charge_pp_response(data, status) {
    if(data.status == 'ok') {
        if(buy_type == 'execute') {
            // PP購入完了
            target = 'div#chargePPBox_complete';
            $(target).children('h3').text('「' + data.content_name + '」 購入完了');
            $(target).children('div.chargePPBox_container').children('div.chargePPBox_status').html('「' + data.content_name + '」を購入しました。<br />Crysta / PP の残高は、マイページでご確認ください。');
            // PPを更新する
            reloadMoney();
        } else {
            // 購入準備OK時
            target = 'div#chargePPBox_confirm';
            $(target).children('h3').text('「' + data.content_name + '」 購入確認');
            $(target).children('div.chargePPBox_container').children('div.chargePPBox_status').html('「' + data.content_name + '」を ' + data.pay_crysta + ' Crysta で購入します。');
        }
    } else {
        if(data.text == 'not_logged_in') {
            // 未ログイン時
            target = 'div#chargePPBox_notLoggedIn';
        } else if(data.text == 'cis_session_out_of_time') {
            // CISセッション無効時
            target = 'div#chargePPBox_loginTimeout';
        } else if(data.text == 'crysta_empty') {
            // crystaが足りないとき
            target = 'div#chargePPBox_lackCrysta';
            $(target).children('div.chargePPBox_container').children('div.chargePPBox_status').html(data.error_message);
        } else {
            // その他各通信エラーなど
            target = 'div#chargePPBox_error';
            $(target).children('div.chargePPBox_container').children('div.chargePPBox_status').html(data.error_message);
        }
    }
    _centerOverlay();
    $(target).show(100);
}

function crysta_item_response(data, status) {
    if(data.status == 'ok') {
        if(buy_type == 'execute') {
            // PP購入完了
            target = 'div#chargePPBox_complete';
            $(target).children('h3').text('「' + data.content_name + '」 購入完了');
            $(target).children('div.chargePPBox_container').children('div.chargePPBox_status').html('「' + data.content_name + '」を購入しました。<br />Crystaの残高は、マイページでご確認ください。');
            // PPを更新する
            reloadMoney();
        } else {
            // 購入準備OK時
            target = 'div#chargePPBox_confirm';
            $(target).children('h3').text('「' + data.content_name + '」 購入確認');
            $(target).children('div.chargePPBox_container').children('div.chargePPBox_status').html('「' + data.content_name + '」を ' + data.pay_crysta + ' Crysta で購入します。');
        }
    } else {
        if(data.text == 'not_logged_in') {
            // 未ログイン時
            target = 'div#chargePPBox_notLoggedIn';
        } else if(data.text == 'cis_session_out_of_time') {
            // CISセッション無効時
            target = 'div#chargePPBox_loginTimeout';
        } else if(data.text == 'crysta_empty') {
            // crystaが足りないとき
            target = 'div#chargePPBox_lackCrysta';
            $(target).children('div.chargePPBox_container').children('div.chargePPBox_status').html(data.error_message);
        } else {
            // その他各通信エラーなど
            target = 'div#chargePPBox_error';
            $(target).children('div.chargePPBox_container').children('div.chargePPBox_status').html(data.error_message);
        }
    }
    _centerOverlay();
    $(target).show(100);
}
