// open links in a new window
function formatLinks(){
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
			anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
}

// popup window for video player
// james[at]cookiecrook[dot]com)
var sUserAgent = navigator.userAgent.toLowerCase();
var isIE = document.all?true:false;
var isNS4 = document.layers?true:false;
var isOp = (sUserAgent.indexOf('opera')!=-1)?true:false;

var isMoz = (sUserAgent.indexOf('mozilla/5')!=-1 && sUserAgent.indexOf('opera')==-1 && sUserAgent.indexOf('msie')==-1)?true:false;

function pop(oAnchor,sWindow,sProps){
	var sUrl = '';
	if(oAnchor.getAttribute) sUrl = oAnchor.getAttribute('href');
	if(sUrl=='' && isIE) sUrl = window.event.srcElement.getAttribute('href');
	if(sUrl=='') sUrl = oAnchor.href;
	var sWindowName = sWindow?sWindow:'_blank';
	if(!sProps) sProps = 'width=640,height=480,scrollbars,resizable,toolbar,status,menubar,location';
	if(sUrl) var oPopup = window.open(sUrl,sWindowName,sProps);
	// An Opera bug returns too early if you focus the window, so we don't focus it in that browser.
	// Only a noticable defect (in that browser) if a window is already open and hidden behind another window.
	if(oPopup && !isOp) oPopup.focus();
	// If popup was created successfully, cancel link in calling window.
	// Acts as regular link in browser that has popup blocking enabled.
	return (oPopup)?false:true;
}

// @name      The Fade Anything Technique
// @namespace http://www.axentric.com/aside/fat/
// @version   1.0-RC1-m
// @author    Adam Michela
// @modified  Richard Livsey / 28/04/05
// @modified  Adrian Bengtson / 06/10/05

var Fat = {
	make_hex : function (r,g,b) 
	{
		r = r.toString(16); if (r.length == 1) r = '0' + r;
		g = g.toString(16); if (g.length == 1) g = '0' + g;
		b = b.toString(16); if (b.length == 1) b = '0' + b;
		return "#" + r + g + b;
	},
	fade_all : function ()
	{
		var a = document.getElementsByTagName("*");
		for (var i = 0; i < a.length; i++) 
		{
			var o = a[i];
			var r = /fade-?(\w{3,6})?/.exec(o.className);
			if (r)
			{
				if (!r[1]) r[1] = "";
				if (!o.id)
					o.id = 'fader_'+Math.floor(Math.random()*100);
		
				Fat.fade_element(o.id,null,1500,"#"+r[1]);
			}
		}
	},
	fade_element : function (id, fps, duration, from, to) 
	{
		if (!fps) fps = 60;
		if (!duration) duration = 500;
		if (!from || from=="#") from = "#dfffb3";
		if (!to) to = this.get_bgcolor(id);

		if (this.timers[id])
			this.cancel_fade(id);
			
		this.timers[id] = new Array();
		
		var frames = Math.round(fps * (duration / 1000));
		var interval = duration / frames;
		var delay = interval;
		var frame = 0;
		
		if (from.length < 7) from += from.substr(1,3);
		if (to.length < 7) to += to.substr(1,3);
		
		var rf = parseInt(from.substr(1,2),16);
		var gf = parseInt(from.substr(3,2),16);
		var bf = parseInt(from.substr(5,2),16);
		var rt = parseInt(to.substr(1,2),16);
		var gt = parseInt(to.substr(3,2),16);
		var bt = parseInt(to.substr(5,2),16);
		
		var r,g,b,h;
		this.end = false;
		while (frame < frames)
		{
			r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames));
			g = Math.floor(gf * ((frames-frame)/frames) + gt * (frame/frames));
			b = Math.floor(bf * ((frames-frame)/frames) + bt * (frame/frames));
			h = this.make_hex(r,g,b);
		
			this.timers[id][this.timers[id].length] = setTimeout("Fat.set_bgcolor('"+id+"','"+h+"')", delay);

			frame++;
			delay = interval * frame; 
		}
		this.timers[id][this.timers[id].length] = setTimeout("Fat.set_bgcolor('"+id+"','"+to+"')", delay);
	},
	cancel_fade : function(id)
	{
		if (!this.timers[id])
			return;
		
		for (var i=0; i<this.timers[id].length; i++)
			clearTimeout(this.timers[id][i]);
	},
	set_bgcolor : function (id, c)
	{
		var o = document.getElementById(id);
		// var tmp = o.innerHTML; // Adrian debug
		// o.innerHTML = tmp + ' ' + c + ' '; // Adrian debug
		o.style.backgroundColor = c;
	},
	get_bgcolor : function (id)
	{
		var o = document.getElementById(id);
		while(o)
		{
			var c;
			/* if (window.getComputedStyle) c = window.getComputedStyle(o,null).getPropertyValue("background-color");
			if (o.currentStyle) c = o.currentStyle.backgroundColor;
			if ((c != "" && c != "transparent") || o.tagName == "BODY") { break; } */
			
			// This is the new part, changed by Adrian to solve a problem with Safari
			if (window.getComputedStyle) c = window.getComputedStyle(o,null).getPropertyValue("background-color");
			else if (o.currentStyle) c = o.currentStyle.backgroundColor;
			else c = document.defaultView.getComputedStyle(o,null).getPropertyValue('background-color');
			if ((c != "" && c != "transparent" && c != "rgba(0, 0, 0, 0)") || o.tagName == "BODY") { break; }
			// End of Adrian change

			o = o.parentNode;
		}
		if (c == undefined || c == "" || c == "transparent" || c == "rgba(0, 0, 0, 0)") c = "#FFFFFF"; // Also changed by Adrian
		var rgb = c.match(/rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
		if (rgb) c = this.make_hex(parseInt(rgb[1]),parseInt(rgb[2]),parseInt(rgb[3]));
		return c;
	},
	timers : new Array()
}

/*
	tableruler()
	written by Chris Heilmann for alistapart.
	enables a rollover of rows for each table with the classname Ã¢â‚¬Å“rulerÃ¢â‚¬Â
*/

function tableruler() {
	if (document.getElementById && document.createTextNode) {
		var tables=document.getElementsByTagName('table');
		for (var i=0;i<tables.length;i++) {
			if(tables[i].className=='ruler') {
				var trs=tables[i].getElementsByTagName('tr');
				for(var j=0;j<trs.length;j++) {
					if(trs[j].className=='odd') {
						if(trs[j].parentNode.nodeName=='TBODY') {
							trs[j].onmouseover=function(){this.className='ruled';return false}
							trs[j].onmouseout=function(){this.className='odd';return false}
						}
					}
					if(trs[j].className=='even') {
						if(trs[j].parentNode.nodeName=='TBODY') {
							trs[j].onmouseover=function(){this.className='ruled';return false}
							trs[j].onmouseout=function(){this.className='even';return false}
						}
					}
					if(trs[j].className=='selected') {
						if(trs[j].parentNode.nodeName=='TBODY') {
							trs[j].onmouseover=function(){this.className='ruled';return false}
							trs[j].onmouseout=function(){this.className='selected';return false}
						}
					}
				}
			}
		}
	}
}

// suckerfish dropdowns
// http://www.jspldog.com/articles/suckerfish/dropdowns/
sfHover = function() {
	var sfEls = document.getElementById("footernav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" hover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" hover\\b"), "");
		}
	}
}
// DISABLED BY JDILL, REPLACED WITH HTML NAV ELEMENTS.
//if (window.attachEvent) window.attachEvent("onload", sfHover);


// flavorFinder();
function mooFX() {
		fx.Background = Class.create();
		fx.Background.prototype = Object.extend(new fx.Base(), {
			initialize: function(el, options) {
				this.el = $(el);
				this.setOptions(options);
				this.now = 0;
			},
			increase: function() {
				this.el.style.backgroundPosition = "0 " + this.now + "px";
			},
			scroll: function(howMuch){
				this.clearTimer();
				this.custom(this.now, howMuch)
			}
		});

			myNewEffect = new fx.Background('test', {duration: 200});
	if (document.getElementById) {
		flavorFinder();
		if (document.getElementById("flavor_finder")) {
			document.getElementById("flavor_finder").style.height = "0";
			finderHeight = new fx.Height('flavor_finder', {duration: 500});
		}
	}
}

// flavor selector
function flavorFinder() {
	if (document.getElementById) {
		if (document.getElementById("altlink")) {
			var finderLink=document.getElementById("altlink");
			finderLink.onclick=function(){
				document.getElementById("flash_wrapper").style.height = "0";
				finderHeight.toggle();
			}
		}
	}
}

// close flavor selector from flash
function revert() {
	myNewEffect.scroll(0);
}

// close flavor selector from flash
function closeFinder() {
	if (document.getElementById) {
		document.getElementById("flash_wrapper").style.height = "480px";
		document.getElementById("flavor_finder").style.height = "0";
	}
}