/* 
	Script library for use with the layer7c report navigation component.
*/


var activeLyr="content";
var activeLyr2="";

//Selected element in LH panel.
var active="";
//Selected element in RH panel.
var active1="";

var flag=false,flag1=false;
var stay,nstay=true;

/*
	NAVIGATE THIS REPORT
	The user selects a CHAPTER in the LH panel, and a PAGE in the RH Panel
	This is the handler for GO button.
	
	<a id="D4987_01_02_01_02" class="txtLnk" href="javascript:setAct1('D4987_01_02_01_02')">Measurement Analysis</a>

	
*/
function go()
{
	// If no option is selected in the Left Hand Panel (SELECT A CHAPTER) return
	if (active1=="") return; 
	var id = active1.replace(/_/g,"-").substr(1);
	var pag = id.substr(5,2) == "00" ? "market-service-document" : "report-document" ;
	window.location.replace(CtxPropagate(pag+".pag?docid="+id));
}


function setAct(id){

  flag=true;
  stay = parseInt(document.getElementById(activeLyr).style.top);
	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);
	document.getElementById(activeLyr).style.top = 0 + "px"; //NN6 bug

  activeLyr2="content"+id;

  nstay=false;
  init();
  
  eventLoader2(activeLyr2);//ml
  
  show(activeLyr2);
  active1="";

}


// Change the colour of the selected element

function setAct1(id){ 
	
  	flag1=true;
	if (active1!="") css(active1).color="#7a7a4a";
	active1=id;
	css(id).color="#990000";
	
	//
	if (active1=="") return; 
	var id = active1.replace(/_/g,"-").substr(1);
	var pag = id.substr(5,2) == "00" ? "market-service-document" : "report-document" ;
	window.location.replace(CtxPropagate(pag+".pag?docid="+id));
	
}



function doOn(id){css(id).color="#990000";}
function doOff(id){if (active!=id && active1!=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=166,scrollH2=166,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)){
	window.status = mouseY
		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);
	window.status = dragT2 + "|" + rulerT2 + "!" + contentT2 ////ML
  	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 offx = parseInt(css('layer7c').top); //naming!!!!!
	var offy = parseInt(css('layer7c').left);

  css('up').left=263+offy+"px";
  css('up').top=126+offx+"px";

  css('down').left=263+offy+"px";
  css('down').top=307+offx+"px";

  css('drag').left=262+offy+"px";
  css('drag').top=140+offx+"px";

  css('ruler').left=262+offy+"px";
  css('ruler').top=140+offx+"px";

  eventLoader(activeLyr);

  css('up2').left=532+offy+"px";
  css('up2').top=126+offx+"px";

  css('down2').left=532+offy+"px";
  css('down2').top=307+offx+"px";

  css('drag2').left=531+offy+"px";
  css('drag2').top=140+offx+"px";

  css('ruler2').left=531+offy+"px";
  css('ruler2').top=140+offx+"px";
  
  css('broScrBar1').top=126+offx+"px";
  css('broScrBar1').left=263+offy+"px";

  css('broScrBar2').top=126+offx+"px";
  css('broScrBar2').left=532+offy+"px";
  
    css('contentClip').top=126+offx+"px";
    css('contentClip').left=11+offy+"px";

    css('contentClipSub').top=126+offx+"px";
    css('contentClipSub').left=289+offy+"px";
}

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);
		document.getElementById(activeLyr2).style.top = 0 + "px";
	}
	contentClipH2 = parseInt(document.getElementById("contentClipSub").offsetHeight);
//  if (test2) document.getElementById(activeLyr2).style.top = test2 + "px";
	scrollLength2 = ((scrollH2-dragH2)/(contentH2-contentClipH2));
	document.onmousedown = down;
  document.onmousemove = move;
	document.onmouseup = up;
}

/*		
* Toggle the display of NAVIGATE THIS REPORT
*/
function toggleDropdown()
{

	if (window.CurrentCatId) {
		setAct("S"+CurrentCatId.replace(/-/g, '_').substr(0,10)+"_00_00");
		
		//  Jun 2004
		//	Disabling this in order to support loading the selected page without clicking GO
	//	setAct1("D"+CurrentCatId.replace(/-/g, '_'));
	}

	init();

	var e = document.getElementById("broBody");
	
	var state = e.style.display=="none" ? "visible" : "hidden";
	e.style.display = e.style.display=="none" ? "block" : "none";

	document.getElementById("broScrBar1").style.visibility = state;
	document.getElementById("broScrBar2").style.visibility = state;
	document.getElementById("up").style.visibility = state;
	document.getElementById("down").style.visibility = state;
	document.getElementById("drag").style.visibility = state;
	document.getElementById("ruler").style.visibility = state;
	document.getElementById("up2").style.visibility = state;
	document.getElementById("down2").style.visibility = state;
	document.getElementById("drag2").style.visibility = state;
	document.getElementById("ruler2").style.visibility = state;
	document.getElementById("contentClip").style.visibility = state;
	document.getElementById("contentClipSub").style.visibility = state;
	
	if (activeLyr != "")
		document.getElementById(activeLyr).style.visibility = state;
	if (active !=  "")
		document.getElementById("content"+active).style.visibility = state;
}


