


/*
 *  clickthrough
 *  does the image loading/rotating shebang 
 *  depends on Tween.js and prototype.js
 *  THIS IS A JavaScript MACARONI!
 *
/* ---------------------------------------- */

var load_all_sections = false;
var current_section = '';
var current_position = 1;
var image_count = 0;
var animation_lock = 0;


// main launcher function 
function loadClickThrough(){
  if (typeof sections != "undefined" && typeof images != "undefined"){
    setcurrent_section(current_section);
    showSections(); 
    showImages();
    showImageNav();
    if (load_all_sections){
      move('current_section');
    }else{
      moveTo(1);
    }
  }
}


function setcurrent_section(new_section){
  sections.each(function(section){
    if (section[0] == new_section || section[1] == new_section){
      current_section = section[0];
      throw $break;
    }else{
      current_section = sections[0][0];
    }
  });
}

function showImages(){
  var imagesHtml = '';
  image_count = 0;
  images.each(function(image){
    if(image[0] == current_section || load_all_sections){
      image_count++;
      imagesHtml += "<img src=\"" + image[1] + "\" alt=\"\"/>"; 
    }
  });
  $('image_roll').innerHTML = imagesHtml;
  image_width = (load_all_sections) ? 510 : 500;
  $('image_roll').style.width =  image_count * image_width + "px";
  $('image_roll').style.left = "0px";
}

function showImageNav(){
  var navHtml = '';
  count = 0;
  images.each(function(image){
    if(image[0] == current_section || load_all_sections){
      count ++;
      css_class = (count == current_position)? 'active' : '';
      navHtml += " <a href=\"\" class=\""+css_class+"\" onclick=\"moveTo("+count+"); return false;\">"+count+"</a>";
      if (count == current_position){
        $('image_description').innerHTML = image[2];
      }
    }
  });
  if ($('image_numbers')) $('image_numbers').innerHTML = navHtml;
}


function showSections(){
  sectionsHtml = old_section = '';
  sections.each(function(section){
    if (section[0] != old_section){
      css_class = (section[0] == current_section)? 'active' : '';
      if (load_all_sections){
        sectionsHtml += " <a href=\"\" class=\""+css_class+"\" onclick=\"setcurrent_section('"+section[0]+"'); move('current_section'); return false;\">"+ section[0] +"</a>";
      }else{
        sectionsHtml += " <a href=\"\" class=\""+css_class+"\" onclick=\"setcurrent_section('"+section[0]+"'); loadClickThrough(); return false;\">"+ section[0] +"</a>";
      }
      
      old_section = section[0];
      if ($(section[1])) $(section[1]).style.display = (section[0] != current_section)? "none" : "block";
    }
  });
  $('image_sections').innerHTML = sectionsHtml;
}


function moveTo(position){
  if (position <= image_count && animation_lock == 0){
    current_position = position;
    animation_lock = 1;
    image_width = (load_all_sections) ? 510 : 500;
    t = new Tween($('image_roll').style, 'left', Tween.regularEaseOut, parseInt($('image_roll').style.left), -(position-1)*image_width , 1, 'px');
    t.start();
    t.onMotionFinished = function(){animation_lock = 0;};
    showImageNav();
  }
}

function move(position){
  if (position == "prev"){
    if (current_position > 1){
      moveTo(current_position - 1);
    }
  }else if(position == 'next'){
    if (current_position <= image_count){
      moveTo(current_position + 1);
    }
    
  // print gallery specific stuff
  }else if(position == 'prev2'){
    if (current_position > 1){
      moveTo(current_position - 2);
      setcurrent_section(sections[(current_position-1)/2][0]);
      showImageNav();
      showSections();
    }
  }else if(position == 'next2'){
    if (current_position <= image_count){
      moveTo(current_position + 2);
      setcurrent_section(sections[(current_position-1)/2][0]);
      showImageNav();
      showSections();
    }
  }else if(position == 'current_section'){
    count = 0;
    images.each(function(image){
      count ++;
      if(image[0] == current_section){
        moveTo(count);
        throw $break;
      }
    });
    showSections();
  } 
}


/*
 *  navShow/Hide
 *  deals with mouseover thing  
 *  depends on prototype.js
 *
/* ---------------------------------------- */

var current_nav_selection = '';
function navShow(section){
  // setting initial nav state
  if (current_nav_selection == ''){
    if ($('evoke_nav').className == 'active'){
      current_nav_selection = 'evoke_nav';
    }else if ($('solutions_nav').className == 'active'){
      current_nav_selection = 'solutions_nav';
    }else{
      current_nav_selection = 'none';
    }
  }
  if (section.id == 'evoke_nav'){
    $('evoke_nav').className = 'active';
    $('solutions_nav').className = '';
  }else{
    $('evoke_nav').className = '';
    $('solutions_nav').className = 'active';
  }
  
}


// this function resets nav to its original state
// with a timeout so the reset is not rapid and jerky
var timer = null;
function navHide(section){
  clearTimeout(timer);
  timer = setTimeout(function(){
    if (section.id != current_nav_selection && current_nav_selection != 'none'){
      if (section.id == 'evoke_nav'){
        $('evoke_nav').className = '';
        $('solutions_nav').className = 'active';
      }else{
        $('evoke_nav').className = 'active';
        $('solutions_nav').className = '';
      }
    }else if (current_nav_selection == 'none'){
      $('evoke_nav').className = '';
      $('solutions_nav').className = '';
    }
  }, 1500);
}


// flipping function for most of the image/text boxes
function adventFlip(box){
  if (box.className.match('active') == "active"){
    box.className = box.className.replace(" active", "");
  }else{
    box.className += ' active';
  }
}

//


var contact_state = 'up';

function showContactLink(element){
  element.innerHTML = (contact_state == 'up') ? 'Contact' : 'Close';
}
function hideContactLink(element){
  element.innerHTML = (contact_state == 'down') ? 'Close' : 'C';
}

function toggleContactInfo(){
  if (animation_lock == 0){
    position = (contact_state == 'up') ? -1 : -100;
    $('contact_link').innerHTML = (contact_state == 'down') ? 'C' : 'Close';
    $('contact_link').className = (contact_state == 'down') ? 'up' : 'down'
    contact_state = (contact_state == 'up') ? 'down' : 'up';
    animation_lock = true;
    t = new Tween($('contact_info').style, 'top', Tween.regularEaseOut, parseInt($('contact_info').style.top), position , 1, 'px');
    t.start();
    t.onMotionFinished = function(){animation_lock = 0;};
  }
}



// identity clickthrough



var identity_images = [];
var current_identity = 0;

function launchIdentityGallery(){
  
  $('gallery_overlay').style.height = (getPageSize()[1] + 'px');
  $('gallery_overlay').style.width = (getPageSize()[2] + 'px');
  //centring viewport
  $('gallery_viewport').style.top = (getPageSize()[3]/2-212 + 'px');
  $('gallery_viewport').style.left = (getPageSize()[2]/2-260 + 'px');
  $('gallery_overlay').style.display = 'block';
  $('gallery_viewport').style.display = 'block';
  
  $('identity_image_top').innerHTML = "<img src=\"" + identity_images[current_identity][1] + "\" alt=''/>";
  $('identity_image_bottom').innerHTML = "<img src=\"" + identity_images[current_identity+1][1] + "\" alt=''/>";
  showIdentityInfo(current_identity);
}

function killIdentityGallery(){
  $('gallery_overlay').style.display = 'none';
  $('gallery_viewport').style.display = 'none';
}

function showIdentityInfo(id){
  $('image_name').innerHTML = identity_images[id][0];
  $('image_description').innerHTML = identity_images[id][2];
  $('image_counter').innerHTML = 'Image ' + (id + 1) + ' of ' + identity_images.length;
}

function fadeTo(position){
  if (animation_lock == false && !((current_identity == 0 && position == 'prev') || (current_identity == identity_images.length - 1 && position == 'next'))){
    
    animation_lock = true;

    next_identity = (position == 'next')? current_identity + 1 : current_identity - 1;
    $('identity_image_bottom').innerHTML = "<img src=\"" + identity_images[next_identity][1] + "\" alt=''/>";
    
    current_identity = (position == 'next')? current_identity + 1 : current_identity - 1;
    showIdentityInfo(current_identity);
    
    
    ot = new OpacityTween($('identity_image_top'), Tween.regularEaseOut, 100, 0, 1);
    ot.start();
    ot.onMotionFinished = function(){
      
      $('identity_image_top').innerHTML = "<img src=\"" + identity_images[current_identity][1] + "\" alt=''/>";
      // resetting opacity
      $('identity_image_top').style['opacity'] = 100;
      $('identity_image_top').style['-moz-opacity'] = 100;
      if($('identity_image_top').filters) $('identity_image_top').filters.alpha['opacity'] = 100;
      
      
      animation_lock = false;
    };
  }
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


//Check if we're in IE 6
var agt				= navigator.userAgent.toLowerCase();
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie6    = (is_ie && agt.indexOf("msie 6.")!=-1 );    


// Activate background image caching on IE6 to prevent image flicker
if (is_ie6) {
	document.execCommand('BackgroundImageCache', false, true);
}

