
var previousColor;
var currentThumbIndex=0;
var thumbnails;
var thumbCount;

/*function nextImage(path,menuCount,color) {
	currentThumbIndex++;
	if(currentThumbIndex >= thumbCount) currentThumbIndex=0;
	
	image=thumbnails[currentThumbIndex];

	var name=getNameFromPath(image.src);
	var imagePath=path+name;
	
	mouseClick(name,imagePath,image,menuCount,color,currentThumbIndex);
}

function previousImage(path,menuCount,color) {
	currentThumbIndex--;
	if(currentThumbIndex < 0) currentThumbIndex=thumbCount-1;
	
	image=thumbnails[currentThumbIndex];

	var name=getNameFromPath(image.src);
	var imagePath=path+name;
	
	mouseClick(name,imagePath,image,menuCount,color,currentThumbIndex);
}*/

function setGalleryName(galleryHome) {
	select=document.GallerySelectForm.GalleryHomeMenu;
	index=select.selectedIndex;
	gallery=select[index].value;
	
	document.GallerySelectForm.GalleryName.value=gallery;
	document.GallerySelectForm.GalleryHome.value=galleryHome + "/" + gallery;
}

function popup(url,width,height) {
	if(width == null) width=630;
	if(height == null) height=400;
	window.open(url,"popup","status=0,height="+height+",width="+width+",resizable=1,dependent=1,fullscreen=0,scrollbars=1");
}

function setMenus(menuCount) {
	var name=document.mainImageForm.image.value;
	
	var indexOne=name.lastIndexOf('+')+1;
	var indexTwo=name.lastIndexOf('.');
	var disabled=null;
	if((indexOne > 0) && (indexTwo > indexOne)) {
		var sub=name.slice(indexOne,indexTwo);
		disabled=new Array(sub.length);
		for(var i=0; i < sub.length; i++) {
			disabled[i]="form"+sub.charAt(i);
		}
	}

	for(var i=1; i <= menuCount; i++) {
		var currentForm="form"+i;
		if(document.forms[currentForm]) document.forms[currentForm].item.disabled=false;
		if(document.forms["pp"+currentForm]) {
			document.forms["pp"+currentForm].quantity.disabled=false;
			document.forms["pp"+currentForm].addCartImg.disabled=false;
		}
		
		if(disabled == null) continue;
	
		for(var j=0; j < disabled.length; j++) {
			if(disabled[j] == currentForm) {
				document.forms[currentForm].item.disabled=true;
				document.forms["pp"+currentForm].quantity.disabled=true;
				document.forms["pp"+currentForm].addCartImg.disabled=true;
			}
		}
	}
}
		
function init(menuCount,color) {
	name=document.mainImageForm.image.value;
	document.mainImageForm.mainImageName.value=stripImageName(name);
	document.mainImageForm.image.value=name;
	
	for(var i=0; i < document.images.length; i++) {
		document.images[i].onmousedown=noDrag;
		document.images[i].ondragstart=noDrag;
		document.images[i].onselectstart=noDrag;
	}
	
	var table=document.getElementById("thumbnail-table");
	thumbnails=table.getElementsByTagName("img");
	thumbCount=thumbnails.length;
	thumbnails[0].style.borderColor=color;
	
	loading=false;
	
	setMenus(menuCount);
}

function inspect(elm){
  var str = "";
  for (var i in elm){
    str += i + ": " + elm.getAttribute(i) + "\n";
  }
  alert(str);
}

function inspectStyle(elm) {
  if (elm.style){
    var str = "";
    for (var i in elm.style){
	  str += i + ": " + elm.style[i] + "\n";
	}
	alert(str);
  }
}

function mouseOver(image,color) { 
	previousColor=image.style.borderTopColor;
	image.style.borderColor=color;
}
		
function mouseOut(image) { 
	image.style.borderColor=previousColor;
}
		
function mouseClick(name,path,image,menuCount,color,index,hasBW,bwimage) { 
	currentThumbIndex=index;
	
	image.style.borderColor=color;
	//previousColor=image.style.borderColor;
	previousColor=color;
			
	document.mainImage.src=path;
	document.mainImageForm.mainImageName.value=stripImageName(name);
	document.mainImageForm.image.value=name;
	
	setBWImage(hasBW,bwimage,menuCount);
	
	setMenus(menuCount);
}
		
function setItem(form,ppForm) { 
	if(form.item.disabled == true) return;
	//image=document.mainImageForm.mainImageName.value;
	image=document.mainImageForm.image.value;
	index=form.item.selectedIndex;
	price=form.item[index].value;
	name=form.item[index].text;
	sindex=name.indexOf(':');
	name=name.substring(0,sindex);
	ppForm.item_name.value=image+": "+name;
	ppForm.amount.value=price;
	
	ppForm.submit();
}

function stripImageName(name) {
	index=name.lastIndexOf('.');
	if(index > 0) name=name.substring(0,index);
	
	index=name.lastIndexOf('+');
	if(index > 0) name=name.substring(0,index);
	
	return name;
}

function getNameFromPath(path) {
	index=path.lastIndexOf('/');
	if(index > 0) name=path.substring(index+1);
	else name=path;
	
	name=name.replace(/%20/g," ");
	return name;
}

function noDrag() {
	return false;
}

function setBWImage(hasBW,bwimage,menuCount) {
	if(hasBW) {
		for(var i=1; i <= menuCount; i++) {
			var currentForm="ppform"+i;

			document.forms[currentForm].on1.value="";
			document.forms[currentForm].os1.value="";
		}
		
		document.mainImageForm.BlackAndWhite.checked=false;
		text=document.getElementById('bwText');
		
		if(bwimage) {
			document.mainImageForm.BlackAndWhite.disabled=false;
			document.mainImageForm.BlackAndWhite.style.visibility='visible';	
			text.style.visibility='visible';
		}
		else {
			document.mainImageForm.BlackAndWhite.disabled=true;
			document.mainImageForm.BlackAndWhite.style.visibility='hidden';
			text.style.visibility='hidden';
		}
	}
}

function colorToBW(imageDir,bwDir,menuCount) {
	var image=getNameFromPath(document.mainImage.src);
	var bw=document.mainImageForm.BlackAndWhite.checked;
	
	if(bw) document.mainImage.src=bwDir+"/"+image;
	else document.mainImage.src=imageDir+"/"+image;
	
	for(var i=1; i <= menuCount; i++) {
		var currentForm="ppform"+i;
		
		if(bw) {
			document.forms[currentForm].on1.value="Options";
			document.forms[currentForm].os1.value="B&W";
		}
		else {
			document.forms[currentForm].on1.value="";
			document.forms[currentForm].os1.value="";
		}
	}

}


