var Main = {};

if(document.addEventListener){
	Main.preventDefault = function(event){
		event.preventDefault();
	};
	Main.stopPropagation = function(event){
		event.stopPropagation();
	};
}
else if(document.attachEvent){
	Main.preventDefault = function(event){
		event.returnValue = false;
	};
	Main.stopPropagation = function(event){
		event.cancelBubble = true;
	};
}

Main.addClass = function(target, theClass){
	if(target.className == ""){
		target.className = theClass;
	}
	else{
		if(! Main.hasClass(target, theClass) ){
			target.className += " " + theClass;
		}
	}
};

Main.getElementsByClass = function(element, theClass){
	var elementArray = [];
	if(element.all){
		elementArray = element.all;
	}
	else{
		elementArray = element.getElementsByTagName("*");
	}
	
	var matchedArray = [];
	var pattern = new RegExp("(^| )" + theClass + "( |$)");
	for(var i = 0 ; i < elementArray.length ; i++){
		if( pattern.test(elementArray[i].className) ){
			matchedArray[matchedArray.length] = elementArray[i];
		}
	}
	return matchedArray;
};

Main.hasClass = function(target, theClass){
	var pattern = new RegExp("(^| )" + theClass + "( |$)");
	if( pattern.test(target.className) ){
		return true;
	}
	else{
		return false;
	}
};

Main.removeClass = function(target, theClass){
	var pattern = new RegExp("(^| )" + theClass + "( |$)");
	target.className = target.className.replace(pattern, "$1");
	target.className = target.className.replace(/ $/, "");
};

Main.getComputedStyle = function(element, styleProperty){
	var computedStyle = null;
	if(typeof element.currentStyle != "undefined"){
		computedStyle = element.currentStyle;
	}
	else{
		computedStyle = document.defaultView.getComputedStyle(element, null);
	}
	return computedStyle[styleProperty];
};

Main.getXMLHttpRequest = function(){
	var xmlHttp;
	try{
		xmlHttp = new XMLHttpRequest();
	}
	catch(error){
		try{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(error){
			try{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(error){
				xmlHttp = null;
			}
		}
	}	
	return xmlHttp;
};

Main.start = function(runnable){
	//This will wait until all document content, including all images, has been downloaded
	//Main.addEventListener(window, "load", runnable.init);
	if(document.addEventListener){
		window.addEventListener("load", runnable.init, false);
	}
	else if(document.attachEvent){
		window.attachEvent("onload", runnable.init);
	}
};

/*
Main.start = function(runnable){
	//This will wait only until the document has been completely parsed,
	// and will not wait for other content like images to be downloaded
	var initOnce = function(){
		if (arguments.callee.done){
			return;
		}
		arguments.callee.done = true;
		runnable.init();
	};
	Main.addEventListener(document, "DOMContentLoaded", initOnce);
	Main.addEventListener(window, "load", initOnce);
};
*/
