/* 
	Object Functions
*/
var objectFunctions = {

	keys: function( obj )
	{
		var tempArr = new Array(), obj = obj || this;
		for( var k in obj ) { tempArr.push( k ); }
		return tempArr; 
	},

	items: function( obj )
	{
		var tempArr = new Array(), obj = obj || this;
		for( var p in obj ) { tempArr.push( obj[p] ); }
		return tempArr; 
	},

	getType: function( obj )
	{
		var isType = null;
		
		if( !isNaN( new Number( obj ) ) ){ isType = 'number'; }

		if( (obj.length != undefined) && (obj.substring != undefined) ){ isType = 'string'; }

		if( (obj.length != undefined) && (obj.sort != undefined) && (obj.pop != undefined) ){ isType = 'array'; }

		if( (obj.nodeType != undefined) && (obj.appendChild != undefined) ){ isType = 'element'; }

		if( (obj.length != undefined) && (obj.call != undefined) && (obj.apply != undefined) ){ isType = 'function'; }

		if( (obj.getDate != undefined) && (obj.getFullYear != undefined) ) { isType = 'date'; }

		if( (obj.exec != undefined) && (obj.test != undefined) ) { isType = 'regexp'; }

		if( (obj != undefined) && (isType == null) ){ isType = 'object'; }
		
		return isType;
	},
	
	each: function( elt, func, param )
	{
		var typeObj = XMUtil.getType( elt );
		
		if( (elt.length != undefined) && (elt.length > 0) ) {
			for( var x=0, curElmt; curElmt = elt[x]; x++ ){ func( curElmt, param ); }
		}
		else {
			for( var p in elt ){ func( elt[p], p, param ); }
		}
	}
}

var XMUtil = XMUtil || new function XMUtil(){};

XMUtil.inherit = function()
{
	var toThisObj = ( arguments.length > 1 ) ? arguments[0] : this, args = ( arguments.length <= 1 ) ? 0 : 1;
	for(var j = args, numOfArgs = arguments.length; j < numOfArgs; j++)
	{
		var fromThisObj = arguments[j];
		for( var i in fromThisObj )
			if( fromThisObj[i] != 'undefined' ){ toThisObj[i] = fromThisObj[i]; }
	}
};

XMUtil.inherit( XMUtil, objectFunctions );
XMUtil.inherit( Object.prototype, objectFunctions);

XMUtil.inherit( 
	/*  *
	Element Functions
	**/
{
	fetch: function( elt, tag, context )
	{
		if( elt.indexOf( '#' ) > -1 ) {
			return document.getElementById( elt.substring( 1 ) );
		}
		else if( elt.indexOf( '.' ) > -1 ) {
		var className = elt.substring( 1 ), context = context || document, classArr = [], tagList = context.getElementsByTagName( tag );
		for( var i=0; curNode = tagList[i]; i++ )
		{
			if( className.test( curNode.className ) )
				classArr.push(curNode);
		}
			return ( classArr.length > 0 ? classArr : null );
		}
		else{ 
			return document.getElementsByTagName( elt );
		}
	},
	
	construct: function( tag, attribs, styles )
	{
		var elt = document.createElement( tag );
		for( var a in attribs ){ elt[a] = attribs[a]; }
		for( var s in styles){ elt.style[s] = styles[s]; } 
	},
	
	elementWeaver : function( xmlNode, option ) //xmlNode: root Node, option -> { container,  clearContainer, shiftToNode }
	{
		var curXMLTag;
		
		if( option.clearContainer )
			{ removeChildNodes( option.container ); }
			
		if( typeof xmlNode == 'string' )
		{
			if( xmlNode.indexOf('</') > -1 || xmlNode.indexOf('/>') > -1 )
			{
				xmlNode = this.convertToFile( xmlNode, 'text/xml' );
				this.elementWeaver(xmlNode.documentElement, { container: option.container, shiftToNode: option.shiftToNode } );
			}
			else
				{ option.container.appendChild( document.createTextNode( xmlNode ) ); }
		}
		else
		{
			if( option.shiftToNode ) 
			{
				if( option.shiftToNode == 'child' )
					curXMLTag = xmlNode.firstChild;
				else if( option.shiftToNode == 'sibling' )
					curXMLTag = xmlNode.nextSibling;
					
				while( curXMLTag && curXMLTag.nodeType != 1 )
				{
					if( curXMLTag.nodeType == 3 )
					{
						if( curXMLTag.nodeValue != 'undefined' ) //appends text nodes
							option.container.appendChild( document.createTextNode( curXMLTag.nodeValue ) );
					}
					else if( curXMLTag.nodeType == 8 ) // appends comment nodes
					{ option.container.appendChild( document.createComment( curXMLTag.nodeValue ) ); }
					
					curXMLTag = curXMLTag.nextSibling;
				}	
			}
			else
				{ curXMLTag = xmlNode; }

			if( curXMLTag )
			{
				var curElement = document.createElement( curXMLTag.nodeName );
				this.copyNodeAttributes( curElement, curXMLTag );
				option.container.appendChild( curElement );

				if( curXMLTag.hasChildNodes() )
					{ this.elementWeaver( curXMLTag, {container:curElement, shiftToNode:'child'} ) }; 

				if( curXMLTag.nextSibling != null )
					this.elementWeaver( curXMLTag, {container:curElement.parentNode, shiftToNode:'sibling'} );
			}
		}
	},

	getNextElement: function( nodeElt )
	{
		if( nodeElt.nodeType == 1 ) { nodeElt = nodeElt.nextSibling; }
		// loop until next element node is found or null is returned
		while( nodeElt && (nodeElt.nodeType != 1) )
		{ 
			nodeElt = nodeElt.nextSibling; 
		}
		
		if( nodeElt && (nodeElt.nodeType == 1) ) { return nodeElt; }	
	},

	getTextNodeValue: function( nodeElt )
	{
		if( nodeElt.nodeType == 3 ) { nodeElt = nodeElt.nextSibling; }
		// loop until next text node is found or null is returned
		while( nodeElt && (nodeElt.nodeType != 3) )
		{
			nodeElt = nodeElt.nextSibling;
		}
		
		if( nodeElt && (nodeElt.nodeType == 3) ) { return nodeElt; }	
	},

	getNextNode: function( nodeElt, nodeType, shiftToChild ) // 1: element node - 2: text node - 8: comment node
	{
		if( shiftToChild ){ nodeElt = nodeElt.firstChild; }
		if( nodeElt.nodeType == nodeType ) { nodeElt = nodeElt.nextSibling; }
		// loop until next node is found or null is returned
		while( nodeElt && (nodeElt.nodeType != nodeType) )
		{
			nodeElt = nodeElt.nextSibling;
		}
		
		if( nodeElt && (nodeElt.nodeType == nodeType) ) { return nodeElt; }	
	},
	
	fetchChildNodes: function( parentElt, eltType )
	{
		var curElt = parentElt.firstChild, tempArr = new Array();
		
		while( curElt )
		{
			if( curElt.nodeType == eltType )
			{
				tempArr.push( curElt );
			}
			curElt = curElt.nextSibling;
		}
		
		return tempArr;
	},

	setObjectToNodeAttr: function( nodeElt, obj, propName )
	{
		if( propName ) {
			if( obj[propName] ) {
				var attr = document.createAttribute( propName );
				attr.value = obj[propName];
				nodeElt.setAttributeNode( attr );
			}
		}
		else
		{
			for( var i in obj ) {
				var attr = document.createAttribute(i);
				attr.value = obj[i];
				nodeElt.setAttributeNode( attr );
			}
		}
	},

	setNodesToObject: function( nodeElt, attrName )
	{
		var tempObj = new Object();
		while( nodeElt )
		{
			tempObj[nodeElt.getAttribute( attrName )] = this.tag_innerText( nodeElt );
			nodeElt = this.getXMLElement( nodeElt );
		}
		return tempObj;
	},
	
	childNodeValueByAttr: function( nodeElt, AttrName, AttrValue )
	{
		nodeElt = this.getXMLElement( nodeElt.firstChild );
		while( nodeElt ) 
		{
			if( nodeElt.getAttribute( AttrName ) == AttrValue ) { return getTextNodeValue( nodeElt.firstChild ); }
			nodeElt = this.getXMLElement( nodeElt );
		}
	},
	
	copyNodeAttributes: function( htmlElt, xmlElt, attrName )
	{
		if( attrName ) {
			var attr = document.createAttribute( attrName );
			attr.value = xmlEL.getAttributeNode( attrName ).value;;
			htmlEL.setAttributeNode( attr );
		}
		else
		{		
			for( var j=0, attrLength=xmlElt.attributes.length; j < attrLength; j++ ) { 
				var attr = document.createAttribute( xmlElt.attributes.item(j).nodeName ); 
				attr.value = xmlElt.attributes.item(j).nodeValue;
				htmlElt.setAttributeNode( attr );
			}
		}
	},
	
	tag_innerText: function( nodeElt )
	{
		return this.getTextNodeValue( nodeElt.firstChild );
	},
	
	removeChildNodes: function( nodeElt )
	{
		while( nodeElt.firstChild ){ nodeElt.removeChild( nodeElt.firstChild ); }
	},
	
	detachNode: function( nodeElt )
	{
		nodeElt.parentNode.removeChild( nodeElt );
	},
	
	removeTheseNode: function( nodeElt, nodeTypeName )
	{
		if( nodeTypeName == 'element node' ){ var nodeTypeNum = 1; }
		if( nodeTypeName == 'text node' ){ var nodeTypeNum = 3; }
		if( nodeTypeName == 'comment node' ){ var nodeTypeNum = 8; }
		
		nodeElt = nodeElt.firstChild;
		
		while( nodeElt )
		{
			if( nodeElt.nodeType == nodeTypeNum ) { nodeElt.parentNode.removeChild( nodeElt ); }
			nodeElt = nodeElt.nextSibling;
		}
	}
} );

XMUtil.inherit(
{
	/*
		Ajax Function
	*/
	Ajax: function ( requestObj ) // requestObj -> async, method, url, infoToSend, callback(), cbParams:{}
	{
		var request = this.getXMLHttpObject();
		var async = requestObj.async ? requestObj.async : true; 
		var method = requestObj.method ? requestObj.method : 'GET';
		var url = requestObj.url ? requestObj.url : '';
		var infoToSend = requestObj.infoToSend ? requestObj.infoToSend : "";
		
		function stateChange()
		{
			var xmlDoc;
			
			if( request.readyState == 4 )
			{
				xmlDoc = request.responseXML.documentElement;
				
				if( requestObj.callback )
				{
					if( requestObj.cbParams )
						requestObj.callback( xmlDoc, requestObj.cbParams );
					else
						requestObj.callback( xmlDoc );
				}
			}
		}
		
		request.onreadystatechange = stateChange;
		request.open( method, url, async );
		request.send( infoToSend );
	},

	getXMLHttpObject: function()
	{
		var xmlHttp = null;
		try {
			xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
		}
		catch(e) {
			try
			{ xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
			catch(e)					// Internet Explorer
			{ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
		}
		return xmlHttp;
	}
} );

XMUtil.inherit(
{
	convertToFile: function( domString, contentType )
	{
		try{
			var parser = new DOMParser();
			return parser.parseFromString( domString, contentType );
		}
		catch(e)
		{
			try{
				var xmldata = new ActiveXObject('MSXML.DomDocument');
				xmldata.async = false;
				xmldata.loadXML(domString);
				return xmldata;
			}
			catch(e)
			{
				var xmldata = new XMLHttpRequest();
				if(!contentType) { contentType = 'application/xml'; }
				xmldata.open('GET', 'data:' + contentType + ';charset=utf-8,' + encodeURIComponent(domString), false);
				
				if(xmldata.overrideMimeType) { xmldata.overrideMimeType(contentType); }
				
				xmldata.send(null);
				return xmldata.responseXML;
			}
		}
	},
	
	insertRowData: function( tableElt, Obj, rowIndex )
	{
		var tRow = tableElt.insertRow( rowIndex ), count=0;
		
		for( var p in Obj )
		{
			this.ElementWeaver( Obj[p], { container: tRow.insertCell(j) } );
			count++;
		}
	},
	
	multiArray: function( row, col )
	{
		var tempArr = new Array( row );
		
		for( var j=0; j < row; j++ ){ tempArr[j] = new Array( col ); }
		return tempArr;
	},

	sortByASCText: function( title1, title2 )
	{
		if( title1.text < title2.text){ var sorter = -1; }
		if( title1.text > title2.text){ var sorter = 1; }	
		return sorter;
	},

	moveSelectOpt: function( select1, select2, orderFunc )
	{		
		var currIndex = select1.selectedIndex, optSelected = false;
		if( currIndex > -1 )
		{
			var tempArr, optSelected = true, item = select1.options[currIndex].remove();
			for(var i=0, optLen = select2.options.length; i < optLen; i++){ 
				tempArr.push( select2.options[i] ); 
			}
			tempArr.push( item );
			if( orderFunc ){ tempArr.sort( orderFunc ); }
			select2.length=0;
			
			for( var g=0, arrLen=tempArr.length; g < arrLen; g++ ){ 
				select2.add( tempArr[g], null );
			}
			return optSelected;
		}
		else { 
			return optSelected; 
		}
	},

	showCase: function( container, startIndex, endIndex, eltType, funcName, milliSec )
	{
		var countIndex = startIndex, showState = null; // 1: element node - 2: text node - 8: comment node
		
		( this.getType(container) == 'array' ) ? ( eltList = container ) : ( eltList = this.fetchChildNodes( container, eltType ) );
		
		this.start = function()
		{
			if( !showState )
			{
				hideAll( eltList, hideItem );
				showItem( eltList[countIndex] );
				showState = setTimeout( funcName + '.play()', milliSec );
			}
		};
		
		function play()
		{
			hideAll( eltList, hideItem );
			
			if( countIndex >= endIndex ) {
				countIndex = startIndex;
			}
			else {
				countIndex++;
			}
			
			showItem( eltList[countIndex] );
			showState = setTimeout( funcName + '.play()', milliSec );
		}
		
		this.play = play;
		
		function nextItem()
		{
			stop();
			hideAll( eltList, hideItem );
			if( countIndex >= endIndex ) {
				countIndex = startIndex;
			}
			else {
				countIndex++;
			}
			showItem( eltList[countIndex] );
		}
		
		this.nextItem = nextItem;
		
		function prevItem()
		{
			stop();
			hideAll( eltList, hideItem );
			if( countIndex <= startIndex ) {
				countIndex = endIndex;
			}
			else {
				countIndex--;
			}
			showItem( eltList[countIndex] );
		}
		
		this.prevItem = prevItem;
		
		function stop()
		{
			clearTimeout( showState );
			showState = null;
		}
		
		this.stop = stop;
		
		function hideItem( curElt )
		{
			if( curElt.style.display != 'none' )
				curElt.style.display = 'none';
		}
		
		this.hideItem = hideItem;
		
		function showItem( curElt )
		{
			if( curElt.style.display != 'block' )
				curElt.style.display = 'block';
		}
		
		this.showItem = showItem;
		
		function hideAll( elmtList )
		{
			XMUtil.each( elmtList, hideItem );
		}
		
		this.hideAll = hideAll
		
		function showAll( elmtList )
		{
			XMUtil.each( elmtList, showItem );
		}
		
		this.showAll = showAll;
	},

	simpleSlide: function( elt_Id, pic_Arr, picURL, startPoint, endPoint, func_name, milliSec )
	{
		var col_Index = startPoint, runShow = null, ssImage = document.getElementById( elt_Id );
		
		function startShow()
		{
			if( !runShow )
			{
				ssImage.src = picURL + pic_Arr[ col_Index ];
				runShow = setTimeout( func_name + '.play()', milliSec );
			}
		}
		
		this.startShow = startShow;
		
		this.play = function()
		{ 
			if( col_Index >= endPoint ) { 
				col_Index = startPoint; 
			}
			else {
				++col_Index;
			}
			 
			 ssImage.src = picURL + pic_Arr[ col_Index ];
			 runShow = setTimeout( func_name + '.play()', milliSec );
		};

		this.prevSlide = function()
		{
			stopShow();
			
			if( col_Index <= startPoint) {
				col_Index = endPoint;
			}
			else {
				col_Index--;
			}
			
			ssImage.src = picURL + pic_Arr[col_Index];
		};
		
		this.nextSlide = function()
		{
			stopShow();
			
			if( col_Index >= endPoint ) {
				col_Index = startPoint;
			}
			else {
				col_Index++
			}
			
			ssImage.src = picURL + pic_Arr[col_Index];
		};
		
		function stopShow()
		{
			if( runShow != null )
			{
				clearTimeout( runShow );
				runShow = null;
			}
		}
		
		this.stopShow = stopShow;
	},
			
	userAgent: navigator.userAgent.toLowerCase()
} );


XMUtil.inherit(
{	
	
	browser: { 
			msie: /msie/.test( XMUtil.userAgent ) && !(/opera/.test( XMUtil.userAgent )),
			mozilla: /mozilla/.test( XMUtil.userAgent ) && !(/compatible|webkit/.test( XMUtil.userAgent )),
			safari: /webkit/.test( XMUtil.userAgent ),
			opera: /opera/.test( XMUtil.userAgent )
		},
	
	event: {
		documentReady: function( func )
		{
			//XMUtil.readyList.push( func );
			XMUtil.readyListener();
			
			if( XMUtil.isReady )
				func();
			else
				XMUtil.readyList.push( func );
			
		}
	},
	
	isReady: false,
	
	readyList: new Array(),
	
	ready: function()
	{
		if( !XMUtil.isReady )
		{
			XMUtil.isReady = true;
			
			
			XMUtil.readyList[0]();
			if( XMUtil.browser.mozilla || XMUtil.browser.opera  )
				document.removeEventListener( 'DOMContentLoaded', XMUtil.ready, false );
				
			if( XMUtil.browser.msie)
				XMUtil.detachNode( XMUtil.fetch( '#__init_script' ) );
		}
	},
	
	readyListener: function()
	{
		if( XMUtil.browser.mozilla || XMUtil.browser.opera  )
			document.addEventListener( 'DOMContentLoaded', XMUtil.ready, false );
			
		if( XMUtil.browser.msie )
		{
			document.write( '<script id=__init_script defer=true src=//:><\/script>' );
			var script_init = XMUtil.fetch( '__init_script' );
			script_init.onreadystatechange = function()
			{
				if( /loaded|complete/.test( XMUtil.readyState ) ) {
					XMUtil.ready();
				}
			}
			script_init = null; // clear from memory
		}
		else if( XMUtil.browser.safari )
		{
			var _timer = setInterval( function()
			{
				if( /loaded|complete/.test( document.readyState ) ){
					clearInterval( _timer );
					_timer = null;
					XMUtil.ready();
				}
			}, 10 );
		}
		
		window.onload = XMUtil.ready;
	}
} );

