// JavaScript Document

// AJAX INIT STUFF -- START
var xmlhttp=null;

try
{
	xmlhttp = new XMLHttpRequest();
}
catch (error)
{
	try
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (error)
	{
		xmlhttp = null;
	}
}
//  END

var scrollCell, contentHeight, timer, lefttime, topStop, theTimer;

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function initialize(){
	scrollCell = document.getElementById('scrollCell');
	contentHeight = scrollCell.offsetHeight; //alert(contentHeight);
	scrollCell.style.top = '0px';
	topStop = 0;
}

function scrollDown(stopScroller){	
	if(stopScroller){
		clearInterval(timer);
	}else{
		//alert(scrollCell.style.top);
		timer=setInterval("moveScroll(true)",50);
	}
}

function scrollUp(stopScroller){	
	if(stopScroller){
		clearInterval(timer);
	}else{
		//alert('start');
		timer=setInterval("moveScroll(false)",50);
	}
}

function moveScroll(down){
	scrollCell = document.getElementById('scrollCell');
	if(down){
		if(parseInt(scrollCell.style.top) > ((contentHeight-300)*-1)){
			scrollCell.style.top = parseInt(scrollCell.style.top) - 16 + 'px';
		}
		//document.getElementById('echo').innerHTML = parseInt(scrollCell.style.top) + '--' + (contentHeight*-1);
	}else{
		if(parseInt(scrollCell.style.top) < 0){
			scrollCell.style.top = parseInt(scrollCell.style.top) + 16 + 'px';
		}
		//document.getElementById('echo').innerHTML = parseInt(scrollCell.style.top) + '--' + (contentHeight*-1);
	}
}

function hideMenu(whichMenu){
	//alert(whichMenu);
	divtohide=document.getElementById(whichMenu);
	divtohide.style.display = 'none';
}

function hideAllMenus(doNotHideMenu){
	document.getElementById('dropMenu1').style.display = 'none';
	document.getElementById('dropMenu2').style.display = 'none';
	document.getElementById('dropMenu3').style.display = 'none';
	document.getElementById('dropMenu4').style.display = 'none';
	document.getElementById('dropMenu5').style.display = 'none';
	document.getElementById('dropMenu6').style.display = 'none';
	document.getElementById('dropMenu7').style.display = 'none';
}

function changeBack(whichMenu){
	//alert(whichMenu);
	theTimer = setTimeout("hideMenu('"+whichMenu+"')", 400);
} 

function showMenu(whichMenu, whichAnchor, positionx, positiony){
	hideAllMenus(whichMenu);
	clearTimeout(theTimer);
	positionMenu(whichMenu, whichAnchor, positionx, positiony);
	document.getElementById(whichMenu).style.display = 'block';		
}

function positionMenu(whichMenu, whichAnchor, positionx, positiony){
	var c = getAnchorPosition(whichAnchor);
	//alert("X = "+c.x+" , Y = "+c.y);
	if (document.getElementById) {
		var o = document.getElementById(whichMenu);
		//alert(o.style.left);
		if (o.style) {
			o.style.left = (c.x + positionx) + 'px';
			o.style.top = (c.y + positiony) + 'px';
		}
	}
}

function createImagesForm(){ //alert('fire');
	numberImages = document.getElementById('numberOfImages').value; //alert(numberImages);
	xmlhttp.open("GET", "/admin/ajaxHandler.php?action=createImagesForm&numberImages=" + numberImages);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			document.getElementById('addImagesForm').innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}

function getPhoto(photoID){ //alert('fire');	
	xmlhttp.open("GET", "/ajaxHandler.php?object=getPhoto&photoID=" + photoID);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			photoElementsString = xmlhttp.responseText; //alert(photoElementsString);
			photoElementsArray = photoElementsString.split('|');
			document.getElementById('photoCaption').innerHTML = photoElementsArray[1];
			document.getElementById('largePhoto').innerHTML = photoElementsArray[0];
			document.getElementById('currentPhoto').value = photoID;
		}
	}
	xmlhttp.send(null);
}

function getPhotoThumbs(pageNumber){ //alert('fire');	
	xmlhttp.open("GET", "/ajaxHandler.php?object=getPhotoThumbs&pageNumber=" + pageNumber);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			photoCatalogString = xmlhttp.responseText;  //alert(photoCatalogString);
			photoCatalogArray = photoCatalogString.split('~');
			document.getElementById('nextPrevPhotos').value = photoCatalogArray[1];
			document.getElementById('photoCatalog').innerHTML = photoCatalogArray[0];
		}
	}
	xmlhttp.send(null);
}

function nextPhoto(){
	photoIDsArray = document.getElementById('nextPrevPhotos').value.split('|');
	currentPhoto = document.getElementById('currentPhoto').value;
	
	for(i=0; i<photoIDsArray.length; i++){
		if(currentPhoto == photoIDsArray[i]){
			photoIdPosition = i;
		}
	}
	if(photoIdPosition == (photoIDsArray.length - 1)){
		getPhoto(photoIDsArray[0]);
	}else{
		getPhoto(photoIDsArray[(photoIdPosition + 1)]);
	}
}

function previousPhoto(){
	photoIDsArray = document.getElementById('nextPrevPhotos').value.split('|');
	currentPhoto = document.getElementById('currentPhoto').value;
	
	for(i=0; i<photoIDsArray.length; i++){
		if(currentPhoto == photoIDsArray[i]){
			photoIdPosition = i;
		}
	}
	if(photoIdPosition == 0){
		getPhoto(photoIDsArray[(photoIDsArray.length - 1)]);
	}else{
		getPhoto(photoIDsArray[(photoIdPosition - 1)]);
	}
}

function getVideo(videoID){ //alert('fire');	
	xmlhttp.open("GET", "/ajaxHandler.php?object=getVideo&videoID=" + videoID);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			document.getElementById('videoCell').innerHTML = xmlhttp.responseText; //alert(photoElementsString);
		}
	}
	xmlhttp.send(null);
}

function getVideoThumbs(pageNumber){ //alert('fire');	
	xmlhttp.open("GET", "/ajaxHandler.php?object=getVideoThumbs&pageNumber=" + pageNumber);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			document.getElementById('videoListing').innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}

function showCommission(commissionID){
	xmlhttp.open("GET", "/ajaxHandler.php?object=getCommission&commissionID=" + commissionID);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			document.getElementById('commissionFrame').innerHTML = xmlhttp.responseText; //alert(photoElementsString);
		}
	}
	xmlhttp.send(null);
}

function getIllustrationMiddle(section, imageID){
	xmlhttp.open("GET", "/ajaxHandler.php?action=getMiddleIllustration&imageID=" + imageID + "&section=" + section);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			document.getElementById('middleImage').innerHTML = xmlhttp.responseText; //alert(photoElementsString);
		}
	}
	xmlhttp.send(null);
}

function getDesignMiddle(section, imageID){
	xmlhttp.open("GET", "/ajaxHandler.php?action=getMiddleDesign&imageID=" + imageID + "&section=" + section);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			document.getElementById('middleImage').innerHTML = xmlhttp.responseText; //alert(photoElementsString);
		}
	}
	xmlhttp.send(null);
}

function getBooksMiddle(section, imageID){
	xmlhttp.open("GET", "/ajaxHandler.php?action=getMiddleBooks&imageID=" + imageID + "&section=" + section);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			document.getElementById('middleImage').innerHTML = xmlhttp.responseText; //alert(photoElementsString);
		}
	}
	xmlhttp.send(null);
}

function catalogRollover(middleImage, section, main){
	document.getElementById('middleImage').innerHTML = '<img src="/images/' + main + '/' + section + '/middle/' + middleImage + '">';
}

function enlargeImage(file, page, section){
	document.getElementById('popupBkg').style.display = 'block';
	document.getElementById('popupImage').style.display = 'block';
	
	xmlhttp.open("GET", "/ajaxHandler.php?action=enlargeImage&page=" + page + '&section=' + section + '&file=' + file);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			document.getElementById('popupImage').innerHTML = xmlhttp.responseText; //alert(photoElementsString);			
		}
	}
	xmlhttp.send(null);
}

function hidePopup(){
	document.getElementById('popupBkg').style.display = 'none';
	document.getElementById('popupImage').style.display = 'none';
}

function shirtColorOption(color){
	document.getElementById('shirtColorWord').innerHTML = color.toUpperCase();
	color = color.split(' ');
	color = color.join('');
	document.getElementById('shirtImage').innerHTML = '<img src="/images/tshirts/' + color + '.gif" width="383" height="350">';
}

function showDefaultShirt(){
	shirtColor = document.getElementById('shirtColor').value;
	document.getElementById('shirtColorWord').innerHTML = shirtColor.toUpperCase();
	shirtColor = shirtColor.split(' ');
	shirtColor = shirtColor.join('');
	document.getElementById('shirtImage').innerHTML = '<img src="/images/tshirts/' + shirtColor + '.gif" alt="' + shirtColor + '" width="383" height="350">';
}

function setShirt(color){
	document.getElementById('shirtColor').value = color;
}

function calcShirtTotal(){
	qty =	document.getElementById('qty').value;
	if(isInteger(qty)){
		if(qty > 10){
			total = qty * 10;
		}else if(qty > 5 && qty < 11){
			total = qty * 12;
		}else{
			total = qty * 15;
		}
	}else{
			total = 15;
			document.getElementById('qty').value = 1;
	}
	document.getElementById('shirtTotal').innerHTML = total;
}

function isInteger(s) {
	return (s.toString().search(/^-?[0-9]+$/) == 0);
}

// getAnchorPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the page.
function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
	var coordinates=getAnchorPosition(anchorname);
	var x=0;
	var y=0;
	if (document.getElementById) {
		if (isNaN(window.screenX)) {
			x=coordinates.x-document.body.scrollLeft+window.screenLeft;
			y=coordinates.y-document.body.scrollTop+window.screenTop;
			}
		else {
			x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
			y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
			}
		}
	else if (document.all) {
		x=coordinates.x-document.body.scrollLeft+window.screenLeft;
		y=coordinates.y-document.body.scrollTop+window.screenTop;
		}
	else if (document.layers) {
		x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}

function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
}	

function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}

function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
}












