/*
 * J-Best JavaScript Library
 * copyright 2008 RCS
 */



//全角英数字を半角英数字に変換
String.prototype.zen2han = function () {
    var work = '';
    for (lp = 0;lp < this.length; lp++) {
        unicode = this.charCodeAt(lp);
        if ((0xff00 < unicode) && (unicode < 0xff1a)) {
            work += String.fromCharCode(unicode - 0xfee0);
        } else if ((0xff20 < unicode) && (unicode < 0xff3b)) {
            work += String.fromCharCode(unicode - 0xfee0);
        } else if ((0xff40 < unicode) && (unicode < 0xff5b)) {
            work += String.fromCharCode(unicode - 0xfee0);
        } else {
            work += String.fromCharCode(unicode);
        }
    }
    return work;
}

//全角ひらがなを全角カタカナに変換
String.prototype.hira2kata = function () {
    var work = '';
    for (lp = 0;lp < this.length; lp++) {
        unicode = this.charCodeAt(lp);
        if ((0x3041 <= unicode) && (unicode <= 0x3093)) {
            work += String.fromCharCode(unicode + 0x60);
        } else {
            work += String.fromCharCode(unicode);
        }
    }
    return work;
}


jQuery.fn.extend({
    /**
     * jQuery CheckAll plugin
     * すべてのチェックボックスがONの場合、全チェックボックスをOFFにする。
     * それ以外は全チェックボックスをONにする。
     */
	checkAll : function() {
        if (jQuery(this).filter(":checked").length == jQuery(this).length) {
            jQuery(this).removeAttr('checked');
        } else {
            jQuery(this).attr('checked', 'checked');
        }
	},
    /**
     * jQuery fixWith plugin
     * CSS が "display: block;float:left" のような要素の幅を指定した幅の
     * 最小の倍数となるよう調整する。
     */
	fixWidth : function(width) {
        jQuery(this).each(function() {
            var w = jQuery(this).width();
            if ((w % width) != 0) {
    	        w = (Math.floor(w / width)+1) * width;
                jQuery(this).width(w);
    	    }
        });
	}

});

// for jQuery history plugin
function jbest_pageload(hash_orig)
{
    var hash = hash_orig;
    var fn = location.pathname.substr(location.pathname.lastIndexOf('/')+1);
    if (fn == 'mag.html') {
        if (!hash.match(/^mag[efp]?(\/index|\/edit|\/confirm|\/complete)/)) {
    		hash = 'mag/';
    	}
    } else if (fn == 'entry.html') {
        if (!hash.match(/^entry(|\/edit|\/confirm|\/complete)/)) {
    		hash = 'entry/';
    	}
    } else if (!hash.match(/^(area|detail|search|result|news)/)) {
  		hash = 'top/';
    }
    //update element
    if (typeof pageTracker != "undefined") {
        jQuery("#content > .mainbody:first").ajaxSuccess(function(event, request, settings){
            pageTracker._trackPageview(hash_orig);
        });
    }
    jQuery("#content > .mainbody:first").ajaxError(function(event, request, settings){
        jQuery(this).html(request.responseText);
    });
    jQuery("#content > .mainbody:first").load(hash);
}
function jbest_content(html)
{
    jQuery("#content > .mainbody:first").html(html);
}
function jbest_set_rel_history()
{
	jQuery("a[rel='history']").click(function(){
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		jQuery.historyLoad(hash);
		return false;
	});

}
jQuery(document).ready(function(){
	//jQuery history plugin
	jQuery.historyInit(jbest_pageload);
	//jQuery blockUI plugin
    jQuery.blockUI.defaults.css.backgroundColor = '#ffe';
    jQuery.blockUI.defaults.css.border = '1px solid #f00';
    jQuery.blockUI.defaults.css.padding = '1em';

});

