// JavaScript Document for Daniel Crane 
var galleryObj; 
//unobtrusive image scroll in gallery
var  preparegallery=function()
	{	
		//make sure DOM compatable
		if (!document.getElementsByTagName) return false;
		if(!document.getElementById) return false;
		
		// make sure elements exist
		
		if (!document.getElementById("gallery")) return false; 
		if (!document.getElementById("printgallery"))return false; 
		
		//apply style to  div
		var printgallery = document.getElementById("printgallery"); 
			
			printgallery.style.position="absolute"; 
			printgallery.style.top="0px"; 
			
		// attach animation behavior to up and down arrows
		
		var uparrow=document.getElementById("uparrow"); 
		var downarrow=document.getElementById("downarrow");
		
		uparrow.onmouseover=function()
			{
					 
					moveElement("printgallery","down",50); 
			}
		downarrow.onmouseover=function(){
				moveElement("printgallery","up",50);
				}
	 	uparrow.onmouseout=function()
			{
					 
					stopscroll("printgallery"); 
			}
		downarrow.onmouseout=function()
			{
					 
					stopscroll("printgallery"); 
			}		
	};

var prepareimgs=function()
	{
			//make sure DOM compatable
		if (!document.getElementsByTagName) return false;
			
		var galleryImgs=document.getElementsByTagName("div"); 
		
		for (var imgno=0;imgno<galleryImgs.length;imgno++)
			{
				if (galleryImgs[imgno].className.match("sidepic"))
						{
								galleryImgs[imgno].onmouseover=function()
									{
											this.style.background="#FBF6E2";
											this.style.border="1px solid #cccccc";
											var sizeset=false;
											for (var x=0;x<this.attributes.length;x++)
												{
														changetext(this.attributes[x].name,this.attributes[x].value);
														if(this.attributes[x].name !="size" && sizeset==false)
														{
															
																var sizeelem=document.getElementById("size");
																if(sizeelem)
																{
																sizeelem.innerHTML='';	
																}
															
														}
														else
														{
															sizeset=true;
														}
												}
											
											enlargeimg(this);
									}
								galleryImgs[imgno].onmouseout=function()
									{
											this.style.background="#FBF6E3";
											this.style.border="none";
									}
								galleryImgs[imgno].onclick=function()
									{
											this.style.background="#FBF6E2";
											this.style.border="1px solid #cccccc";
											var sizeset=false;
												for (var x=0;x<this.attributes.length;x++)
												{
														changetext(this.attributes[x].name,this.attributes[x].value);
														if(this.attributes[x].name !="size" && sizeset==false)
														{
															sizeelem=document.getElementById("size");
															if(sizeelem)
															{
																sizeelem.innerHTML='';	
															}	
														}
														else
														{
														sizeset=true;	
														}
												}
											enlargeimg(this);
									}
						}
			}
	};
function changetext(objText,newstr)
	{
	var r
	if (!document.getElementById) return false;
		
	if (!document.getElementById(objText)) return false;
		
		
		r = document.getElementById(objText);
		 r.innerHTML =newstr;
		 
		 
		 if (objText=='title')
		 {
			if (document.getElementById('currentImg'))
			{
				document.getElementById('currentImg').value=newstr;
				
			}
		 }	
	}
function enlargeimg(obj)
	{
		if (!document.getElementById) return false;
		var imgsrc=obj.getAttribute("largeimg");
		var largealt=obj.getAttribute("title");
		var zoomimg=obj.getAttribute("zoomimg");
		
		
		imgsrc="images/"+imgsrc;
		zoomimgsrc="images/"+zoomimg;
		
		var largeimg=document.getElementById("large");
		
		largeimg.setAttribute("src",imgsrc); 
		largeimg.setAttribute("alt",largealt);
		setpopup(zoomimgsrc,largealt);
	}
	
function setpopup(imgsrc,largealt)
	{
			if (!document.getElementById) return false;
			var zoomlink=document.getElementById("zoom");
			zoomlink.setAttribute("title",largealt);
			zoomlink.setAttribute("href",imgsrc);
	}
	
	
function moveElement(elementID,direction,increment)
	{	
		//check element exists
		
		if (!document.getElementById(elementID)) return false;
		
		var functioncmd= "doScroll('"+elementID+"',"+increment+",'"+direction+"')";
		
		galleryObj=document.getElementById(elementID);
		//scheck object property and stop it if it is already scrolling
		
		if (galleryObj.scrolling)	
				clearTimeout(galleryObj.scrolling);
				
			
				doScroll(elementID,increment,direction);
		
		
		

		}


function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


// function to start the scrolling 
function doScroll(elementID,amt,direction)
	{
			var inc=amt;
			var elementObj=document.getElementById(elementID);
			var ypos=elementObj.offsetTop;
			var finaly=direction+"arrow";
			//set the final resting point
			finaly=findPosY(document.getElementById(finaly));
			var lastpic=findPosY(document.getElementById("lastpic"));
			var firstpic=findPosY(document.getElementById("firstpic"));
						y=parseInt(ypos,10);
						
								
						if (direction=="up")
							{
							
							if(lastpic-200>finaly)
							{
								inc=Math.ceil((inc/1.75));
								y=y-inc;
							}
							
							}
						else
						{
						if(firstpic+400<finaly)
							{
							inc=Math.ceil((inc/1.75));
							y=y+inc;
							}
						
						}
							
						
						y=y+"px";
						elementObj.style.top = y;	
					var functioncmd= "doScroll('"+elementID+"',"+amt+",'"+direction+"')";
			 		elementObj.scrolling=setTimeout(functioncmd,215);
	}
//function to stop scrolling action by clearing the timeout
function stopscroll(elementID)
	{
			galleryObj=document.getElementById(elementID);
		
	
		if (galleryObj.scrolling)	
				clearTimeout(galleryObj.scrolling);
	}

// initiation code adopted ffrom J Keith DOM Scripting
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function doPopups() {

          var zoom = document.getElementById("zoom");
	      zoom.onclick = function() {
		 var zoomwindow=window.open("zoom.html", "Zoom",
    "width=500,height=500,resizable");
		 if (zoomwindow)
		 {
			
			
		  if (zoomwindow.open) {
					zoomwindow.close;
		     }
		  		zoomwindow.focus();
           		zoomwindow.document.write(writeJS(this.getAttribute("href"),this.getAttribute("title")));
				zoomwindow.document.close();
		 }
			

          return false;   
        }
		
      }

function popup()
{
		if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
		
  var pageLinks = document.getElementsByTagName("a");
  for (var i = 0; i < pageLinks.length; i++) 
  {
	  
	  if(pageLinks[i].className=='popup')
	  	{	
			 
			pageLinks[i].onclick=function()
				{
					var destination= this.getAttribute('href');
					 
					openPopUp(destination);
					return false;
				}
		}
		
			
   }
   
}

function openPopUp(linkURL) {
	
	if(linkURL !="") 
		{
		window.open(linkURL,'popup','scrollbars=yes,width=350,height=800');
		
	return false;}
}

function writeJS(imgsrc,imgalt){
if(null==imgalt)
{imgalt="";}
var str='';
str+='<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd">';
str+='<html xmlns="http:\/\/www.w3.org\/1999\/xhtml">';
str+='<head>';
str+='<meta http-equiv="Content-Type" content="text\/html; charset=iso-8859-1" \/>';
str+='<title>Daniel Crane Sporting Art<\/title>';
str+='<link href="style\/popup.css" rel="stylesheet" type="text\/css" \/>';
str+='<\/head>';
str+='<body>';
str+=' <div align="center">';
str+='<img id="mainimg"  border="1"  src="';
str+=imgsrc+'"  alt="'+imgalt+'" \/>';
str+='<br \/>'+imgalt;
str+=' <\/div>';
str+='<\/body>';
str+='<\/html>';
return(str);
}
writeJS();
//-->

	
	

addLoadEvent(preparegallery);
addLoadEvent(prepareimgs);	
addLoadEvent(doPopups);
addLoadEvent(popup);
