/* extends img and div to allow for easy setting of rollover/mousedown states, changing graphic and setting tooltip */

Element.extend({
	setState: function(options){
		if(this.getTag() == 'img' || this.getTag() == 'div'){
			this.refresh();
			if(options.over){
				this.addEvent('mouseenter', function(){ this.mouseEnter(); });	
				this.addEvent('mouseleave', function(){ this.mouseLeave(); });
			}
			else{
				this.removeEvents('mouseenter');	
				this.removeEvents('mouseleave');
			}
			if(options.down){
				this.addEvent('mousedown', function(){ this.mouseDown(); });
				this.addEvent('mouseup', function(){ this.mouseUp(); });
			}
			else{
				this.removeEvents('mousedown');
				this.removeEvents('mouseup');
			}
			if(options.tip){
				this.setTip(options.tip);
			}
		}
	},
	setTip: function(tip){
		this.setProperty('alt', tip);	
		this.setProperty('title', tip);	
		new Tips(this, {showDelay: 250});
	},
	getImage: function(){
		return (this.getTag() == 'img') ? this.getProperty('src') : this.getStyle('background-image').substring(4, this.getStyle('background-image').length - 1);
	},
	setImage: function(src){
		if(this.getTag() == 'img'){
			this.setProperty('src', src);
		}
		else if(this.getTag() == 'div'){
			this.setStyle('background-image', 'url('+src+')');
		}
		this.refresh();
	},
	refresh: function(){
		if(this.getTag() == 'img' || this.getTag() == 'div'){
			var file = this.getImage().replace(/"/g, '');
			this.imageSrc = file.substring(0, file.length - 4);
			this.imageExt = file.substring(file.length - 4);
			new Asset.images([this.imageSrc+'-over'+this.imageExt, this.imageSrc+'-down'+this.imageExt, this.imageSrc+'-selected'+this.imageExt]);
		}
	},
	mouseEnter: function(){
		if(this.getTag() == 'img'){
			this.setProperty('src', this.imageSrc+'-over'+this.imageExt);
		}
		else if(this.getTag() == 'div'){
			this.setStyle('background-image', 'url('+this.imageSrc+'-over'+this.imageExt+')');
		}
	},
	mouseLeave: function(){
		if(this.getTag() == 'img'){
			this.setProperty('src', this.imageSrc+this.imageExt);
		}
		else if(this.getTag() == 'div'){
			this.setStyle('background-image', 'url('+this.imageSrc+this.imageExt+')');
		}
	},
	mouseDown: function(){
		if(this.getTag() == 'img'){
			this.setProperty('src', this.imageSrc+'-down'+this.imageExt);
		}
		else if(this.getTag() == 'div'){
			this.setStyle('background-image', 'url('+this.imageSrc+'-down'+this.imageExt+')');
		}
	},
	mouseUp: function(){
		if(this.getTag() == 'img'){
			this.setProperty('src', this.imageSrc+'-over'+this.imageExt);
		}
		else if(this.getTag() == 'div'){
			this.setStyle('background-image', 'url('+this.imageSrc+'-over'+this.imageExt+')');
		}
	}
});

Element.Events.extend({
	'wheelup':{
		type: Element.Events.mousewheel.type,
		map: function(event){
			event = new Event(event);
			if (event.wheel >= 0) this.fireEvent('wheelup', event)
		}
	},
	'wheeldown':{
		type: Element.Events.mousewheel.type,
		map: function(event){
			event = new Event(event);
			if (event.wheel <= 0) this.fireEvent('wheeldown', event)
		}
	}
});