function imageSwap_register_construct(obj_refer,swap_url,obj_image) {
//	funkce, ktera vola objekt imageSwap, je pouzita, aby se nemusel inicializovat objekt imageSwap
//	parametry:
//		obj_refer	- objekt/id objektu, ktereho se udalost tyka, muze byt i pole objektu
//		swap_url	- retezec, nebo pole s adresami obrazku pro jednotlive pole, bud je ve formatu
//					  udalost(over,click)#url nebo jen url (bere, ze udalost je over)
//		obj_image	- objekt/id obrazku, ktery se bude menit, pokud neni zadan, bere se, ze obrazek je obj_image=obj_refer,
//					  ten ale tim padem musi byt jen jeden
	if (typeof(OKi98_reservedVariable_imageSwap_obj)=='undefined') OKi98_reservedVariable_imageSwap_obj=new imageSwap('OKi98_reservedVariable_imageSwap_obj');
	if (typeof(obj_image)=='undefined') OKi98_reservedVariable_imageSwap_obj.register_image(obj_refer,swap_url);
		else OKi98_reservedVariable_imageSwap_obj.register_image(obj_refer,swap_url,obj_image);
	}

//****************Objekt imageSwap****************
/*
obsluha prepinani obrazku, vylepseno cachovani obrazku, mensi naroky na pamet, na obrazek se da povesit vice udalosti
najednou (i cizich), obsluha chyb, u meniciho obrazku vyrazuje onload, protoze pri meneni obrazku by se porad spoustel,
pokud nenalezne nejake id, tak ceka nez se nacte cela stranka, tzn. ze objekty nemusi vubec existovat v dobe volani
jejich registrace
	
	Parametry konstruktoru:
		name_obj	- nazev objektu, pod prepinac spusten
		
	
	Metody:
	
	Verejne:
		register_image					- u objektu zaregistruje pozadovane udalosti, promenne jsou stejne jako u 
										  imageSwap_register_construct
				
	Privatni:
		register_method					- zaregistruje potrebne metody
		get_variable_construction		- vrati konstrukci promenne, tak aby mohlo byt pouzito
										  prom=eval(get_variable_construction(var))
		register_image_retry			- naplanuje nove spusteni registrace
		register_image_test				- otestuje spravnost predanych promennych
		register_image_set_image_path	- nastavi cestu k obrazku, ktery se bude menit
		register_image_cache			- nacte obrazky, ktere jsou potreba
		register_image_set_event		- nastavi udalosti
		manageEvent_on					- zpracuje on udalost
		plan_manageEvent_off			- naplanuje zpracovani off udalosti
		manageEvent_off					- zpracuje off udalost
		
OKi98
*/
function imageSwap(obj_name) {
	this.error=false;
	this.userAgent=get_userAgent();
	if (this.userAgent=='na') {
//		alert('používáte neznámý typ prohlížeče, změny obrázků nebudou fungovat');
		this.error=true;
		}
	this.register_method=imageSwap_register_method;
	this.register_method();
	if (!this.error) {
		this.lock=false;
		this.obj_refer=new Array();
		this.swap_url=new Array();
		this.image_path=new Array();
		this.imageCache=new Array();
		this.imageCache['count']=0;
		this.obj_image=new Array();
		this.timer=new Array();
		this.names=new Array();
		this.names['obj']=obj_name;
		}
	}

function imageSwap_register_method() {
	this.get_variable_construction=imageSwap_get_variable_construction;
	this.register_image=imageSwap_register_image;
	this.register_image_retry=imageSwap_register_image_retry;
	this.register_image_test=imageSwap_register_image_test;
	this.register_image_set_image_path=imageSwap_register_image_set_image_path;
	this.register_image_cache=imageSwap_register_image_cache;
	this.register_image_set_event=imageSwap_register_image_set_event;
	this.manageEvent_on=imageSwap_manageEvent_on;
	this.plan_manageEvent_off=imageSwap_plan_manageEvent_off;
	this.manageEvent_off=imageSwap_manageEvent_off;
	}

function imageSwap_get_variable_construction(variable) {
	var i,construct;
	switch (get_type(variable)) {
		case 'array':
			construct='new Array(';
			for (i=0;i<variable.length;i++) construct+=((i) ? ',' : '')+this.get_variable_construction(variable[i]);
			construct+=')';
		break;
		case 'string':
			construct='\''+variable+'\'';
		break;
		case 'html_element':
			construct='document.getElementById(\''+set_htmlElement_id(variable)+'\')';
		break;
		default:
			construct=variable;
		break;
		}
	return construct;
	}

function imageSwap_register_image(obj_refer,swap_url,obj_image) {
	if (!this.error) {
		if (typeof(obj_image)=='undefined') obj_image='';
		if (!this.lock) {
//			aby cachovani bylo efektivni, tak muze probihat pouze jedna registrace v danem case
			var i,j,currentEvent=new Array();
			this.lock=true;
			if (!this.register_image_test(obj_refer,swap_url,obj_image)) {
				if (!this.error) {
					this.obj_image['path'].onload='';//vyradim onload, aby se porad nespoustel pri prepinani obrazku
					this.register_image_set_image_path();
					this.register_image_cache();
					this.register_image_set_event();
					}
				}
				else this.register_image_retry(obj_refer,swap_url,obj_image);
			this.lock=false;
			this.error=false;
			}
			else this.register_image_retry(obj_refer,swap_url,obj_image);
		}
	}

function imageSwap_register_image_retry(obj_refer,swap_url,obj_image) {
	var param=this.get_variable_construction(obj_refer)+','+this.get_variable_construction(swap_url);
	if (obj_image!='') param+=','+this.get_variable_construction(obj_image);
	setTimeout(this.names['obj']+'.register_image('+param+');',50);
	}

function imageSwap_register_image_test(obj_refer,swap_url,obj_image) {
	var i,tmp_path,matches,activeEvent=new Array(),retry=false;
	this.obj_refer=(get_type(obj_refer)!='array') ? new Array(obj_refer) : obj_refer;
	for (i=0;i<this.obj_refer.length;i++) {
		if (get_type(this.obj_refer[i])=='string') {
			if (!(tmp_path=document.getElementById(this.obj_refer[i]))) {
				if (window.document.readyState=='complete') {
					alert('nebyl nalezen element s id='+this.obj_refer[i]);
					this.error=true;
					}
					else retry=true;					
				}
				else this.obj_refer[i]=tmp_path;
			}
		}
	if (get_type(swap_url)!='array') swap_url=new Array(swap_url);
	for (i=0;i<swap_url.length;i++) {
		this.swap_url[i]=new Array();
		if (!(matches=swap_url[i].match(/^(over|click)#(.*)$/i))) {
			matches=new Array();
			matches[1]='over';
			matches[2]=swap_url[i];
			}
		if (typeof(activeEvent[''+matches[1]+''])=='undefined') {
			this.swap_url[i]['event']=matches[1];
			this.swap_url[i]['url']=matches[2];
			activeEvent[''+matches[1]+'']=1;
			}
			else {
				alert('událost '+matches[1]+' je použita vícekrát');
				this.error=true;
				}
		}
	if (obj_image=='') {
		if (this.obj_refer.length>1) {
			alert('nebyl zadán obrázek, který se má při událostech měnit');
			this.error=true;
			}
			else this.obj_image['path']=this.obj_refer[0];
		}
		else if (typeof(obj_image)=='string') {
			if (!(tmp_path=document.getElementById(obj_image))) {
				if (window.document.readyState=='complete') {
					alert('nebyl nalezen element s id='+obj_image);
					this.error=true;
					}
					else retry=true;
				}
				else this.obj_image['path']=tmp_path;
			}
		else this.obj_image['path']=obj_image;
	return retry;
	}

function imageSwap_register_image_cache() {
	var i,j,idImage='na';
	i=0;
	while ((i<this.imageCache['count']) && (idImage=='na')) {
		if (this.imageCache[''+i+''].src==this.obj_image['path'].src) idImage=i;
			else i++;
		}
	if (idImage=='na') {
		idImage=this.imageCache['count'];
		this.imageCache['count']++
		this.imageCache[''+idImage+'']=this.obj_image['path'].cloneNode(true);
		}
	this.obj_image['idImage']=idImage;
	for (i=0;i<this.swap_url.length;i++) {
		j=0;
		idImage='na';
		while ((j<this.imageCache['count']) && (idImage=='na')) {
			if (this.imageCache[''+j+''].src==this.swap_url[i]['url']) idImage=j;
				else j++;
			}
		if (idImage=='na') {
			idImage=this.imageCache['count'];
			this.imageCache['count']++
			this.imageCache[''+idImage+'']=new Image();
			this.imageCache[''+idImage+''].src=this.swap_url[i]['url'];
			}
		this.swap_url[i]['idImage']=idImage;
		}
	}
	
function imageSwap_register_image_set_image_path() {
	var obj_image_id=set_htmlElement_id(this.obj_image['path']);
	if (typeof(this.image_path[''+obj_image_id+'']=='undefined')) {
		this.image_path[''+obj_image_id+'']=this.obj_image['path'];
		this.timer[''+obj_image_id+'']='na';
		}
	}

function imageSwap_register_image_set_event() {
	var i,j,currentEvent=new Array();
	for (i=0;i<this.obj_refer.length;i++) {
		for (j=0;j<this.swap_url.length;j++) {
			switch (this.swap_url[j]['event']) {
				case 'over':
					currentEvent['on']='mouseover';
					currentEvent['off']='mouseout';
				break;
				case 'click':
					currentEvent['on']='mousedown';
					currentEvent['off']='mouseup';
				break;
				}
			switch (this.userAgent) {
				case 'ie':
					eval('this.obj_refer['+i+'].attachEvent(\'on'+currentEvent['on']+'\',function anonymous() {'+this.names['obj']+'.manageEvent_on(\''+this.obj_image['path'].id+'\','+this.swap_url[j]['idImage']+');})');
					eval('this.obj_refer['+i+'].attachEvent(\'on'+currentEvent['off']+'\',function anonymous() {'+this.names['obj']+'.plan_manageEvent_off(\''+this.obj_image['path'].id+'\','+this.obj_image['idImage']+');})');
				break;
				case 'ns':
					eval('this.obj_refer['+i+'].addEventListener(\''+currentEvent['on']+'\',function anonymous() {'+this.names['obj']+'.manageEvent_on(\''+this.obj_image['path'].id+'\','+this.swap_url[j]['idImage']+');},false)');
					eval('this.obj_refer['+i+'].addEventListener(\''+currentEvent['off']+'\',function anonymous() {'+this.names['obj']+'.plan_manageEvent_off(\''+this.obj_image['path'].id+'\','+this.obj_image['idImage']+');},false)');
				break;
				}
			}
		}
	}

function imageSwap_manageEvent_on(idImage_refer,idImage_swap) {
	if (this.timer[''+idImage_refer+'']!='na') {
		clearTimeout(this.timer[''+idImage_refer+'']);
		this.timer[''+idImage_refer+'']='na';
		}
	this.image_path[''+idImage_refer+''].src=this.imageCache[''+idImage_swap+''].src;
	}

function imageSwap_plan_manageEvent_off(idImage_refer,idImage_swap) {
//	musi se pouzit pres timer, protoze by pri vice spoustejicich objektech doslo k problikavani
	eval('this.timer[\''+idImage_refer+'\']=setTimeout(function anonymous() {'+this.names['obj']+'.manageEvent_off(\''+idImage_refer+'\','+idImage_swap+');},1)');
	}

function imageSwap_manageEvent_off(idImage_refer,idImage_swap) {
	this.image_path[''+idImage_refer+''].src=this.imageCache[''+idImage_swap+''].src;
	this.timer[''+idImage_refer+'']='na';
	}

/*
this script was created by OKi98, all are tested for IE and Mozilla. I dont care about other browsers.
Script can be freely used but keep this note intact.
*/





