/**
 * @author Kelly
 */

function gallery_size_type()
{
	var width=null;
	var height=null;
}

  var gallery_EnableDebug=false;
  var gallery_ImageSize=new gallery_size_type();

  var NOIFRAMES=false;
  

function gallery_setOpacity(Element,value)
{
    Element.style.opacity = value/10;
    Element.style.filter = 'alpha(opacity=' + value*10 + ')';
}

function gallery_myInfoType()
{
    var Form=null;
    var BackgroundNode=null;
    var HasIFrame=false;
	var doc=null;
	var ImageList=null;
}



function gallery_SelfDestruct(Element)
{
	if (Element.parentNode)
	{
		Element.parentNode.removeChild(Element);
	}
}

function gallery_ShowElement(Element,DoNotSetPrevious)
{
	Element.style.overflow="auto";
	Element.style.visibility="visible";
	Element.style.display="block";
	if (DoNotSetPrevious==true)
		return;
	if (Element.secretWidth!=null)
		Element.width=Element.secretWidth;
	else
		Element.width=null;
	if (Element.secretHeight!=null)
		Element.height=Element.secretHeight;
	else
		Element.height=null;
}

function gallery_HideElement(Element)
{
	Element.secretWidth=Element.width;
	Element.secretHeight=Element.height;
	Element.style.overflow="hidden";
	Element.style.visibility="hidden";
	Element.style.display="none";

}

function gallery_GenerateImage(doc,ImageLoc,Caption,IsHidden)
{

	var Element=doc.createElement("span");
	var Image=doc.createElement("img");
	gallery_HideElement(Element);
	Image.src=ImageLoc;
	if (gallery_ImageSize.width!=null)
		Image.width=gallery_ImageSize.width;
	if (gallery_ImageSize.height!=null)
		Image.height=gallery_ImageSize.height;
	Element.style.width=Image.width;
	if (IsHidden==true)
	{
		gallery_HideElement(Element);
	}
	else
		gallery_ShowElement(Element,true);

	
	var theCaption=doc.createElement("span");
	theCaption.style.backgroundColor="black";

	theCaption.style.padding="4px";
	//theCaption.style.paddingLeft="5px";
	//theCaption.style.paddingRight="5px";
	theCaption.style.border="white 3px double";
	theCaption.style.position="relative";
	theCaption.style.top="8px";
	gallery_setOpacity(theCaption,6);
	//theCaption.style.overflow="visible";

	var innerCaption=doc.createElement("span");
	innerCaption.style.backgroundColor="transparent";
	innerCaption.style.color="white";
	innerCaption.style.position="relative";
	innerCaption.style.fontWeight="bold";
	//gallery_setOpacity(innerCaption,10);

	innerCaption.innerHTML=Caption;
	theCaption.appendChild(innerCaption);
	//theCaption.appendChild(doc.createElement("br"));

	Element.appendChild(Image);
	Element.appendChild(doc.createElement("br"));
	Element.appendChild(theCaption);
	Element.appendChild(doc.createElement("br"));
	Element.appendChild(doc.createElement("br"));
	return Element;
}

function gallery_LoadImages(doc,ImageArray,CaptionArray,container,KeepContainerContents)
{
	if (KeepContainerContents!=true)
		container.innerHTML="";
	var Images=new Array();
	for (var i=0;i < ImageArray.length;++i)
	{
		try
		{
			Images.push(gallery_GenerateImage(doc,ImageArray[i],CaptionArray[i],true));
			
		}
		catch(e)
		{
			Images.push(gallery_GenerateImage(doc,ImageArray[i],"",true));
		}
	}
	for (var k=0;k < Images.length;++k)
	{
		container.appendChild(Images[k]);
	}
	return Images;
}

function gallery_ApplyButtonStyle(button)
{
	button.style.border="black 1px solid";
	button.style.cursor="pointer";
	button.style.backgroundColor="palegoldenrod";
	button.style.margin="2px 3px 2px 3px";
	button.style.padding="0px 0px 0px 0px";
}

function gallery_LoadGalleryOfImages(doc,ImageArray,CaptionArray,index,top,left,width,height,ResponseEvent)
{
    var Form=doc.body;
    var ModalNode = doc.createElement("span");
    var BackgroundNode = doc.createElement("span");
   
    Form.appendChild(BackgroundNode);

    BackgroundNode.style.zIndex=170;
    BackgroundNode.style.position="absolute";
    BackgroundNode.style.left="0px";
    BackgroundNode.style.top="0px";
    BackgroundNode.style.height="200%";
    BackgroundNode.style.width="100%";
    BackgroundNode.style.backgroundColor="gray";
    BackgroundNode.style.display="inline";
    BackgroundNode.innerHTML="<br>";

    gallery_setOpacity(BackgroundNode,6);
   
    ModalNode.style.zIndex=170;
    ModalNode.style.display="inline";
    ModalNode.style.position="absolute";
	ModalNode.style.textAlign="center";
    if (left!=null)
    {
		ModalNode.style.left=left;
		ModalNode.CustomLeft=left;
	}
	else
		ModalNode.style.left=parseInt(window.screen.availWidth * .05)+"px";
	//alert(ModalNode.style.left);
	var scrolly=gallery_getScrollXY()[1];

    if (top!=null)
    {
		ModalNode.style.top=top;
		ModalNode.CustomTop=top;
	}
	else
		ModalNode.style.top=parseInt(window.screen.availHeight * .02)+"px";
	//alert(ModalNode.style.top);

	if (scrolly > 0)
		ModalNode.style.top=(scrolly+30)+"px";
    if (height!=null)
	{
		gallery_ImageSize.height=height;
		//ModalNode.style.height=height;
		//ModalNode.CustomHeight=height;
	}
	else
		ModalNode.style.height="65%";
    if (width!=null)
	{
		gallery_ImageSize.width=width;
		//ModalNode.style.width=width;
		//ModalNode.CustomWidth=width;
	}
	else
		ModalNode.style.width="60%";

    ModalNode.myInfo=new gallery_myInfoType();
    ModalNode.myInfo.Form=Form;
    ModalNode.myInfo.BackgroundNode=BackgroundNode;
	ModalNode.myInfo.doc=doc;

    ModalNode.id="ModalNode";
    ModalNode.style.overflow="visible";
	ModalNode.ImageIndex=index;
	ModalNode.ImageArray=ImageArray;
	ModalNode.CaptionArray=CaptionArray;
    ModalNode.style.marginRight="auto";
    ModalNode.style.marginLeft="auto";
	//ModalNode.style.border="solid 1px black";
	ModalNode.style.backgroundColor="transparent";


    var contentspancontainer=doc.createElement("div");
    ModalNode.appendChild(contentspancontainer);
    contentspancontainer.style.position="relative";
    //contentspancontainer.style.display="inline";
    contentspancontainer.style.left="0px";
    contentspancontainer.style.top="0px";
    //contentspancontainer.style.width="100%";
    //contentspancontainer.style.height="10px";
    contentspancontainer.style.overflow="visible";
    contentspancontainer.innerHTML=" ";

	ModalNode.ImageList=gallery_LoadImages(doc,ImageArray,CaptionArray,contentspancontainer);
	gallery_ShowElement(ModalNode.ImageList[ModalNode.ImageIndex]);
	
	//var tempimage=GenerateImage(doc,ModalNode.ImageArray[ModalNode.ImageIndex],ModalNode.CaptionArray[ModalNode.ImageIndex],true);
	//contentspancontainer.appendChild(tempimage);

	//ModalNode.appendChild(doc.createElement("br"));
	
	//var parentspantag=doc.createElement("div");
	//parentspantag.style.position="relative";
	//parentspantag.style.width="";
    var spantag=doc.createElement("span");
    spantag.style.position="relative";
  	spantag.style.display="block";
    spantag.style.left="0px";
    spantag.style.top="7px";
    spantag.style.height="34px";
    //spantag.style.width="100%";
    //spantag.style.marginRight="auto";
    //spantag.style.marginLeft="auto";
	//spantag.style.textAlign="center";
	//spantag.style.backgroundColor="#777232";
	//spantag.style.border="ridge 2px gray";

	
    var leftbutton=doc.createElement("input");
    leftbutton.type="button";
	if (ModalNode.ImageIndex <= 0)
		leftbutton.disabled=true;
	else
		leftbutton.disabled=false;
    leftbutton.value="Left";

    leftbutton.style.position="relative";
    leftbutton.style.left="0px";
    leftbutton.style.top="2px";
    leftbutton.style.width="60px";
    leftbutton.style.height="24px";
	gallery_ApplyButtonStyle(leftbutton);
    spantag.appendChild(leftbutton);

   var okbutton=doc.createElement("input");
    okbutton.type="button";
    okbutton.value="OK";
    okbutton.style.position="relative";
    okbutton.style.left="10px";
    okbutton.style.top="2px";
    okbutton.style.width="60px";
    okbutton.style.height="24px";
	gallery_ApplyButtonStyle(okbutton);
    spantag.appendChild(okbutton);

    var rightbutton=doc.createElement("input");
    rightbutton.type="button";
	if (ModalNode.ImageIndex >= ModalNode.ImageArray.length-1)
		rightbutton.disabled=true;
	else
		rightbutton.disabled=false;
    rightbutton.value="Right";
    rightbutton.style.position="relative";
    rightbutton.style.left="20px";
    rightbutton.style.top="2px";
    rightbutton.style.width="60px";
    rightbutton.style.height="24px";
	gallery_ApplyButtonStyle(rightbutton);
	
    spantag.appendChild(rightbutton);

 	//parentspantag.appendChild(spantag);
	ModalNode.appendChild(spantag);

  	ModalNode.appendChild(doc.createElement("br"));
	
    ModalNode.okbutton=okbutton;
    ModalNode.TextResponse="";

    ModalNode.style.overflow="visible";

   okbutton.onclick=function(){return gallery_ReturnPopUp(ModalNode,true,ResponseEvent);};
    rightbutton.onclick=function(){return Cycle_GalleryRight(ModalNode,leftbutton,rightbutton);};
    leftbutton.onclick=function(){return Cycle_GalleryLeft(ModalNode,leftbutton,rightbutton);};
     Form.appendChild(ModalNode);
	//ShowElement(tempimage);

}

function Cycle_GalleryRight(ModalNode,leftbutton,rightbutton)
{
	if (ModalNode.ImageIndex >= ModalNode.ImageArray.length-1)
		return;
	gallery_HideElement(ModalNode.ImageList[ModalNode.ImageIndex]);
	ModalNode.ImageIndex++;

	gallery_ShowElement(ModalNode.ImageList[ModalNode.ImageIndex]);
	//contentspancontainer.innerHTML=" ";

	//contentspancontainer.appendChild(GenerateImage(ModalNode.myInfo.doc,ModalNode.ImageArray[ModalNode.ImageIndex],ModalNode.CaptionArray[ModalNode.ImageIndex]));

	if (ModalNode.ImageIndex <= 0)
		leftbutton.disabled=true;
	else
		leftbutton.disabled=false;
   rightbutton.type="button";
	if (ModalNode.ImageIndex >= ModalNode.ImageArray.length-1)
		rightbutton.disabled=true;
	else
		rightbutton.disabled=false;

}

function Cycle_GalleryLeft(ModalNode,leftbutton,rightbutton)
{
	if (ModalNode.ImageIndex <= 0)
		return;
		
	gallery_HideElement(ModalNode.ImageList[ModalNode.ImageIndex]);
	ModalNode.ImageIndex--;

	gallery_ShowElement(ModalNode.ImageList[ModalNode.ImageIndex]);

  //  contentspancontainer.innerHTML="";

	//contentspancontainer.appendChild(GenerateImage(ModalNode.myInfo.doc,ModalNode.ImageArray[ModalNode.ImageIndex],ModalNode.CaptionArray[ModalNode.ImageIndex]));

	if (ModalNode.ImageIndex <= 0)
		leftbutton.disabled=true;
	else
		leftbutton.disabled=false;
   rightbutton.type="button";
	if (ModalNode.ImageIndex >= ModalNode.ImageArray.length-1)
		rightbutton.disabled=true;
	else
		rightbutton.disabled=false;

}

function gallery_ReturnPopUp(ModalNode,Result,ResponseEvent)
{
    try
    {
    
        gallery_setOpacity(ModalNode,3);
		try
		{
	       gallery_ResponseEvent(Result,ModalNode.TextResponse);
		}
		catch (e2)
		{
		}
        ModalNode.myInfo.Form.removeChild(ModalNode.myInfo.BackgroundNode);
        ModalNode.myInfo.Form.removeChild(ModalNode);
        return Result;
    }
    catch(e)
    {
        return false;
    }
}

function gallery_CycleGalleryRight(ModalNode,leftbutton,rightbutton,contentspancontainer)
{
	if (ModalNode.ImageIndex >= ModalNode.ImageArray.length-1)
		return;
	ModalNode.ImageIndex++;

    contentspancontainer.innerHTML=" ";

	contentspancontainer.appendChild(GenerateImage(ModalNode.myInfo.doc,ModalNode.ImageArray[ModalNode.ImageIndex],ModalNode.CaptionArray[ModalNode.ImageIndex]));

	if (ModalNode.ImageIndex <= 0)
		leftbutton.disabled=true;
	else
		leftbutton.disabled=false;
   rightbutton.type="button";
	if (ModalNode.ImageIndex >= ModalNode.ImageArray.length-1)
		rightbutton.disabled=true;
	else
		rightbutton.disabled=false;

}

function gallery_CycleGalleryLeft(ModalNode,leftbutton,rightbutton,contentspancontainer)
{
	if (ModalNode.ImageIndex <= 0)
		return;
	ModalNode.ImageIndex--;

    contentspancontainer.innerHTML="";

	contentspancontainer.appendChild(GenerateImage(ModalNode.myInfo.doc,ModalNode.ImageArray[ModalNode.ImageIndex],ModalNode.CaptionArray[ModalNode.ImageIndex]));

	if (ModalNode.ImageIndex <= 0)
		leftbutton.disabled=true;
	else
		leftbutton.disabled=false;
   rightbutton.type="button";
	if (ModalNode.ImageIndex >= ModalNode.ImageArray.length-1)
		rightbutton.disabled=true;
	else
		rightbutton.disabled=false;

}

//this function shoud be useful for debugging
function gallery_OpenWindowWithText(param_Text)
{
	WindowHandle=window.open ("", "_blank", "height=480, width=800, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no");
	Headers="<html><head></head><body><p mode=\"wrap\"><xmp style=\"word-wrap: normal; word-break:break-all; WIDTH: 100%; HEIGHT: 100%\">";
	End="</xmp></p></body></html>";
	WindowHandle.document.write(Headers+param_Text+End);
	return WindowHandle;
}

function gallery_OpenExceptionWindow(param_Text)
{
	WindowHandle=window.open ("", "_blank", "height=480, width=800, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no");
	WindowHandle.document.write(param_Text);
	return WindowHandle;
}

function gallery_getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;NOIFRAMES
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}


///OLD CODE

/*function gallery_LoadGallery(doc,ImageArray,CaptionArray,index,top,left,width,height,showcontainer,ResponseEvent)
{
	
    var Form=doc.body;
    var ModalNode = doc.createElement("span");
    var BackgroundNode = doc.createElement("span");
   
    Form.appendChild(BackgroundNode);

    BackgroundNode.style.zIndex=170;
    BackgroundNode.style.position="absolute";
    BackgroundNode.style.left="0px";
    BackgroundNode.style.top="0px";
    BackgroundNode.style.height="200%";
    BackgroundNode.style.width="100%";
    BackgroundNode.style.backgroundColor="gray";
    BackgroundNode.style.display="inline";
    BackgroundNode.innerHTML="<br>";

    gallery_setOpacity(BackgroundNode,6);
   
    ModalNode.style.zIndex=170;
    ModalNode.style.display="inline";
    ModalNode.style.position="absolute";
    if (left!=null)
    {
		ModalNode.style.left=left;
		ModalNode.CustomLeft=left;
	}
	else
		ModalNode.style.left="20%";
	var scrolly=gallery_getScrollXY()[1];
    if (top!=null)
    {
		ModalNode.style.top=top;
		ModalNode.CustomTop=top;
	}
	else
		ModalNode.style.top="30%";
	if (scrolly > 0)
		ModalNode.style.top=(scrolly+30)+"px";
    if (height!=null)
	{
		gallery_ImageSize.height=height;
		//ModalNode.style.height=height;
		//ModalNode.CustomHeight=height;
	}
	else
		ModalNode.style.height="60%";
    if (width!=null)
	{
		gallery_ImageSize.width=width;
		//ModalNode.style.width=width;
		//ModalNode.CustomWidth=width;
	}
	else
		ModalNode.style.width="60%";

    ModalNode.myInfo=new gallery_myInfoType();
    ModalNode.myInfo.Form=Form;
    ModalNode.myInfo.BackgroundNode=BackgroundNode;
	ModalNode.myInfo.doc=doc;
    if (showcontainer!=true)
	{
	}
	else
    {
		ModalNode.style.border="solid 1px black";
		ModalNode.style.backgroundColor="whitesmoke";
    }
    ModalNode.id="ModalNode";
    ModalNode.style.overflow="visible";
	ModalNode.ImageIndex=index;
	ModalNode.ImageArray=ImageArray;
	ModalNode.CaptionArray=CaptionArray;
    ModalNode.style.marginRight="auto";
    ModalNode.style.marginLeft="auto";

    var contentspancontainer=doc.createElement("div");
    ModalNode.appendChild(contentspancontainer);
    contentspancontainer.style.position="relative";
    //contentspancontainer.style.display="inline";
    contentspancontainer.style.left="0px";
    contentspancontainer.style.top="0px";
    //contentspancontainer.style.width="100%";
    //contentspancontainer.style.height="10px";
    contentspancontainer.style.overflow="visible";
    contentspancontainer.innerHTML=" ";

	var tempimage=GenerateImage(doc,ModalNode.ImageArray[ModalNode.ImageIndex],ModalNode.CaptionArray[ModalNode.ImageIndex],true);
	contentspancontainer.appendChild(tempimage);


    var spantag=doc.createElement("div");
    spantag.style.position="relative";
    //spantag.style.display="inline";
    spantag.style.left="0px";
    spantag.style.top="15px";
    spantag.style.height="34px";
    //spantag.style.width="100%";
    spantag.style.marginRight="auto";
    spantag.style.marginLeft="auto";

	
    var leftbutton=doc.createElement("input");
    leftbutton.type="button";
	if (ModalNode.ImageIndex <= 0)
		leftbutton.disabled=true;
	else
		leftbutton.disabled=false;
    leftbutton.value="Left";

    leftbutton.style.position="relative";
    leftbutton.style.left="0px";
    leftbutton.style.top="2px";
    leftbutton.style.width="60px";
    leftbutton.style.height="24px";
    spantag.appendChild(leftbutton);

   var okbutton=doc.createElement("input");
    okbutton.type="button";
    okbutton.value="OK";
    okbutton.style.position="relative";
    okbutton.style.left="10px";
    okbutton.style.top="2px";
    okbutton.style.width="60px";
    okbutton.style.height="24px";
    spantag.appendChild(okbutton);

    var rightbutton=doc.createElement("input");
    rightbutton.type="button";
	if (ModalNode.ImageIndex >= ModalNode.ImageArray.length-1)
		rightbutton.disabled=true;
	else
		rightbutton.disabled=false;
    rightbutton.value="Right";
    rightbutton.style.position="relative";
    rightbutton.style.left="20px";
    rightbutton.style.top="2px";
    rightbutton.style.width="60px";
    rightbutton.style.height="24px";
    spantag.appendChild(rightbutton);

 	ModalNode.appendChild(spantag);

  
	
    ModalNode.okbutton=okbutton;
    ModalNode.TextResponse="";

    ModalNode.style.overflow="visible";
	//if (gallery_IsIE6orOlder())
	//{
	//	gallery_EncapsulateWithAnIFrame(doc,ModalNode);
	//}

     rightbutton.onclick=function(){return CycleGalleryRight(ModalNode,leftbutton,rightbutton,contentspancontainer);};
    leftbutton.onclick=function(){return CycleGalleryLeft(ModalNode,leftbutton,rightbutton,contentspancontainer);};
   okbutton.onclick=function(){return gallery_ReturnPopUp(ModalNode,true,ResponseEvent);};
    Form.appendChild(ModalNode);
	ShowElement(tempimage);
}

function CycleGalleryRight(ModalNode,leftbutton,rightbutton,contentspancontainer)
{
	if (ModalNode.ImageIndex >= ModalNode.ImageArray.length-1)
		return;
	ModalNode.ImageIndex++;

    contentspancontainer.innerHTML=" ";

	contentspancontainer.appendChild(GenerateImage(ModalNode.myInfo.doc,ModalNode.ImageArray[ModalNode.ImageIndex],ModalNode.CaptionArray[ModalNode.ImageIndex]));

	if (ModalNode.ImageIndex <= 0)
		leftbutton.disabled=true;
	else
		leftbutton.disabled=false;
   rightbutton.type="button";
	if (ModalNode.ImageIndex >= ModalNode.ImageArray.length-1)
		rightbutton.disabled=true;
	else
		rightbutton.disabled=false;

}

function CycleGalleryLeft(ModalNode,leftbutton,rightbutton,contentspancontainer)
{
	if (ModalNode.ImageIndex <= 0)
		return;
	ModalNode.ImageIndex--;

    contentspancontainer.innerHTML="";

	contentspancontainer.appendChild(GenerateImage(ModalNode.myInfo.doc,ModalNode.ImageArray[ModalNode.ImageIndex],ModalNode.CaptionArray[ModalNode.ImageIndex]));

	if (ModalNode.ImageIndex <= 0)
		leftbutton.disabled=true;
	else
		leftbutton.disabled=false;
   rightbutton.type="button";
	if (ModalNode.ImageIndex >= ModalNode.ImageArray.length-1)
		rightbutton.disabled=true;
	else
		rightbutton.disabled=false;

}

*/
