﻿var _current_index = 0;
var _current_page = 0;
var _max_page_size = 6; /* keep above 0 to avoid error */

function next_page()
{
	var _max_pages = max_pages();
		
	if( _current_page < _max_pages )
	{
	    _current_page += 1;
	}
	else
	{
	    _current_page = 0;
	}
	
	setup_gallery();
}

function previous_page()
{
    var _max_pages = max_pages();
    
	if( _current_page == 0 )
	{
	    _current_page = _max_pages;
	}
	else
	{
	    _current_page = _current_page - 1;
	}
	
	setup_gallery();
}			

function setup_gallery()
{
   if( page_start_index() < _imgs.length )
   {
        change_feature(page_start_index())
   }
   
   setup_thumbs();
}

function setup_thumbs()
{
    var tbl_gallery = document.getElementById('tbl_gallery');
   
   /* Remove all cells from the row */
   
   var cell_count = tbl_gallery.rows[0].cells.length
   
   for( c = 0; c < cell_count; c++ )
   {
        tbl_gallery.rows[0].deleteCell(0);
   }
         
   /* **** */ 
        
   for(x = page_start_index(); x <= page_end_index(); ++x )
   {
        var _cell = tbl_gallery.rows[0].insertCell(tbl_gallery.rows[0].cells.length); 
        _cell.style.width = "50px";
        _cell.style.backgroundColor = "#0C1F25";
                
        var _thumb = new Image()            
        _cell.appendChild(_thumb)
        
        if( _imgs[x] == null )
        {
             _thumb.src = "images/transparent.gif"
        }
        else
        {        
            _thumb.src = _imgs[x].thumbnail;           
            _thumb.style.border = "none";
            _thumb.style.width = "50px";
            _thumb.style.height = "50px";
            eval("_thumb.onmouseover = function () {  change_feature(" + x + "); this.style.cursor = 'hand' }")
        }
    }
   
   /* Create Previous Page Link */
   var _cell_prev_link = tbl_gallery.rows[0].insertCell(0); 
   _cell_prev_link.style.backgroundColor = "#0C1F25";
   
   var _a_previous = _cell_prev_link.appendChild(document.createElement("a"));
   _a_previous.setAttribute("href","javascript:void(0)")
   eval("_a_previous.onclick= function() { previous_page() }")
   
   var _img_previous = new Image()   
   _a_previous.appendChild(_img_previous)

   _img_previous.src = "images/portfolio_arrowleft.gif"
   _img_previous.style.border = "none";
   
  
  /* Create Next Page Link */ 
   
   var _cell_next_link = tbl_gallery.rows[0].insertCell(tbl_gallery.rows[0].cells.length); 
    _cell_next_link.style.backgroundColor = "#0C1F25";

   var _a_next = _cell_next_link.appendChild(document.createElement("a"));
   _a_next.setAttribute("href","javascript:void(0)")
   eval("_a_next.onclick= function() { next_page() }")
   
   var _img_next = new Image()   
   _a_next.appendChild(_img_next)      

   _img_next.src = "images/portfolio_arrowright.gif"
   _img_next.style.border = "none";
   
    
}

function change_feature(index)
{
    if( index < _imgs.length )
    {
        document.getElementById('img_feature').src = _imgs[index].large;        
        document.getElementById('gallery_index_info').innerHTML = index + 1 + ' of ' + _imgs.length
        
        if( _imgs[index].caption != "" )
        {
            document.getElementById('gallery_caption').innerHTML = ' - ' + _imgs[index].caption;        
        }
        else
        {
            document.getElementById('gallery_caption').innerHTML = "";
        }
    }      
}

function max_pages()
{
    return Math.ceil((_imgs.length / _max_page_size)) - 1;
}

function page_start_index()
{
    return (_current_page * _max_page_size);
}

function page_end_index()
{
    return ((_current_page * _max_page_size) + (_max_page_size - 1));
}

/* Image Object for the gallery */

function img()
{
    this.large = "";
    this.thumbnail = ""; 
    this.caption = "";
}

