/**
* 
* Copyright (C) 2005 Matsuda Shota
* http://sgssweb.com/
* admin@sgssweb.com
*
* ------------------------------------------------------------------------
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
* ------------------------------------------------------------------------
*/

/*
2006-9-11
	- Supports multiple events.
2005-4-23
	- Events are no longer automatically assigned
2005-4-23
	- Now multiple replacements can be set.
2005-4-17
	- Packaged.
*/

/*

Functions:
    
    - RCLoad(image_object, {status_name_1: [regexp_1, replacement_1, 
                                            ... , 
                                            regexp_n, replacement_n], 
                            ... ,
                            status_name_n: [regexp_1, replacement_1, 
                                            ... , 
                                            regexp_n, replacement_n]});
        
    - RCActivate (image_object, status_name_x);
    
    - RCInactivate (image_object);
    
    
Examples:

    <img src="an_image_url_status_is_normal.jpg" alt="" 
         onload="RCLoad(this, {hover:['normal', 'hover'], active['normal', 'active']});"
         onmouseover="RCActivate(this, 'hover');"
         onmouseout="RCInactivate(this);"
         onmousedown="RCActivate(this, 'active');"
         onmouseup="RCActivate(this, 'hover');" />

	<a href="somewhere"
	   onmouseover="RCActivate(document.getElementById('image_id'), 'hover');"
	   onmouseout="RCInactivate(document.getElementById('image_id'));"
	   onmousedown="RCActivate(document.getElementById('image_id'), 'active');"
	   onmouseup="RCActivate(document.getElementById('image_id'), 'hover');">
        <img src="an_image_url_status_is_normal.jpg" alt="" id="image_id"
             onload="RCLoad(this, {hover:['normal', 'hover'], active['normal', 'active']});" />
    </a>

*/


function RolloverController_activate(id) {
	if (this.activeImages[id] && 
		this.activeImages[id].src) {
		this.image.src = this.activeImages[id].src;
	}
}

function RolloverController_inactivate() {
	if (this.initialURL) {
		this.image.src = this.initialURL;
	}
}

function RolloverController(image, actions) {
			
	this.image = image;
	this.initialURL = image.src;
	this.activeImages = new Array();
	
	for (var id in actions) {
		
		var replacedURL = image.src;
		for (var i = 0; i < actions[id].length; i += 2) {
			replacedURL = replacedURL.replace(actions[id][i], actions[id][i + 1]);
		}
		this.activeImages[id] = new Image();
		this.activeImages[id].src = replacedURL;
	}
	
	this.image.onload = function() {};
}


RolloverController.prototype.activate = RolloverController_activate;
RolloverController.prototype.inactivate = RolloverController_inactivate;



function RCLoad(image, actions) {
	image.rc = new RolloverController(image, actions);
}

function RCActivate(image, id) {
	if (image.rc) {
		image.rc.activate(id);
	}
}

function RCInactivate(image) {
	if (image.rc) {
		image.rc.inactivate();
	}
}

