/**
 * common.js
 *
 * @pachage   moyorieki
 * @see       jquery-1.4.2.min.js
 * @copyright 2006-2010 Infinite Points Co., Ltd.
 */

/**
 *
 *
 */
jQuery(function($){

    $("#test").click(function(){
        alert('test');
        return false;
    });
    // Under construction
    $('input[id^="under_construction"]').click(function(){
        alert('Under construction !!');
    });

    // return to top
    $('a[id^="link_to_top"]').click(function () {
        $(this).blur();
        $('html,body').animate({ scrollTop: 0 }, 'slow');
        return false;
    });

    // return to bottom
    $('a[class^="link_to_bottom"]').click(function () {
        $(this).blur();
        var bottom = $('#footer').offset().top;
        $('html,body').animate({ scrollTop: bottom }, 'slow');
        return false;
    });
    
    // show_shop_xxx
    $('input[id^="show_shop_"]').click(function(){
        var idName = $(this).attr('id');
        var ar = idName.split('_');
        var s_id = ar[2];
        //alert(s_id);
        window.open(shop_path + s_id, '_blank');
    });
    // show_demo_shop_xxx
    $('input[id^="show_demo_shop_"]').click(function(){
        var idName = $(this).attr('id');
        var ar = idName.split('_');
        var s_id = ar[3];
        //alert('Under construction !! id:' + s_id);
        window.open(demo_shop_path + s_id + '/' + demo_token, '_blank');
    });

    // show_demo_inputshop__xxx
    $('input[id^="show_demo_inputshop_"]').click(function(){
        var idName = $(this).attr('id');
        var ar = idName.split('_');
        var s_id = ar[3];
        var type = ar[4];
        //alert(s_id);
        window.open(demo_inputshop_path + type + '/' + s_id + '/' + demo_token, '_blank');
    });

    // show_demo_listshop
    $('input[id^="show_demo_listshop"]').click(function(){
        window.open(demo_listshop_path, '_blank');
    });

    // show_demo_pushteacher
    $('input[id^="show_demo_pushteacher"]').click(function(){
        window.open(demo_pushteacher_path, '_blank');
    });

    // show_demo_pr_index
    $('input[id^="show_demo_pr_index"]').click(function(){
        window.open(demo_pr_index_path, '_blank');
    });

    // show_demo_pr_station
    $('input[id^="show_demo_pr_station"]').click(function(){
        window.open(demo_pr_station_path, '_blank');
    });

    // show_station_xxx
    $('input[id^="show_station_"]').click(function(){
        var idName = $(this).attr('id');
        var ar = idName.split('_');
        var area_code = ar[2];
        //alert(s_id);
        window.open(station_path + area_code, '_blank');
    });

    // show_demo_inputstation__xxx
    $('input[id^="show_demo_inputstation_"]').click(function(){
        var idName = $(this).attr('id');
        var ar = idName.split('_');
        var area_code = ar[3];
        //alert(s_id);
        window.open(demo_inputstation_path + area_code + '/' + demo_token, '_blank');
    });
    
    // show_demo_shop_xxx
    $('input[id^="show_demo_care_menu_"]').click(function(){
        var idName = $(this).attr('id');
        var ar = idName.split('_');
        var s_id = ar[4];
        //alert('Under construction !! id:' + s_id);
        window.open(demo_care_menu_path + s_id + '/' + demo_token, '_blank');
    });

    // close button
    $('input[id^="close_"]').click(function(){
        window.close();
    });

    // admin_care_menu_guide_preview
    $('input[id^="admin_care_menu_guide_preview"]').click(function(){
        window.open(demo_care_menu_guide_preview_path, '_blank');
    });
    // admin_care_menu_profile_preview
    $('input[id^="admin_care_menu_profile_preview"]').click(function(){
        window.open(demo_care_menu_profile_preview_path, '_blank');
    });
    // admin_other_area_preview 2011.10.17
    $('input[id^="admin_other_area_preview"]').click(function(){
        window.open(demo_other_area_preview_path, '_blank');
    });

    // IE6で接続された場合に表示 2010.08.17
    $(window).load(function(){
        if (typeof document.documentElement.style.maxHeight == "undefined") {
            if(navigator.userAgent.indexOf("MSIE 6.") != -1){
                var $ie6Html  = '<div class="attention_txt getie clearfix">' + "\n";
                    $ie6Html += '<div class="txt">' + "\n";
                    $ie6Html += 'WindowsはInternetExplorer7.0以上、Firefox3.5以上のブラウザでのご利用をお勧めします。それ以外の<br />ブラウザでご利用いただく場合、サイトの一部が正常に表示できないことがございます。最新のブラウザを<br />ダウンロードしてご利用ください。' + "\n";
                    $ie6Html += '</div>' + "\n";
                    $ie6Html += '<div class="bnr">' + "\n";
                    $ie6Html += '<a href="http://www.microsoft.com/japan/windows/internet-explorer/worldwide-sites.aspx" target="_blank"><img src="http://www.microsoft.com/library/media/1041/japan/windows/images/internet-explorer/logo-downloads/IE8-Download_120x54.jpg" width="120" height="54" alt="Internet Explorer 8 をダウンロード"></a>' + "\n";
                    $ie6Html += '</div>' + "\n";
                    $ie6Html += '</div>' + "\n";
                $("#top-ie6html").html($ie6Html);
            }
        }
        return false;
    });

});


/**
 *
 *
 */
jQuery.fn.extend({

    doSubmit: function(formId, act) {
        var form = $("#" + formId);
        var old_act = form.attr("action");
        form.attr("action", form.attr("action") + act);
        form.submit();
        form.attr("action", old_act);   //actionを元に戻す(IEでは問題ないがFirefoxなどの場合はブラウザで戻った場合にactionが残ったままになるので)
        //submitかimageボタンで呼ばれた場合に、2回submitが行われる可能性があったので、ここでsubmitの処理をキャンセルする。
        if ($(this).attr("type") == 'submit' || $(this).attr("type") == 'image') {
            $("form").one("submit", function(event){
                event.preventDefault();
            });
        }
        return this;
    },

    doLocation: function(formId, act) {
        var form = $("#" + formId);
        var old_act = form.attr("action");
        form.attr("action", form.attr("action") + act);
        location.href = form.attr("action"); 
        form.attr("action", old_act);   //actionを元に戻す(IEでは問題ないがFirefoxなどの場合はブラウザで戻った場合にactionが残ったままになるので)
        //submitかimageボタンで呼ばれた場合に、2回submitが行われる可能性があったので、ここでsubmitの処理をキャンセルする。
        if ($(this).attr("type") == 'submit' || $(this).attr("type") == 'image') {
            $("form").one("submit", function(event){
                event.preventDefault();
            });
        }
        return this;
    },
    
    rewriteActionSubmit: function(formId, act) {
        var form = $("#" + formId);
        var old_act = form.attr("action");
        form.attr("action", act);
        form.submit();
        form.attr("action", old_act);   //actionを元に戻す(IEでは問題ないがFirefoxなどの場合はブラウザで戻った場合にactionが残ったままになるので)
        //submitかimageボタンで呼ばれた場合に、2回submitが行われる可能性があったので、ここでsubmitの処理をキャンセルする。
        if ($(this).attr("type") == 'submit' || $(this).attr("type") == 'image') {
            $("form").one("submit", function(event){
                event.preventDefault();
            });
        }
        return this;
    },
    
    oneTimeSubmit: function(formId, act) {
        this.doSubmit(formId, act);
        
        $(this).attr("disabled", "disabled");

        //setting for button background
        var regex = new RegExp("background-image:\\s.+url", "i");
        var style = $(this).attr("style");
        if (style == null ) {
            style = "";
        }
        if (style.search(regex)){
            $(this).css({opacity: "0.4"});
            $(this).css({cursor: "default"});
        }
        
        return this;
    },

    newWindowSubmit: function(formId, act) {
        var form = $("#" + formId);
        var old_act = form.attr("action");
        window.open('','_preview');
        form.attr("action", act);
        form.attr("target", '_preview');
        form.submit();
        form.attr("target", '');
        form.attr("action", old_act);   //actionを元に戻す(IEでは問題ないがFirefoxなどの場合はブラウザで戻った場合にactionが残ったままになるので)
        //submitかimageボタンで呼ばれた場合に、2回submitが行われる可能性があったので、ここでsubmitの処理をキャンセルする。
        if ($(this).attr("type") == 'submit' || $(this).attr("type") == 'image') {
            $("form").one("submit", function(event){
                event.preventDefault();
            });
        }
        return this;
    },
    
    area_station_ajax: function(now_min_code, area_minor_code_id, area_code_id) {

        var area_minor_code = $(area_minor_code_id).val();
        if (area_minor_code == undefined) {
            return null;
        }

        $(area_code_id).children().remove();

        if (area_minor_code == "") {
            var opt = $("<option>").html('---').attr({ value: ''});
            $(area_code_id).append(opt);
            return null;
        }
        //alert(area_minor_code);

        $.ajax({
            type: "POST",
            data: "am_code=" + area_minor_code,
            url: base_url + "ajax/selectstation",
            error: function(msg){ alert('ajax_error'); },
            dataType: "json",
            success: function(msg){
                //alert(msg);
                var opt = $("<option>").html('---').attr({ value: ''});
                $(area_code_id).append(opt);

                $.each(msg, function(i) {
                    var val = this;
                    if (now_min_code == val.area_code) {
                        var opt = $("<option>").html(val.area_station_name).attr({ value: val.area_code, selected: 'selected'});
                    } else {
                        var opt = $("<option>").html(val.area_station_name).attr({ value: val.area_code});
                    }
                    $(area_code_id).append(opt);
                });
            }
        });
    },

    // 最寄り駅ドクター用駅名プルダウン 2010.09.28
    area_station_clinic_ajax: function(now_min_code, area_minor_code_id, area_code_id) {

        var area_minor_code = $(area_minor_code_id).val();
        if (area_minor_code == undefined) {
            return null;
        }

        $(area_code_id).children().remove();

        if (area_minor_code == "") {
            var opt = $("<option>").html('---').attr({ value: ''});
            $(area_code_id).append(opt);
            return null;
        }
        //alert(area_minor_code);

        $.ajax({
            type: "POST",
            data: "am_code=" + area_minor_code,
            url: base_url + "ajax/selectstation",
            error: function(msg){ alert('ajax_error'); },
            dataType: "json",
            success: function(msg){
                //alert(msg);
                var opt = "";
                $(area_code_id).append(opt);

                $.each(msg, function(i) {
                    var val = this;
                    if (now_min_code == val.area_code) {
                        var opt = $("<option>").html(val.area_station_name).attr({ value: val.area_code, selected: 'selected'});
                    } else {
                        var opt = $("<option>").html(val.area_station_name).attr({ value: val.area_code});
                    }
                    $(area_code_id).append(opt);
                });
            }
        });
    },

    htmlspecialchars: function(txt) {
        txt = txt.replace(/&/g, "&amp;");
        txt = txt.replace(/"/g, "&quot;");
        txt = txt.replace(/'/g, "&quot;");
        txt = txt.replace(/</g, "&lt;");
        txt = txt.replace(/>/g, "&gt;");
        return txt;
    },

    nl2br: function(txt) {
        txt = txt.replace(/\r\n/g, "<br />");
        txt = txt.replace(/(\n|\r)/g, "<br />");
        return txt;
    }

});


