$(document).ready(function() {
    fixHtml();
    bindEvent();
    effect();
    fixCss();
    adsense();
    addClips();
});



function fixHtml() {
    // Fix abbr elements
    if ($.browser.msie && typeof document.documentElement.style.msInterpolationMode == "undefined") {
        var obj = document.getElementsByTagName('abbr');

        for (var i = 0; i < obj.length; i ++) {
            var objOld = obj.item(i);
            var objNew = document.createElement('abbr');

            objNew.title = objOld.title;
            objOld.parentNode.insertBefore(objNew, objOld);

            while (objOld.nextSibling.nodeName != '/ABBR') {
                objNew.appendChild(objOld.nextSibling);
            }

            objOld.parentNode.removeChild(objOld.nextSibling);
            objOld.parentNode.removeChild(objOld);
        }
    }
}



function bindEvent() {
    var e;



    // Clear contents of textarea
    if ((e = $('textarea')).length > 0) {
        e.bind('focus', function() {
            var t;

            (t = this).def_value || (t.def_value = t.value);
            t.def_value == t.value && (t.value = '');
        });
    }



    // Contact form with AJAX
    var option = {
        beforeSubmit: formValidate,
        success: formSubmit
    }

    $('#Contact').ajaxForm(option);
}



function effect() {
/*
    // Tooltip
    $('a[@title], abbr[@title]').ToolTip({
        position: 'mouse',
        delay: 200
    });
*/


    if ((tabContent = $('div.amazon>div.section')).length > 0) {
        var tabNavi = 'div.amazon>ul.navigation.section';

        tabContent.addClass('tab').hide();
        $(tabNavi).addClass('tab');

        $(tabNavi + '>li:first-child').addClass('current');
        $(tabContent.get(0)).addClass('current');
        $(tabContent.get(0)).show();

        $(tabNavi + '>li>a').click(function() {
            var target = $(this).attr('href');

            $(tabNavi + '>li').removeClass('current');
            tabContent.hide();
            tabContent.removeClass('current');

            $(this).parent('li').addClass('current');
            $(target).addClass('current');
            $(target).show();

            return false;
        });
    }

/*
    $('body.resource.modx.resource.html div.page>p+div.section').before('<p>見出しをクリックすると内容が表示されます。</p>');
    $('body.resource.modx.resource.html div.page>div.section>div.section').hide();
    $('body.resource.modx.resource.html div.page>div.section>h2').click(function(){
        $(this).parent().children('div.section').slideToggle('fast');

        return false;
    });
*/



    // Smooth scroll
//    $('a:not(.tab li a)').ScrollToAnchors(250, 'easeout');
    $('.navigation.speech:not(.tab) a, .navigation.page:not(.tab) a, .navigation.section:not(.tab) a').click(function(){
        $($(this).attr('href')).ScrollTo(250, 'easeout');

 　　   return false;
    });
}



function fixCss() {
    if ($.browser.msie) {
        // *[accesskey]
        $('a[@accesskey],label[@accesskey]').each(function() {
            $(this).before('<kbd class="key">' + $(this).attr('accesskey') + '</kbd>');
        });

        // q
        $('q').each(function() {
            $(this).before('“');
            $(this).after('”');
        });

        // span.note
        $('span.note').each(function() {
            $(this).prepend('(注: ');
        });
        $('span.note').each(function() {
            $(this).append(')');
        });
    }



/*
    // pre
    if ($.browser.msie) {
        $('pre').css('word-wrap', 'break-word');
    } else {
        $('pre').css('white-space', '-moz-pre-wrap');
        $('pre').css('white-space', '-pre-wrap');
        $('pre').css('white-space', '-o-pre-wrap');
        $('pre').css('white-space', 'pre-wrap');
    }

    $('pre').css('overflow', 'visible');
*/



    // table
    $('tbody tr:nth-child(even)').addClass('even');

    if ((t = $('table')).length > 0) {
        t.tableHover({
            rowClass: 'hover',
            colClass: 'hover',
            headRows: true,  
            footRows: true,
            headCols: true,
            footCols: true
        });
    }



    // form dl dd:last-child
    $('form dl dd:last-child').addClass('last-child');



    // Tooltip
    $('#tooltipHelper').css('-moz-opacity', '0.80');
}



function adsense() {
    var e;

    if ((e = $('object[@data*="adsense.html"]')).length > 0) {
        e.wrap('<div class="adsense"></div>');
	}

    if ((e = $('iframe[@name="google_ads_frame"]')).length > 0) {
        e.wrap('<div class="adsense"></div>');
    }
}



function addClips() {
    $('div.page>ul.navigation.page').prepend('<li class="bookmark"><a href="http://www.addclips.org/addclips.php" onclick="AddClipsId = \'265E0FF05B025\'; AddClipsUrl = location.href; AddClipsTitle = document.title; return AddClipsWindow(this);"><img src="http://img.addclips.org/addclips_2.gif" width="96" height="16" alt="このページをブックマーク" /></a></li>');
}



function validator() {
    this.initialize.apply(this, arguments);
}



validator.prototype = {
    initialize: function(p) {
        this._form = p.form;
        this._name = p.name;
        this._message = p.message || p.name + 'を入力してください';
        this._classError = p.classError || 'error';
        this._rules = p.rules;
        this._isError = false;
    },
    isError: function() {
        return this._isError;
    },
    validate: function() {
        this._isError = false;

        if (this._form) {
            var form = '#' + this._form;
        } else {
            var form = '';
        }

        var obj = $('form' + form + ' *[@name="' + this._name + '"]');

        if (this._rules.required) {
            if (!$.trim(obj.val())) {
                this._isError = true;
            }
        }

        if (this._rules.email) {
            if ($.trim(obj.val()).search(/^[\w\d][\w\d\,\.\-]*\@([\w\d\-]+\.)+([a-zA-Z]+)$/) == -1) {
                this._isError = true;
            }
        }

        if (this._rules.eq) {
            if (obj.val() != this._rules.eq) {
                this._isError = true;
            }
        }

        if (this._rules.ne) {
            if (obj.val() == this._rules.ne) {
                this._isError = true;
            }
        }

        if (this._isError) {
            obj.addClass(this._classError);
            html = '<label for="' + obj.attr('id') + '" class="' + this._classError + '">' + this._message + '</label>';

            if (obj.next('label').length == 0) {
                $(html).insertAfter(obj);
            }
        } else {
            obj.removeClass(this._classError);
            obj.next('label').remove();
        }
    }
};



function formValidate(formData, jqForm, options) {
    var name = new validator({
        name: 'name',
        message: 'お名前を入力してください。',
        rules: {
            required: true
        }
    });

    var email = new validator({
        name: 'email',
        message: 'メールアドレスを正しく入力してください。',
        rules: {
            required: true,
            email: true
        }
    });

    var message = new validator({
        name: 'message',
        message: 'メッセージを入力してください。',
        rules: {
            required: true,
            ne: 'お問い合わせ内容をご記入ください。'
        }
    });

    name.validate();
    email.validate();
    message.validate();

    if (name.isError() || email.isError() || message.isError()) {
        return false;
    }

    return true;
}



function formSubmit(responseText, statusText) {
    alert("お問い合わせ内容を送信しました。");

    $('#Contact input[@type="text"]').each(function() {
        $(this).val('');
    });

    $('#Message').val('お問い合わせ内容をご記入ください。');
}
