
// created by: Geoff Pack, Feb 2008
// last modified: Geoff Pack, August 2008

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function setLocation(location) {
	createCookie("local", location, 14);
}

// need to do this before onload event!
var abcLocal = readCookie("local");

function popIt(width, height) {
	if (width && height) {
		popup = window.open('', 'popup', 'width=' + width + ',height=' + height + ',,top=20,left=20,resizable=yes,status=no');
	 }
	else {
		popup = window.open('', 'popup', 'width=420,height=280,,top=20,left=20,resizable=yes,status=no');
	}
	popup.focus();
}


// Son of Suckerfish Dropdowns
// http://www.htmldog.com/articles/suckerfish/dropdowns/

sfHover = function() {
	var sfEls = document.getElementById("mainNav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


// Show/Hide functions clean up later...
function show(id) {
	document.getElementById(id).style.display = 'block';
}
function hide(id) {
	document.getElementById(id).style.display = 'none';
}

// Scroller

function scroll(N, id, deltaX, backButton, nextButton) {
	var n = 1;
	var x1 = 0;
	this.N = N;
	var obj = document.getElementById(id);

	this.back = function() {
		n--;
		if (n < 1) n = 1;
		x2 = (1-n) * deltaX;
		//document.getElementById(id).style.marginLeft = x2 + 'px';
		moveIt(id,x1,x2);
		x1 = x2;
		
		// modify buttons
		if (backButton && nextButton) {
			classRemove(document.getElementById(backButton),'off');
			classRemove(document.getElementById(nextButton),'off');
			if (n == 1) {classAdd(document.getElementById(backButton),'off')};
		}
		
		return false;	
	}
	this.next = function() {
		n++;
		if (n > N) n = N;
		x2 = (1-n) * deltaX;
		//document.getElementById(id).style.marginLeft = x2 + 'px';
		moveIt(id,x1,x2);
		x1 = x2;
		
		// modify buttons
		if (backButton && nextButton) {
			classRemove(document.getElementById(backButton),'off');
			classRemove(document.getElementById(nextButton),'off');
			if (n == N) {classAdd(document.getElementById(nextButton),'off')};
		}
		
		return false;	
	}
	this.pos = function(X) {
		x2 = (1-X) * deltaX;		
		moveIt(id,x1,x2);
		x1 = x2;
		n=X;
	
		return false;		
	}
}

//var weather = new scroll(4,'weatherScroller',210,'weatherBack','weatherNext');
var weather = new scroll(4,'weatherScroller',220,null,null);
var bestof = new scroll(3,'bestOfScroller',330,'bestOfBack','bestOfNext');

var scrollTimer=new Array(); // timeouts for each object using these fns

function moveIt(id,x1,x2) {
    // clear other scrolls for this object
    if (scrollTimer[id]) clearTimeout(scrollTimer[id]);

	vx = 40; // pixels
	if (x2<x1) vx=-Math.abs(vx);
    x1+=vx;
 
    // test to see if reached destination
    if (vx<0) {if (x1<=x2) {x1=x2; vx=0;}}
    else {if (x1>=x2) {x1=x2; vx=0;}}	

	document.getElementById(id).style.marginLeft = x1 + 'px';	
	if (vx!=0) scrollTimer[id] = setTimeout("moveIt('"+id+"',"+x1+","+x2+")",20);
}


function createWeatherNav () {
	var scroller = document.getElementById("weatherScroller");
	var nav = document.getElementById("weatherNav");
	
	if (nav) {
		// nav is null if location not set...	
		nav.innerHTML = "";
		var dayName;
		
		// get all the days in the Weather scroller
		var N = scroller.childNodes.length;
		var j=0;
		
		for (var i=0; i<N; i++) {	
			// more childnodes than just the days, so ignore others
			if (scroller.childNodes[i].id) {
				j++;
	
				// find day names
				dayName = scroller.childNodes[i].childNodes[1].childNodes[0].nodeValue.substring(0,3);
				
				// write into weatherNav	
				nav.innerHTML += '<a href="#" onclick="return weather.pos('+j+')">' + dayName + '</a> ';
			}
		}
	}
}

function findiViewLinks() {
	// converts furtherInfo links to iView into buttons next to the header
	var links = document.getElementsByTagName('A');

	for (var i=0, j=links.length; i<j; i++) {
		var link = links[i];
		
		if (link.href.indexOf('/iview/#/') !=-1) {
			var li = link.parentNode;
			var ul = li.parentNode;
			var div = ul.parentNode;
			
			// write link before heading			
			var newNode = link.cloneNode(true);
			div.insertBefore(newNode,div.firstChild);

			// change text to image
			newNode.innerHTML = '<img class="iView_button" src="/homepage/2008/styles/iview_button.gif" alt="watch this on iView" height="21" width="75">';			
			
			// remove li from list
			ul.removeChild(li);
		}
	}
}

addLoadEvent(createWeatherNav);
addLoadEvent(findiViewLinks);
