
// created by: Geoff Pack, Feb 2008
// last modified: Geoff Pack, Apr 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
		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);
		//scrollTo(id,x1,x2,10);
		x1 = x2;
		
		// modify buttons
		classRemove(document.getElementById(backButton),'off');
		classRemove(document.getElementById(nextButton),'off');
		if (n == N) {classAdd(document.getElementById(nextButton),'off')};
		
		return false;	
	}
}

var weather = new scroll(4,'weatherScroller',210,'weatherBack','weatherNext');
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);
}


