function Glue_Core( debug )
{
	if(debug) this._debug = debug;
}

Glue_Core.prototype =
{
	version:'1.0',
	_debug: false,
	init:function(){},
	getByID:function( element_id )
	{
		return document.getElementById
			? document.getElementById(element_id)
			: document.all[element_id];
	},
	getByTag:function( tag_name, parent )
	{
		if( parent == null ){
			parent = document;
		}

		return parent.getElementsByTagName(tag_name).item(0);
	},
	getByTags:function( tag_name, parent )
	{
		if( parent == null ){
			parent = document;
		}

		return parent.getElementsByTagName(tag_name);
	},
	flipAllListingFlag:function( form_id )
	{
		var f = this.getByID(form_id);
		var flg = f.listing_flag.checked;
		var i = 0;
		while( i < f.elements.length ){
			if( f.elements[i].id.match('flag')){
				f.elements[i].checked = flg;
			}
			i++;
		}

		return;
	},
	deleteSelectedItems:function( form_id, object_label )
	{
		var f = this.getByID(form_id);
		var ids = f.elements["id[]"];
		var sum = 0;
		if(ids.length){
			for( var i = 0; i < ids.length; i++ ){
				if(ids[i].checked){
					sum++;
				}
			}
		}else{
			if(ids.checked){
				sum++;
			}
		}

		if( sum == 0 ){
			alert( this.translate( 'Did not select any [_1] to delete.', object_label ));
			return;
		}

		if( sum > 0 ){
			var msg = ( sum > 1 )
				? this.translate( 'Do you sure you want to delete the selected [_1]?', object_label )
				: this.translate( 'Do you sure you want to delete this [_1]?', object_label );
			if( confirm(msg)){
				f.submit();
			}
		}
	},
	//getByClass: function( class_name, parent )
	//{
	//},
	reset:function( form_id )
	{
		this.getByID(form_id).reset();
	},
	save:function( form_id )
	{
		this.getByID(form_id).submit();
	},
	show:function( element_id, display_type )
	{
		if( display_type == null ){
			display_type = 'block';
		}
		var e = this.getByID(element_id);
		e.style.display = display_type;

		return e;
	},
	hide:function( element_id )
	{
		this.getByID(element_id).style.display = 'none';
	},
	getPageSize:function()
	{
		var x, y;
		if(    window.innerHeight
			&& window.scrollMaxY
		){	
			x = window.innerWidth + window.scrollMaxX;
			y = window.innerHeight + window.scrollMaxY;
		}else if( document.body.scrollHeight > document.body.offsetHeight ){
			// all but Explorer Mac
			x = document.body.scrollWidth;
			y = document.body.scrollHeight;
		}else{
			// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			x = document.body.offsetWidth;
			y = document.body.offsetHeight;
		}

		var ww, wh;
		if(self.innerHeight){
			// all except Explorer
			if(document.documentElement.clientWidth){
				ww = document.documentElement.clientWidth; 
			}else{
				ww = self.innerWidth;
			}
			wh = self.innerHeight;
//		}else if(   document.documentElement
//				 && document.documentElement.clientHeight
//		){
//			// Explorer 6 Strict Mode
//			ww = document.documentElement.clientWidth;
//			wh = document.documentElement.clientHeight;
		}else if(document.body){
			// other Explorers
			ww = document.body.clientWidth;
			wh = document.body.clientHeight;
		}

		var pw, ph;
		if( y < wh ){
			ph = wh;
		}else{ 
			ph = y;
		}

		if( x < ww ){	
			pw = ww; //x;
		}else{
			pw = x; //ww;
			if( y > wh ){
				// スクロールバーの幅を減算
				if(navigator.platform.indexOf("Win") != -1) {
					pw = pw - 16;
				}else{
					pw = pw - 15;
				}
			}
		}

		return new Array( pw, ph, ww, wh );
	},
	getPageScroll:function()
	{
		var x, y;
		if(self.pageYOffset){
			y = self.pageYOffset;
			x = self.pageXOffset;
		}else if(   document.documentElement
				 && document.documentElement.scrollTop
		){
			// Explorer 6 Strict
			y = document.documentElement.scrollTop;
			x = document.documentElement.scrollLeft;
		}else if(document.body){
			// all other Explorers
			y = document.body.scrollTop;
			x = document.body.scrollLeft;
		}

		return new Array( x, y );
	},
	// for Ajax
	getXmlHttpRequest:function()
	{
		var q;
		if(window.XMLHttpRequest){
			q = new XMLHttpRequest();
		// IE
		}else if(window.ActiveXObject){
			try{
				q = new ActiveXObject('Msxml2.XMLHTTP');
			}catch(e){
				q = new ActiveXObject('Microsoft.XMLHTTP');
			}
		}

		return q;
	},
	// for formatter
	getSelected:function( id )
	{
		var e = this.getByID(id);
		if(document.selection){
			e.focus();
			var range = document.selection.createRange();

			return range.text;
		}else{
			var len = e.textLength;
			var start = e.selectionStart;
			var end = e.selectionEnd;
			if( end == 1 || end == 2 && len != undefined ){
				end = len;
			}

			return e.value.substring( start, end );
		}
	},
	setSelection:function( id, val )
	{
		var e = this.getByID(id);
		if(document.selection){
			e.focus();
			var range = document.selection.createRange();
			range.text = val;
			range.select();
		}else{
			var top = e.scrollTop;
			var len = e.textLength;
			var start = e.selectionStart;
			var end = e.selectionEnd;
			if( end == 1 || end == 2 && len != undefined ){
				end = len;
			}
			e.value = e.value.substring( 0, start ) + val + e.value.substr( end, len );
			e.selectionStart = start + val.length;
			e.selectionEnd = start + val.length;
			e.scrollTop = top;
		}
		e.focus();
	},
	encloseTag:function( id, tag, attr, val )
	{
		var str = this.getSelected(id);
		var html  = '<'+ tag;
		if( attr != null ){
			html += ' '+ attr +'="'+ val +'"';
		}
		html += '>'+ str +'</'+ tag +'>';
		this.setSelection( id, html );

		return;
	},
	encloseList:function( id, tag, attr, val )
	{
		var str = this.getSelected(id);
		var reg = new RegExp( "\\n", "gm" );
		str = str.replace( reg, "</li>\n<li>");
		var html = '<'+ tag;
		if( attr != null ){
			html += ' '+ attr +'="'+ val +'"';
		}
		html += '>'+"\n"+'<li>'+ str +'</li>'+"\n"+'</'+ tag +'>'+"\n";
		this.setSelection( id, html );

		return;
	},
	encloseLink:function( id, is_mail )
	{
		var str = this.getSelected(id);
		var link = '';
		if(!is_mail){
			if( str.match(/^https?:/)){
				link = str;
			}else if( str.match(/^(\w+\.)+\w{2,5}\/?/)){
				link = 'http://'+ str;
			}else{
				link = 'http://';
			}
		}else{
			if( str.match(/@/)){
				link = str;
			}
		}
		var msg = is_mail
			? this.translate('Enter email address:')
			: this.translate('Enter URL:');
		var link_str = prompt( msg, link );
		if( link_str != null ){
			if( str == '' ){
				str = link_str;
			}
			if(is_mail){
				link_str = 'mailto:'+ link_str;
			}
			this.setSelection( id, '<a title="'+ str +'" href="'+ link_str +'">'+ str +'</a>' );
		}

		return;
	},
	translate:function( phrase, args )
	{
		phrase = lexicon[phrase];
		if(args){
			if( typeof(args) != 'Array' ){
				var str = args;
				args = new Array(str);
			}
			var i = 0;
			while( i < args.length ){
				//var lphrase = this.translate(args[i]);
				phrase = phrase.replace( new RegExp( '\\[_' + (i+1) + '(?:\:[^\\]]+)?\\]', 'g' ), args[i] );
				i++;
			}
		}

		return phrase;
	}
}

function initCore()
{
	glue = new Glue_Core();
	glue.init();
}
initCore();
//Event.observe( window, 'load', initCore, false );

Array.prototype.shuffle = function()
{
	var i = this.length;
	while(i){
		var j = Math.floor( Math.random() * i );
		var t = this[--i];
		this[i] = this[j];
		this[j] = t;
	}

	return this;
}
Array.prototype.clone = function()
{
	return Array.apply( null, this )
}


/*
function reset( form_id )
{
	glue.getByID(form_id).reset();
}

function toggle( element_id )
{
	var e = glue.getByID(element_id);
	if( e.style.display == 'block' ){
		hide(element_id);
	}else{
		show(element_id);
	}
}
function show( element_id )
{
	glue.getByID(element_id).style.display = 'block';
}

function hide( element_id )
{
	glue.getByID(element_id).style.display = 'none';
}*/