/* viriathus.js */

//alert("Screen rez is "+window.screenX+"x"+window.screenY+"!");
//alert("Screen rez is "+screen.width+"x"+screen.height+"!");

var index_page = "index.php";

var logo = document.getElementById("logo");
var navpanel = document.getElementById("nav-panel");
var header_text = document.getElementById("section-header-text");

logo.onclick = function(){window.location=index_page;};
logo.style.cursor = "pointer";




/* fill the column with tombstones or images
var pic_column = document.getElementById("column");
pic_column.appendChild(document.createTextNode("Testing"));
//if(secnavlink[current_link_num][i]
*/



/* debug and testing stuff */
var debug;				//document.getElementById("debug_out");
//debug.value = "";
var my_distance = 268;	//document.getElementById("my_distance").value;
var my_speed = 1.8;		//document.getElementById("my_speed").value
var my_shift = 2.3;		//document.getElementById("my_shift").value
var my_fps = 50;		//document.getElementById("my_fps").value;



navpanel.style.marginLeft = "0px"; //this MUST stay uncommented!

var button_width = 225; //must be int or nav will break, and must match css!
var button_height = 24; //must be int or nav will break!
var submenu_line_height = 16; //16 bust be int!
var secnav_padding = 212; //212 additional padding under secondary navigation (total secnav height + padding, hence large value)
var button_marginleft = "12px";
var submenu_link_indent = "24px"; //flush left would match button_marginleft
var button_fontsize = "14px";

var button_bgcolor = "#303030";
var button_activecolor = "#303030";
var button_textcolor = "#ffffff"; //#7b2a0e
var button_activetextcolor = "#ffffff"; //#000000
var button_bdrcolor = "#000000"; //#aaaaaa
var button_submenubg = "#64d4f3"; //#72b6c8 (darker)
var button_submenuhilite = "#88002c";

var timer;
var timeout_interval = (1000/parseInt(my_fps)); //20ms (50fps)
var accel_speed = parseFloat(my_speed); //2.5;
var shifting_factor = parseFloat(my_shift); //0.5 //smaller numbers for more dramatic shifts
//var steps = 3; //num of pixels to move per animation frame
var direction = 0; //direction of nav item


//is a button already active?
var active_button = -1;


/* primary navigation */
var linkname = new Array();
linkname[0] = "ABOUT VIRIATHUS";
linkname[1] = "VIRIATHUS CAPITAL";
linkname[2] = "VIRIATHUS RESEARCH";
linkname[3] = "VIRIATHUS CONSULTING";
linkname[4] = "VIRIATHUS SERVICES";
linkname[5] = "OTHER BUSINESSES";
linkname[6] = "PRESS ROOM";
linkname[7] = "CONTACT US";

var link_image = new Array();
link_image[0] = "navigation/about.png";
link_image[1] = "navigation/capital.png";
link_image[2] = "navigation/research.png";
link_image[3] = "navigation/consulting.png";
link_image[4] = "navigation/services.png";
link_image[5] = "navigation/other_biz.png";
link_image[6] = "navigation/press.png";
link_image[7] = "navigation/contact.png";

//show page title for this section
if(parseInt(section) > -1){
	header_text.appendChild(document.createTextNode(linkname[parseInt(section)]));
}

/*secondary navigation
NOTE: if link is other than a number (i.e. a real page), prefix
it with ./ - this will tell index.php to include the link from
a file instead of pulling the content from the database.
*/
var secnav = new Array(7); //needs to equal number of top-level links
var secnavlink = new Array(7); //ditto

secnav[0] = new Array();
secnav[0][0] = "Our History";
secnav[0][1] = "Our Management Team";
secnav[0][2] = "Advisory Board";
//secnav[0][3] = "Contact Us";
secnavlink[0] = new Array();
secnavlink[0][0] = "1";
secnavlink[0][1] = "2";
secnavlink[0][2] = "3";
//secnavlink[0][3] = "4";

secnav[1] = new Array();
secnav[1][0] = "About Capital";
secnav[1][1] = "Capital Team";
secnav[1][2] = "Corporate Finance";
secnav[1][3] = "Strategic Advisory";
secnav[1][4] = "Equity Advisory";
secnav[1][5] = "Debt Advisory";
secnav[1][6] = "Private Sales & Trading";
secnav[1][7] = "Private Fund Marketing";
secnav[1][8] = "Transactions";
secnavlink[1] = new Array();
secnavlink[1][0] = "10";
secnavlink[1][1] = "11";
secnavlink[1][2] = "12";
secnavlink[1][3] = "13";
secnavlink[1][4] = "14";
secnavlink[1][5] = "17";
secnavlink[1][6] = "15";
secnavlink[1][7] = "18";
secnavlink[1][8] = "16";

secnav[2] = new Array();
secnav[2][0] = "About Research";
secnav[2][1] = "Research Offerings";
secnav[2][2] = "Distribution";
secnav[2][3] = "Research Team";
secnav[2][4] = "Research Archive";
secnavlink[2] = new Array();
secnavlink[2][0] = "20";
secnavlink[2][1] = "21";
secnavlink[2][2] = "23";
secnavlink[2][3] = "24";
secnavlink[2][4] = "25";

secnav[3] = new Array();
secnav[3][0] = "About Consulting";
secnav[3][1] = "Consulting Team";
secnav[3][2] = "Financial Marketing";
secnav[3][3] = "Market Intelligence";
secnav[3][4] = "Investor Roadshows";
secnav[3][5] = "Transactional & Situational Programs";
secnav[3][6] = "Corporate Communications";

secnavlink[3] = new Array();
secnavlink[3][0] = "30";
secnavlink[3][1] = "36";
secnavlink[3][2] = "31";
secnavlink[3][3] = "32";
secnavlink[3][4] = "33";
secnavlink[3][5] = "34";
secnavlink[3][6] = "35";

secnav[4] = new Array();
secnav[4][0] = "";
secnavlink[4] = new Array();
secnavlink[4][0] = "60";

secnav[5] = new Array();
secnav[5][0] = "";
secnavlink[5] = new Array();
secnavlink[5][0] = "90";

secnav[6] = new Array();
secnav[6][0] = "";
secnavlink[6] = new Array();
secnavlink[6][0] = "70";

secnav[7] = new Array();
secnav[7][0] = "";
secnavlink[7] = new Array();
secnavlink[7][0] = "80";




for(i=0;i<linkname.length;i++){ //create navigation

	window["link"+i] = document.createElement("div");
	
	window["linkimg"+i] = document.createElement("img");
	window["linkimg"+i].src = link_image[i];
	window["linkimg"+i].style.border = "0px";
	window["linkimg"+i].style.marginLeft = button_marginleft;	
	window["linkimg"+i].onclick = function(){activate(this.parentNode)};
	window["linkimg"+i].style.cursor = "pointer";
	window["link"+i].appendChild(window["linkimg"+i]);
	/* for text-based links
	window["linktext"+i] = document.createElement("span");
	window["linktext"+i].appendChild(document.createTextNode(linkname[i]));
	window["linktext"+i].style.marginLeft = button_marginleft;
	window["linktext"+i].onclick = function(){activate(this.parentNode)};
	window["linktext"+i].style.cursor = "pointer";
	window["link"+i].appendChild(window["linktext"+i]);
	*/
	
	window["link"+i].style.position = "relative";
	window["link"+i].style.left = "0px"; //have to set this initially, otherwise JavaScript can't get it later
	window["link"+i].style.fontSize = button_fontsize; //have to set this initially, otherwise JavaScript can't get it later
	window["link"+i].style.fontWeight = "normal";
	window["link"+i].style.lineHeight = "24px";
	window["link"+i].style.color = button_textcolor;
	//window["link"+i].style.display = "block";
	//window["link"+i].style.overflowy = "hidden";
	//window["link"+i].style.paddingTop = "2px";
	window["link"+i].style.letterSpacing = "-0.2px";
	window["link"+i].style.whiteSpace = "nowrap";
	window["link"+i].style.marginTop = "0px"; //0px
	window["link"+i].style.marginBottom = "4px"; //4px
	window["link"+i].style.marginLeft = "0px";
	//window["link"+i].style.opacity = "1";
	window["link"+i].style.backgroundColor = button_bgcolor;
	window["link"+i].style.width = button_width+"px";
	window["link"+i].style.height = button_height+"px";
	window["link"+i].style.border = button_bdrcolor+" 0px solid";
	window["link"+i].style.borderLeft = "0px";
	window["link"+i].style.borderRight = "0px";
	window["link"+i].name = "link"+i;
	window["link"+i].active = 0;

	navpanel.appendChild(window["link"+i]); //attach main nav
	
	//grab coordinates
	window["link"+i].init_offset_l = window["link"+i].offsetLeft; //this line must come after it's attached to div!
	window["link"+i].init_offset_t = window["link"+i].offsetTop; //this line must come after it's attached to div!
	
	//update debug
	//document.getElementById("my_distance").value = 8+parseInt(window["link"+i].style.width)+
	//parseInt(document.getElementById("nav-panel").style.marginLeft);
}




function activate(obj){ //slide main nav links

	var linknum = parseInt(obj.name.substr(obj.name.length-1,1));
	
	if(active_button > -1 && active_button != linknum){ //close a button that's already open
	
		//alert("Button "+active_button+" is already active!");
		var obj_num = active_button;
		active_button = -1; //reset
		
		window["link"+obj_num].style.backgroundColor = button_bgcolor;
		window["link"+obj_num].style.color = button_textcolor;
		
		if(secnav[obj_num][0] != ""){ // there is secondary nav on this button
		
			secondary_nav_stop(window["link"+obj_num]);
			direction = 0;
		}
		
		//button's initial offset is stored within its object
		//timer = setInterval(function(){move_callback(obj,obj.init_offset_t)},timeout_interval); //THIS link
		if((parseInt(obj_num)+1) < linkname.length){
			timer = setInterval(function(){move_callback(window["link"+obj_num],obj,window["link"+(parseInt(obj_num)+1)].init_offset_t)},timeout_interval);
		}else{
			activate(obj);
		}
		return;
	}
	
	//var init_fontsize = obj.style.fontSize;
	//var sec_bg = "#00ff00";
	//var sec_fontsize = "50px";
	
	
	if(obj.active == 0){ //if the button isn't already open (resting state)
		
		if(secnav[linknum][0] == ""){ //if this button doesn't have secondary navigation
			var current_link_num = linknum;
			obj.style.backgroundColor = button_activecolor;
			obj.style.color = button_activetextcolor;
			window.location = index_page+"?page="+secnavlink[linknum][0]+"&section="+(current_link_num+1);
			return;
		}
		
		var target_distance = 0;
		obj.active = 1; //disable clicking temporarily
		
		timeout_interval = (1000/parseInt(my_fps));
		accel_speed = parseFloat(my_speed);
		shifting_factor = parseFloat(my_shift);
		
		if(linknum == (linkname.length-1)){ //is this the last link in the list?
			if(direction == 1){
				secondary_nav_stop(obj,0);
				direction = 0;				
			}else{
				secondary_nav_start(obj,0);
				direction = 1;
			}
			obj.active = 0;
		
		//if(parseInt(obj.offsetTop) == obj.init_offset_t){ //if TOP is at rest position
		}else if(window["link"+(linknum+1)].offsetTop == window["link"+(linknum+1)].init_offset_t){ //if TOP is at rest position
			//window["linktext"+String(obj.name).substr(-1,1)].style.fontWeight = "bold";
			obj.style.backgroundColor = button_activecolor;
			obj.style.color = button_activetextcolor;
			direction = 1;
			active_button = linknum;

			//target_distance = parseInt(document.getElementById("my_distance").value) + (parseInt(obj.name.substr(-1,1)) * button_height); //this is the target coord for the move
			
			//take into account secondary nav links when determining target distance (submenu line height + padding value)
			target_distance = ((parseInt(secnav[linknum].length) * submenu_line_height)+secnav_padding) + (linknum * (button_height+4));
			timer = setInterval(function(){move_callback(obj,null,target_distance)},timeout_interval);
			
		}else{ //close navigation
			//window["linktext"+String(obj.name).substr(-1,1)].style.fontWeight = "normal";
			obj.style.backgroundColor = button_bgcolor;
			obj.style.color = button_textcolor;
			secondary_nav_stop(obj);
			direction = 0;
			active_button = -1;

			//button's initial offset is stored within its object
			//timer = setInterval(function(){move_callback(obj,null,obj.init_offset_t)},timeout_interval); //THIS link
			timer = setInterval(function(){move_callback(obj,null,window["link"+(linknum+1)].init_offset_t)},timeout_interval);
		}
	}
}




function activate2(obj){ //restore nav & sec nav state upon page change (no animation)
	
	var linknum = parseInt(obj.name.substr(obj.name.length-1,1));
	
	if(obj.active == 0){
		
		if(secnav[linknum][0] == ""){ //if this button doesn't have secondary navigation
			obj.style.backgroundColor = button_activecolor;
			obj.style.color = button_activetextcolor;
			active_button = linknum;
			return;
		}

		var target_distance = 0;
		obj.active = 1; //disable clicking temporarily
				
		if(window["link"+linknum].offsetTop == window["link"+linknum].init_offset_t){ //if TOP is at rest position
			
			//window["linktext"+String(obj.name).substr(-1,1)].style.fontWeight = "bold";
			obj.style.backgroundColor = button_activecolor;
			obj.style.color = button_activetextcolor;
			direction = 1;
			active_button = linknum;

			target_distance = ((parseInt(secnav[linknum].length) * submenu_line_height));
			//target_distance = parseInt(document.getElementById("my_distance").value)-165; //this is the target coord for the move

			for(i=0;i<linkname.length;i++){
	
				if(linknum != i){ //if this iteration ISN'T the current button (we don't want to move ourself!)
					
					if(linknum < i){ //this link is BELOW the active one
						//window["link"+i].style.top = ((parseInt(window["link"+i].offsetTop) - window["link"+i].init_offset_t) + Math.ceil((rate)/shifting_factor))+"px";
						window["link"+i].style.top = ((parseInt(window["link"+i].offsetTop) - window["link"+i].init_offset_t) + target_distance) +"px";
							
					}
				}
			}
		}
		
		secondary_nav_start(obj,1);
		for(n=0;n<linkname.length;n++){
			window["link"+n].active = 1; //re-enable other buttons
		}
		obj.active = 0;
	}
}






function move_callback(obj,obj_reopen,distance){ //this gets called repeatedly during button animation
	
	var linknum = parseInt(obj.name.substr(obj.name.length-1,1));

	distance = parseInt(distance);
	//alert("Current object name: "+obj.name);
	
	//var initial_x = window["link"+(parseInt(obj.name.substr(-1,1))+1)].init_offset_l;
	var initial_y = window["link"+(linknum+1)].init_offset_t;
	var current_x = parseInt(obj.offsetLeft);
	//var current_y = parseInt(obj.offsetTop); //monitor coords for THIS button
	var current_y;
	
	if(linknum != (linkname.length-1)){
		current_y = parseInt(window["link"+(linknum+1)].offsetTop); //monitor coords for NEXT button
		//alert("current_y is "+current_y+"\ninitial_y is "+initial_y+"\ndistance is "+distance);
	}else{
		alert(window["link"+(linknum+1)].name + " was undefined!");
		//current_y = 0;
		//distance = 0;
	}

	var move_amt = 0;
	var rate = 0;
	var fontrate = 0;

	if(distance > current_y){ //move down
	
		if(distance > current_y){
			
			move_amt = Math.abs(current_y - distance); //current amount to change for x (changes constantly)
			rate = move_amt/Math.pow(accel_speed,2); //here we calculate the acceleration
			//fontrate = (move_amt/Math.pow(accel_speed,2)/4);
			
			//obj.style.top = ((current_y-obj.init_offset_t) + Math.ceil(rate))+"px"; //move code for THIS button

			for(i=0;i<linkname.length;i++){

				if(linknum != i){ //if this iteration ISN'T the current button (we don't want to move ourself!)
				
					if(linknum < i){ //this link is BELOW the active one
						//window["link"+i].style.top = ((parseInt(window["link"+i].offsetTop) - window["link"+i].init_offset_t) + Math.ceil((rate)/shifting_factor))+"px";
						window["link"+i].style.top = ((parseInt(window["link"+i].offsetTop) - window["link"+i].init_offset_t) + Math.ceil(rate)) +"px";
						
					}else{ //this link is ABOVE the active one
						//window["link"+i].style.top = (parseInt(window["link"+i].offsetTop)-(Math.ceil(rate)/shifting_factor))+"px";
					}
				}
			}
		}
		
	}else{ //move up (or move completed)
	
		if(current_y > distance){
			
			move_amt = Math.abs(current_y - initial_y); //current amount to change for y (changes constantly)
			rate = move_amt/Math.pow(accel_speed,2); //here we calculate the acceleration
			//fontrate = (move_amt/Math.pow(accel_speed,2)/4);
			
			//obj.style.top = ((current_y-obj.init_offset_t) - Math.ceil(rate))+"px"; //move code for THIS button
			
			for(i=0;i<linkname.length;i++){

				if(linknum != i){ // if the iteration we're on ISN'T the current button (we don't want to move ourself!)

					//this is a link other than the active one, so shift it
					if(linknum < i){ //this link is BELOW the active one
						//window["link"+i].style.top = ((parseInt(window["link"+i].offsetTop) - window["link"+i].init_offset_t) - Math.ceil(rate))+"px";
						window["link"+i].style.top = ((parseInt(window["link"+i].offsetTop) - window["link"+i].init_offset_t) - Math.ceil(rate))+"px";

					}else{ //this link is ABOVE the active one
						//window["link"+i].style.top = (parseInt(window["link"+i].offsetTop)+(Math.ceil(rate)/shifting_factor))+"px";
					}
				}
			}

		}else{ //move completed
			
			if(direction == 1){ //button open
				//alert("Nav button now open!");
				secondary_nav_start(obj,0);
				for(n=0;n<linkname.length;n++){
					window["link"+n].active = 1;
				}

			}else{ //button closed
				for(n=0;n<linkname.length;n++){
					window["link"+n].active = 0; //re-enable other buttons
				}
			}
			
			obj.active = 0;
			clearInterval(timer);
			
			if(obj_reopen != null && direction < 1) activate(obj_reopen); //launch another link immediately after closing this one
		}
	}
	
	/*
	debug.value = "Target distance: "+distance+
				" | Current: "+current_y+
				" | FPS: "+(1000/timeout_interval)+
				" | active: "+obj.active;
	*/
}




function secondary_nav_start(obj,restore){ //do secondary navigation (restore means restore after page load)

	var current_link_num = parseInt(obj.name.substr(obj.name.length-1,1));

	var secnav_div = document.createElement("div"); //container div for secondary
	
	secnav_div.style.fontFamily = "Helvetica,sans-serif";
	secnav_div.style.fontSize = "11px";
	secnav_div.style.lineHeight = submenu_line_height+"px";
	secnav_div.style.letterSpacing = "0px";
	secnav_div.style.fontStyle = "normal";
	secnav_div.style.marginLeft = "0px"; //1px
	secnav_div.style.marginTop = "0px"; //3px
	secnav_div.style.backgroundColor = button_submenubg;
	//secnav_div.onmouseover = function(){secnav_div.style.backgroundColor = "#000000";};
	
	for(i=0;i<secnav[current_link_num].length;i++){ //create navigation
	
		window["secnav_bg"+i] = document.createElement("div");
		window["secnav_a"+i] = document.createElement("a");
		
		window["secnav_a"+i].href = index_page+"?page="+secnavlink[current_link_num][i]+"&section="+(current_link_num+1);
		
		window["secnav_a"+i].style.marginLeft = submenu_link_indent;
		window["secnav_a"+i].appendChild(document.createTextNode(secnav[current_link_num][i]));
		window["secnav_a"+i].onmouseover = function(){this.parentNode.style.backgroundColor = button_submenuhilite;this.style.color = "#ffffff"};
		window["secnav_a"+i].onmouseout = function(){this.parentNode.style.backgroundColor = button_submenubg;this.style.color = "#000000"};		
				
		//window["linktext"+String(obj.name).substr(-1,1)].style.fontWeight = "bold";
		//window["secnav"+i].style.cursor = "pointer";
		//window["secnav"+i].onclick = function(){alert("Clicked secondary nav: "+this)};
		//window["secnav"+i].style.textDecoration = "none";
		
		window["secnav_bg"+i].appendChild(window["secnav_a"+i]);
		secnav_div.appendChild(window["secnav_bg"+i]);
	}
	obj.appendChild(secnav_div);
	if(restore != 1){
		window.location = index_page+"?page="+secnavlink[current_link_num][0]+"&section="+(current_link_num+1);
	}
}



function secondary_nav_stop(obj){ //remove secondary navigation
	obj.removeChild(obj.lastChild);
}


if(section > -1){
	activate2(window["link"+section]);
}


function externalLinks() {
	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";
		}
	}
}
window.onload = function() {externalLinks();}


function tomb_link(link){
	//alert("This tombstone links to: "+link);
	//window.location=link;
	if(link != "/tombstones/attachments/"){
		window.open(link);
		return true;
	}else{
		return false;
	}
}
	
var i;
tstone = new Array();
for(i=0;document.getElementById("tombstone"+i);i++){
	tstone[i] = document.getElementById("tombstone"+i);
	turl[i] = document.getElementById("tomburl"+i);
	if(turl[i].value != "") tstone[i].style.cursor = "pointer";
	//tstone[i].onclick = function(){window.location=index_page;}
	//tstone[i].onclick = function(){tombstone_click(this);}
}
//alert("Found "+i+" tombstones on the page.");


