function getDocHeight() {
//Height of doc not viewport
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function galleryPictureClick(elem)
{   //Fn to display the modal box and load the image and caption
    //Elements are created in HTML and hidden
    var newImage=new Image();
    var imageId=elem.id.split("_");  //Image id is im3_4  where 3=gallery and 4 is the image id.
    $('galleryfade').style.height=getDocHeight()+"px";
    $('gallerymedium').style.top=posTop()+"px";
    $('galleryfade').show();
    $('gallerymedium').show();
    newImage.onload=function()
    {   //Create the onload function before changing the image source!
        picWidth=this.width;
        picHeight=this.height;
        pic=this;
        //Scriptaculous Morph for the animation
        $('imagebox').morph('width: '+picWidth+'px; height: '+picHeight+'px;',
        {afterFinish: function() {$('mediumimage').src=pic.src;
            $('caption').innerHTML=$('caption2').innerHTML=captions[imageId[1]];
            $('imageref').href='gallery_images.php?image='+imageId[1]+'&id='+imageId[0].replace("im","");
            Cufon.refresh('big');
            },
            duration: 0.5}
        );
    };
    newImage.src=elem.src.replace("_thumb.","_medium.");
}

function galleryPictureHide()
{   //HJide modal box
    $('galleryfade').fade();
    $('gallerymedium').fade({afterFinish: function() {
                $('caption').innerHTML="Loading...&nbsp;";
                $('caption2').innerHTML="";
                $('mediumimage').src='theme/wait_clock_yellow.gif';
                $('imagebox').style.height='';
                $('imagebox').style.width='';

            },
            duration: 0.5});
}
function copyElement(elem, target, suffix)
{   //Fn to copy an element into anothe part f the dom - i.e. a DIV or IMG
    //Requires prototype.  As its a copy it adds the suffix to the id to ensure uniqueness
    var element=$(elem);
    var copyElement=element.cloneNode(true);
    var copyLength=copyElement.childNodes.length-1;
    copyElement.id=elem+suffix;

    for(var i=0;copyLength>=i;i++)
    {
	    if(copyElement.childNodes[i].id)
        {
    	    var copyNode=copyElement.childNodes[i];
    	    var firstId=copyNode.id;
    	    copyNode.id=firstId+suffix; 
        }
    }
    $(target).appendChild(copyElement);
}
function ieFix(elem, height, fitHeight)
{  //ie css layout bug fix!
    margin=(fitHeight-height)/2;  //Negative if bigger than the required height!
    elem.style.margin='auto';
    elem.style.marginTop=margin+'px';
}
function addViewed(thisPic, image, id)
{   //Keeps track of the recently viewed images
    //if(isset($_COOKIE['viewedpics']))
    var viewedPics=new Array(); 
    viewedPics=unserialize($_COOKIE('viewedpics')); //Get the images viewed so far
    newViewedPics=new Array(); //We will want to alter the list order if the same pic is viewed twice
    thumbName= thisPic.replace(".jpg","_thumb.jpg");  //Get the image name and create the thumbnail name
    thumbName= thumbName.replace(".png","_thumb.png"); //Allow for jpgs, gifs and pngs  
    thumbName= thumbName.replace(".gif","_thumb.gif");
    $("s_carouselcontent").innerHTML="&nbsp;";
    for(i in viewedPics)
    { 
        if(viewedPics[i]!=null && viewedPics[i]!=thumbName+"|"+image+"|"+id)
        {   //re-order the images if the same is selected twice
            newViewedPics.push(viewedPics[i]);
            tmpDetails=viewedPics[i].split("|");
            $("s_carouselcontent").innerHTML+="<a href='gallery_images.php?image="+tmpDetails[1]+"&id="+tmpDetails[2]+"'><img id='viewedimage"+tmpDetails[1]+"_"+tmpDetails[2]+"'src='gallery/"+tmpDetails[0]+"' style='margin-right: 5px; height: 100%;'/></a>";
        }    
    }
    $("s_carouselcontent").innerHTML+="<a href='gallery_images.php?image="+image+"&id="+id+"'><img id='viewedimage"+image+"_"+id+"'src='gallery/"+thumbName+"' style='margin-right: 5px; height: 100%;'/></a>";
    newViewedPics.push(thumbName+"|"+image+"|"+id);  //Add the new image to the end
    resizeViewed(newViewedPics);
}

function resizeViewed(newViewedPics)
{    //Count the number of images and make room if too many
    if(typeof(newViewedPics)=='undefined') {
        var newViewedPics=new Array(); 
        var viewedPics=unserialize($_COOKIE('viewedpics')); //Get the images viewed so far
        for(i in viewedPics)
        { 
            newViewedPics.push(viewedPics[i]);
        }
    }
    var nowish=new Date();
    var contentDiv =  $('s_carouselcontent').childElements();
    var contentDivWidth=0;
    $('recentrow').style.display='';  //Need to display so we can get widths
    contentDiv.each(function(node){ contentDivWidth+=node.getWidth();});
    while($("s_carouselouter").offsetWidth<contentDivWidth && $('s_carouselcontent').firstDescendant()!=null)
    {   //Remove images off the front of the DIV and the cookie
        $('s_carouselcontent').firstDescendant().remove();
        contentDivWidth=0;
        contentDiv.each(function(node){ contentDivWidth+=node.getWidth();});
        newViewedPics.shift();
    }
    $('recentrow').style.display='none'; //Hide Again
    setcookie("viewedpics", serialize(newViewedPics), (Math.round(nowish.getTime()/1000)+3600));
}
