// Javascript file for Research Finder
var activeLyr="";
var activeLyr2="";

var active="";

var active1="";
var active2="";

var flag=false,flag1=false;

var stay,nstay=true;

//,test,test2;

	brolo = new Image();brolo.src = "images/spe/browse.gif";
	advlo = new Image();advlo.src = "images/spe/advanced.gif";

// Set the Active Layer
function setAct(id){

  flag=true;
	if (active!="") css(active).color="#7a7a4a";
	active=id;
	css(id).color="#990000";
	if (active1!="") css(active1).color="#7a7a4a";
	if (activeLyr!="") hide(activeLyr);
	if (activeLyr2!="") hide(activeLyr2);
	if (activeLyr!="") document.getElementById(activeLyr).style.top = 0 + "px"; //NN6 bug
  activeLyr="content"+id; 
  if (document.getElementById(activeLyr)==null) activeLyr="";
  if (activeLyr!="") show(activeLyr); 
  nstay=false;
  init();
  active1="";

}

function setAct1(id){
  nstay=true;
  stay = parseInt(document.getElementById(activeLyr).style.top);
  flag1=true;
	if (active1!="") css(active1).color="#7a7a4a";
	active1=id;
	css(id).color="#990000";
  if (active2!="") css(active2).color="#7a7a4a";
	if (activeLyr2!="") hide(activeLyr2);
  if (activeLyr2!="")	document.getElementById(activeLyr2).style.top = 0 + "px"; //NN6 bug
  activeLyr2=  id.substr(0,3)=="all" ? "allSectors" : "content"+id; 
  if (document.getElementById(activeLyr2)==null) activeLyr2="";
  if (activeLyr2!="") show(activeLyr2);
  init1();
  active2="";
}

function setAct2(id){
//  test2 = parseInt(document.getElementById(activeLyr2).style.top);
  flag1=true;
	if (active2!="") css(active2).color="#7a7a4a";
	active2=id;
	css(id).color="#990000";
}

function doOn(id){css(id).color="#990000";}

function doOff(id){if (active!=id && active1!=id && active2!=id) css(id).color="#7a7a4a";}

var upL,upL2,upT,upT2,downL,downL2,downT,downT2,dragL,dragL2,dragT,dragT2,rulerL,rulerL2,rulerT,rulerT2,contentT,contentT2,contentH,contentH2,contentClipH,contentClipH2,scrollLength,scrollLength2,startY,startY2;
var clickUp=false,clickUp2=false,clickDown=false,clickDown2=false,clickDrag=false,clickDrag2=false,clickAbove=false,clickAbove2=false,clickBelow=false,clickBelow2=false;
var upH=14,upH2=14,upW=15,upW2=15,downH=14,downH2=14,downW=15,downW2=15,dragH=14,dragH2=14,dragW=17,dragW2=17,scrollH=95,scrollH2=95,speed=4,mouseY,mouseX;
var dom = document.getElementById ? true:false;
var ie4 = document.all ? true:false;
var timer = setTimeout("",500);

function down(e){
	getMouse(e);
  if (mouseX >= 400){
  	down2(e);
  }else{
  	if(document.all && event.button!=1) return true;
 		startY = (mouseY - dragT);
 		if(mouseX >= upL && (mouseX <= (upL + upW)) && mouseY >= upT && (mouseY <= (upT + upH))){
  		clickUp = true;
  		return scrollUp();
 		}else if(mouseX >= downL && (mouseX <= (downL + downW)) && mouseY >= downT && (mouseY <= (downT + downH))){
  		clickDown = true;
  		return scrollDown();
 		}else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= dragT && (mouseY <= (dragT + dragH))){
  		clickDrag = true;
  		return false;
		}else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= rulerT && (mouseY <= (rulerT + scrollH))){
  		if(mouseY < dragT){
  		clickAbove = true;
  		clickUp = true;
  		return scrollUp();
 		}else{
  		clickBelow = true;
  		clickDown = true;
  		return scrollDown();
  	}
	}else{
  	return true;
	}
 }
}

function down2(e){
	if(document.all && event.button!=1) return true;
  getMouse(e);
  startY2 = (mouseY - dragT2);
	if(mouseX >= upL2 && (mouseX <= (upL2 + upW2)) && mouseY >= upT2 && (mouseY <= (upT2 + upH2))){
		clickUp2 = true;
		return scrollUp2();
	}else if(mouseX >= downL2 && (mouseX <= (downL2 + downW2)) && mouseY >= downT2 && (mouseY <= (downT2 + downH2))){
		clickDown2 = true;
		return scrollDown2();
	}else if(mouseX >= dragL2 && (mouseX <= (dragL2 + dragW2)) && mouseY >= dragT2 && (mouseY <= (dragT2 + dragH2))){
		clickDrag2 = true;
		return false;
	}else if(mouseX >= dragL2 && (mouseX <= (dragL2 + dragW2)) && mouseY >= rulerT2 && (mouseY <= (rulerT2 + scrollH2))){
		if(mouseY < dragT2){
			clickAbove2 = true;
			clickUp2 = true;
			return scrollUp2();
		}else{
			clickBelow2 = true;
			clickDown2 = true;
			return scrollDown2();
		}
	}else{
		return true;
	}
}

function move(e){
	getMouse(e);
	if ((clickDrag && contentH > contentClipH) && (mouseX < 400)){
		dragT = (mouseY - startY);
  	if(dragT < (rulerT)) dragT = rulerT;		
		if(dragT > (rulerT + scrollH - dragH)) dragT = (rulerT + scrollH - dragH);
		contentT = ((dragT - rulerT)*(1/scrollLength));
		contentT = eval('-' + contentT);
  	moveTo();
		if(ie4)
			return false;
	}else	if ((clickDrag2 && contentH2 > contentClipH2) && (mouseX >= 400)){
		dragT2 = (mouseY - startY2);
  	if(dragT2 < (rulerT2)) dragT2 = rulerT2;		
		if(dragT2 > (rulerT2 + scrollH2 - dragH2)) dragT2 = (rulerT2 + scrollH2 - dragH2);
		contentT2 = ((dragT2 - rulerT2)*(1/scrollLength2));
		contentT2 = eval('-' + contentT2);
  	moveTo2();
		if(ie4)
			return false;
	}
}

function move2(e){
	if(clickDrag2 && contentH2 > contentClipH2){
		getMouse(e);
		dragT2 = (mouseY - startY2);
  	if(dragT2 < (rulerT2)) dragT2 = rulerT2;		
		if(dragT2 > (rulerT2 + scrollH2 - dragH2)) dragT2 = (rulerT2 + scrollH2 - dragH2);
		contentT2 = ((dragT2 - rulerT2)*(1/scrollLength2));
		contentT2 = eval('-' + contentT2);
  	moveTo2();
		if(ie4)
			return false;
	}
}

function up(){
	clearTimeout(timer);
	clickUp=false;clickDown=false;clickDrag=false;clickAbove=false;clickBelow=false;clickUp2=false;clickDown2=false;clickDrag2=false;clickAbove2=false;clickBelow2=false;
	return true;
}

function getT(){
	contentT = parseInt(document.getElementById(activeLyr).style.top);
}

function getT2(){
	contentT2 = parseInt(document.getElementById(activeLyr2).style.top);
}

function getMouse(e){
	if(ie4){
		mouseY = event.clientY + document.body.scrollTop;
		mouseX = event.clientX + document.body.scrollLeft;
	}else if(dom){
		mouseY = e.pageY;
		mouseX = e.pageX;
  }
}

function moveTo(){
	document.getElementById(activeLyr).style.top = contentT + "px";
	document.getElementById("drag").style.top = dragT + "px";
	document.getElementById("ruler").style.top = dragT + "px";
}

function moveTo2(){
	document.getElementById(activeLyr2).style.top = contentT2 + "px";
	document.getElementById("drag2").style.top = dragT2 + "px";
	document.getElementById("ruler2").style.top = dragT2 + "px";
}

function scrollUp(){
	getT();
	if(clickAbove){
		if(dragT <= (mouseY-(dragH/2)))	return up();
	}
	if(clickUp){
		if(contentT < 0){		
			dragT = dragT - (speed*scrollLength);
			if(dragT < (rulerT)) dragT = rulerT;
			contentT = contentT + speed;
			if(contentT > 0) contentT = 0;
			moveTo();
			timer = setTimeout("scrollUp()",25);
		}
	}
	return false;
}

function scrollUp2(){
	getT2();
	if(clickAbove2){
		if(dragT2 <= (mouseY-(dragH2/2)))
			return up();
	}
	if(clickUp2){
		if(contentT2 < 0){		
			dragT2 = dragT2 - (speed*scrollLength2);
			if(dragT2 < (rulerT2)) dragT2 = rulerT2;
			contentT2 = contentT2 + speed;
			if(contentT2 > 0) contentT2 = 0;
			moveTo2();
			timer = setTimeout("scrollUp2()",25);
		}
	}
	return false;
}

function scrollDown(){
	getT();
	if(clickBelow){
		if(dragT >= (mouseY-(dragH/2)))
			return up();
	}
	if(clickDown){
		if(contentT > -(contentH - contentClipH)){			
			dragT = dragT + (speed*scrollLength);
			if(dragT > (rulerT + scrollH - dragH)) dragT = (rulerT + scrollH - dragH);
			contentT = contentT - speed;
			if(contentT < -(contentH - contentClipH)) contentT = -(contentH - contentClipH);
			moveTo();
			timer = setTimeout("scrollDown()",25);
		}
	}
	return false;
}

function scrollDown2(){
	getT2();
	if(clickBelow2){
		if(dragT2 >= (mouseY-(dragH2/2))) return up();
	}
	if(clickDown2){
		if(contentT2 > -(contentH2 - contentClipH2)){			
			dragT2 = dragT2 + (speed*scrollLength2);
			if(dragT2 > (rulerT2 + scrollH2 - dragH2)) dragT2 = (rulerT2 + scrollH2 - dragH2);
			contentT2 = contentT2 - speed;
			if(contentT2 < -(contentH2 - contentClipH2)) contentT2 = -(contentH2 - contentClipH2);
			moveTo2();
			timer = setTimeout("scrollDown2()",25);
		}
	}
	return false;
}

function init(){

	var offy = parseInt(css('finder').top)-50;
	var offx = parseInt(css('finder').left);

  css('up').left=263+offx+"px";
  css('up').top=234+offy+"px";
  css('down').left=263+offx+"px";
  css('down').top=343+offy+"px";
  css('drag').left=262+offx+"px";
  css('drag').top=248+offy+"px";
  
  if (css('adv').visibility=="hidden")
  	css('drag').visibility = "visible";
  
  css('ruler').left=262+offx+"px";
  css('ruler').top=248+offy+"px";
  
  if (activeLyr!="") eventLoader(activeLyr);
  
  css('contentClip').top=234+offy+"px";
  css('contentClip').left=11+offx+"px";

  init1();
}


function init1(){

	var offy = parseInt(css('finder').top)-50;
	var offx = parseInt(css('finder').left);

  css('up2').left=532+offx+"px";
  css('up2').top=234+offy+"px";
  css('down2').left=532+offx+"px";
  css('down2').top=343+offy+"px";
  css('drag2').left=531+offx+"px";
  css('drag2').top=248+offy+"px";

  if (css('adv').visibility=="hidden")
  	css('drag2').visibility = "visible";

  css('ruler2').left=531+offx+"px";
  css('ruler2').top=248+offy+"px";
  //eventLoader(activeLyr);
  if (activeLyr2!="") eventLoader2(activeLyr2);

    css('contentClipSub').top=234+offy+"px";
    css('contentClipSub').left=289+offx+"px";

  
}

function init2(){
	init1();
}

// Show the Advanced Search Layer
function showAdv()
{
	hide('drag');
	hide('ruler');
	if (activeLyr!="") hide(activeLyr);
	hide('drag2');
	hide('ruler2');
	if (activeLyr2!="") hide(activeLyr2);
	hide('bro');
	hide('broCnt');
	hide('contentClip');
	show('adv');
	show('advCnt');
}


// Show the Browse Layer
function showBro()
{
	hide('adv');
	hide('advCnt');
	init();
	show('drag');
	show('ruler');
	if (flag) show(activeLyr);
	show('drag2');
	show('ruler2');
	if (flag1) show(activeLyr2);
	show('bro');
	show('broCnt');
	show('contentClip');
}



function initiateBrowse()
{
	var mkt = active;
	var sec  = active1;
	var pat = active2;
	
	var code = "";
	
	if (sec==""||sec.substr(0,3)=="all") 
		code = mkt;
	else if (pat==""||pat.substr(0,3)=="all") 
		code = sec;
	else
		code = pat;

	StdSetCookie("FcmFinderCode", code, 0);

	NavOpen('research-index-by-code.pag?code='+code);
}


function submitSearch()
{
     window._AllowSubmit = true;
 
    var sQry = StdGetFieldValue('SchQuery', "searchform");
     sQry = sQry.replace(/(\b)(\w\w\w\w)-\w\w(\b)/g, "$1$2-  $3");
     document.forms["searchform"].SchQuery.value = sQry;
     
     document.forms["searchform"].submit();
}

// Load a user's Saved Search.  Used on Research Finder
function loadSearch()
{
  var sId = StdGetFieldValue('SearchList', "searchform");
  if (sId==null || sId== '') {
		return;
  }
  window.location.replace("research-finder.pag?srchid="+sId);
  document.forms.searchform.SearchList.blur( );
}

function saveSearchAs()
{
	var sName = StdGetFieldValue("SaveAs", "searchform");
	if (sName==null || sName== '') {
		alert("Enter a name for the search");
		document.forms.searchform.SaveAs.focus();
		return;
	}
	window._AllowSubmit = true;
	NavPostAction('SaveSearch', '&name='+escape(sName));
}

// Update a user's stored search
function updateSearch()
{
	var sName = window.CurrentSearchName;
	if (sName==null || sName== '') {
		return;
	}
	NavPostAction('SaveSearch', '&name='+escape(sName));
}

function deleteSearch()
{
	var sName = window.CurrentSearchName;
	if (sName==null || sName== '') {
		return;
	}
	NavGetAction('DeleteSearch', '&name='+escape(sName));
}




function doStay(){
  if (stay){
    if (nstay) document.getElementById(activeLyr).style.top = stay + "px";

  }
}
function eventLoader(activeLyr){
	upL = parseInt(document.getElementById("up").style.left);
	upT = parseInt(document.getElementById("up").style.top);
	downL = parseInt(document.getElementById("down").style.left);
	downT = parseInt(document.getElementById("down").style.top);
	dragL = parseInt(document.getElementById("drag").style.left);
	dragT = parseInt(document.getElementById("drag").style.top);
	rulerT = parseInt(document.getElementById("ruler").style.top);
	contentH = parseInt(document.getElementById(activeLyr).offsetHeight);
	contentClipH = parseInt(document.getElementById("contentClip").offsetHeight);
  document.getElementById(activeLyr).style.top = 0 + "px";
 // if (test) document.getElementById(activeLyr).style.top = test + "px";
	scrollLength = ((scrollH-dragH)/(contentH-contentClipH));
  eventLoader2(activeLyr2);
  doStay();
}

function eventLoader2(activeLyr2){
	upL2 = parseInt(document.getElementById("up2").style.left);
	upT2 = parseInt(document.getElementById("up2").style.top);
	downL2 = parseInt(document.getElementById("down2").style.left);
	downT2 = parseInt(document.getElementById("down2").style.top);
	dragL2 = parseInt(document.getElementById("drag2").style.left);
	dragT2 = parseInt(document.getElementById("drag2").style.top);
	rulerT2 = parseInt(document.getElementById("ruler2").style.top);
	if (activeLyr2!="")  contentH2 = parseInt(document.getElementById(activeLyr2).offsetHeight);
	contentClipH2 = parseInt(document.getElementById("contentClipSub").offsetHeight);
	if (activeLyr2!="")  document.getElementById(activeLyr2).style.top = 0 + "px";
//  if (test2) document.getElementById(activeLyr2).style.top = test2 + "px";
	scrollLength2 = ((scrollH2-dragH2)/(contentH2-contentClipH2));
	document.onmousedown = down;
  document.onmousemove = move;
	document.onmouseup = up;
}
