var selectedInputBox = null
var Timer=null;
var startValueSliderBS = 0;
var startValueSliderPL = 0;

function displayResult(benchmarktype)
{
	var action;

	if (benchmarktype == 'BS')
	{
		action = 'viewWeightedAverageValues'
	}
	else
	{
		action = 'viewCustomValues'
	}
	 
	if (window.document.getElementById('peergroupsincluded').length == 0)
	{
		alert("You must selected at least one peergroup !");
	
	}
	else
	{
		if (benchmarktype == 'BS') {
			window.document.getElementById('loading').style.display='block';
			toggleSearchInformation();
			window.document.getElementById('div_tabs_BS').style.display='none';
			window.document.getElementById('div_tabs_PL').style.display='none';
			window.document.getElementById('slider_BS').style.display='none';
			window.document.getElementById('slider_PL').style.display='none';
			window.document.getElementById('div_content').style.display='none';
			startValueSliderBS = 0;
			startValueSliderPL = 0;		
			hScrolling('div_content_BS',startValueSliderBS);
			hScrolling('div_content_PL',startValueSliderPL);		
		}

		makeDestinationList(window.document.getElementById('peergroupsincluded'), window.frames['rpc_frame'].document.forms['myForm'].elements['selectedpeergoups']);
	
		window.document.getElementById('action').value = action;
		window.frames['rpc_frame'].document.forms['myForm'].elements['action'].value = action;
		window.document.getElementById('benchmarktype').value = benchmarktype;
		window.frames['rpc_frame'].document.forms['myForm'].elements['benchmarktype'].value = benchmarktype;
		window.frames['rpc_frame'].document.getElementById('myForm').submit();
	}
}

function hScrolling(divID,position) {
	var Objet=document.getElementById(divID);
	Objet.style.left = "-" + position + "px";
}	

function showStructure()
{
	window.document.getElementById('div_content_PL').style.display='none';
	window.document.getElementById('div_label_PL').style.display='none';
	window.document.getElementById('div_tabs_PL').style.display='none';
	window.document.getElementById('div_content_BS').style.display='block';
	window.document.getElementById('div_label_BS').style.display='block';
	window.document.getElementById('div_tabs_BS').style.display='block';
	window.document.getElementById('div_content').style.height = window.document.getElementById('div_content_BS').offsetHeight;
	var sliderMaxValue = parseInt(window.document.getElementById('div_content_BS').offsetWidth) - parseInt(window.document.getElementById('div_content').offsetWidth);
	//alert(sliderMaxValue);
	
	if (sliderMaxValue <= 0)
	{
		window.document.getElementById('slider_BS').style.display='none';
	}
	else
	{
		window.document.getElementById('slider_BS').style.display='block';
		$("#slider_BS").slider({
			steps: 1000,
			minValue: 0,
			maxValue: sliderMaxValue,
			startValue: startValueSliderBS,
			slide: function(e, ui) { 
				hScrolling('div_content_BS',ui.slider.curValue);
				startValueSliderBS = ui.slider.curValue;
			}
		});
		}
}

function showPerformance()
{
	window.document.getElementById('div_content_BS').style.display='none';
	window.document.getElementById('div_label_BS').style.display='none';
	window.document.getElementById('div_tabs_BS').style.display='none';
	window.document.getElementById('div_content_PL').style.display='block';
	window.document.getElementById('div_label_PL').style.display='block';
	window.document.getElementById('div_tabs_PL').style.display='block';
	window.document.getElementById('div_content').style.height = window.document.getElementById('div_content_PL').offsetHeight;
	var sliderMaxValue = parseInt(window.document.getElementById('div_content_PL').offsetWidth) - parseInt(window.document.getElementById('div_content').offsetWidth);
	//alert(sliderMaxValue);
	
	if (sliderMaxValue <= 0)
	{
		window.document.getElementById('slider_PL').style.display='none';
	}
	else
	{	
		window.document.getElementById('slider_PL').style.display='block';
		$("#slider_PL").slider({
			steps: 1000,
			minValue: 0,
			maxValue: sliderMaxValue,
			startValue: startValueSliderPL,
			slide: function(e, ui) { 
				hScrolling('div_content_PL',ui.slider.curValue);
				startValueSliderPL = ui.slider.curValue;			
			}
		});
	}
}


var viewport = {
  getWinWidth: function () 
  {
    this.width = 0;
    if (window.innerWidth)
		this.width = window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth) 
  		this.width = document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth) 
  		this.width = document.body.clientWidth;
  },
  
  getWinHeight: function () 
  {
    this.height = 0;
    if (window.innerHeight) 
		this.height = window.innerHeight - 18;
  	else if (document.documentElement && document.documentElement.clientHeight) 
  		this.height = document.documentElement.clientHeight;
  	else if (document.body && document.body.clientHeight) 
  		this.height = document.body.clientHeight;
  },
  
  getScrollX: function () 
  {
    this.scrollX = 0;
  	if (typeof window.pageXOffset == "number") 
		this.scrollX = window.pageXOffset;
  	else if (document.documentElement && document.documentElement.scrollLeft)
  		this.scrollX = document.documentElement.scrollLeft;
  	else if (document.body && document.body.scrollLeft) 
  		this.scrollX = document.body.scrollLeft; 
  	else if (window.scrollX) 
		this.scrollX = window.scrollX;
  },
  
  getScrollY: function () 
  {
    this.scrollY = 0;    
    if (typeof window.pageYOffset == "number") 
		this.scrollY = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
  		this.scrollY = document.documentElement.scrollTop;
  	else if (document.body && document.body.scrollTop) 
  		this.scrollY = document.body.scrollTop; 
  	else if (window.scrollY) 
		this.scrollY = window.scrollY;
  },
  
  getAll: function () {
    this.getWinWidth(); this.getWinHeight();
    this.getScrollX();  this.getScrollY();
  }
  
}


function showContextInfo(myArea, divID) 
{
    myArea.style.cursor = 'hand';

    viewport.getAll();

    var offX=0;
    var offY=0;

	var x=window.document.getElementById(divID).offsetLeft;
	var y=window.document.getElementById(divID).offsetTop;

	if(x+window.document.getElementById(divID).offsetWidth+offX>viewport.width+viewport.scrollX)
	{
		x=x-window.document.getElementById(divID).offsetWidth-offX;
		if(x<0)
			x=0;
	}
	else 
		x=x+offX;
	if(y+window.document.getElementById(divID).offsetHeight+offY>viewport.height+viewport.scrollY)
	{
		y=y-window.document.getElementById(divID).offsetHeight-offY;
		if(y<viewport.scrollY)
			y=viewport.height+viewport.scrollY-window.document.getElementById(divID).offsetHeight;
	}
	else 
		y=y+offY;


	window.document.getElementById(divID).style.left=x+"px";
	window.document.getElementById(divID).style.top=y+"px";

	window.document.getElementById(divID).style.visibility = 'visible';
    //Tooltip.show(e,divID)
}

function hideContextInfo(divID) 
{
	window.document.getElementById(divID).style.visibility = 'hidden';
    //Tooltip.hide();
}

function showBubbleTooltip(divID)
{
	if (selectedInputBox != null) {
		hideBubbleTooltip();
	}	

	selectedInputBox = window.document.getElementById(divID);
    
    //myElement.bubbletooltip.style.display = 'block';
	selectedInputBox.style.display = 'block';
 
    document.getElementById("btc").appendChild(selectedInputBox);
    Locate(selectedInputBox);
}

function hideBubbleTooltip()
{
	var d=document.getElementById("btc");
	if(d.childNodes.length>0) 
		d.removeChild(d.firstChild);

}

function Locate(selectedInputBox)
{
	boxPosition = findPos(selectedInputBox);
	var posx=boxPosition[0], posy=boxPosition[1];
	var offX=-1;
	var offY=18;
	var tooltipClassName = "bubbletooltip";

	viewport.getAll();

	window.document.getElementById("btc").className="bubbletooltip";

	posx=posx

	if(posy+window.document.getElementById("btc").offsetHeight+offY>viewport.height+viewport.scrollY)
	{
		posy=posy-window.document.getElementById("btc").offsetHeight-offY;
		if(posy<viewport.scrollY)
		{
			posy=viewport.scrollY;
		}
		else
		{
			tooltipClassName = tooltipClassName + "_b";
		}
	}
	else
	{
		tooltipClassName = tooltipClassName + "_t";
		posy=posy+offY;
	}

	if(posx+window.document.getElementById("btc").offsetWidth+offX>viewport.width+viewport.scrollX-20)
	{
		posx=posx-window.document.getElementById("btc").offsetWidth-offX;

		if(posx<viewport.scrollX)
		{
			posx=viewport.scrollX;
		}
		else
		{
			tooltipClassName = tooltipClassName + "_r";
		}
	}
	else
	{
		tooltipClassName = tooltipClassName + "_l";
		posx=posx+offX;
	}

	if (tooltipClassName.indexOf('_l')>0) {
		posx+=20;
	}
	if (tooltipClassName.indexOf('_b')>0) {
		posy+=25;
	}


	window.document.getElementById("btc").className = tooltipClassName;
	window.document.getElementById("btc").style.left=posx+"px";
	window.document.getElementById("btc").style.top=posy+"px";

}

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		curleft = obj.offsetLeftcurtop = obj.offsetTop
		while (obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function printContent(divID, period){
	var myWin = window.open ('/en/miv-benchmarks-'+period+'/printContent.asp?div='+divID, 'PrintFriendly', 'width=660,height=500,scrollbars=yes,status=no');
	myWin.focus();
} 
		
function vScroll(divID,sense,offset) {
	var Objet=document.getElementById(divID);
	if(parseInt(Objet.style.top) + (offset*sense)>0)  {
		clearTimeout(Timer);
	}
	else if(parseInt(Objet.style.top) + (offset*sense)<-(Objet.offsetHeight-document.getElementById("div_content").offsetHeight)) {
		clearTimeout(Timer);
	}
	else {
		Objet.style.top = (parseInt(Objet.style.top) + (offset*sense)) + "px";
	}
	Timer = setTimeout("vScroll('" + divID + "'," + sense + "," + offset + ");", 30);
}

function hScroll(divID,sense,offset) {
	var Objet=document.getElementById(divID);
	if(parseInt(Objet.style.left) + (offset*sense)>0)  {
		clearTimeout(Timer);
	}
	else if(parseInt(Objet.style.left) + (offset*sense)<-(Objet.offsetWidth-document.getElementById("div_content").offsetWidth)) {
		clearTimeout(Timer);
	}
	else {
		Objet.style.left = (parseInt(Objet.style.left) + (offset*sense)) + "px";
	}
	Timer = setTimeout("hScroll('" + divID + "'," + sense + "," + offset + ");", 30);
}	

		