/*
JavelinVP.com (version 2.0)
Website by LaMa Media
Copyright (c) 2009-2011 - All Rights Reserved
*/


// ****************************************************************
// GLOBALS
// ****************************************************************
/*global window, document, navigator, clearTimeout, setTimeout, alert, escape, unescape, location */

var kNO = 0,
	kYES = 1,
	kBUILD = 2,
	kDELIM = '|';

var vPage,
	vCount = 0,
	vChunk = 0,
	vChunks = [],
	vOverview = 0,
	vOverviews = [],
	vSubNav = 0,
	vDepth = '',
	vPages = ['strategy|Strategy', 'team|Team', 'portfolio|Portfolio', 'ceos|From Our CEO<span class="lc">s</span>', 'news|News', 'contact|Contact'],
	vSN = { exists:0, height:0, goal:0, goto:'', DELAY:250, SPEED:40, PIXELS:10, MAX_HEIGHT:176, CHUNK_HEIGHT:23, GODELAY:1 },
	vHN = { timeout:0, num:0, first:0, cur:1, next:1, opacity:0, paused:0, pauseable:0, TIME:7000, SPEED:18, DELTA:10, DELAY:1 };


if(!Array.indexOf) {
	Array.prototype.indexOf = function(obj) {
		var t;
		for (t = 0; t < this.length; t++) {
			if (this[t] === obj) {
				return t;
			}
		}
		return -1;
	};
}


// ****************************************************************
// UTILITIES
// ****************************************************************

function fOpacity(rID, rValue) {
    var tStyle = document.getElementById(rID).style,
		tValue = (rValue / 100);
    tStyle.opacity = tValue; 
    tStyle.MozOpacity = tValue; 
    tStyle.KhtmlOpacity = tValue; 
    tStyle.filter = 'alpha(opacity=' + rValue + ')'; 
} 

function fReplaceChar(rStr, rChar, rReplace) {
	var tPos, tLen, tStrL, tStrR;
	while ((tPos = rStr.indexOf(rChar)) >= 0) {
		tLen = rStr.length;
		tStrL = rStr.substring(0, tPos);
		tStrR = rStr.substring(tPos + 1, tLen);
		rStr = (tStrL + rReplace + tStrR);
	}
	return (rStr);
}

function fGetItem(rStr, rNum, rDelim) {
	var tCount, tPos, tItem;
	for (tCount = 0; tCount <= rNum; tCount++) {
		tPos = rStr.indexOf(rDelim);
		if (tPos < 0) { tPos = rStr.length; }
		tItem = rStr.substring(0, tPos);
		tPos = tItem.length + 1;
		rStr = rStr.substring(tPos, rStr.length);
	}
	return (tItem);
}

function fCount() {
	return(++vCount);
}


// ****************************************************************
// COOKIES
// ****************************************************************

function fCookieSet(rName, rValue, rDays) {
	var tDate;
	if (typeof(rDays) !== 'undefined') {
		tDate = new Date();
		tDate.setDate(tDate.getDate() + rDays);
		document.cookie = (rName + '=' + escape(rValue) + '; expires=' + tDate.toGMTString() + '; path=/');
	} else {
		document.cookie = (rName + '=' + escape(rValue) + '; path=/');
	}
}

function fCookieGet(rName) {
	var tOffset, tEnd,
		tSearch = (rName + '='),
		tValue = '';
	if (document.cookie.length > 0) {
		tOffset = document.cookie.indexOf(tSearch);
		if (tOffset !== -1) {
			tOffset += tSearch.length;
			tEnd = document.cookie.indexOf(';', tOffset);
			if (tEnd === -1) { tEnd = document.cookie.length; }
			tValue = unescape(document.cookie.substring(tOffset, tEnd));
      }
   }
  return(tValue);
}


// ****************************************************************
// HOME NEWS
// ****************************************************************

function fHNSwitch() {
	document.getElementById('hn-' + vHN.cur).style.display = 'none';
	vHN.cur += vHN.next;
	if (vHN.cur < 1) { vHN.cur = vHN.num; }
	if (vHN.cur > vHN.num) { vHN.cur = 1; }
	document.getElementById('hn-' + vHN.cur).style.display = 'block';
	setTimeout(fHNShow, vHN.DELAY);
}

function fHNHide() {
	if (vHN.paused && vHN.opacity === 100) {
		vHN.timeout = setTimeout(fHNHide, vHN.SPEED);
	} else {
		vHN.pauseable = 0;
		vHN.opacity -= vHN.DELTA;
		if (vHN.opacity <= 0) {
			vHN.opacity = 0;
			fHNSwitch();
		} else {
			vHN.timeout = setTimeout(fHNHide, vHN.SPEED);
		}
		fOpacity(('hn-' + vHN.cur), vHN.opacity);
	}
}

function fHNShow() {
	vHN.opacity += vHN.DELTA;
	if (vHN.opacity >= 100) {
		vHN.opacity = 100;
		vHN.timeout = setTimeout(fHNHide, vHN.TIME);
		vHN.next = 1;
		vHN.pauseable = 1;
	} else {
		setTimeout(fHNShow, vHN.SPEED);
	}
	fOpacity(('hn-' + vHN.cur), vHN.opacity);
}

function fHNStart() {
	vHN.num = vCount;
	if (vHN.first === 0 || (vHN.first > vHN.num)) {
		vHN.cur = Math.floor(Math.random()*vHN.num) + 1;
	} else {
		vHN.cur = vHN.first;
	}
	fOpacity(('hn-' + vHN.cur), vHN.opacity);
	document.getElementById('hn-' + vHN.cur).style.display = 'block';
	document.getElementById('home-news').style.display = 'block';
	setTimeout(fHNShow, vHN.DELAY);
}

function fHNGo(rDir) {
	if (vHN.pauseable) {
		clearTimeout(vHN.timeout);
		vHN.next = rDir;
		fHNHide();
	}
}

function fHNOver(rOver) {
	if (vHN.pauseable) {
		vHN.paused = rOver;
	}
}


// ****************************************************************
// CHUNK
// ****************************************************************

function fChunkAdd(rStr) {
	vChunks.push(rStr);
}

function fChunkTitles() {
	var t,
		tDivs = document.getElementsByTagName('div');
	for (var t = 0; t < tDivs.length; t++) {
    	if (tDivs[t].className == 'chunk') {
			tDivs[t]._title = tDivs[t].title;
			tDivs[t].onmouseover = function() { this.title = ''; }
			tDivs[t].onmouseout = function() { this.title = this._title; }
    	}
	}
}

function fShow(rChunk) {
	var t, tTitle;
	if (isNaN(rChunk)) {
		for (t = 1; t <= vChunks.length; t++) {
			tTitle = document.getElementById('c-' + t).getAttribute('title');
			if (tTitle === rChunk) {
				rChunk = t;
				break;
			}
		}
		if (isNaN(rChunk)) {
			rChunk = 0;
		}
	}
	rChunk = parseInt(rChunk, 10);
	if (rChunk < 1 || rChunk > vChunks.length) {
		rChunk = (vOverview) ? 0 : 1;
	}
	fSNHide();
	vChunk = rChunk;
	fSNShow();
}

// ****************************************************************
// SUBNAV
// ****************************************************************

function fSNStyle(rDisplay, rColor, rBackColor, rCursor) {
	document.getElementById('c-' + vChunk).style.display = rDisplay;
	if (vSN.exists && vChunk > 0) {
		document.getElementById('n-' + vChunk).style.color = rColor;
		document.getElementById('n-' + vChunk).style.backgroundColor = rBackColor;
		document.getElementById('n-' + vChunk).style.cursor = rCursor;
	}
}

function fSNShow() {
	fSNStyle('block', '#2f5d96', '#e0e0e0', 'default');
}

function fSNHide() {
	fSNStyle('none', '#333', '#fff', 'pointer');
}

function fSNClick(rObj) {
	var tChunk = parseInt(rObj.id.substr(2), 10);
	fShow(tChunk);
}

function fSNOver(rObj, rOver) {
	var tChunk = parseInt(rObj.id.substr(2), 10);
	if (tChunk !== vChunk) {
		document.getElementById('n-' + tChunk).style.backgroundColor = rOver ? '#bbd5ee' : '#fff';
	}
}

function fSNGoto() {
	window.location.href = vSN.goto;
}

function fSNDoOpen() {
	vSN.height += vSN.PIXELS;
	if (vSN.height >= vSN.goal) {
		vSN.height = vSN.goal;
		document.getElementById('sn-' + vPage).style.overflow = 'auto';
	} else {
		setTimeout(fSNDoOpen, vSN.SPEED);
	}
	document.getElementById('sn-' + vPage).style.height = vSN.height + 'px';
}

function fSNOpen() {
	document.getElementById('sn-' + vPage).style.overflow = 'hidden';
	vSN.goal = (vChunks.length * vSN.CHUNK_HEIGHT);
	if (vSN.goal > vSN.MAX_HEIGHT) {
		vSN.goal = vSN.MAX_HEIGHT;
	}
	console.log(vChunks.length);
	fSNShow();
	setTimeout(fSNDoOpen, vSN.DELAY);
}

function fSNDoClose() {
	vSN.height -= vSN.PIXELS;
	if (vSN.height <= 0) {
		vSN.height = 0;
		document.getElementById('sn-' + vPage).style.display = 'none';
		setTimeout(fSNGoto, vSN.GODELAY);
	} else {
		setTimeout(fSNDoClose, (vSN.SPEED / 2));
	}
	document.getElementById('sn-' + vPage).style.height = vSN.height + 'px';
}

function fSNClose() {
	document.getElementById('sn-' + vPage).style.overflow = 'hidden';
	fSNDoClose();
}

function fSNSort(rA, rB) {
	rA = rA.toLowerCase();
	rB = rB.toLowerCase();
	if (rA > rB) {
		return 1;
	} else if (rA <rB) {
		return -1;
	} else {
		return 0;
	}
}

function fSubNav() {
	var t, tChunk, tName,
		tLen = vChunks.length,
		tHTML = '<div id="sn-' + vPage + '"><ul>';
	vSN.exists = 1;
	for (t = 0; t < tLen; t++) {
		tChunk = vChunks[t];
		tName = fGetItem(tChunk, 0, kDELIM);
		tName = fReplaceChar(tName, '{', '<');
		tName = fReplaceChar(tName, '}', '>');
		tHTML += '<li id="n-' + (t+1) + '" onclick="fSNClick(this);" onmouseover="fSNOver(this, 1);" onmouseout="fSNOver(this, 0);">' + tName + '</li>';
	}
	tHTML += '</ul></div>';
	document.getElementById('sub-' + vPage).innerHTML = tHTML;
	vChunk = 1;
	fSNOpen();
}


// ****************************************************************
// OVERVIEW
// ****************************************************************

function fOverview() {
	var t, tStr, tCompany, tLogo, tOverview, tLocal,
		tHTML = '';
	for (t = 0; t < vOverviews.length; t++) {
		tStr = vOverviews[t];
		tCompany = fGetItem(tStr, 0, kDELIM);
		tLogo = fGetItem(tStr, 1, kDELIM);
		tOverview = fGetItem(tStr, 2, kDELIM);
		tLocal = fGetItem(tStr, 3, kDELIM);
		tHTML += '<div id="o-' + (t+1) + '" class="ov-item" onclick="';
		tHTML += (tLocal === '#') ? 'fSNClick(this);' : 'fLink(\'' + tLocal + '\');';
		tHTML += '">';
		tHTML += '<img src="../media/logo/small/' + tLogo + '.gif" width="80" height="30" alt="' + tCompany + '" />' + tOverview + '</div>';
	}
	document.getElementById('overview').innerHTML = tHTML;
}

function fOVAdd(rCompany, rLogo, rOverview, rLocal) {
	rLocal = (typeof(rLocal) === 'undefined') ? '#' : rLocal;
	vOverviews.push(rCompany + kDELIM + rLogo + kDELIM + rOverview + kDELIM + rLocal);
	fChunkAdd(rCompany);
}


// ****************************************************************
// NAV
// ****************************************************************

function fNavBar() {
	var t, tStr, tPage, tName, tLink,
		tHTML = '<div id="logolink" class="logo" onclick="fNavClick(\'' + vDepth + '../index\');"></div><div class="navspc"></div><ul id="navlist">';
		
	for (t = 0; t < vPages.length; t++) {
		tStr = vPages[t];
		tPage = fGetItem(tStr, 0, kDELIM);
		tName = fGetItem(tStr, 1, kDELIM);
		tLink = (vDepth + tPage);
		tHTML += '<li class="nav-item"><div class="nav';
		if (tPage === vPage) {
			tHTML += ' nav-on';
		}
		tHTML += '" onclick="fNavClick(\'' + tLink + '\');"><span>' + tName + '</span></div></li>';
		if (vSubNav) {
			tHTML += '<div id="sub-' + tPage + '" class="nav-sub"></div>';
		}
	}
	tHTML += '</ul><div class="navbot"></div>';
	document.getElementById('navbar').innerHTML = tHTML;
	if (vSubNav === kYES) {
		fSubNav();
	}
	if (vOverview !== kNO) {
		fShow(0);
	}
}

function fNavClick(rLink) {
	var tType = (rLink === (vDepth + 'contact')) ? '.php' : '.html';
	if (rLink === vPage) {
		if (vOverview) {
			fShow(0);
		}
	} else {
		if (vSN.exists) {
			vSN.goto = (rLink + tType);
			fSNClose();
		} else {
			window.location.href = (rLink + tType);
		}
	}
}


// ****************************************************************
// PAGE
// ****************************************************************

function fTeamImg(rObj, rOver) {
	var tStr = 'Javelin Venture Partners',
		tAlt = rObj.getAttribute('alt');
	if (rOver) {
		tStr = fGetItem(tAlt, 0, ',') + ', <span class="team-capem">' + fGetItem(tAlt, 1, ',') + '</span>';
	}
	document.getElementById('team-caption').innerHTML = tStr;
}

function fNewsOver(rChunk, rOver) {
	document.getElementById('l-' + rChunk).style.backgroundPosition = rOver ? '0 -25px' : '0 0';
}

function fFormSentRefresh() {
	window.location.href = '../index.html';
}

function fFormSent() {
	setTimeout(fFormSentRefresh, 5000);
}

function fFooter() {
	var tHTML = '<div id="copy">Copyright &copy; 2012 Javelin Venture Partners</div>';
	document.getElementById('footer').innerHTML = tHTML;
}

function fPage(rPage, rSubNav, rOverview) {
	var tArgs = window.location.search.substr(1).split('?'),
		tChunk = tArgs[0];
	vSubNav = (typeof(rSubNav) === 'undefined') ? kNO : rSubNav;
	vOverview = (typeof(rOverview) === 'undefined') ? kNO : rOverview;
	vPage = rPage;
	if (vPage === 'index') {
		fHNStart();
	} else {
		if (vOverview === kBUILD) {
			fOverview();
		}
		fNavBar();
	}
	fFooter();
	if (vChunks.length > 0) {
		fChunkTitles();
		if (tChunk !== '') {
			fShow(tChunk);
		}
	}
}

function fPage2(rPage) {
	vDepth = '../';
	fPage(rPage, kNO, kNO);
}


// ****************************************************************
// LINKS
// ****************************************************************

function fOffsiteLink(rLink) {
	var tWin = window.open(rLink, "_blank");
	tWin.focus();
}

function fLink(rLink, rOffsite) {
	if (rOffsite === kYES) {
		fOffsiteLink(rLink);
	} else {
		window.open(rLink, "_top");
	}
}

function fEmailLink(rID, rName, rDomain, rDisplay) {
	var tAddress = rName + String.fromCharCode(64) + rDomain,
		tDisplay = (typeof(rDisplay) === 'undefined') ? tAddress : rDisplay,
		tLink = '<a href="mailto:' + tAddress + '">' + tDisplay + '</a>';
	document.getElementById(rID).innerHTML = tLink;
}

