
/**
 *
 * onloads
 */
window.onload = onload_init;

function onload_init() {
  externalLinksByHref();
  init_slideshow(); // should only be called for homepage?
}

/*
**
** expandable portfolio navigation
**
*/

//not used? var nav = document.getElementById('navigation');
var navCurrent = -1;
var navList = new Array();

function navCollapseAll() {
  //console.debug('collapse')
  for (var i=0; i<navList.length; i++) {
    navList[i].className.replace('selected', '');
  }
  return true;
}

function navExpand(liIndex) {
  if (liIndex == navCurrent) {
    return false; // false meaning success (no need to change) 
  }
  if (!navList[liIndex]) {
    return true; // true meaning failure
  }
  if (!navCollapseAll()) {
    return true; // true meaning failure
  }	
  //console.debug('expand')
  navList[liIndex].className += ' selected';
  //navList[liIndex].style.display = 'block';
  return false; // false meaning success
}

function navInit() {
  if (document.getElementById('leftcol')) {
    var c = document.getElementById('leftcol').getElementsByTagName('ul')[2];
    var j=0;
    for (var i=0; i<c.childNodes.length; i++) {
      if (c.childNodes[i].nodeName == 'LI' 
    	  && (c.childNodes[i].childNodes[0].pathname == '/portfolio/' 
    		    || c.childNodes[i].childNodes[0].pathname == 'portfolio/')) {
        c.childNodes[i].childNodes[0].onclick = new Function('return navExpand(' + j + ');');
        navList[j] = c.childNodes[i];
        j++;
      }
    }
  }
  //console.debug(navList);
}




/**
 * the 'target' attribute is not XHTML compliant..
 *
 */
function externalLinksByHref() {
  var base_url = (document.URL ? document.URL : document.location.href).substr(0,(document.URL ? document.URL : document.location.href).indexOf('/',8));
  if (document.getElementsByTagName) {
    var anchors = document.getElementsByTagName('a');
    for (var i=0; i<anchors.length; i++) {
      var anchor = anchors[i];
      if ((anchor.getAttribute('href').indexOf('http:\/\/')==0 || anchor.getAttribute('href').indexOf('https:\/\/')==0)
          && anchor.getAttribute('href').indexOf(base_url)<0) {
        anchor.target = '_blank';
      }
    }
  }
  else if (document.links) {
    var anchors = document.links;
    for (var i=anchors.length-1; i>=0; i--) {
      var anchor = anchors[i];
      if ((anchor.href.indexOf('http:\/\/')==0 || anchor.href.indexOf('https:\/\/')==0)
           && anchor.href.indexOf(base_url)<0) {
        anchor.target = '_blank';
      }
    } 
  }
}


/**
 *
 * homepage thumbnail rotation
 */
var slideshow_enabled = false;
var slideshow_speed = 2400; //milliseconds
var slideshow_index = 16;
var slideshow_containers;
var slideshow_timer;
var slideshow_images = new Array();
var slideshow_links = new Array();
var slideshow_baseuri = '/files/portfolio';

function init_slideshow() {
  if (slideshow_enabled) {
    slideshow_containers = document.getElementById('image-grid').getElementsByTagName('img');
    slideshow_timer = setInterval('run_slideshow()', slideshow_speed);
  }
}

function run_slideshow() {
  //establish a random position in the grid
  var slideshow_grid_position = Math.floor(Math.random() * 16)
  //test that the next image is not already on screen
  for (i=0; i<slideshow_containers.length; i++) {
    if (i==slideshow_grid_position) {
      //no point testing the position that we intend to replace
      continue;
    }
    else if (slideshow_images[slideshow_index].slice(slideshow_images[slideshow_index].lastIndexOf('/')+1) 
    		   == slideshow_containers[i].src.slice(slideshow_containers[i].src.lastIndexOf('/')+1)) {
      //console.debug('conflict trying to replace: ' + (slideshow_containers[i].src.slice(slideshow_containers[i].src.lastIndexOf('/')+1)) + ' with: ' + slideshow_images[slideshow_index].slice(slideshow_images[slideshow_index].lastIndexOf('/')+1));
      //place new image in the conflict position
      slideshow_grid_position = i
    }
  }
  //change thumbnail
  slideshow_containers[slideshow_grid_position].src = slideshow_baseuri + '/' + slideshow_images[slideshow_index];
  //change link 
  slideshow_containers[slideshow_grid_position].parentNode.href = slideshow_links[slideshow_index];
  slideshow_index++;
  //console.debug('grid pos: ' + slideshow_grid_position + ' sequence: ' + slideshow_index + ' replace: ' + slideshow_images[slideshow_index]);
  if (slideshow_index == (slideshow_images.length - 1)) {
    slideshow_index=0;
  }
  
}



/**
 *
 * thumbnail rollovers
 */

var image_data = new Object();

function add_image_data(p_filename, p_link, p_title, p_publication, p_location, p_country, p_edition, p_client, p_credit, p_limited_edition_flag, p_rights_managed_flag, p_related) {
  var data = new Object();
  data['Image'] = p_title;
  data['Publication'] = p_publication;
  data['Edition'] = p_edition;
  data['Location'] = p_location;
  data['Country'] = p_country;
  data['Client'] = p_client;
  
  //disabled until useful
  //data['Credit'] = p_credit;
  //disabled until photo sales
  //data['Limited edition print'] = p_limited_edition_flag;
  //data['Rights managed image'] = p_rights_managed_flag;
  image_data[url_decode(p_filename)] = data;
  slideshow_images.push(p_filename);
  slideshow_links.push(p_link);
}

function reveal_image_info(p_imagesrc, tearsheet_flag) {
  if (tearsheet_flag == 1) {
    filename = url_decode(p_imagesrc.slice(p_imagesrc.indexOf('thumb/')+6));
  }
  else {
	filename = url_decode(p_imagesrc.slice(p_imagesrc.indexOf('portfolio/')+10));  
  }
  var t = document.getElementById('details');
  // remove all children from element t
  while (t.firstChild) {
    t.removeChild(t.firstChild);
  }
  // add new children
  for (fieldname in image_data[filename]) {
    if (image_data[filename][fieldname] != '' && image_data[filename][fieldname] != 0) {
      var p = document.createElement("p");
      p.innerHTML = '<span class="title">' + fieldname + '</span><br />' + "\n";
      if (image_data[filename][fieldname] != 1) {
        p.innerHTML = p.innerHTML + image_data[filename][fieldname];
      }
      t.appendChild(p);
    }
  }
  return false;
}



/**
 * misc utility
 *
 */
function print_r(theObj) {
  if(theObj.constructor == Array || theObj.constructor == Object) {
    document.write("<ul>");
    for(var p in theObj) {
      if(theObj[p].constructor == Array || theObj[p].constructor == Object) {
        document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>");
        print_r(theObj[p]);
        document.write("</ul>");
      }
      else {
        document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>");
  }
}

function url_decode(string) {
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = string;
   var decoded = '';
   var i = 0;
   while (i < encoded.length) {
     var ch = encoded.charAt(i);
     if (ch == "+") {
       decoded += " ";
       i++;
     }
     else if (ch == "%") {
      if (i < (encoded.length-2) 
          && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
          && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
        decoded += unescape( encoded.substr(i,3) );
        i += 3;
      } else {
        //console.debug( 'bad escape combination near ...' + encoded.substr(i) );
        decoded += "%[ERROR]";
        i++;
      }
    }
    else {
     decoded += ch;
     i++;
    }
  }
  return decoded;
}



//sponsors page ie6 fix
//onmouseover="this.className='transON'" onmouseout="this.className='transOFF'
//http://www.mandarindesign.com/opacity.html
