/* Pre load images, set max number of images */
var numImages = 10;
var imagez = new Array();

for(i=1;i<=numImages;i++) {
    imagez[i] = new Image();
    imagez[i].src = "/images/portfolio/photos/"+i+".jpg"
}


/* Keep track of things */
var onThumb = null;
var onCaption = null;
var photoPath = "/images/portfolio/photos/";

Event.observe(window, 'load', function() {
        init();
    } 
);

 
/*
on load checks for query params and loads default image  
attaches event handler
*/

function init() {
    var params = window.location.href.toQueryParams(); 
    
    if (params.photo == undefined) {
        loadMain(1);
        loadThumb("thumb_1","/images/portfolio/photos/thumbs/port_1_roll.jpg");
    } else {
        var photo = params.photo;
        loadMain(photo);

        var thumbID = "thumb_"+photo;
        var thumbPath = "/images/portfolio/photos/thumbs/port_"+photo+"_roll.jpg";
        
        loadThumb(thumbID,thumbPath);
        onThumb = photo;
    }
    
    navImages = $$('img.nav');
    for(i=0;i<navImages.length;i++) {
        $(navImages[i]).observe('mouseover',displayImage);
    }            
    
    
}

//attaches event handler to thumb, 
function displayImage(event) {
    if (onThumb != null) {
        var thumbID = "thumb_"+onThumb;
        var thumbPath = "/images/portfolio/photos/thumbs/port_"+onThumb+".jpg"; 
        loadThumb(thumbID,thumbPath); 
        
    } else {
        var thumbPath = "/images/portfolio/photos/thumbs/port_1.jpg"; 
        loadThumb('thumb_1',thumbPath);
        onThumb=1;             
    } 
    
    activeImage = event.target.id;
    
    var photo = activeImage.slice(6);
    loadMain(photo);

    var thumbID = "thumb_"+photo;
    var thumbPath = "/images/portfolio/photos/thumbs/port_"+photo+"_roll.jpg";                        
      
    loadThumb(thumbID,thumbPath); 
    
    onThumb = photo;               
}        

function loadMain(photoNumber) {  
    safari=(navigator.userAgent.indexOf('Safari')!=-1);
    
    if(safari) {
        $('main-image').src = "/images/global/spacer.gif";
        $('main-image').style.display = 'block';
        $('main-image').style.display = 'none';
        $('main-image').src = photoPath+photoNumber+".jpg";
        showCaption(photoNumber);
        $('main-image').style.display = 'inline';        
    } else {
        $('main-image').style.display = 'none';
        $('main-image').src = photoPath+photoNumber+".jpg";
        showCaption(photoNumber);
        Effect.Appear('main-image');    
    }     
}

function loadThumb(id,path) {
    $(id).src = path;
}
        
//update caption
function showCaption(captionNumber) {
    if(onCaption != null) {
        var id = "caption_"+captionNumber;
        $(onCaption).style.display = "none";
        $(id).style.display = "block";
        onCaption = id;
    } else {
        var id = "caption_"+captionNumber;
        $(id).style.display = "block";
        onCaption = id;            
    }
    return true;    
}
        
        