/*
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
RAM.Media BV SwitchThumbs JS - Copyright 2009 - Copy prohibited.   
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/
var dom = location.protocol+'//'+location.hostname+'/ram/files/';
var ads=[],ct=0,cur=0,t,src,id,dir;

function idSubDir(id){
	if(id.length < 4 || id < 1000){
		return '1/'+id;
	}else{
		var sub_dir = (Math.floor(id / 1000)) * 1000;
		return sub_dir+'/'+id;
	}
}
function switchThumb(o){
	t=0;
	if(o){
		id=o.id;
		if(id.indexOf('l')!=-1){
			if(typeof(limg)!='undefined'){
				id=id.replace('l','');
				src=limg[id];
				dir='local/'+idSubDir(id)+'/thumbs/';
				t=1;
			}
		}else if(id.indexOf('h')!=-1){
			if(typeof(himg)!='undefined'){
				id=id.replace('h','');
				src=himg[id];
				dir='hosted/'+idSubDir(id)+'/thumbs/';
				t=3;
			}
		}else{
			if(typeof(gimg)!='undefined'){
				id=id.replace('g','');
				src=gimg[id];
				dir='galleries/'+idSubDir(id)+'/';
				t=2;
			}
		}
		if(t!=0 && src.length>1){
			cur++;
			RAMexecLoop(o,cur,1);
		}
	}
}
function RAMexecLoop(o,prev,a){
	var n;
	if(o && cur==prev){
		try {
			n=(ct+1)%src.length;
			if(ads[n]&&(ads[n].complete || ads[n].complete==null)){
				o.src=ads[ct=n].src;
			}
			ads[n=(ct+1)%src.length]=new Image;
			ads[n].onerror=function(){resetThumb(o);};			
			if(parseInt(src[n])){
				ads[n].src=dom+dir+src[n]+'.jpg';
			}else{
				ads[n].src=src[n];
			}
			if(a==1){
				RAMexecLoop(o,prev);
			}else{
				setTimeout(function(){RAMexecLoop(o,prev);},500);
			}
		} catch (e) {
			// do nothing
		}
	}
}
function resetThumb(o){
	try {
		if(src.length > 0){
			if(parseInt(src[0])){
				o.src=dom+dir+src[0]+'.jpg';
			}else{
				o.src=src[0];
			}
		}
	} catch (e) {
		// do nothing
	}
	ct=0;
	ads=[],prev='',cur++;
}
