//where we have images
var imagesPath = 'http://www.zweitausendeins.de/img/imgbrowser/';
//for ajax function
var getUrl = 'http://www.zweitausendeins.de/getimage/';

var ie = (!window.opera && document.all);
var dom = (!document.all && document.getElementById);

function getImageId(imageName)
{
	var re = new RegExp("(\[0-9]+).jpg");
	var m = re.exec(imageName);
	var imageId = -1;
	if ((m != null)&&(m.index > 0))	imageId = m[1];
	return imageId;
}
	
function changeToNormalPicture(link)
{
	return link.replace("_tb_","_no_");
}
function changeToThumbnailPicture(link)
{
	return link.replace("_no_","_tb_");
}
function openBigWindow(img)
{
	if(img.src.indexOf('images.jpg') == -1)
		window.open(changeToNormalPicture(img.src),'image','width=250,height=170');
}
function ajaxManager()
	{
	var args = ajaxManager.arguments;
	switch (args[0])
		{
		case "load_image":
			//create connection
			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
			//pageWidth = (dom) ? innerWidth : document.body.clientWidth;
			gallery = document.getElementById("galleryLYR1");
			gallery2 = document.getElementById("galleryLYR2");
			imgTag = document.getElementById('image_tb1');
			imgTag2 = document.getElementById('image_tb2');
			imgTag3 = document.getElementById('image_tb3');
			nextTag = document.getElementById('idNext');
			prevTag = document.getElementById('idPrev');
			
			if (x)
				{
				x.onreadystatechange = function()
					{
					if (x.readyState == 2)
						{	
							//we had picture already
							if(args[1] > 0)
							{
								if(args[4] == 'forw')
								{
									imgTag.src = changeToThumbnailPicture(imgTag2.src);
									imgTag2.src = changeToNormalPicture(imgTag3.src);
									//imgTag.onclick = imgTag2.onclick;
									imgTag3.src = imagesPath+"wait20.gif";
								} else
								{
									//rev
									imgTag3.src = changeToThumbnailPicture(imgTag2.src);
									imgTag2.src = changeToNormalPicture(imgTag.src);
									//imgTag2.onclick = imgTag.onclick;
									imgTag.src = imagesPath+"wait20.gif";
								}
							} else
							{
								//we didn't have any picture
								imgTag.src = imagesPath+"wait20.gif";
								imgTag2.src = imagesPath+"wait20.gif";
								imgTag3.src = imagesPath+"wait20.gif";
							}

						}
					else if (x.readyState == 4 && x.status == 200)
						{
							emptyPrevNextTags = false;
							if(x.responseText.indexOf('no image') == -1)
							{
								if(args[4] == 'forw')
								{	
									//first time
									if(args[1] == 0)
									{
										//split names
										imagesName = x.responseText.split('|');
										img1Name = imagesName[0];
										img2Name = imagesName[1];
										img3Name = imagesName[2];
										//only one image
										if((img2Name.indexOf('*') != -1)&&(img3Name.indexOf('*') != -1))
										{
											//set big one 
											//alert(img2Name);
											//alert(img3Name);
											imgTag2.src = changeToNormalPicture(img1Name);
											//imgTag2.onclick = function(){openBigWindow(imgTag2)};
											imgTag.src = imagesPath+'images.jpg';
											leftArrow.src = imagesPath+'dot.jpg';
											imgTag3.src = imagesPath+'images.jpg';
											rightArrow.src = imagesPath+'dot.jpg';
											emptyPrevNextTags = true;
										} else
										{
											//get image tag
											imgTag.src = img1Name;
											//imgTag.onclick = function(){openBigWindow(imgTag)};
											//set second message
											if(img2Name.indexOf('*')==-1)
											{
												imgTag2.src = changeToNormalPicture(img2Name);
												//imgTag2.onclick = function(){ openBigWindow(imgTag2)};
											}	
											else
											{
												imgTag2.src = imagesPath+'images.jpg';
											}
											//and third
											if(img3Name.indexOf('*')==-1)
											{
												imgTag3.src = img3Name;
												//imgTag3.onclick = function(){ openBigWindow(imgTag3)};
											}	
											else
											{
												imgTag3.src = imagesPath+'images.jpg';
												//rightArrow.src = imagesPath+'dot.jpg';
											}
										}

									} 
									else
									{
										//we had pictures just load the second one
										imgTag3.src = x.responseText;
										//imgTag3.onclick = function(){ openBigWindow(imgTag3)};
										img2Name = imgTag2.src;
									}
								} else
								{
									imgTag.src = x.responseText;
								}
								
								if(!emptyPrevNextTags)
								{
									//set next/prev 
									imageId = getImageId(imgTag2.src);
									imageId2 = getImageId(imgTag.src);
									
									imageId3 = getImageId(imgTag3.src);
									if(img3Name.indexOf('*')==-1)
									{
										nextTag.onmousedown = function() {ajaxManager('cycle_gallery','forw',imageId3,args[2])};
										nextTag.style.cursor = 'pointer';
										rightArrow.src = imagesPath+'right_arrow.jpg';
									} else
									{
										nextTag.onmousedown = '';
										nextTag.style.cursor = 'auto';
										rightArrow.src = imagesPath+'dot.jpg';
									}
									prevTag.onmousedown = function(){ajaxManager('cycle_gallery','rev',imageId2,args[2])};
									prevTag.style.cursor = 'pointer';								
									leftArrow.src = imagesPath+'left_arrow.jpg';
								} else
								{
									prevTag.onmousedown = '';
									prevTag.style.cursor = 'auto';	
									leftArrow.src = imagesPath+'dot.jpg';
									nextTag.onmousedown = '';
									nextTag.style.cursor = 'auto';
									rightArrow.src = imagesPath+'dot.jpg';

								}
							} else
							{
								if(args[4] == 'forw')
								{
									imgTag3.src = imagesPath+'images.jpg';
									nextTag.onmousedown = '';
									nextTag.style.cursor = 'auto';
									rightArrow.src = imagesPath+'dot.jpg';
									imageId = getImageId(imgTag.src);
									prevTag.onmousedown = function() {ajaxManager('cycle_gallery','rev',imageId,args[2])};
									prevTag.style.cursor = 'pointer';
									leftArrow.src = imagesPath+'left_arrow.jpg';
								} else if(args[4] == 'rev')
								{
									imgTag.src = imagesPath+'images.jpg';
									nextTag.onmousedown = function() {ajaxManager('cycle_gallery','forw',0,args[2])};
									nextTag.style.cursor = 'pointer';
									rightArrow.src = imagesPath+'right_arrow.jpg';
									prevTag.onmousedown = '';
									prevTag.style.cursor = 'auto';	
									leftArrow.src = imagesPath+'dot.jpg';
								}
							}
						}
					}
				x.open("GET", getUrl+"?imageNo="+args[1]+"&movieNo="+args[2]+"&direction="+args[4], true);
				x.send(null);
				}
			break;

		case "cycle_gallery":
				ajaxManager('load_image', args[2], args[3], 'galleryLYR1',args[1]);
			break;
		case "start_up":
				ajaxManager('load_image', 0, args[1],'galleryLYR1','forw');
		break;
		}
	}