var photo_parsecache=Array();
var photo_url_weight_sum=0;
for (var i = 0; i < PHOTO_URL.length; i++){
	if (PHOTO_URL[i] && PHOTO_URL[i].status){
		photo_url_weight_sum +=PHOTO_URL[i].weight;
	}
}
var photo_itemviewpage_mainpic;

function photo_show_image(imagelink,title,id){
	var retval;
	retval='<img src="'+imagelink+'" ';
	if(typeof CUSTOM_PHOTO_BORDER=='undefined' || !CUSTOM_PHOTO_BORDER){
		retval+=' style="border: 0px;" ';
	}
	if(title){
		retval+=' alt="'+title+'"';
	}
	if(id){
		retval+=' id="'+id+'"';
	}
	document.write(retval+' />');
}
function photo_open_pop(title,imagelink){
	picWindow=window.open("","VateraPic","scrollbars=yes,menubar=no,width=300,height=300,screenX=20,screenY=20");
	picWindow.document.write("<HEAD><TITLE>"+title+"</TITLE></HEAD><BODY onload='picWindow_onload()'>");
	picWindow.document.write("<CENTER><a href='#' onclick='self.close();'>");
	picWindow.document.write('<img src="'+imagelink+'" border="0" alt="'+title+'" id="vpic">');
	picWindow.document.write("</a></CENTER></BODY>");
	picWindow.document.write("<script language='javascript'>function picWindow_onload(){");
	picWindow.document.write("var tmppic=document.getElementById('vpic');");
	picWindow.document.write("window.resizeTo(Math.min(window.screen.availWidth-40,tmppic.width+60),Math.min(window.screen.availHeight-40,tmppic.height+90));");
	picWindow.document.write("}");
	picWindow.document.write("</"+"script>");
//<!-- Medián WebAudit Vatera Aukció Képek 1/2 -->
	picWindow.document.write('<script type="text/javascript" src="http://img.vatera.hu/js/median.js"></'+'script>');
	picWindow.document.write('<img style="position:absolute;top:-100px;left:-100px" src="http://audit.median.hu/cgi-bin/track.cgi?uc=11771518574561&dc=1&ui='+same+'" width="1" height="1">');
//<!-- Medián WebAudit Vatera Aukció Képek 1/2 -->
	picWindow.document.write("</HTML>");
	picWindow.document.close();
	picWindow.focus();
	return true;
}
function photo_get_server(num){
	/*FIXME:  remove this section after a while (e.g on 2011.11.01)*/
	if (num > 1474644408 && num < 1481418176) {
		if (num%4 < 2) {
			return 1;
		}
		return 0;
	}
	if(photo_url_weight_sum){
		var tmp=num%photo_url_weight_sum;
		var cnt=-1;
		for (var i = 0; i < PHOTO_URL.length; i++){
			if (PHOTO_URL[i].status){
				cnt +=PHOTO_URL[i].weight;
			}
			if(tmp <= cnt){
				return i;
			}
		}
	}else{
		return 0;
	}
}
function photo_hash(txt){
	var retval='';
	txt=parseInt(txt);
	txt=txt.toString(16);
	txt=''+txt;
	for (var i = 0; i <= txt.length; i++){
		retval = txt.substring(i, i+1) + retval;
	}
	return retval.replace(new RegExp(/^(\w\w)(\w\w)(\w*)/g),'$1/$2/$3_').toLowerCase();
}
function photo_parse(product_id,product_image){
// JS caching can be turn off here:
//	if(1){
	if(!photo_parsecache[product_id]){
		var picdata=product_image.split(',');
		var retval={'images':Array(),imagecount:0};
		for (var i = 0; i < picdata.length; i++){
			if(picdata[i].length>0){
				if(picdata[i].substring(0,1) == 'L'){
					retval.product_id=picdata[i].substring(2);
				}else if(picdata[i].substring(0,1) == 'D'){
					retval.defaultimage=picdata[i].substring(2);
				}else if(picdata[i].match(new RegExp(/^\d+:\d+$/))){
					var tmp=picdata[i].split(':');
					var img={'servers':Array()};
					for (var j=0;j< tmp[1].length;j++){
						if(tmp[1].substring(j,j+1)>0){
							img.servers[j]=1;
							img.lastserver=j;
						}
					}
					retval.images[tmp[0]]=img;
					retval.lastimage=tmp[0];
					retval.imagecount++;
				}			
			}
		}
		photo_parsecache[product_id]=retval;
	}
	return photo_parsecache[product_id];
}
function photo_get_link(product_id,size,number,product_image,nocache){
	var serverid;
	serverid=photo_get_server(product_id);
	var picdata=photo_parse(product_id,product_image);
	if(!number){
		number=picdata.defaultimage;
		if(!number){
			number=1;
		}
	}
	if(!picdata.images[number]){
		number=picdata.defaultimage;
		if(!picdata.images[number]){
			number=picdata.lastimage;
		}
	}
	if(picdata.product_id){
		product_id=picdata.product_id;
		serverid=photo_get_server(product_id);
	}
	if(!picdata.images[number].servers[serverid]){
		serverid=picdata.images[number].lastserver;
	}
	imagelink=photo_hash(product_id)+number+'_'+size+'.jpg';
	if(nocache == '1'){
		imagelink+='?x='+Math.floor(Math.random()*1000000);
	}
	var result = PHOTO_URL[serverid].url+imagelink;
	result.replace("ad", "au");
	return result;
}
function photo_put(product_id,size,number,title,product_image,nocache){
	var imagelink=photo_get_link(product_id,size,number,product_image,nocache);
	if(size<100){
		title='';
	}
	photo_show_image(imagelink,title);
}
function photo_put_linked(product_id,size,number,title,product_image,nocache){
	var bigimage=photo_get_link(product_id,'big',number,product_image,nocache);
	document.write("<a href='javascript:;' onclick=\"photo_open_pop('"+title.replace(/["']/g,'')+"','"+bigimage+"');return false;\">"); // '
	photo_put(product_id,size,number,title,product_image,nocache);
	document.write("</a>\n");
}
function photo_gallery(product_id,title,product_image){
	var picdata=photo_parse(product_id,product_image);
	photo_itemviewpage_mainpic=picdata;
	if( !picdata.images){
		return 1;
	}
	if( picdata.imagecount == 1){
		photo_put_linked(product_id,300,0,title,product_image);
		return 1;
	}
	
	document.write('<table cellspancing="0" cellpadding="0" border="0"><tr>');
	var lastpic;
	var cnt=1;
	for (var i in picdata.images){
		if(i.length>2){
			continue;
		}
		document.write('<td>');
		photo_put_linked(product_id,300,i,title,product_image);
		document.write('</td>');
		lastpic=i;
		if(cnt == 2){
			document.write('</tr><tr>');
			cnt=1;
		}else{
			cnt=2;
		}
	}
	document.write("</tr></table>\n");
	document.write(Translation_photo.zoom+'<br>');
	if(picdata.defaultimage){
		photo_toggle(picdata.defaultimage);
	}else{
		photo_toggle(lastpic);
	}
}
function photo_toggle(target){
	var picdata=photo_itemviewpage_mainpic;
	for (var i in picdata.images){
		if(i.length>2){
			continue;
		}
		var tmp=document.getElementById('photo'+i);
		if(tmp!=null){
			if(i == target){
				try{tmp.style.display = '';}catch(e){}
			}else{
				try{tmp.style.display='none';}catch(e){}
			}
		}
	}
}
/*function photo_popupupload_open(element){
	var x,y;
	x = (screen.width - 300) / 2;
	y = (screen.height - 50) / 2
	msgWindow=window.open("","Vateraload","scrollbar=no,menubar=no,width=300,height=80,screenX="+x+",screenY="+y);
	msgWindow.document.write("<HEAD><TITLE> Vatera - "+Translation_photo.upload+" </TITLE></HEAD>")
	msgWindow.document.write("<CENTER><BIG><img src='http://img.vatera.hu/images/item/add/wait.gif'> "+Translation_photo.uploadprogress+"</BIG></CENTER>")
	msgWindow.focus();
	element.form.submit();
	window.onunload=photo_popupupload_close;
	return true;
}
function photo_popupupload_close(){
	msgWindow.close();
}*/

