/* COT support scripts. 
*/

var scrollWidth;
var caseBox;
var study_images;
var content_blocks;

init_page();

////////////////////////////////////

function adjust_container() {
	$("content_holder").style.width = innerWidthElt(window)+"px";
	$("content_holder").style.height = [565,innerHeightElt(window)].max()+"px";
	$("container").style.height = $("content_holder").style.height;//innerHeightElt(window)+"px";
}
function page_resized() {
	adjust_container();
}

function init_page() {
	document.observe("dom:loaded", do_init_page);
	Event.observe(window, "resize", page_resized);
}
function do_init_page() {
	init_scrollbar_width();
	
	//initial initialization of container height (depending on OS scroll height)
	var newheight = ($('content').getHeight()+scrollWidth)+"px";
	$('content_holder').style.height = newheight;
	$('container').style.height = newheight;
	adjust_container();
	
	$("content").style.width = $('nav_5').positionedOffset()[0]+$('nav_5').getWidth() + 30 +"px";
	
	//initting navigation links
	$$('#navmenu a').each(function(elt) {
		Event.observe(elt, 'click', navLinkClick);
	})
	
	//initting case studies links
	$$('#casenav a.nav').each(function(elt) {
		Event.observe(elt, 'click', caseNavClick);
	});
	$$('#content .main a.nav').each(function(elt) {
		Event.observe(elt, 'click', caseNavClick);
	});
	
	caseBox = new CaseBox();
	initLinks();
	setup_content_arrows();
}

function nav_goto(label) {
	new Effect.ContentScrollTo(label);
}
function navLinkClick(event) {
	new Effect.ContentScrollTo(event.target.rel);
	event.stop();
}
function caseNavClick(event) {
	// console.log("opening case study url:"+event.target.href);
	// console.log(event.target.tagName);
	event.stop();
	var targ = event.target;
	targ = $(targ);
	if (event.target.tagName=='A') {
		// console.log("going path 1")
		caseBox.openURL(event.target.href);
	} else {
		// console.log("going path 2");
		// console.log(targ);
		// console.log("go elt:"+targ.up());
		// console.log("HREF:"+event.target.up('a').href);
		caseBox.openURL(event.target.up('a').href);
	}
}

function init_scrollbar_width() {
	var withScroll, noScroll;
	withScroll = innerHeightElt($('scrollcalc'));
	$("scrollcalc_inner").hide();
	noScroll = innerHeightElt($('scrollcalc'));
	scrollWidth = noScroll-withScroll;
}

function innerHeightElt(elt) {
	return innerWidthHeight()[1];
}
function innerWidthElt(elt) {
	return innerWidthHeight()[0];
}


function innerWidthHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight]
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
}


function content_navprev(evt) {
	// console.log("going to previous link");
	
	var curElt = content_blocks.indexOf(evt.target.up(1).id);
	if (curElt>0 || curElt>= content_blocks.length) {
		new Effect.ContentScrollTo(content_blocks[curElt-1])
	}
	
	evt.stop();
}
function content_navnext(evt) {
	// console.log("going to next link");
	
	var curElt = content_blocks.indexOf(evt.target.up(1).id);
	if (curElt < content_blocks.length-1) {
		new Effect.ContentScrollTo(content_blocks[curElt+1]);
	}
	
	evt.stop();
}

function setup_content_arrows() {
	var holder, prevbut, nextbut;
	
	content_blocks = $$('#content .main .block');
	var totalblocks = content_blocks.length;
	
	$$('#content .main a.prev').each(function(elt) {
		Event.observe(elt, 'click', content_navprev);
	});
	$$('#content .main a.next').each(function(elt) {
		Event.observe(elt, 'click', content_navnext);
	});

	/*
	content_blocks.each(function(elt, idx) {	
		// this.contentpane = new Element('div', {id:'lb_pane'});
		holder = new Element('div');
		holder.className = 'navholder';
		
		prevbut = new Element('a');
		prevbut.className = 'prev';
		
		nextbut = new Element('a');
		nextbut.className = 'next';
		
		prevbut.innerHTML = "&nbsp;"; //"<i>&laquo;</i> BACK";
		nextbut.innerHTML = "&nbsp;"; //"NEXT <i>&raquo;</i>";
		
		Event.observe(prevbut, 'click', content_navprev);
		Event.observe(nextbut, 'click', content_navnext);
		
		if (idx>0) {
			holder.appendChild(prevbut);
		}
		if (idx<totalblocks-1) {
			holder.appendChild(nextbut);
		}
		elt.appendChild(holder);
	});
	*/
	content_blocks = content_blocks.pluck('id');
}

//PATCH for scriptaculous horizontal scrolling

Effect.ContentScrollTo = Class.create();

Effect.ContentScrollTo = Class.create(Effect.Base, {
  initialize: function(element) {
    this.element = $(element);
    if (!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({
      x: 0,
	  holder: null
    }, arguments[1] || { });
    this.start(options);
  },
  setup: function() {
	this.options.holder = $('content_holder');
	
	var eltOff = this.element.positionedOffset()[0];
		
	var scrolloff = this.options.holder.cumulativeScrollOffset();
	
	this.originalLeft = parseFloat(scrolloff[0] || '0');

	this.options.x = eltOff - this.originalLeft - 220;
  },
  update: function(position) {
	this.options.holder.scrollLeft = (this.options.x  * position + this.originalLeft).round();
  }
});

// external links handling
function initLinks() {
  for (i in document.links) {
    link = document.links[i];
    if (link.rel && link.rel.indexOf('external')!=-1) {
      link.onclick = onExternalLinkActivate;
      link.onkeypress = onExternalLinkActivate;
    }
  }
}

function onExternalLinkActivate() {
  window.open(this.href);
  return false;
}
