/*
 * Eyefi Behaviour
 *
 * Functions for easy frontend javascripting
 *
 * Documentation can be found on:
 * https://i.eyefi.nl/xiki/ECML/eyefibehaviour
 *
 * Author: Arjan Scherpenisse <arjan@eyefi.nl>
 */

var EB = new Array();

EB.namespaceprefix = "eb:";
EB.scriptprefix = "/js/toolkit/";

// addEvent and removeEvent are (c) John Resig;
// http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
EB.addEvent = function( obj, type, fn ) {
	if (obj.addEventListener) {
        obj.addEventListener( type, fn, false );
    } else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); };
		obj.attachEvent( "on"+type, obj[type+fn] );
	} else {
        obj["on"+type] = fn;
    }
};

EB.removeEvent = function( obj, type, fn ) {
	if (obj.removeEventListener) {
		obj.removeEventListener( type, fn, false );
    } else if (obj.detachEvent) {
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	} else {
        obj["on"+type] = null;
    }
};

EB.getElementOptions = function(el, prefix, defaults) {
    if (prefix == null) prefix = EB.namespaceprefix;
    var opts = defaults;
    for (var i=0; i<el.attributes.length; i++) {
        if (el.attributes[i].name.substr(0, prefix.length) == prefix) {
            var n = el.attributes[i].name.substr(prefix.length, el.attributes[i].name.length-prefix.length);
            opts[n] = el.attributes[i].value;
        }
    }
    return opts;
}

EB.includeScript = function(file, type) {
    var head = document.getElementsByTagName("HEAD")[0];
    if (!head) { alert("EB.includeScript: Er moet wel een HEAD tag in je HTML file staan, idioot!"); return false; }

    var s = document.createElement("SCRIPT");
    if (!type) type = "text/javascript";
    s.setAttribute("type", type);
    s.setAttribute("src", file);
    head.appendChild(s);
}

EB.setCookie = function(name, value, options) {
    document.cookie= name + "=" + escape(value) +
        ((options.expires) ? "; expires=" + options.expires.toGMTString() : "") +
        ((options.path) ? "; path=" + options.path : "") +
        ((options.domain) ? "; domain=" + options.domain : "") +
        ((options.secure) ? "; secure" : "");
}

EB.getCookie = function(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else
        begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
        end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
}

EB.delCookie = function(name, path, domain) {
    if (EB.getCookie(name)) {
        document.cookie = name + "=" + 
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

// get the target element of the last fired event
EB.getEventTarget = function(e) {
    if (!e) e = window.event; 
    return (e.target) ? e.target : e.srcElement;
};

EB.registerAttribute = function(tagname, attribute, callback) {
    if (typeof EB._attributes != "object") EB._attributes = new Array();
    if (typeof EB._attributes[tagname] != "object") EB._attributes[tagname] = new Array();
    EB._attributes[tagname][EB._attributes[tagname].length] = [attribute, callback];
}

EB.processAttributes = function() {
    if (typeof EB._attributes != "object") return;
    for(var tag in EB._attributes) {
        if (typeof EB._attributes[tag] == "function") continue;
        
        var elems = document.getElementsByTagName(tag);
        for (var i=0; i<elems.length; i++) {
            var eventlist = EB._attributes[tag];
            for (var j=0; j<eventlist.length; j++) { 
                if (!elems[i].getAttribute(EB.namespaceprefix+eventlist[j][0])) continue;
                eventlist[j][1](elems[i]);
            }
        }
    }
}


// Preload all images with the eb:src_mouseover behaviour
EB.addHoverImage = function(img) {

    // Check for the eb:src_mouseover tag
    var src_mouseover = img.getAttribute(EB.namespaceprefix+"src_mouseover");
    if (!src_mouseover) return;
        
    // Save the source of the original image
    img.src_original = img.src;

    // Preload the image
    var preloadimg = new Image();
    preloadimg.src = src_mouseover;
        
    // Assign mouseover() and mouseout() functions
    img.EBHoverImageOver = function(e) {
        img.src = img.getAttribute(EB.namespaceprefix+"src_mouseover");
    };
    img.EBHoverImageOut = function(e) {
        img.src = img.src_original;
    };
    
    EB.addEvent(img, "mouseover", img.EBHoverImageOver);
    EB.addEvent(img, "mouseout", img.EBHoverImageOut);
};

/**
 * Add nofocus behaviour to <A> tags with eb:nofocus="true" attribute.
 *
 * Solution: create a small <A> tag, positioned absolute in the left top,
 * and set the focus to this on a mouse release
 */
EB.addNoFocus = function(a) {
    if (!document.getElementById("EB_unfocus_dots")) {
        var e = document.createElement("A");
        e.id = "EB_unfocus_dots";
        e.href = "javascript:;";
        e.style.position = "absolute";
        e.style.top = "0"; e.style.left="0";
        document.body.appendChild(e);
    }
    document.getElementById("EB_unfocus_dots").href = a.href;
    EB.addEvent(a, "click", function() { var d = document.getElementById("EB_unfocus_dots"); d.style.top = (document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop+10)+"px"; d.focus(); } );
}

/**
 * Hover class
 * eb:class_hover="xx";
 */
EB.addHoverClass = function(el) {
    var hoverclass = el.getAttribute(EB.namespaceprefix+"class_mouseover");
    el.EBHoverClassOver = function() {
        if (el.className != hoverclass) {
            el.tmpClassName = el.className;
            el.className = hoverclass;
        }
    };
    el.EBHoverClassOut = function() {
        if (el.className == hoverclass) {
            el.className = el.tmpClassName;
        }
    };
    EB.addEvent(el, "mouseover", el.EBHoverClassOver);
    EB.addEvent(el, "mouseout", el.EBHoverClassOut);
}

/**
 * Link elements
 */
EB.linkEventOver = function(e) {
    var t = EB.getEventTarget(e);
    var linkname = t.getAttribute(EB.namespaceprefix+"link");
    for (var i=0; i<EB.links[linkname].length; i++) {
        var el = EB.links[linkname][i];
        if (el == t) continue;
        if (el.EBHoverImageOver) el.EBHoverImageOver(e);
        if (el.EBHoverClassOver) el.EBHoverClassOver(e);
        if (el.EBDivHrefOver) el.EBDivHrefOver(e);
    }
}

EB.linkEventOut = function(e) {
    var t = EB.getEventTarget(e);
    var linkname = t.getAttribute(EB.namespaceprefix+"link");
    for (var i=0; i<EB.links[linkname].length; i++) {
        var el = EB.links[linkname][i];
        if (el == t) continue;
        if (el.EBHoverClassOut) el.EBHoverClassOut(e);
        if (el.EBHoverImageOut) el.EBHoverImageOut(e);
        if (el.EBDivHrefOut) el.EBDivHrefOut(e);
        
    }
}
EB.addLink = function(x) {
    var linkname = x.getAttribute(EB.namespaceprefix+"link");
    if (typeof EB.links != "object") EB.links = new Array();
    if (typeof EB.links[linkname] != "object") EB.links[linkname] = new Array();

    EB.links[linkname][EB.links[linkname].length] = x;

    EB.addEvent(x, "mouseover", EB.linkEventOver);
    EB.addEvent(x, "mouseout", EB.linkEventOut);
}

/**
 * Toggle elements
 */
EB.toggle = function(id, attribute) {
    if (!attribute) attribute = 'style.display';
    var states = [];
    if (arguments.length < 3) {
        states = ["none", "block"];
    } else {
        var i=0;
        for (i=2;i<arguments.length;i++)
        states[states.length] = arguments[i];
    }
    
    var target = typeof id == "string"?document.getElementById(id):id;
    var l = attribute.split(".");
    var att = eval("target."+attribute);
    if (!att) eval("target."+attribute+" = '"+states[0]+"';");
    
    for (i=0; i<states.length; i++) {
        if (att == states[i]) {
            att = states[(i+1)%states.length];
            eval("target."+attribute+" = att;");
            break;
        }
    }
}

/**
 * Toggle elements in a group: only one open at the same time. 
 */
EB.toggleGroup = function(id) {
    var target = document.getElementById(id);

    if (!target) alert('toggleGroup: unknown element \''+id+'\'');

    var group = target.getAttribute(EB.namespaceprefix+"togglegroup");
    if (!usecookie) {
        var usecookie = target.getAttribute(EB.namespaceprefix+"usecookie");
        if (usecookie == "true") usecookie = true; else usecookie = false;
		var cookiepath = target.getAttribute(EB.namespaceprefix+"cookiepath");
    }

    {
        for (var i=0; i<EB.togglegroups[group].length; i++) {
            var el = EB.togglegroups[group][i];
            el.style.display = "none";
        }
        target.style.display = "block";
        if (usecookie) EB.setCookie("EB_togglegroup_"+group, id, {path:cookiepath}); 
        // return true;
    }
}

EB.addToggleGroup = function(el) {
    if (!EB._togglestatus) EB._togglestatus = [];

    var group = el.getAttribute(EB.namespaceprefix+"togglegroup");
    if (typeof EB.togglegroups != "object") EB.togglegroups = new Array();
    if (typeof EB.togglegroups[group] != "object") EB.togglegroups[group] = new Array();
    EB.togglegroups[group][EB.togglegroups[group].length] = el;

	var defaultopen = el.getAttribute(EB.namespaceprefix+"defaultopen");
	defaultopen = defaultopen == "true";

    var usecookie = el.getAttribute(EB.namespaceprefix+"usecookie");
    if (usecookie == "true") usecookie = true; else usecookie = false;
    if (!EB._togglestatus[group] && usecookie && EB.getCookie("EB_togglegroup_"+group) == el.id) {
        el.style.display = "block";
    } else if (!EB._togglestatus[group] && (!usecookie || !EB.getCookie("EB_togglegroup_"+group)) && defaultopen) {
		el.style.display = "block";
	} else if (!EB._togglestatus[group] && usecookie && EB.getCookie("EB_togglegroup_"+group) != el.id && !defaultopen) {
        el.style.display = "none";
    }
}


/**
 * Initialize scrolling div
 */
EB.initScroller = function(el) {
    if(el.getAttribute(EB.namespaceprefix+"scroller") != "true") return;
	
    if (!EB.scroller) {
        EB.includeScript(EB.scriptprefix+"eb-scroller.js");
        setTimeout(function(){EB.initScroller(el);}, 100);
        return;
    }

    EB.scroller.create(el);
}

/**
 * Initialize scrolling div
 */
EB.initAutocomplete = function(el) {
    //if(!el.getAttribute(EB.namespaceprefix+"autocomplete") != "true") return;
	
    if (!EB.autocomplete) {
        EB.includeScript(EB.scriptprefix+"eb-autocomplete.js");
        setTimeout(function(){EB.initAutocomplete(el);}, 100);
        return;
    }

    EB.autocomplete.create(el);
}

/**
 * Initialize empty value/class.
 **/
EB.initEmptyValue = function(el) {
	var emptyVal = el.getAttribute(EB.namespaceprefix+"emptyvalue");
	
	if (el.value == "") el.value = emptyVal;

	EB.addEvent(el, "focus", function() {
		if (el.value == emptyVal) el.value = "";
	});
	EB.addEvent(el, "blur", function() {
		if (el.value == "") el.value = emptyVal;
	});
	EB.addEvent(el.form, "submit", function() {
		// Prevent submittal of wrong information.
		if (el.value == emptyVal) el.value = "";
	});
}

EB.initEmptyClass = function(el) {
	var emptyVal = el.getAttribute(EB.namespaceprefix+"emptyvalue");
	if (emptyVal == null) emptyVal = "";
	
	var emptyClass = el.getAttribute(EB.namespaceprefix+"emptyclass");
	var normalClass = el.className;
	
	if (el.value == "" || el.value == emptyVal) el.className = emptyClass;

	EB.addEvent(el, "focus", function() {
		el.className = normalClass;
	});
	EB.addEvent(el, "blur", function() {
		if (el.value == "" || el.value == emptyVal) el.className = emptyClass;
	});
}

// Link to previous page
EB.goBack = function() {
    window.history.go(-1);
}

// Add a bookmark
EB.addFavorite = function(url, title) {
    if (!document.all) {
        alert('Please hit ctrl-b to bookmark this page');
    } else {
        external.AddFavorite(url,title);
    }
}

// Check browser
EB.checkBrowser = function() {
    if (typeof EB._browser == "object") return EB._browser;
    var b = new Array();
	b.ver=navigator.userAgent.toUpperCase(); b.dom=document.getElementById?1:0;
	b.mac=(b.ver.indexOf("PPC")!= -1)?1:0; b.win=(b.ver.indexOf("Win")!= -1)?1:0;
	b.ie5=(b.ver.indexOf("MSIE")!= -1 && b.dom && parseInt(navigator.appVersion) >= 4)?1:0;
	b.ie4=(document.all && !b.dom)?1:0; b.ie=(b.ie5 || b.ie4);
	b.saf=(b.ver.indexOf("SAFARI")>-1 && b.mac) ?1:0;
	b.ns6=(b.dom && b.ver.indexOf("GECKO")!= -1)?1:0; b.ns4=(document.layers && !b.dom)?1:0; 
	b.ns=(b.ns4 || b.ns6 || b.saf);
	b.eyefi=(b.ie || b.ns);
    EB._browser = b;
	return EB._browser;
}

EB.window = {
    
    /**
     * Center a window.
     * Example:
     * EB.addEvent(window, "load", EB.window.center);
     * this centers a window on load.
     */
    center: function() {
        if (!window.moveTo || !window.resizeTo) return;
        var saw = screen.availWidth;
        var sah = screen.availHeight;
        var ww; var wh;
        if (document.documentElement) {
            ww = document.documentElement.clientWidth;
            wh = document.documentElement.clientHeight;
        } else {
            ww = document.body.clientWidth;
            wh = document.body.clientHeight;
        }
        window.moveTo((saw-ww)/2, (sah-wh)/2);
    },

    /**
     * Close a window. Doh ;-)
     */
    close: function() {
        window.close();
    },

    /**
     * Link from popup to main window
     * EB.window.openInMain('/index.php');
     */
    openInMain: function(url) {
        opener.parent.location=url;
        opener.focus();
        EB.window.close();
    },

    /**
     * Make the window fullscreen
     */
    fullscreen: function() {
        if (!window.moveTo || !window.resizeTo) return;
        window.moveTo(0,0) 
        window.resizeTo(screen.availWidth,screen.availHeight)
    },

    /**
     * Make a popup window
     * EB.window.popup('aap.php', 320, 240);
     * EB.window.popup('aap.php', 320, 240, ['location','resizable']); // resizablepopup with location bar
     */
    popup: function(url, w, h, opts) {
        var all_options = {'toolbar': false, 'location': false, 'directories': false,
                           'status': false, 'menubar': false,
                           'scrolling': false, 'scrollbars': false, 'resizable': false};
        if (typeof opts == "object") {
            for (var j=0; j<opts.length; j++) all_options[opts[j]] = true;
        }
        s = "";
        for (var i in all_options) s += i+"="+(all_options[i]?"yes":"no")+",";
        if (w) s += "width="+w+",";
        if (h) s += "height="+h+",";
        s = s.substr(0,s.length-1);
        return window.open(url,null,s);        
    }
};



// FLASH
// ************* START NEW CODE *******************************
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function ControlVersion() {
	var version, axo, e;
// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry
	try {
		// version will be set for 7.X or greater players
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {
	}

	if (!version) {
		try {
			// version will be set for 6.X players only
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			// installed player is some revision of 6.0
			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
			// so we have to be careful. 
			// default to the first public version
			version = "WIN 6,0,21,0";
			// throws if AllowScripAccess does not exist (introduced in 6.0r47)		
			axo.AllowScriptAccess = "always";
			// safe to call for 6.0r47 or greater
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version) {
		try {
			// version will be set for 4.X or 5.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version) {
		try {
			// version will be set for 3.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) {
		}
	}

	if (!version) {
		try {
			// version will be set for 2.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}	
	return version;
}

// JavaScript helper required to detect Flash Player PlugIn version information
function GetSwfVer(){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;			
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
			var versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
	}	
	return flashVer;
}

// When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) {
	versionStr = GetSwfVer();
	if (versionStr == -1 ) {
		return false;
	} else if (versionStr != 0) {
		if(isIE && isWin && !isOpera) {
			// Given "WIN 2,0,0,11"
			tempArray         = versionStr.split(" "); 	// ["WIN", "2,0,0,11"]
			tempString        = tempArray[1];			// "2,0,0,11"
			versionArray      = tempString.split(",");	// ['2', '0', '0', '11']
		} else {
			versionArray      = versionStr.split(".");
		}
		var versionMajor      = versionArray[0];
		var versionMinor      = versionArray[1];
		var versionRevision   = versionArray[2];

        // is the major.revision >= requested major.revision AND the minor version >= requested minor
		if (versionMajor > parseFloat(reqMajorVer)) {
			return true;
		} else if (versionMajor == parseFloat(reqMajorVer)) {
			if (versionMinor > parseFloat(reqMinorVer))
				return true;
			else if (versionMinor == parseFloat(reqMinorVer)) {
				if (versionRevision >= parseFloat(reqRevision))
					return true;
			}
		}
		return false;
	}
}

// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 8;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 0;
// Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
var hasProductInstall = DetectFlashVer(6, 0, 65);
// Version check based upon the values defined in globals
var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
var hasFlash = false;

EB._detectFlash = function() {
    if (hasFlash) return;
    if (hasReqestedVersion) { hasFlash=true; }
}
// ********************** END NEW CODE ***********************
EB.writeFlash = function(ourSwf, ourWidth, ourHeight, ourBg, msgOrPage, noFlashMsg) {
    EB._detectFlash();
    if (hasFlash) {
        document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
        document.write('  codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" ');
        document.write(' ID=FLASH_AD WIDTH='+ourWidth+' HEIGHT='+ourHeight+'>');
		if(ourBg=='transparent') {
	        document.write(' <PARAM NAME=movie VALUE="'+ourSwf+'"> <PARAM NAME=quality VALUE=high> <PARAM NAME="wmode" VALUE="transparent"> '); 
		} else {
	        document.write(' <PARAM NAME=movie VALUE="'+ourSwf+'"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE='+ourBg+'> '); 
		}
        document.write(' <EMBED src="'+ourSwf+'" quality=high ');
		if(ourBg=='transparent') {
    	    document.write(' swLiveConnect=FALSE WIDTH='+ourWidth+' HEIGHT='+ourHeight+' WMODE="transparent"');
		} else {
		    document.write(' swLiveConnect=FALSE WIDTH='+ourWidth+' HEIGHT='+ourHeight+' bgcolor='+ourBg);
		}
		document.write(' TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">');
        document.write(' </EMBED>');
        document.write(' </OBJECT>');
	} else { // if the right flash player version has not been found:
		if (msgOrPage==1) { document.write('<meta http-equiv="Refresh" content="1; URL='+noFlashMsg+'">'); }
		else if (msgOrPage==2) { EB.window.popup(noFlashMsg,'noflash',350,350); }
		else if (msgOrPage==3) { document.write('<a href="http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank">'+noFlashMsg+'</a>');  }
		else if (msgOrPage==4) { alert(noFlashMsg); }
		else if (msgOrPage==5) { document.write('<a href="http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank"><img src="'+noFlashMsg+'" width="'+ourWidth+'" height="'+ourHeight+'" style="border: 0" /></a>');  }
	}
};

// <span eb:rate="true" />
EB.initRate = function(el) {
    var opts = EB.getElementOptions(el, null, {"num": 5, "half": false, "value": 0,
    "img_on": "img/star_on.gif", "img_off": "img/star_off.gif", "img_half": "img/star_half.gif", "img_over": "img/star_over.gif",
    "readonly": false, "url": false, "update_input": false});

    opts.num = parseInt(opts.num);
    opts.value = parseFloat(opts.value);
    var img_on = document.createElement("IMG"); img_on.src = opts.img_on;
    var img_off = document.createElement("IMG"); img_off.src = opts.img_off;
    var img_half = document.createElement("IMG"); img_half.src = opts.img_half;

    var id;
    if (!opts.readonly) {
        var img_over = document.createElement("IMG"); img_over.src = opts.img_over;
        if (!EB.rate) EB.rate = { globalid: 1 };
        id = EB.rate.globalid++;
        EB.rate[id] = { images: [], opts: opts, img_on: img_on, img_off: img_off, img_half: img_half, img_over: img_over };
    }
    
    // first clear contents of the span
    while (el.childNodes.length) el.removeChild(el.childNodes[0]);
    
    // add <num> images
    for (i=0; i<opts.num; i++) {
        var orig;
        if (i+1 <=opts.value)
            orig = img_on;
        else if (i+1 <= opts.value+0.5)
            orig = img_half;
        else
            orig = img_off;
        
        var im = orig.cloneNode(false);        
        if (!opts.readonly) {
            im.rate_id = id;
            im.i = i;
            im.orig = orig;
            im.over = img_over;
            im.style.cursor = "pointer";
            EB.addEvent(im, "mouseover", EB.rateMouseOver);
            EB.addEvent(im, "mouseout", EB.rateMouseOut);
            EB.addEvent(im, "click", EB.rateMouseClick);
            EB.rate[id].images[EB.rate[id].images.length] = im;
        }
        el.appendChild(im);
    }
    if (opts.update_input) {
        document.getElementById(opts.update_input).value = opts.value;
    }
    
}

EB.rateMouseOver = function(e) {
    var img = EB.getEventTarget(e);
    var r = EB.rate[img.rate_id];
    for (i=0; i<=img.i; i++) {
        r.images[i].src = r.images[i].over.src;
    }

}

EB.rateMouseOut = function(e) {
    var img = EB.getEventTarget(e);
    var r = EB.rate[img.rate_id];
    for (i=0; i<=img.i; i++) {
        r.images[i].src = r.images[i].orig.src;
    }
}

EB.rateMouseClick = function(e) {
    var img = EB.getEventTarget(e);
    var r = EB.rate[img.rate_id];
    for (i=0; i<r.opts.num; i++) {
        var im = (i <= img.i) ? r.img_on : r.img_off;
        r.images[i].orig = im;
        r.images[i].src = im.src;
    }
    // handling of rate action
    if (r.opts.update_input) {
        // update a hidden element
        var h = document.getElementById(r.opts.update_input);
        h.value = img.i+1;
    }
    if (r.opts.url) {
        alert("FIXME: submit to "+r.opts.url)
    }
    
}



// ATTRIBUTE REGISTRATIE GEDEELTE
EB.registerAttribute("IMG", "src_mouseover", EB.addHoverImage);
EB.registerAttribute("INPUT", "src_mouseover", EB.addHoverImage);
EB.registerAttribute("A", "class_mouseover", EB.addHoverClass);
EB.registerAttribute("IMG", "class_mouseover", EB.addHoverClass);
EB.registerAttribute("A", "nofocus", EB.addNoFocus);

EB.registerAttribute("DIV", "togglegroup", EB.addToggleGroup);
EB.registerAttribute("UL", "togglegroup", EB.addToggleGroup);
EB.registerAttribute("IFRAME", "togglegroup", EB.addToggleGroup);

EB.registerAttribute("A", "link", EB.addLink);
EB.registerAttribute("IMG", "link", EB.addLink);

EB.registerAttribute("SPAN", "rate", EB.initRate);

EB.registerAttribute("DIV", "scroller", EB.initScroller);

EB.registerAttribute("INPUT", "emptyvalue", EB.initEmptyValue);
EB.registerAttribute("INPUT", "emptyclass", EB.initEmptyClass);
EB.registerAttribute("INPUT", "autocomplete", EB.initAutocomplete);

// LOAD GEDEELTE
EB.addEvent(window, "load", EB.processAttributes);
EB.loaded = true;
