/**
 * キープ君さようならバージョン
 */

Event.observe(window, 'load', function(){
    shopClass     = new shop();
    couponClass   = new coupon();
    sendmailClass = new sendmail();

    $$('a[href^=#]:not([href=#])').each(function(element) {
        element.observe('click', function(event) {
            new Effect.ScrollTo(this.hash.substr(1));
            Event.stop(event);
        }.bindAsEventListener(element))
    });
});


Ajax.Responders.register({
    onCreate: function(){
        Element.show('loadNow');
    },
    onComplete: function(){
        Element.hide('loadNow');
    }
});


var tool = Class.create();
tool.prototype = {
    initialize : function()
    {
    //        var query = $H({});
    //        new Ajax.Request('/json/tool',{
    //                method:'post',
    //                parameters: query.toQueryString(),
    //                onSuccess: function(transport){
    //                    var json = transport.responseText.evalJSON();
    //                    json.coupon.each(function( cpid ){
    //                        couponClass.render( cpid );
    //                    });
    //                    $('myToolBox_keepCouponCount').innerHTML = json.coupon.length;
    //
    //                    json.shop.each(function( shop ){
    //                        shopClass.render( shop );
    //                    });
    //                    $('myToolBox_keepShopCount').innerHTML = json.shop.length;
    //                },
    //                onFailure: function(request) {
    //                    //alert('読み込みに失敗しました');
    //                },
    //                onException: function (request) {
    //                    //alert('読み込み中にエラーが発生しました');
    //                }
    //            });
    }
}


var shop = Class.create();
shop.prototype = {
    initialize : function(){

        var elems = $$(".btnShopKeep");
        elems.each( function(item){
            var s = item.id.split('_');

            //イベントリスナー登録
            Event.observe(item.id, 'click', function(){
                shopClass.keepToggle(s[1]);
            });
        });
    },



    /**
     * 有効
     */
    keepd:function(sd)
    {
        if( $('btnShopKeep_'+sd) ){
            $('btnShopKeep_'+sd).innerHTML = '<span class="SkeepedBtn">キープ済み</span>';
        }
    },

    /**
     * 無効
     */
    unkeepd:function(sd)
    {
        if( $('btnShopKeep_'+sd) ){
            $('btnShopKeep_'+sd).innerHTML = '<span class="SkeepBtn">このお店をキープ</span>';
        }
    },

    /**
     * お店キープ/トグル処理
     */
    keepToggle:function(sd){
        var query = $H({
            sd : sd
        });
        new Ajax.Request('/json/shoptoggle',{
            method:'post',
            postBody: query.toQueryString(),
            onSuccess: function(transport){
                //alert(json ? Object.inspect(json) : "no JSON object");
                var json = transport.responseText.evalJSON();

                if(json.count){
                    Element.update('keepShopCount',json.count);
                    //shopClass.render(json.shop);


                    //キープ済みボタンに変更
                    this.keepd(sd);

                }
                if(json.unset){
                    //既にキープ済みだったので解除
                    this.unkeepd(sd);

                }
            }.bind(this),
            onFailure: function(request) {
            //alert('onFailure:読み込みに失敗しました');
            },
            onException: function (response) {
            //alert('onException:'+response.responseText);
            }
        });
        return false;
    },



    keepDel:function(sd){
        var query = $H({
            sd : sd
        });
        new Ajax.Request('/json/shopkeepdel',{
            method:'post',
            parameters: query.toQueryString(),
            onSuccess: function(json,e){
                new Effect.BlindUp('myToolBox_keepShop_sd_'+ sd ,{
                    afterFinishInternal:function(){
                        Element.remove('myToolBox_keepShop_sd_'+sd);
                    }
                });
                $('myToolBox_keepShopCount').innerHTML = e.count;

                //キープ済みボタンに変更
                if( $('btnShopKeep_'+sd) ){
                    //$('btnCuponKeep_'+shop.sd).style.backgroundImage = 'url()';
                    $('btnShopKeep_'+sd).innerHTML= '<span class="SkeepBtn">このお店をキープ</span>';
                }

            },
            onFailure: function(request) {
            //alert('読み込みに失敗しました');
            },
            onException: function (request) {
            //alert('読み込み中にエラーが発生しました');
            }
        });
    },

    histroyDel:function(sd){
        var query = $H({
            sd : sd
        });
        new Ajax.Request('/json/shophistroydel',{
            method:'post',
            parameters: query.toQueryString(),
            onSuccess: function(json,e){
                new Effect.BlindUp('shop_'+ sd ,{
                    afterFinishInternal:function(){
                        Element.remove('shop_'+e.sd);
                    }
                });
                $('keepCouponCount').innerHTML = e.count;
            },
            onFailure: function(request) {
            //alert('読み込みに失敗しました');
            },
            onException: function (request) {
            //alert('読み込み中にエラーが発生しました');
            }
        });
    },


    histroyAllDel:function(){
        var query = $H({});
        new Ajax.Request('/json/histroyalldel',{
            method:'post',
            parameters: query.toQueryString(),
            onSuccess: function(json){
                Element.update('histroyBox','<br /><i>最近チェックした店舗はありません</i><br /><br />');
            //                    new Effect.BlindUp('histroyBox' ,{
            //                        afterFinishInternal:function(){
            //                            Element.remove('histroyBox');
            //                        }
            //                    });
            },
            onFailure: function(request) {
            //alert('読み込みに失敗しました');
            },
            onException: function (request) {
            //alert('読み込み中にエラーが発生しました');
            }
        });
    }
}


var coupon = Class.create();
coupon.prototype = {
    initialize : function(){

        var elems = $$(".btnCouponKeep");
        elems.each( function(item){
            var s = item.id.split('_');

            //イベントリスナー登録
            Event.observe(item.id, 'click', function(){
                couponClass.keepToggle(s[1]);
            });
        });
    },



    /**
     * 有効
     */
    keepd:function(cpid)
    {
        if( $('btnCouponKeep_'+cpid) ){
            $('btnCouponKeep_'+cpid).innerHTML = 'キープ済み';

            Element.removeClassName('btnCouponKeep_'+cpid , 'CkeepBtn');
            Element.addClassName('btnCouponKeep_'+cpid , 'CkeepedBtn');
        }
    },

    /**
     * 無効
     */
    unkeepd:function(cpid)
    {
        if( $('btnCouponKeep_'+cpid) ){
            $('btnCouponKeep_'+cpid).innerHTML = 'このクーポンをキープ';

            Element.removeClassName('btnCouponKeep_'+cpid , 'CkeepedBtn');
            Element.addClassName('btnCouponKeep_'+cpid , 'CkeepBtn');
        }
    },

    /**
     * クーポンキープ/トグル処理
     */
    keepToggle:function(cpid){
        var query = $H({
            cpid : cpid
        });
        new Ajax.Request('/json/coupontoggle',{
            method:'post',
            postBody: query.toQueryString(),
            onSuccess: function(transport){
                var json = transport.responseText.evalJSON();

                //if(json.count){
                    Element.update('keepCouponCount',json.count);
                    //shopClass.render(json.shop);


                    //キープ済みボタンに変更
                    this.keepd(cpid);

                //}
                if(json.unset){
                    //既にキープ済みだったので解除
                    this.unkeepd(cpid);

                }
            }.bind(this),
            onFailure: function(request) {
            //alert('onFailure:読み込みに失敗しました');
            },
            onException: function (response) {
            //alert('onException:'+response.responseText);
            }
        });
        return false;
    },



    del:function(cpid){
        var query = $H({cpid : cpid});

        new Ajax.Request('/json/coupondel',{
                method:'post',
                parameters: query.toQueryString(),
                onSuccess: function(json,e){
                    new Effect.BlindUp('myToolBox_keepCoupon_cp_'+ e ,{
                        afterFinishInternal:function(){
                            Element.remove('myToolBox_keepCoupon_cp_'+e);
                        }
                    });
                    $('keepCouponCount').innerHTML = $('keepCouponCount').innerHTML -1;
                    if($('myToolBox_keepCouponCount')){
                        $('myToolBox_keepCouponCount').innerHTML = $('myToolBox_keepCouponCount').innerHTML -1
                    }
                }
            });
    }
}


var sendmail = Class.create();
sendmail.prototype = {
    initialize : function(){},

    coupon:function(cpid)
    {
        if ( $('frmAccount').value=='' ){
            $('keepcoupon_result').innerHTML = '未入力';

            Field.focus('frmAccount');
            $('frmAccount').style.backgroundColor='#ffaaaa';

        }else{
            var params = $H({
                account: $('frmAccount').value,
                domain: $('frmDomain').value,
                cpid:cpid
            });

            new Ajax.Request('/json/sendmailcoupon',{
                method: 'post',
                parameters:params.toQueryString(),
                onSuccess: getResponse,
                onLoading:function(){
                    $('btnSend').disabled = true;
                //new Insertion.Top('btnSend',loadingIcon)
                },
                onComplete:function(){
                    $('btnSend').disabled = false;
                }
            //postBody: params
            });
        }

        function getResponse(req,e){
            if (e.status=='NG'){
                $('send_coupon_msg').innerHTML = e.messsage;
                Field.focus('keepcoupon_account');
                $('frmAccount').style.backgroundColor='#ffaaaa';
            }else if(e.status=='OK'){
                $('frmAccount').style.backgroundColor='#ffffff';
                $('send_coupon_outer').innerHTML = e.messsage;
            }else{
                $('send_coupon_msg').innerHTML = 'Error:' + req.responseText;


            }
        }
    },



    keepcoupon:function()
    {
        if ( $('keepcoupon_frmAccount').value=='' ){
            $('keepcoupon_result').innerHTML = '未入力';

            Field.focus('keepcoupon_frmAccount');
            $('keepcoupon_frmAccount').style.backgroundColor='#ffaaaa';

        }else{
            var params = $H({
                account: $('keepcoupon_frmAccount').value,
                domain: $('keepcoupon_frmDomain').value
            });

            new Ajax.Request('/json/sendmailkeepcoupon',{
                method: 'post',
                parameters:params.toQueryString(),
                onSuccess: getResponse,
                onLoading:function(){
                    $('btnSendKeepCoupon').disabled = true;
                //new Insertion.Top('btnSend',loadingIcon)
                },
                onComplete:function(){
                    $('btnSendKeepCoupon').disabled = false;
                    reutrn;
                }
            //postBody: params
            });
        }

        function getResponse(req,e){
            if (e.status=='NG'){
                $('keepcoupon_result').innerHTML = e.messsage;
                Field.focus('keepcoupon_frmAccount');
                $('keepcoupon_frmAccount').style.backgroundColor='#ffaaaa';
            }else if(e.status=='OK'){
                $('keepcoupon_frmAccount').style.backgroundColor='#ffffff';
                $('keepcoupon_outer').innerHTML = e.messsage;
            }else{
                $('keepcoupon_msg').innerHTML = 'Error:' + req.responseText;
            }
        }

    }
}





/**
 *
 */
function send_mobile_towntown()
{
    if ( $('frmAccount').value=='' ){
        $('send_mobile_towntown_msg').innerHTML = '未入力';
        Field.focus('frmAccount');
        $('frmAccount').style.backgroundColor='#ffaaaa';

    }else{
        var params = $H({
            method:'mobile_towntown',
            account: $('frmAccount').value,
            domain: $('frmDomain').value
        });

        new Ajax.Request('/json/sendmail/',
        {
            method: 'post',
            parameters:params.toQueryString(),
            onComplete: getResponse
        //postBody: params
        }
        );
    }

    function getResponse(req){
        var lines = req.responseText.split("=");
        if (lines[0]=='NG'){
            $('send_mobile_towntown_msg').innerHTML = lines[1];
            Field.focus('account');
            $('frmAccount').style.backgroundColor='#ffaaaa';
        }else if(lines[0]=='OK'){
            $('frmAccount').style.backgroundColor='#ffffff';
            $('send_mobile_towntown_outer').innerHTML = lines[1] + '';
        }else{
            $('send_mobile_towntown_msg').innerHTML = 'Error:' + req.responseText;
        }
    }
}

/**
 *
 */
function send_mobile_towntown()
{
    if ( $('frmAccount').value=='' ){
        $('send_mobile_towntown_msg').innerHTML = '未入力';
        Field.focus('frmAccount');
        $('frmAccount').style.backgroundColor='#ffaaaa';

    }else{
        var params = $H({
            method:'mobile_towntown',
            account: $('frmAccount').value,
            domain: $('frmDomain').value
        });

        new Ajax.Request('/json/sendmail/',
        {
            method: 'post',
            parameters:params.toQueryString(),
            onComplete: getResponse
        //postBody: params
        }
        );
    }

    function getResponse(req){
        var lines = req.responseText.split("=");
        if (lines[0]=='NG'){
            $('send_mobile_towntown_msg').innerHTML = lines[1];
            Field.focus('account');
            $('frmAccount').style.backgroundColor='#ffaaaa';
        }else if(lines[0]=='OK'){
            $('frmAccount').style.backgroundColor='#ffffff';
            $('send_mobile_towntown_outer').innerHTML = lines[1] + '';
        }else{
            $('send_mobile_towntown_msg').innerHTML = 'Error:' + req.responseText;
        }
    }
}

/**
 * 本稼動サイトビルダーURL
 *
 */
function send_mobile_shop(shopname,username)
{
    if ( $('frmAccount').value=='' ){
        $('send_mobile_shop_msg').innerHTML = '未入力';
        Field.focus('frmAccount');
        $('frmAccount').style.backgroundColor='#ffaaaa';

    }else{

        var params = $H({
            method:'mobile_shop',
            account: $('frmAccount').value,
            domain: $('frmDomain').value,
            username:username,
            shopname:shopname
        });

        new Ajax.Request('/json/sendmail/',{
            onLoading:function(){
                $('btnSend').disabled = true;
            //new Insertion.Top('btnSend',loadingIcon)
            },
            onComplete:function(){
                $('btnSend').disabled = false;
            },
            method: 'post',
            onSuccess: getResponse,
            parameters:params.toQueryString()
        });
    }

    function getResponse(req){
        var lines = req.responseText.split("=");
        if (lines[0]=='NG'){
            $('send_mobile_shop_msg').innerHTML = lines[1];
            Field.focus('account');
            $('frmAccount').style.backgroundColor='#ffaaaa';
        }else if(lines[0]=='OK'){
            $('frmAccount').style.backgroundColor='#ffffff';
            $('send_mobile_shop_outer').innerHTML = lines[1] + '';
        }else{
            $('send_mobile_shop_msg').innerHTML = 'Error:' + req.responseText;
        }
        Element.hide('loadNow');
    }
}

