
/************************
Custom Scrollbars
*************************/
var updateScrollBars = function(){
	try {

	if (!dragging && $("menucontent")){
		// global values
		var viewW = winW;
		var trackW = (winW-12);
		
		if ($("content").getElementsByTagName("div")[0]){
			// scrollcontent
			var contentW = $("content").getElementsByTagName("div")[0].clientWidth;
			var viewRatio = viewW/contentW;
			var scrollFactor = trackW/contentW;
			
			// apply to piece
			var scrollPiece = $("scrollpiececontent");
			scrollPiece.style.left = Math.round($("content").scrollLeft*scrollFactor)-18+"px";
			scrollPiece.style.width = Math.round(viewRatio*trackW)-64+3+"px";
		}
		
		// scrollmenu
		var contentW = $("menucontent").clientWidth+17;
		var viewRatio = viewW/contentW;
		var scrollFactor = trackW/contentW;
		
		// apply to piece
		var scrollPiece = $("scrollpiecemenu");
		scrollPiece.style.left = Math.round($("menupane").scrollLeft*scrollFactor)-18+"px";
		scrollPiece.style.width = Math.ceil(viewRatio*trackW)-64+6+"px";
	}
	} catch(e) {}
}

var viewW, trackW,contentW,scrollFactor;

var CscrollpieceDragStart = function(){
	dragging = true;
	trackW = (winW-12);
	
	// scrollcontent
	contentW = $("content").getElementsByTagName("div")[0].clientWidth;
	scrollFactor = trackW/contentW;
	
	// apply to piece
	$("content").scrollLeft = ((document.getElementById("scrollpiececontent").offsetLeft-32)+18)/scrollFactor;
}
var CscrollpieceDrag = function(){
	$("content").scrollLeft = ((document.getElementById("scrollpiececontent").offsetLeft-32)+18)/scrollFactor;
}
var CscrollpieceDragEnd = function(){
	$("content").scrollLeft = ((document.getElementById("scrollpiececontent").offsetLeft-32)+18)/scrollFactor;
	dragging = false;
}

/****/

var MscrollpieceDragStart = function(){
	dragging = true;
	trackW = (winW-12);
	
	// scrollcontent
	contentW = $("menucontent").clientWidth+17;
	scrollFactor = trackW/contentW;
	
	// apply to piece
	//$("menupane").scrollLeft = Math.round((document.getElementById("scrollpiecemenu").offsetLeft)*scrollFactor)-32-44-5;
	$("menupane").scrollLeft = (parseInt(document.getElementById("scrollpiecemenu").style.left)+18)/scrollFactor;
}
var MscrollpieceDrag = function(){
	$("menupane").scrollLeft = (parseInt(document.getElementById("scrollpiecemenu").style.left)+18)/scrollFactor;
}
var MscrollpieceDragEnd = function(){
	$("menupane").scrollLeft = (parseInt(document.getElementById("scrollpiecemenu").style.left)+18)/scrollFactor;
	dragging = false;
}


/*************************/

var urlString = "";
var oldString = "";

/*******onload**********/

var loadCount = 0;


var spreadList, projectList, clientList,curSpread,curProject,curClient,curProjectName,curClientName;
clientList = new Array();
projectList = new Array();

var parseLinks = function(){
	clientList = new Array();
	projectList = new Array();
	var menuATags = $("menucontent").getElementsByTagName("a");
	for (i=0;i<menuATags.length;i++){
		var curParentTag = menuATags[i].parentNode.tagName;
		if (curParentTag == "P"){
			clientList.push(menuATags[i]);
		}
		projectList.push(menuATags[i]);
	}
	var allProjects = "";
	for (i=0;i<projectList.length;i++){
		allProjects+=projectList[i].href.slice(projectList[i].href.indexOf("clients/")+8)+" ("+i+"), ";
	}
}

var processCurrent = function(next){
	if (contentUrl){
		var scrollW = 3;
		curProjectName = contentUrl.slice(contentUrl.indexOf("clients/")+8);
		curClientName = curProjectName.slice(0,curProjectName.indexOf("/"));
		for (i=0;i<projectList.length;i++){
			if (projectList[i].href.indexOf(curProjectName) != -1){
				curProject = i;
				persistentCurrentProject = i;
				//break;
			}
		}
		var scrollCounts = 0;
		var noneFound = true;
		for (i=0;i<clientList.length;i++){
			if (clientList[i].href.indexOf("/"+curClientName)!= -1){
				curClient = i;
				noneFound = false;
			}
			if (noneFound){
				scrollW += parseInt(clientList[i].parentNode.clientWidth)+4;
			}
		}
		if (next){
			if (scrollW+winW <= $("menucontent").clientWidth){
				$("menupane").scrollLeft = scrollW;
			} else {
				$("menupane").scrollLeft = $("menucontent").clientWidth-winW+4;
			}
		}
		for (i=0;i<clientList.length;i++){
			if (i != curClient){
				var theOtherLinks = clientList[i].parentNode.getElementsByTagName("a");
				for (j=0;j<theOtherLinks.length;j++){
					if (theOtherLinks[j].className != "c"){
						theOtherLinks[j].style.visibility = "hidden";	
					} else {
						theOtherLinks[j].style.visibility = "visible";
						theOtherLinks[j].style.color = "#505050";
					}
				}
				clientList[i].parentNode.style.backgroundImage = "url(images/tray_bgleft.png)";
				clientList[i].parentNode.getElementsByTagName("span")[0].style.backgroundImage = "url(images/tray_bgright.png)";
				clientList[i].parentNode.style.cursor = "default";
			}
		}
		var theOtherLinks = clientList[curClient].parentNode.getElementsByTagName("a");
		for (j=0;j<theOtherLinks.length;j++){
			if (theOtherLinks[j].className == "c"){
				theOtherLinks[j].style.visibility = "hidden";	
			} else {
				theOtherLinks[j].style.visibility = "visible";
			}
		}
		clientList[curClient].parentNode.style.backgroundImage = "url(images/tray_bgleft-active.png)";
		clientList[curClient].parentNode.getElementsByTagName("span")[0].style.backgroundImage = "url(images/tray_bgright-active.png)";
		clientList[curClient].style.color = "#ababab";
		clientList[curClient].parentNode.style.cursor = "pointer";
		
	}
}

var scrollIt = false;

var generateLists = function(){
	parseLinks();
	if (contentUrl){
		curProjectName = contentUrl.slice(contentUrl.indexOf("clients/")+8);
		curClientName = curProjectName.slice(0,curProjectName.indexOf("/"));
		for (i=0;i<projectList.length;i++){
			if (projectList[i].href.indexOf(curProjectName) != -1){
				curProject = i;
				break;
			}
		}
		for (i=0;i<clientList.length;i++){
			if (clientList[i].href.indexOf(curClientName)!= -1){
				curClient = i;
				break;
			}
		}
		topLink = false;
		StateManager.setState(projectList[persistentCurrentProject].href.slice(projectList[persistentCurrentProject].href.lastIndexOf("clients/")+8));
		scrollIt = true;
	}
}

var loadComplete = function(){
	loadCount = 0;
	updateScrollBars();
	var contentScroller = new Draggable('scrollpiececontent',{onStart:CscrollpieceDragStart,onDrag:CscrollpieceDrag,onEnd:CscrollpieceDragEnd,constraint:'horizontal',starteffect:'',endeffect:'',snap: function(x) {return[x<winW-64-27-$("scrollpiececontent").clientWidth ? (x > -18 ? x : -18 ) : winW-64-27-$("scrollpiececontent").clientWidth];}});
	var menuScroller = new Draggable('scrollpiecemenu',{onStart:MscrollpieceDragStart,onDrag:MscrollpieceDrag,onEnd:MscrollpieceDragEnd,constraint:'horizontal',starteffect:'',endeffect:'',snap: function(x) {return[x<winW-64-27-$("scrollpiecemenu").clientWidth ? (x > -18 ? x : -18 ) : winW-64-27-$("scrollpiecemenu").clientWidth];}});
	//adjust width of menucontent
	var menuContentWidth = 4;
	for (i=0;i<$("menucontent").getElementsByTagName("div")[0].getElementsByTagName("p").length;i++){
		menuContentWidth += $("menucontent").getElementsByTagName("div")[0].getElementsByTagName("p")[i].clientWidth+4;
	}
	$("menucontent").style.width = menuContentWidth+"px";
	updateScrollBars();
	if ($("content").getElementsByTagName("div")[0] && $("content").getElementsByTagName("div")[0].className == "grey"){
		$("portfoliocontent").className = "grey";
	} else {
		$("portfoliocontent").className = "";
	}
	if (isIE){
		$("content").scrollLeft = 1;
	}
	if (scrollIt){
		processCurrent(true);
		scrollIt = false;
	}
}

var resetScrollPos = function(){
	$("content").scrollLeft = 0;
}

var pShowContent = function(){
	theImgs = $("content").getElementsByTagName("img");
	for (i=0;i<theImgs.length;i++){
		var theSrc = theImgs[i].src;
		theSrc = theSrc.slice(theSrc.lastIndexOf("/")+1);
		if (contentUrl.lastIndexOf(".html") != -1){
			contentUrl = contentUrl.slice(0,contentUrl.lastIndexOf("/")+1);
		}
		if (!isIE){
			theImgs[i].src = contentUrl+theSrc;
		} else {
			theImgs[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+contentUrl+theSrc+"', sizingMethod='scale')";
			theImgs[i].src = "images/spacer.gif";
		}
	}
	if ($("content").getElementsByTagName("div")[0] && $("content").getElementsByTagName("div")[0].className == "grey"){
		$("contentpane").className = "grey";
		$("scrollcontent").className = "grey";
		$("nproject").getElementsByTagName("img")[0].src = "images/nextnav/next_project_up_grey.png";
		$("nclient").getElementsByTagName("img")[0].src = "images/nextnav/next_client_up_grey.png";
		document.getElementsBySelector("#nextnav a")[0].style.color = "#1B1716";
		document.getElementsBySelector("#nextnav a")[1].style.color = "#1B1716";
	} else {
		$("contentpane").className = "";
		$("scrollcontent").className = "";
		$("nproject").getElementsByTagName("img")[0].src = "images/nextnav/next_project_up.png";
		$("nclient").getElementsByTagName("img")[0].src = "images/nextnav/next_client_up.png";
		document.getElementsBySelector("#nextnav a")[0].style.color = "#9F9788";
		document.getElementsBySelector("#nextnav a")[1].style.color = "#9F9788";
	}
	if ($("content").getElementsByTagName("h1")[0]){
		$("nproject").getElementsByTagName("em")[0].firstChild.nodeValue = "View Project ";
	} else {
		$("nproject").getElementsByTagName("em")[0].firstChild.nodeValue = "Next Project ";
	}
	
	new Effect.Appear('contentpane', {duration:1, beforeUpdate:winResize, afterUpdate:resetScrollPos, afterFinish:loadComplete});
	
	new Effect.Appear('scrollcontent', {duration:0.5});
}

var pInsertContent = function(){
	new Ajax.Updater('content', contentUrl, { method:'get', onComplete:pShowContent });
}

var pInsertBlank = function(){
	new Ajax.Updater('content', "blank.html", { method:'get', onComplete:pShowContent });
}

/*****************
Element Behaviours
******************/

var prules = {
	'#nproject' : function(el){
		el.onmouseover = function(){
			if (this.parentNode.parentNode.parentNode.className != "grey"){
				this.style.backgroundImage = "url(images/nextnav/next_bg_left.png)";
				this.getElementsByTagName("em")[0].style.backgroundImage = "url(images/nextnav/next_bg_right.png)";
				this.getElementsByTagName("img")[0].src = "images/nextnav/next_project_over.png";
				this.style.color = "#333";
			} else {
				this.style.backgroundImage = "url(images/nextnav/next_bg_left_grey.png)";
				this.getElementsByTagName("em")[0].style.backgroundImage = "url(images/nextnav/next_bg_right_grey.png)";
				this.getElementsByTagName("img")[0].src = "images/nextnav/next_project_over_grey.png";
				this.style.color = "#fff";
			}
			this.style.cursor = "pointer";
		}
		el.onmouseout = function(){
			this.style.backgroundImage = "none";
			this.getElementsByTagName("em")[0].style.backgroundImage = "none";
			if (this.parentNode.parentNode.parentNode.className != "grey"){
			this.getElementsByTagName("img")[0].src = "images/nextnav/next_project_up.png";
			this.style.color = "#9F9788";
			} else {
				this.getElementsByTagName("img")[0].src = "images/nextnav/next_project_up_grey.png";
				this.style.color = "#1B1716";
			}
			this.style.cursor = "default";
		}
		el.onclick = function(){
			this.style.backgroundImage = "none";
			this.getElementsByTagName("em")[0].style.backgroundImage = "none";
			if (this.parentNode.parentNode.parentNode.className != "grey"){
			this.getElementsByTagName("img")[0].src = "images/nextnav/next_project_up.png";
			this.style.color = "#9F9788";
			} else {
				this.getElementsByTagName("img")[0].src = "images/nextnav/next_project_up_grey.png";
				this.style.color = "#1B1716";
			}
			topLink = false;
			if ((curProject+1) < projectList.length){
				StateManager.setState(projectList[curProject+1].href.slice(projectList[curProject+1].href.lastIndexOf("clients/")+8));
			} else {
				StateManager.setState(projectList[0].href.slice(projectList[0].href.lastIndexOf("clients/")+8));
			}
			
			processCurrent(true);
		}
		el.onfocus = function(){
			this.blur();
		}
	},
	'#nclient' : function(el){
		el.onmouseover = function(){
			if (this.parentNode.parentNode.parentNode.className != "grey"){
			this.style.backgroundImage = "url(images/nextnav/next_bg_left.png)";
			this.getElementsByTagName("em")[0].style.backgroundImage = "url(images/nextnav/next_bg_right.png)";
			this.getElementsByTagName("img")[0].src = "images/nextnav/next_client_over.png";
			this.style.color = "#333";
			} else {
				this.style.backgroundImage = "url(images/nextnav/next_bg_left_grey.png)";
				this.getElementsByTagName("em")[0].style.backgroundImage = "url(images/nextnav/next_bg_right_grey.png)";
				this.getElementsByTagName("img")[0].src = "images/nextnav/next_client_over_grey.png";
				this.style.color = "#fff";
			}
			this.style.cursor = "pointer";
		}
		el.onmouseout = function(){
			this.style.backgroundImage = "none";
			this.getElementsByTagName("em")[0].style.backgroundImage = "none";
			if (this.parentNode.parentNode.parentNode.className != "grey"){
				this.getElementsByTagName("img")[0].src = "images/nextnav/next_client_up.png";
				this.style.color = "#9F9788";
			} else {
				this.getElementsByTagName("img")[0].src = "images/nextnav/next_client_up_grey.png";
				this.style.color = "#1B1716";
			}
			this.style.cursor = "default";
		}
		el.onclick = function(){
			this.style.backgroundImage = "none";
			this.getElementsByTagName("em")[0].style.backgroundImage = "none";
			if (this.parentNode.parentNode.parentNode.className != "grey"){
				this.getElementsByTagName("img")[0].src = "images/nextnav/next_client_up.png";
				this.style.color = "#9F9788";
			} else {
				this.getElementsByTagName("img")[0].src = "images/nextnav/next_client_up_grey.png";
				this.style.color = "#1B1716";
			}
			topLink = false;
			if ((curClient+1) < clientList.length){
				StateManager.setState(clientList[curClient+1].href.slice(clientList[curClient+1].href.lastIndexOf("clients/")+8));
			} else {
				StateManager.setState(clientList[0].href.slice(clientList[0].href.lastIndexOf("clients/")+8));
			}
			processCurrent(true);
		}
		el.onfocus = function(){
			this.blur();
		}
	},
	'#menupane p' : function(el){
		el.onmouseover = function(){
			var theOthers = this.parentNode.getElementsByTagName("p");
			for (i=0;i<theOthers.length;i++){
				if (theOthers[i].getElementsByTagName("a")[0].style.visibility == "visible"){
					theOthers[i].style.backgroundImage = "url(images/tray_bgleft.png)";
					theOthers[i].getElementsByTagName("span")[0].style.backgroundImage = "url(images/tray_bgright.png)";
					theOthers[i].getElementsByTagName("a")[0].style.color = "#505050";
					theOthers[i].style.cursor = "default";
				}
			}
			this.style.backgroundImage = "url(images/tray_bgleft-active.png)";
			this.getElementsByTagName("span")[0].style.backgroundImage = "url(images/tray_bgright-active.png)";
			this.getElementsByTagName("a")[0].style.color = "#ababab";
			try{
			if (this.getElementsByClassName("c")[0].style.visibility != "hidden"){
				this.style.cursor = "pointer";
			} else {
				this.style.cursor = "default";
			}} catch(e) {}
		}
		el.onclick = function(){
			/*var theOthers = this.parentNode.getElementsByTagName("p");
			for (i=0;i<theOthers.length;i++){
				var theOtherLinks = theOthers[i].getElementsByTagName("a");
				for (j=0;j<theOtherLinks.length;j++){
					if (theOtherLinks[j].className != "c"){
						theOtherLinks[j].style.visibility = "hidden";	
					} else {
						theOtherLinks[j].style.visibility = "visible";
						theOtherLinks[j].style.color = "#505050";
					}
				}
				theOthers[i].style.backgroundImage = "url(images/tray_bgleft.png)";
				theOthers[i].getElementsByTagName("span")[0].style.backgroundImage = "url(images/tray_bgright.png)";
				theOthers[i].style.cursor = "default";
			}*/
			var theLinks = this.getElementsByTagName("a");
			for (i=0;i<theLinks.length;i++){
				if (theLinks[i].className != "c"){
					theLinks[i].style.visibility = "visible";
				} else {
					theLinks[i].style.visibility = "hidden";
				}
			}
			this.style.cursor = "default";
			topLink = false;
			StateManager.setState(this.getElementsByTagName("a")[0].href.slice(this.getElementsByTagName("a")[0].href.lastIndexOf("clients/")+8));
			processCurrent(false);
			return false;
		}
		el.onmouseout = function(){
			try{
			if (this.getElementsByClassName("c")[0].style.visibility != "hidden"){
				this.style.backgroundImage = "url(images/tray_bgleft.png)";
				this.getElementsByTagName("span")[0].style.backgroundImage = "url(images/tray_bgright.png)";
				this.getElementsByTagName("a")[0].style.color = "#505050";
				this.style.cursor = "default";
			}
			} catch(e) {}
		}
		el.onfocus = function(){
			this.blur();
		}
	},
	'#menupane p a' : function(el){
		el.onmouseover = function(){
			this.parentNode.style.backgroundImage = "url(images/tray_bgleft-active.png)";
			this.parentNode.getElementsByTagName("span")[0].style.backgroundImage = "url(images/tray_bgright-active.png)";
			this.style.color = "#ababab";
		}
		el.onmouseout = function(){
			try{
			if (this.parentNode.parentNode.getElementsByClassName("c")[0].style.visibility != "hidden"){
				this.parentNode.style.backgroundImage = "url(images/tray_bgleft.png)";
				this.parentNode.getElementsByTagName("span")[0].style.backgroundImage = "url(images/tray_bgright.png)";
				this.style.color = "#505050";
				this.style.cursor = "default";
			}} catch(e) {}
		}
		el.onclick = function(){
			topLink = false;
			StateManager.setState(this.href.slice(this.href.lastIndexOf("clients/")+8));
			processCurrent(false);
			return false;
		}
		el.onfocus = function(){
			this.blur();
		}
	},
	'#menupane p span a' : function(el){
		el.onmouseover = function(){
			this.style.cursor = "default";
		}
		el.onclick = function(){
			return false;
			this.style.cursor = "default";
		}
		el.onfocus = function(){
			this.blur();
		}
	},
	'#menupane p span a img' : function(el){
		el.onmouseover = function(){
			this.style.width = this.clientWidth+2+"px";
			this.style.cursor = "pointer";
		}
		el.onmouseout = function(){
			this.style.width = this.clientWidth-2+"px";
			this.style.cursor = "default";
		}
		el.onclick = function(){
			topLink = false;
			StateManager.setState(this.parentNode.href.slice(this.parentNode.href.lastIndexOf("clients/")+8));
			processCurrent(false);
			return false;
		}
		el.onfocus = function(){
			this.blur();
		}
	},
	'#content' : function(el){
		el.onscroll = function(){
			updateScrollBars();
		}
	},
	'#menupane' : function(el){
		el.onscroll = function(){
			updateScrollBars();
		}
	}
};
Behaviour.register(prules);


/***************************************
****************************************
Ugly hack to make sure Behaviour rules
get applied after Ajax content updates
****************************************/

Ajax.Responders.register({ 
	onComplete: function() { 
		if(Ajax.activeRequestCount==0) { 
			Behaviour.apply(); 
		} 
	} 
});