// JavaScript Document


// Begin Scroller Script
function verifyCompatibleBrowser(){ 
    this.ver=navigator.appVersion 
    this.dom=document.getElementById?1:0 
    this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0; 
    this.ie4=(document.all && !this.dom)?1:0; 
    this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
    this.ns4=(document.layers && !this.dom)?1:0; 
    this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5) 
    return this 
}

bw = new verifyCompatibleBrowser(); 
 
var speed=50; 
var loop, timer;
 
function ConstructObject(obj,nest){ 
    nest=(!nest) ? '':'document.'+nest+'.' 
    this.el=bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?eval(nest+'document.'+obj):0; 
    this.css=bw.dom?document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?eval(nest+'document.'+obj):0; 
    this.scrollHeight=bw.ns4?this.css.document.height:this.el.offsetHeight 
    this.clipHeight=bw.ns4?this.css.clip.height:this.el.offsetHeight 
    this.up=MoveAreaUp;this.down=MoveAreaDown; 
    this.MoveArea=MoveArea; this.x; this.y; 
    this.obj = obj + "Object" 
    eval(this.obj + "=this") 
    return this 
} 
function MoveArea(x,y){ 
    this.x=x;this.y=y 
    this.css.left=this.x + "px"; 
    this.css.top=this.y + "px";
} 
 
function MoveAreaDown(move){ 
	if(this.y>-this.scrollHeight+objContainer.clipHeight){ 
    this.MoveArea(0,this.y-move) 
    if(loop) setTimeout(this.obj+".down("+move+")",speed) 
	}
} 
function MoveAreaUp(move){ 
	if(this.y<0){ 
    this.MoveArea(0,this.y-move) 
    if(loop) setTimeout(this.obj+".up("+move+")",speed) 
	}
} 
 
function PerformScroll(speed){
	if(initialised){
		loop=true; 
		if(speed>0) {
			objScroller.down(speed);
		}
		else { 
			objScroller.up(speed);
		}
	} 
} 
 
function CeaseScroll(){ 
    loop=false 
    if(timer) clearTimeout(timer)
} 
var initialised;

function InitialiseScrollableArea(){ 
    objContainer=new ConstructObject('scroll_container') 
    objScroller=new ConstructObject('scroll_item','scroll_container') 
    objScroller.MoveArea(0,0) 
    objContainer.css.visibility='visible' 
    initialised=true; 
}

// End Scroller Script




// JavaScript Document

if (location.hostname == "dangermouse") {
		var _DOMAIN = 'dangermouse/web/dave_arden';
} else {
	var _DOMAIN = 'www.fraynework.com/staging/dave_arden';
}


function initRollovers() {
	var all_links;
	var link;
	var idx;
	var tmp_image;
	var section_id = getSectionId();
	
	if (!document.getElementsByTagName) {
		return;   
	}
	
	all_links = document.getElementsByTagName("a");
	
	for (idx = 0; idx < all_links.length; idx++) {
		link = all_links[idx];
		
		if (link.className && (' ' + link.className + ' ').indexOf(' rollover ') != -1) {
			if (link.childNodes && link.childNodes.length == 1 && link.childNodes[0].nodeName.toLowerCase() == 'img') {
				// Pre-load state 2 images
				tmp_image = new Image();
				tmp_image.src = link.childNodes[0].src.replace(/(\.[^.]+)$/, "_f1$1");

				if (link.childNodes[0].id && link.childNodes[0].id == section_id) {
					// Don't attach a listener. Instead, set rollover state permanently
					link.childNodes[0].src = link.childNodes[0].src.replace(/(\.[^.]+)$/, "_f2$1");
				} else {	
					// Attach listeners
					link.onmouseover = mouseover;
					link.onmouseout = mouseout;
					link.onfocus = mouseover;
					link.onblur = mouseout;
				}
			}
		}
	}
}

function findTarget(e) {
	var target;
	
	if (window.event && window.event.srcElement) {
		target = window.event.srcElement;
	}
	else if (e && e.target) {
		target = e.target;	   
	}
	
	if (!target) return null;
	
	while (target != document.body && target.nodeName.toLowerCase() != "a") {
		target = target.parentNode;   
	}
	
	if (target.nodeName.toLowerCase() != "a") return null;
	
	return target;
}

function mouseover(e) {
	var target = findTarget(e);
	var img_tag;
	
	if (!target) return;
	
	img_tag = target.childNodes[0];
	
	if (img_tag.src.indexOf("_f1.") < 0) img_tag.src = img_tag.src.replace(/(\.[^.]+)$/, "_f1$1"); 
}

function mouseout(e) {
	var target = findTarget(e);
	var img_tag;
	
	if (!target) return;
	
	img_tag = target.childNodes[0];
	img_tag.src = img_tag.src.replace(/_f1(\.[^.]+)$/, "$1");   
}

function getSectionId() {
	var current_url = location.href;
	var pattern = new RegExp("^http:\\/\\/" + _DOMAIN + "\\/(\\w+)");
	var regexp_results = pattern.exec(current_url);
	
	if (RegExp.$1) {
		return RegExp.$1;
	} else {
		return '';
	}
}

function initPage() {
	initRollovers();
	InitialiseScrollableArea();
}

window.onload = initPage;
