/************************************************************
***                                                       ***
***          Copyright 2009 by anema                      ***
***          www.anema.at                                 ***
***                                                       ***
************************************************************/
/*[version]2009-01-13 17:02[/version]*/

/**
Verwendungsbeispiel im Head einer HTML-Datei:


<script language="javascript" type="text/javascript" src="jscript/SwappedIcon.js"></script>
<script type="text/javascript">
var ail = new AnemaIconList();

//ail.registerIcon( 'sn_sep', 'images/sn_sep_c.gif', 'images/sn_sep_h.gif' );
//ail.registerIcon( 'nbtn_npr', 'images/navbuttons/nbtn_npr_c.gif', 'images/navbuttons/nbtn_npr_h.gif' );
//ail.registerIcon( '', 'images/navbuttons/_c.gif', 'images/navbuttons/_h.gif' );
//ail.registerIcon( '', 'images/_h.gif', 'images/_h.gif' );
//ail.registerIcon( '', '', '' );

function initOnload(){
	ail.load();
	ail.activateSwapping();
}

function preloadIconList(){
	ail.preload();
}

registerAnemaIconListOnload( initOnload );
setTimeout( preloadIconList, 0 );	// Wird ausgeführt, sobald das Dokument steht.
</script>



PHP-seitig wird die Klasse von den swap_utils.php unterstützt.
Soll ein Bild aktiv sein, dh. nicht in den kalten Zustand wechseln,
einfach zweimal die heiße Version übergeben.

*/




function SwappedIcon( id, imgCold, imgHot ){
	this.id = id;
	this.imgCold = imgCold;
	this.imgHot = imgHot;
	this.element = null;
	// Der Elementtyp gibt an, ob es sich um ein <img>-Tag handelt oder nicht.
	// Wichtig, weil bei <img>-Tags das src-Attribut geändert wird, bei anderen
	// Tags per Style-Objekt das Hintergrundbild (background-image = "url(path)".
	this.elementType = "none";
	this.active = true;
}

SwappedIcon.prototype.activate = function(){
	this.active = true;
};

// Icon wird nicht mehr heiß.
SwappedIcon.prototype.deactivate = function(){
	this.coolDown();
	this.active = false;
};

SwappedIcon.prototype.preload = function(){
	new Image().src = this.imgHot;
	new Image().src = this.imgCold;
};

SwappedIcon.prototype.load = function(){
	this.element = document.getElementById( this.id );
	if( this.element ){
		this.elementType = (this.element.tagName.toLowerCase()=="img") ? "image" : "other";
		if( this.elementType == "image" ){
			this.element.src = this.imgCold;// else alert( this.id );
		}else{
			this.element.style.backgroundImage = "url(" + this.imgCold + ")";
		}
		this.element.swapper = this;
	}
};
//
SwappedIcon.prototype.heatUp = function(){
	if( !this.active ) return;
	if( this.elementType == "image" ){
		this.element.src = this.imgHot;// else alert( this.id );
	}else{
		this.element.style.backgroundImage = "url(" + this.imgHot + ")";
	}
};
SwappedIcon.prototype.coolDown = function(){
	if( this.elementType == "image" ){
		this.element.src = this.imgCold;// else alert( this.id );
	}else{
		this.element.style.backgroundImage = "url(" + this.imgCold + ")";
	}
};
SwappedIcon.prototype.setHandlers = function(){
	if( !this.element ){
		this.load();
		if( !this.element ){
			// Immer noch nichts.
			return;
		}
	}
	var self = this;
	var onmouseOverHandler = function(){ self.heatUp(); };
	var onmouseOutHandler = function(){ self.coolDown(); };

	if( this.element.addEventListener ){
		this.element.addEventListener( "mouseover", onmouseOverHandler, false );
		this.element.addEventListener( "mouseout", onmouseOutHandler, false );
	}else if( window.attachEvent ){
		this.element.attachEvent( "onmouseover", onmouseOverHandler );
		this.element.attachEvent( "onmouseout", onmouseOutHandler );
	}else{
		this.element.onmouseover = onmouseOverHandler;
		this.element.onmouseout = onmouseOutHandler;
	}
}


function AnemaIconList(){
	this.list = new Array();
}
AnemaIconList.prototype.registerIcon = function( id, imgCold, imgHot ){
	this.list.push( new SwappedIcon(id, imgCold, imgHot) );
}
AnemaIconList.prototype.load = function(){
	for( var i=0; i<this.list.length; ++i ){
		this.list[i].load();
	}
}
AnemaIconList.prototype.preload = function(){
	for( var i=0; i<this.list.length; ++i ){
		this.list[i].preload();
	}
}
AnemaIconList.prototype.activateSwapping = function(){
	for( var i=0; i<this.list.length; ++i ){
		this.list[i].setHandlers();
	}
}

function registerAnemaIconListOnload( initFunction ){
	if( window.addEventListener ){
		window.addEventListener( "load", initFunction, false );
	}else if( window.attachEvent ){
		window.attachEvent( "onload", initFunction );
	}else{
		window.onload = initFunction;
	}
}
