// cu.com front page 
if(!window.spi){
	window.spi = new Object();
}
spi.storyboard = new Object();
spi.storyboard.stories = []
spi.storyboard.unlocked = true; // can the main image area be changed?
spi.storyboard.starting_image = null
spi.storyboard.images = new Array();
spi.storyboard.track_events = true; // do we call the event tracking code?

function STORY(sid){
	this.sid = sid
	this.dom = null
	this.heading = null
	this.copy = null
	this.links = null
	this.image = null
	this.image_url = null
	var s = document.getElementById(sid)
	if(s){ 
		this.dom = s
		var h = this.dom.getElementsByTagName('H2')
		if(h.length>0){ this.heading = h[0] }
		var d = this.dom.getElementsByTagName('DIV')
		if(d.length>0){ this.copy = d[0] }
		var i = this.dom.getElementsByTagName('IMG')
		if(i.length>0){
			this.image = i[0];
			this.image_url = (this.image.parentNode.tagName.toUpperCase()=='A') ? this.image.parentNode.href : null; 
		}
		var u = this.dom.getElementsByTagName('UL')
		if(u.length>0){ this.links = u[0] }
	}
}

function restoreMenu(){
	var main = document.getElementById('story_box');
	accordion('story_box',1000,0,500)
}

function restoreSplashImage(){
	var img = document.getElementById('storyboard_main_img')
	if(img && spi.storyboard.starting_image ){ 
		img.src = spi.storyboard.starting_image; // faster
	}
}

function getResetLink(label){
	var p = document.createElement('p');
	var a = document.createElement('a');
	a.onclick = function (){ 
		spi.storyboard.unlocked = true;
		restoreMenu(); 
		restoreSplashImage(); 
		if(spi.storyboard.track_events){ 
			pageTracker._trackEvent('Storyboard','back to all stories',label)
		}
		return false; 
	}
	a.href="#";
	a.innerHTML = 'Back to all stories';
	p.appendChild(a)
	return p;
}

function appendEventTracking(node){
	// tracking on links
	if(spi.storyboard.track_events){ 
		var aa = node.getElementsByTagName('A')
		for(var k=0;k<aa.length;k++){
			aa[k].onclick = function(){
				pageTracker._trackEvent('Storyboard','view category',this.innerHTML)
			};
		}
	}
}


function story(n){
	var s = getMenu(n)
	// get the story box
	var main = document.getElementById('story_box');
	if(main && s){
		// add clones of the story contents
		var heading = s.heading.cloneNode(true)
		var links = s.links.cloneNode(true); 
		appendEventTracking(links)
		main.innerHTML = ''; // start with a clean slate
		main.appendChild(heading)
		main.appendChild(s.copy.cloneNode(true))
		main.appendChild(links)
		// add a return link
		var rset = getResetLink(heading.innerHTML)
		main.appendChild( rset );
		// expand the story box
		spi.storyboard.unlocked  = false
		accordion('story_box',1000,0,500)
	}
}

function swap(n){
	var s = getMenu(n);
	var main = document.getElementById('main_splash_box');
	if(main && s && ! spi.storyboard.lock_main){
		var ii = main.getElementsByTagName('IMG')
		if(ii.length>0){ 
			_setDefaultImage(ii[0].src)
			ii[0].id = 'storyboard_main_img'
			cancelBlend()
			blendimage('main_splash_box','storyboard_main_img',s.image.src,800)
			linkImage(ii[0],s.image_url);
		}
	}
}

function linkImage(node,url){
	if(node){
		var parent = node.parentNode;
		var parent_is_link = (parent.tagName.toUpperCase()=='A') ? true : false;
		if(url && parent_is_link){
			// we have a URL and a linked image; replace the URL
			parent.href = url
		} else if(parent_is_link) {
			// we have NO URL but a linked image; remove the link from the image
			parent.parentNode.innerHTML = parent.innerHTML; 
		} else if(url){
			// we have a URL and the image is not linked; create a link around the image
			var a = document.createElement('a')
			a.href=url
			a.appendChild(node);
			parent.innerHTML = '';
			parent.appendChild(a);
		} else {
			// we have neither a URL nor a linked image; do nothing
		}
	}
}

function getMenu(obj){
	var href = obj.href
	var s = null
	if(href){
		var sid = _extractSID(href)
		if(sid){
			for(var i=0;i<spi.storyboard.stories.length;i++){
				var x = spi.storyboard.stories[i]
				if(x.sid==sid){ s = x; break; }
			}
		}
	}
	return s
}

function _extractSID(href){
	var sid = null
	a = href.indexOf('#');
	if(a>-1){ 
		sid = href.substr(a+1,href.length)
		if(sid.length==0){ sid=null
		}
	}
	return sid	
}

function _setDefaultImage(url){
	if(! spi.storyboard.starting_image ){ spi.storyboard.starting_image = url }
}

// call this once the stories have been loaded into the DOM
function collectStories(){
	// get the stories
	var stories = document.getElementById('stories');
	if(stories){
		count = 0;
		var x = stories.getElementsByTagName('DIV')
		for(var i=0;i<x.length;i++){
			var d = x[i]
			if(d.className=='story'){
				// duplicate the source content for our storyboard
				var id = d.id
				if(id.length>0){
					var s = new STORY(id)
					window.spi.storyboard.stories.push(s)
				}
				// image preload
				var imgs = d.getElementsByTagName('IMG')
				for(var j=0;j<imgs.length;j++){
					simplePreload(imgs[j].src)
				}
			}
			count ++
		}
	}
}

// adds event handlers to the menu items
function activateMenu(){
	var c = document.getElementById('main_text_menu')
	if(c){
		var aa = c.getElementsByTagName('A')
		for(i=0;i<aa.length;i++){ 
			var ln = aa[i]
			ln.onmouseover = function(){ 
				if(spi.storyboard.unlocked){
					swap(this)
					if(spi.storyboard.track_events){ 
						pageTracker._trackEvent('Storyboard','rollover story',this.innerHTML); 
					}
				}
			}
			ln.onmousedown = function(){ 
				story(this);
				spi.storyboard.unlocked = false;
				if(spi.storyboard.track_events){ 
					pageTracker._trackEvent('Storyboard','expand story',this.innerHTML)
				}
				return false; 
			}
			
		}
	}
	// detect if we should start with a story open
	var sid = _extractSID(window.location.href)
	if(sid){
		var a = new Object(); // not necessary to have a DOM element
		a.href = window.location.href
		swap(a)
		story(a)
	}
}

// open/close a container (div?) by adjusting the height 
function accordion(objid,millisec,minh,maxh){
	var div = document.getElementById(objid)
	if(div){
		//set the animation
		var d = maxh-minh
		var step_speed = Math.ceil(d/millisec)/4
		var h = div.style.height;
		var h = scrubHeightValue(div.style.height)
		var x = (h<maxh) ? 1 : -1; // are we going towards zero or infinity?
		var minmax = (h<maxh) ? maxh : minh; 
		if(x==1){ setDivHeight(objid,maxh)
		} else { setDivHeight(objid,minh)
		}
	}
}

function scrubHeightValue(h){
	h = String(h);
	if(h.length==0){ h = '0' }
	var y = h.indexOf('p'); if(y>-1){ h = h.substr(0,y); }
	h = Number(h)
	return h
}

function setDivHeight(objid,height){
	var h = scrubHeightValue(height)
	var div = document.getElementById(objid)
	if(div){ div.style.height = h+'px';
	}
}

function incrementDivHeight(objid,increment,maxh,minh){
	var div = document.getElementById(objid)
	if(div){ 
		var h = scrubHeightValue(div.style.height)+increment
		setDivHeight(objid,h)
		if(h>=maxh || h<=minh){ 
			clearInterval(spi.accordion) 
			spi.storyboard.unlocked = true

		}
	}	
}

// Example:
// simplePreload( '01.gif', '02.gif' ); 
function simplePreload(){
	var args = simplePreload.arguments;
	var a = spi.storyboard.images.length
	for(var i=0; i<args.length; i++){
		spi.storyboard.images[a+i] = new Image;
		spi.storyboard.images[a+i].src = args[i];
	}
}

// placeholder object in case the google version does not load as expected
// this should be overwritten by the real deal later in the page execution
var pageTracker = {_trackEvent:function(){}}
