/**
	*	Deextra.com
	*	eXtra Utility Attributes
	*	extent xHTML for more functions
**/
var com;
if(!com)com = {};
com.deextra = {};
com.deextra.xua = {};

/**
 *Effect for Mouse Over Image(<img > tag),with add an Attribute named "moro"(Mouse over rollover)
**/
com.deextra.xua.addImageMouseOverEffect = function(){
	var images = document.getElementsByTagName("img");
	for(var i = 0; i < images.length; i++){
		var overURL = images[i].getAttribute("Hover");
		if(overURL){
			/*cache the image*/
			(new Image()).src = overURL;
			baseURL = images[i].src;
			images[i].onmouseover = function(){this.src = overURL;};
			images[i].onmouseout	= function(){this.src = baseURL;};
		}
	}
};
com.deextra.xua.addTextContainerLengthLimit = function(){
	var affectedTags = new Array("span","div","a");
	for(var i = 0; i < affectedTags.length; i++){
		var tags = document.getElementsByTagName(affectedTags[i]);
		for(var j = 0; j < tags.length; j++){
			var limitedLength = tags[j].getAttribute("MaxLength");
			if(limitedLength){
				var maxLength = parseInt(limitedLength);
				var overflowEllipsis = tags[j].getAttribute("Ellipsis") || "...";
				if(tags[j].innerHTML.length >= maxLength && tags[j].innerHTML.length >= overflowEllipsis.length){
					tags[j].innerHTML = tags[j].innerHTML.substring(0,maxLength - overflowEllipsis.length) + overflowEllipsis;
				}
			}
		}
	}
};



/*********************************************************************************************************************************************/
/*
!!!!!!!!!!!!!!!Prototype JS Required!!!!!!!!!!!!!!!

Ajax Link as <a href="xxxx" ajaxTarget="div1">Click Here</a>
click the link will update the element "div1" with linked page content
*/
/*********************************************************************************************************************************************/
com.deextra.xua.addAjaxLinks = function(){
	var tags = document.getElementsByTagName("a");
	for(var i = 0; i < tags.length; i++){
		var ajaxTarget = tags[i].getAttribute("ajaxTarget");
		if(ajaxTarget){
			tags[i].onclick = function(){
				new Ajax.Updater(ajaxTarget,this.href,{method:"GET"});
				return false;
			}
		}
	}
};


/*********************************************************************************************************************************************/
/*
!!!!!!!!!!!!!!!Prototype JS Required!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Google XSLT Required!!!!!!!!!!!!!!!!
Pre-load common XSLT based template engie
use xmlSource="[xmlfile].xml" to load xml with inner XSL styled
*/
/*********************************************************************************************************************************************/

com.deextra.xua.addPreLoadContentXSLT = function(){
	var affectedTags = new Array("div");
	for(var i = 0; i < affectedTags.length; i++){
		var tags = document.getElementsByTagName(affectedTags[i]);
		for(var j = 0; j < tags.length; j++){
			var xmlSource = tags[j].getAttribute("xmlSource");
			if(xmlSource){
				var tag = tags[j];
				new Ajax.Request(xmlSource,{
				asynchronous:false,
				onSuccess:function(transport){
					function clearText(text){
						return text.replace(/^\s*/, '').replace(/\n\s*/g, '\n');
					};
					var xmlText = clearText(transport.responseText);
					var xslText = clearText(tag.innerHTML);
					var xml = xmlParse(xmlText);
					var xslt = xmlParse(xslText);
					var xhtml = xsltProcess(xml, xslt);
					tag.innerHTML = xhtml;
					},
				method:"GET"
				});
			}
		}
	}
}

com.deextra.xua.initUtilities = function(){
	com.deextra.xua.addTextContainerLengthLimit();
	com.deextra.xua.addAjaxLinks();
	//com.deextra.xua.addPreLoadContentXSLT();
	com.deextra.xua.addImageMouseOverEffect();
};

if(window.addEventListener)
	window.addEventListener("load",com.deextra.xua.initUtilities,false);
else if(window.attachEvent)
	window.attachEvent("onload",com.deextra.xua.initUtilities);