
if(!window.mach3) mach3 = new Object();

mach3.alphaImage = Class.create();
mach3.alphaImage.prototype = {
	
	// Properties
	name:{
		script:"alphaimage.js",
		alphaImage:".alphaImage",
		alphaMenu:".alphaMenu",
		alphaMenuActive:".Active"
	},
	path:{
		blank:"blank.gif",
		script:null
	},
	
	// Public Methods
	initialize:function(){
		this.path.script = this._getScriptPath();
		this.path.blank = this.path.script + this.path.blank;
	},
	
	run:function(){
		for (i= 0;i<$$( this.name.alphaImage ).length;i++) {
			this.load( $$( this.name.alphaImage )[i] );
		}
		for(i=0;i<$$(this.name.alphaMenu).length;i++){
			this.loadMenu($$(this.name.alphaMenu)[i]);
		}
	},
	load:function(ele){
		if(!this._isIE6()) return null;
		var src;
		var method;
		var style = this._currentStyle(ele);
		
		if( ele.tagName.match(/^img$/i)){
			src = ele.src;
			method = "scale";
			ele.src = this.path.blank;
		}else{
			src = style.backgroundImage.replace(/^url\(|\)$|\"/ig,"");
			method = "crop";
		}
		this._setFilter( ele, src, method );
	},
	loadMenu:function(ele){
		if (this._isIE6()) {
			var style = this._currentStyle(ele);
			var src = style.backgroundImage.replace(/^url\(|\)$|\"/ig, "");
			
			ele.style.backgroundImage = "url(" + this.path.blank + ")";
			ele.style.overflow = "hidden";
			ele.innerHTML = "";
			ele.style.textIndent = 0;
			ele.style.cursor = "pointer";
			
			var inner = document.createElement("div");
			this._setFilter(inner, src, "crop");
			inner.style.width = style.width;
			inner.style.height = (ele.offsetHeight * 3) + "px";
			inner.style.backgroundImage = "url(" + this.path.blank + ")";
			ele.appendChild(inner);
		}
		
		if(!this._menuOnActive(ele)){
			this._menu_onmouseover = this._menuOnMouseOver.bindAsEventListener(this,ele);
			this._menu_onmouseout = this._menuOnMouseOut.bindAsEventListener(this,ele);
			Event.observe(ele,"mouseover",this._menu_onmouseover);
			Event.observe(ele,"mouseout",this._menu_onmouseout);
		}
		
	},
	
	// Private Methods
	_isIE6:function(){
		return (Prototype.Browser.IE && typeof document.documentElement.style.msInterpolationMode=="undefined" );
	},
	_getScriptPath:function(){
		eles = document.getElementsByTagName("script");
		for(i=0;i<eles.length;i++){
			paths = eles[i].src.split("/");
			if( paths.last() == this.name.script ){
				return paths.without( this.name.script ).join("/") + "/";
			}
		}
	},
	_currentStyle:function(ele){
		style = ele.currentStyle || document.defaultView.getComputedStyle(ele,"");
		return style;
	},
	_setFilter:function(ele,src,method){
		var tmpl = new Template("background-image:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=#{method}, src='#{src}');");
		var filterString = tmpl.evaluate({
			src: src,
			method: method
		})
		ele.style.cssText = filterString;
	},
	_menuOnMouseOver:function(event,ele){
		var marginTop = -(ele.offsetHeight);
		this._setBgValign(ele,marginTop);
	},
	_menuOnMouseOut:function(event,ele){
		this._setBgValign(ele,0);
	},
	_menuOnActive:function(ele){
		var active = Element.classNames(ele).toString().split(" ").indexOf("Active");
		if(active>=0){
			var marginTop = -(ele.offsetHeight*2);
			this._setBgValign(ele,marginTop);
			return true;
		}
		return false;
	},
	_setBgValign:function(ele,posY){
		if(this._isIE6()){
			ele.firstChild.style.marginTop = posY + "px";
		}else{
			ele.style.backgroundPosition = "0px " + posY + "px";
		}
	}
}


/*
mach3.alphaImage = new alphaImageClass();
Event.observe(window,"load",function(){
	mach3.alphaImage.run();
});
*/