/*
 fixdap.js is lightweight framework by jquery (for fixdap project)
 http://fixdap.com/js/fixdap.js
 version: 0.1 / 2007-12-12

 Copyright (C) 2007 livedoor Co.,Ltd.
 Code licensed under the BSD License
*/

var fixdap = jQuery.noConflict();

fixdap(function () {
   /*----------------------------------
     OS判定
   ----------------------------------*/
   fixdap.isWin = /^(win)/.test( navigator.platform.toLowerCase() );

   fixdap.ajaxSetup({
       cache : true
   });
   /*----------------------------------
     プロジェクトページ全体に適用する処理
   ----------------------------------*/
   /*
       ※ Ctrl+Enterによるクイックサブミット
   */
   fixdap.getScript("/js/jquery/plugins/jquery.quickSubmit.js", function () {
           setTimeout(function () {
               fixdap("textarea").quickSubmit();
           }, 1);
   });
   /*-----------------
     ページ単位の処理
   -----------------*/
   var page = fixdap.grep([
                     {
                        regex   : /^\/$/,
                        handler : function () {
                        }
                     },
                     {
                        regex : /^(\/project\/create\/)$/,
                        handler : function () {
                           /*
                                ※ fillinメッセージ
                           */
                           /*
                           fixdap.getScript("/js/jquery/plugins/jquery.fillinMsg.js", function () {
                               setTimeout(function () {
                                   fixdap("#name").fillinMsg("プロジェクト名を入力");
                                   fixdap("#description").fillinMsg("概要を入力");
                               }, 1);
                           });
                           */
                           /*
                                ※ signalInterface
                           */
                           fixdap.getScript("/js/jquery/plugins/jquery.signalInterface.js", function () {
                               setTimeout(function () {
                                  fixdap("#name").dispatchSignal({
                                       max : 64
                                  });
                                  fixdap("#project_uri").dispatchSignal({
                                       max : 32
                                  });
                               }, 1);
                           });
                           /*
                             ※ 重複チェック
                           */
                           fixdap.getScript("/js/library/tmpRoundCorner.js", function () {
                               setTimeout(function () {
                                   fixdap.extend(fixdap.check_id_setting, {
                                         target_obj : "id-check",
                                         target_val : "project_uri",
                                         target_api : "/internal_api/available_project/",
                                         margin_left : 0
                                   });
                                   fixdap("div.bubble > cite.rounded").css("left", "30px");
                                   fixdap("#id-check").mouseup( fixdap.check_id ).keydown(function (event) {
                                           var kc = event.which || event.keyCode;
                                           if ( kc == 13 )
                                               return fixdap.check_id.call( this );
                                   }).blur(function () {
                                           fixdap("#bubble-box").hide();
                                   });
                               }, 1);
                           });
                           /*
                                ※ フォーカス
                           */
                           fixdap("#name").focus();
                        }
                     },
                     {
                        regex   : /^\/p\/[^/]+\/$/,
                        handler : function () {
                           /*
                                ※ ソートリンクへハンドラを設定する
                           */
                           fixdap(".sortlink").click(function () {
                               var args = fixdap( this ).attr("id").split("_");
                               fixdap("#hidden_sort").val( args[0] );
                               fixdap("#hidden_d").val( args[1] );
                               setTimeout(function () {
                                   location.search = "?" + fixdap(".serialize_target").serialize();
                               }, 1);
                           });
                           /*
                                ※ 完了したタスクを表示する、しないのトグルボタン
                           */
                           fixdap("#show_finished").click(function () {
                               fixdap("#hidden_show_finished").val() == "1"
                                                                     ? fixdap("#hidden_show_finished").val( 0 )
                                                                     : fixdap("#hidden_show_finished").val( 1 );
                               location.search = "?" + fixdap(".serialize_target").serialize();
                           });
                           /*
                                ※ プルダウンメニューの変更によるページ移動
                           */
                           fixdap("#memchange").change(function () {
                               location.search = "?" + fixdap(".serialize_target").serialize();
                           });
                        }
                     },
                     {
                        regex : /^\/p\/[^/]+\/[0-9]+\/$/,
                        handler : function () {
                        }
                     },
                     {
                        regex : /^\/p\/[^/]+\/[0-9]+\/(?:add)$/,
                        handler : function () {
                           /*
                                ※ 文字カウント
                           */
                           fixdap.getScript("/js/jquery/plugins/jquery.signalInterface.js", function () {
                               setTimeout(function () {
                                  fixdap("#body").dispatchSignal({
                                       max : 15000
                                  });
                               }, 1);
                           });
                           /*
                                ※ 原文の引用
                                残りのタスクはテキストエリアへのフォーカス処理
                           */
                           fixdap("#ibot").click(function () {
                               var reply    = fixdap("#body").focus().val();
                               var original = fixdap.map( fixdap("#originaltext").val().split(/\x0D\x0A|\x0D|\x0A/), function (sentence) {
                                                  return "> " + sentence;
                                              }).join("\n");
                               fixdap("#body").val( reply + "\n\n" + original );
                           });
                           /*
                               ※ 添付ファイルのトグル
                           */
                           fixdap("#toggle_file_box").one("click", function () {
                                   fixdap(".inputfile").removeClass("hidelist");
                                   fixdap( this ).remove();
                                   fixdap(".inputfile input[type='file']").get(0).focus();
                           });
                           /*
                               ※ fillinメッセージ
                           */
                           fixdap.getScript("/js/jquery/plugins/jquery.fillinMsg.js", function () {
                               setTimeout(function () {
                                   fixdap("#body").fillinMsg("本文を入力");
                               }, 1);
                           });
                           /*

                           */
                           var selects = fixdap("td#dead_line_zone select");
                           fixdap("input#undetermined").bind("change", function () {
                               selects.attr("disabled", this.checked);
                           }).attr("checked") && selects.attr("disabled", true);
                        }
                     },
                     {
                        regex : /^\/p\/[^/]+\/[0-9]+\/(?:modify)$/,
                        handler : function () {
                           /*
                                ※ fillinメッセージ
                           */
                           fixdap.getScript("/js/jquery/plugins/jquery.fillinMsg.js", function () {
                               setTimeout(function () {
                                   fixdap("#title").fillinMsg("タイトルを入力");
                               }, 1);
                           });
                           /*
                                ※ 文字カウント
                           */
                           fixdap.getScript("/js/jquery/plugins/jquery.signalInterface.js", function () {
                               setTimeout(function () {
                                  fixdap("input#title").dispatchSignal({
                                       max : 200
                                  });
                               }, 1);
                           });
                           /*

                           */
                           var selects = fixdap("td#dead_line_zone select");
                           fixdap("input#undetermined").bind("change", function () {
                               selects.attr("disabled", this.checked);
                           }).attr("checked") && selects.attr("disabled", true);
                        }
                     },
                     {
                        regex   : /^\/p\/[^/]+\/task\/(?:create)$/,
                        handler : function () {
                           /*
                                ※ fillinメッセージ
                           */
                           fixdap.getScript("/js/jquery/plugins/jquery.fillinMsg.js", function () {
                               setTimeout(function () {
                                   fixdap("#title").fillinMsg("タイトルを入力");
                                   fixdap("#body").fillinMsg("本文を入力");
                               }, 1);
                           });
                           /*
                                ※ signalInterface
                           */
                           fixdap.getScript("/js/jquery/plugins/jquery.signalInterface.js", function () {
                               setTimeout(function () {
                                  fixdap("input.char-check-target[type='text']").dispatchSignal({
                                       max : 200
                                  });
                                  fixdap("#body").dispatchSignal({
                                       max : 15000
                                  });
                               }, 1);
                           });
                           /*
                               ※ 添付ファイルのトグル
                           */
                           fixdap("#toggle_file_box").one("click", function () {
                                   fixdap(".inputfile").removeClass("hidelist");
                                   fixdap( this ).remove();
                                   fixdap(".inputfile input[type='file']").get(0).focus();
                           });
                           /*

                           */
                           var selects = fixdap("td#dead_line_zone select");
                           fixdap("input#undetermined").bind("change", function () {
                               selects.attr("disabled", this.checked);
                           }).attr("checked") && selects.attr("disabled", true);
                        },
                        summary : "タスクの新規作成"
                     },
                     {
                        regex : /^\/register\/(?:step1)$/,
                        handler : function () {
                             fixdap.getScript("/js/library/tmpRoundCorner.js", function () {
                                 setTimeout(function () {
                                     fixdap.extend(fixdap.check_id_setting, {
                                           target_obj : "id-check",
                                           target_val : "fixdap_id",
                                           target_api : "/internal_api/available_id/"
                                     });
                                     fixdap("#id-check").mouseup( fixdap.check_id ).keydown(function (event) {
                                             var kc = event.which || event.keyCode;
                                             if ( kc == 13 )
                                                 return fixdap.check_id.call( this );
                                     }).blur(function () {
                                             fixdap("#bubble-box").hide();
                                     });
                                 }, 1);
                             });
                        }
                     },
                     {
                        regex : /^(?:\/login\/)$/,
                        handler : function () {
                                /*
                                    form-focus
                                */
                                var fields = fixdap("#fixdap_id, #password").filter(function () {
                                    return fixdap( this ).val() == "";
                                });
                                fields.length ? fields[0].focus() : fixdap("#password").focus();
                        }
                     },
                     {
                        regex : /^(?:\/register\/)$/,
                        handler : function () {
                                /*
                                      form-focus
                                */
                                fixdap("#email").focus();
                        }
                     },
                     {
                        regex : /^(?:\/reminder\/)$/,
                        handler : function () {
                                /*
                                      form-focus
                                */
                                fixdap("#email").focus();
                        }
                     }
              ], function (page) {
                     return page.regex.test( location.pathname );
              }).shift() || {
                     handler : function () {},
                     summary : ""
              };
    page.handler.apply(this);
});