

// RESIZABLE /////////////////////////////////////////////////////////////////////////////////

function getBrowserWidth()
{

  var myWidth = 0;
  
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
   
  return myWidth;
  
}

/*function switch_img(link)
{
	
	for (var i =0; i < link.childNodes.length; i++)
	{
		if (link.childNodes[i].tagName == "IMG")
		break;
	}
		
	var img = null;
	
	if (i < link.childNodes.length)	
	{
		img = link.childNodes[i];
	}
	
	var t = "";
	
	if (img.src.indexOf("_over.gif") > 0)
	{
		img.src = img.src.replace("_over.gif",".gif");
	}
	else 
	{
		img.src = img.src.replace(".gif","_over.gif");	
	}
		
	return;
}*/

if (topref == "" || topref == "undefined") {
	var topref = document.referrer;
}

function onresize_handler()
{
		
//	var oIframe = document.getElementById("iframe_holder");
//		
//	var oDoc = oIframe.contentWindow || oIframe.contentDocument;
//    if (oDoc.document) 
//		{ oDoc = oDoc.document; }
//	
//	var oBaseURI = oDoc.URL;
//	
//	if (oBaseURI.match(/search\.asp$/)) 
//		{	
//		  var window_dimensions = getWindowDimensions();
//		  
//		  var divscroll = oDoc.getElementById("divscroll");
//		  if (divscroll)
//			{
//			divscroll.style.height=(window_dimensions.height-161)+"px";
//		  
//			var subheader = oDoc.getElementById("subheader");
//			var div2fixed = oDoc.getElementById("div2fixed");
//			
//			oIframe.style.overflow="hidden";			
//			oIframe.style.height=subheader.clientHeight + div2fixed.clientHeight + divscroll.offsetHeight + 9 + "px";
//			var middle_table = oDoc.getElementById("heading_table");
//			var main_container = document.getElementById("main_container");
//			var top_menu_middle = document.getElementById("top_menu_middle");
//			if (middle_table.offsetWidth > main_container.offsetWidth)
//				{
//				//oIframe.style.width = window_dimensions.width-20+"px";
//				///top_menu_middle.style.width = window_dimensions.width-20 - 4 - 142 +"px";
//				//main_container.style.width=window_dimensions.width-20+"px";				
//				}
//			}
//		}
//	else 
//		{
		var main_container = document.getElementById("main_container");
		//var top_menu_middle = document.getElementById("top_menu_middle");
		
		if (original_window_width == null ) 
			{ original_window_width = main_container.offsetWidth; }
		//else 
		//	{
			//oIframe.style.width = original_window_width -8 +"px";
			//main_container.style.width = original_window_width +"px";
			//top_menu_middle.style.width = original_window_width -4 -142+"px";
		//	}
		
		//oIframe.style.height=oDoc.body.offsetHeight+"px";	  
	//	if (oBaseURI.match(/final\.asp$/) && oDoc.scroll_parent > 0)
//			{
//			var tm = document.getElementById("top_menu_phenomic");
//			var margin = document.body.offsetTop + tm.offsetHeight+ tm.offsetTop + 16 ;
//			window.scrollTo(0,oDoc.scroll_parent + margin  );
//			}
//		//alert(oBaseURI+" "+oDoc.body.offsetHeight);
//		}
	
	}
	
function scroll_y_position (pos)
{
	window.scrollTo(0,pos);
	alert("scroll_y");
	}
	
function preload_images(images_names,images_array)
{
	images_array = new Array(images_names.length);
	for (var i = 0;i < images_names.length; i++)
		{	
		images_array[i] = new Image();
		images_array[i].src = images_names[i];		
		}

}

function preload_over_images(path,names,images_array)
{
	images_array = new Array(names.length*2);
	for (var i = 0;i < names.length; i++)
		{	
		images_array[i*2] = new Image();
		images_array[i*2].src = path+names[i]+".gif";
		
		images_array[i*2 + 1] = new Image();	
		images_array[i*2 + 1].src = path+names[i]+"_over.gif";	
		}
}



///////////////////////////////////////////////////////////////////////////////////


var helpWin;
function help(o, anch)
{

	if (anch == undefined){
		var r = window.location.href;
		if (r.indexOf("default.asp") > -1){
			anch = "default";
		} else if (r.indexOf("search.asp") > -1){
			anch = "search";
		} else if (r.indexOf("orthologies.asp") > -1){
			anch = "orthologies";
		} else if (r.indexOf("final.asp") > -1){
			anch = "final";
		}
	}

	if (anch != "undefined"){
		var url = o.href + "#" + anch;
	} else {
		var url = o.href;
	}
	var str = "toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width=800,height=600";
	helpWin = window.open(url,"helpWin",str);
	helpWin.focus();
	return false;
}

var releasesWin;
function releases(o)
{
	var url = o.href;
	var str = "toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=0,width=500,height=300";
	releasesWin = window.open(url,"releasesWin",str);
	releasesWin.focus();
	return false;
}

var popupWin;
function popup(url, w, h)
{
	var str = "toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=0,width=" + w + ",height=" + h + "";
	popupWin = window.open(url,"popupWin",str);
	popupWin.focus();
}

function blank(o)
{
	window.open(o.href, '_blank');
	return false;
}



function Kcluster(v_phenotype_id, v_k_id, v_type_id, v_num)
{
	document.getElementById('vtv1').style.display 		= "none";
	document.getElementById('vtv2').style.display 		= "none";
	document.getElementById('vtv3').style.display 		= "none";
	document.getElementById('pager_id').style.display	= "none";
	document.getElementById("downloadArea").style.display 	= "none";
	
	
	phenotype_id 	= v_phenotype_id;
	k_id 			= v_k_id;
	
	displayPage 	= 1;
	page_id 		= 1;
			
	if(type_id == 6 || type_id == 5 || type_id == 4 || type_id == 3)
	{
		sortColumn 	= "Score";
		sortOrder 	= "DESC";
	}
	else if (type_id == 1)
	{
		sortColumn 	= "PSPID";
		sortOrder 	= "ASC";
	}
	else
	{
		sortColumn  = "Score";
		sortOrder 	= "DESC";
	}
	
	initializeKField();
		
	document.getElementById("v_Tabs").innerHTML = "Loading cluster details ...";
	initializeTabs();

	document.getElementById("graph").innerHTML = "";
	//initializeGraph();
	getNewClusterId();
	
	
}

function cluster(v_phenotype_id, v_k_id, v_type_id, v_num)
{
	
	document.getElementById('vtv1').style.display 		= "none";
	document.getElementById('vtv2').style.display 		= "none";
	document.getElementById('vtv3').style.display 		= "none";
	document.getElementById('pager_id').style.display	= "none";
	document.getElementById("downloadArea").style.display 	= "none";
	
	
	
		
	phenotype_id 	= v_phenotype_id;
	k_id 			= v_k_id;
	type_id 		= v_type_id;
	
	displayPage 	= 1;
	page_id 		= 1;
	

	if(type_id == 6 || type_id == 5 || type_id == 4 || type_id == 3)
	{
		sortColumn 	= "Score";
		sortOrder 	= "DESC";
	}
	else if (type_id == 1)
	{
		sortColumn  = "PSPID";;
		sortOrder 	= "ASC";
	}
	else
	{
		sortColumn 	= "Score";
		sortOrder 	= "DESC";
	}

	initializeTabs();

	document.getElementById("graph").innerHTML = "";
	//initializeGraph();
	//initializeViews();
	GTProceedchangeView();
	
	document.getElementById('v_InfoLoad').style.display = "block";
	document.getElementById('v_InfoLoad').innerHTML 	= 'Loading cluster details ...';
	document.getElementById("v_info").innerHTML = "";
	initializeInfo();
	
}

function sa(cb)
{

	var state = cb.checked;
	
	var cbs = document.getElementsByTagName('input');
	var i = cbs.length;
	while(i--){
		if (cbs[i].type == "checkbox") cbs[i].checked = state;
	}	
}

function ce()
{
	var exampleLink = document.getElementById('exampleLink');
	var combo = document.getElementById('sections');
	switch(combo.value){
		default:
			exampleLink.innerHTML = examples[combo.selectedIndex];
			break;
	}
}

function se()
{
	document.getElementById("term").value = document.getElementById('exampleLink').innerHTML;
}

function sf(id)
{
	ce();
	document.getElementById("term").focus();
}

function sa(cb)
{

	var state = cb.checked;
	
	
	
	if(state)
	{
		atLeastOneSelected = true;
		
		document.getElementById('SBL').style.color 			= "#5C7898";
		document.getElementById('SBL').style.borderColor  	= "#5C7898";
		
	}
	else
	{
		atLeastOneSelected = false;
		
		document.getElementById('SBL').style.color 			= "#5C7898";
		document.getElementById('SBL').style.borderColor  	= "#5C7898";
		
	}
	
	var cbs = document.getElementsByTagName('input');
	var i = cbs.length;
	while(i--){
		if (cbs[i].type == "checkbox") cbs[i].checked = state;
	}	
	
}

var atLeastOneSelected = false;


function disableFormButton()
{
	
	atLeastOneSelected = false;
	
	var cbs = document.getElementsByTagName('input');
	
	var i = cbs.length;
	while(i--){
		if (cbs[i].type == "checkbox" && cbs[i].checked) {
			
				atLeastOneSelected = true;
				
				//alert("at least one selected");
			
				document.getElementById('SBL').style.color 			= "#5C7898";
				document.getElementById('SBL').style.borderColor  	= "#5C7898";
			
			
			
		}
	}
	
	
	
	
	
	if(!atLeastOneSelected)
	{
		
		//alert("unselected");
		
		document.getElementById('SBL').style.color 			= "#5C7898";
		document.getElementById('SBL').style.borderColor  	= "#5C7898";
	}
	
	
}

function cs()
{
	
	atLeastOneSelected = false;
	
	var all = true;
	var cbs = document.getElementsByTagName('input');
	var i = cbs.length;
	while(i--){
		if (cbs[i].type == "checkbox" && cbs[i].id != "all" && !cbs[i].checked) {
			all = false;
		}
	}	
	
	var j = cbs.length;
	while(j--){
		if (cbs[j].type == "checkbox" && cbs[j].checked) {
			
				atLeastOneSelected = true;
				
				//alert("at least one selected");
			
				document.getElementById('SBL').style.color 			= "#5C7898";
				document.getElementById('SBL').style.borderColor  	= "#5C7898";
						
		}
	}	
	
	
	if(!atLeastOneSelected)
	{
		
		//alert("unselected");
		
		document.getElementById('SBL').style.color 			= "#5C7898";
		document.getElementById('SBL').style.borderColor  	= "#5C7898";
	}

	document.getElementById('all').checked = all;
}

function pf(formId)
{
	
	
	
		var form = document.getElementById(formId);
		var i = 1;
		while (i<arguments.length){
			document.getElementById(arguments[i]).value = arguments[i+1]
			i = i + 2;
		}
		form.submit();

}


function pfOrtho(formId)
{
	
	if(atLeastOneSelected)
	{
	
		var form = document.getElementById(formId);
		var i = 1;
		while (i<arguments.length){
			document.getElementById(arguments[i]).value = arguments[i+1]
			i = i + 2;
		}
		form.submit();
	
	}
	else
	{
		alert("Select at least one orthology.");
	}
}


function pp()
{
	window.scrollTo(0, getAnchorPosition("pt").y);
}

function ps(cnum)
{
	var pagesize = document.getElementById("psCombo"+cnum).value;
	pf('pageForm', 'pagesize', pagesize);
}

function getAnchorPosition(anchorname)
{
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
	} else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
	} else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
	} else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
		}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
		}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
	} else {
		coordinates.x=0; coordinates.y=0; return coordinates;
	}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el)
{
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}

function AnchorPosition_getWindowOffsetLeft (el)
{
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
}	

function AnchorPosition_getPageOffsetTop (el)
{
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}

function AnchorPosition_getWindowOffsetTop (el)
{
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
}

 
function getWindowHeight()
{
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

//start collapsable legends
function constructCollapsableFieldsets() 
{
    var allFsets = document.getElementsByTagName('fieldset');
    var fset = null;
    for (var i=0; i<allFsets.length; i++)
    {
        fset = allFsets[i];
        if(fset.attributes['collapsed']!=null)
            constructCollapsableFieldset(fset, fset.attributes['collapsed'].value);
    }
}
 
//for collapsable fieldset:
function constructCollapsableFieldset(fset, collapsed)
{
    //main content:
    var divContent = fset.getElementsByTagName('div')[0];
    if (divContent == null)
        return;
 
    if (collapsed == 'true')
        divContent.style.display = 'none';
 
    //+/- ahref:
    var ahrefText = getAHrefForToogle(collapsed);
 
    //legend:
    var legend = fset.getElementsByTagName('legend')[0];
    if (legend != null)
        legend.innerHTML = ahrefText + legend.innerHTML;
    else
        fset.innerHTML = '<legend>' + ahrefText + '</legend>' + fset.innerHTML;
}
 
function getAHrefForToogle(collapsed)
{
    var ahrefText = "<a onClick='toogleFieldset(this.parentNode.parentNode);' style='text-decoration: none; cursor:pointer;'>";
    ahrefText = ahrefText + getExpanderItem(collapsed) + "</a>&nbsp;";
    return ahrefText;
}
 
function getExpanderItem(collapsed)
{
    var ecChar;
    if (collapsed=='true')
        ecChar='[+]';
    else
        ecChar='[-]';
 
    return ecChar;
}
 
function toogleFieldset(fset)
{
    var ahref = fset.getElementsByTagName('a')[0];
    var div = fset.getElementsByTagName('div')[0];
 
    if (div.style.display != "none")
    {
        ahref.innerHTML=getExpanderItem('true');
        div.style.display = 'none';
    }
    else
    {
        ahref.innerHTML=getExpanderItem('false');
        div.style.display = '';
    }
}

function preload_images(images_names,images_array)
{
	images_array = new Array(images_names.length);
	for (var i = 0;i < images_names.length; i++)
		{	
		images_array[i] = new Image();
		images_array[i].src = images_names[i];		
		}

}


function preload_over_images(path,names,images_array)
{
	images_array = new Array(names.length*2);
	for (var i = 0;i < names.length; i++)
		{	
		images_array[i*2] = new Image();
		images_array[i*2].src = path+names[i]+".gif";
		
		images_array[i*2 + 1] = new Image();	
		images_array[i*2 + 1].src = path+names[i]+"_over.gif";	
		}
}


//end collapsable legends





function openClusterJam(val) // requiers browser detection variables code positionoed along the pages
{
	var browserIncompatibleMsg = "Your browser is out of date so you may experience problems viewing the next page.\nFor best performance please update your browser to the latest version.\nProceed to the cluster page?";


	/*if(is_ie)
	{
		if(confirm(browserIncompatibleMsg))
		{
			window.open("cluster.asp?phenotype_id=" + val, self.maximize);
		}
	}
	else */ // needs testing
	if((is_fx && is_minor >= 3.5) || is_opera || navigator.appVersion.match("Chrome") || is_ie)
	{
		window.open("cluster.asp?phenotype_id=" + val, self.maximize);
	}
	/*else if(is_fx && (is_minor >= 3.5))
	{
		//alert("compatible");
	}
	else if(is_opera)
	{
		//alert("compatible");
	}
	else if(navigator.appVersion.match("Chrome"))
	{
		//alert("compatible");
	} */
	else
	{
		if(confirm(browserIncompatibleMsg))
		{
			window.open("cluster.asp?phenotype_id=" + val, self.maximize);
		}
	}
	
	
		
}



///////////////////////////////////////////////////////////////////////////////////


var slide = 1;
var x, y, cw;

function dragStart(id)
{
	cw = document.getElementById(id);
	x = parseInt(cw.style.left) - event.clientX;
	y = parseInt(cw.style.top) - event.clientY;
	document.onmousemove = drag;
	document.onmouseup = dragStop;
}

function dragStop()
{
	document.onmousemove = null;
}

function drag()
{
	var nx = Math.min(x + event.clientX, document.body.clientWidth - 195);
	var ny = Math.min(y + event.clientY, document.body.clientHeight - 24);
	cw.style.left = Math.max(nx, 0);
	cw.style.top = Math.max(ny, 0);
}

function go(num)
{
	slide += num;
	if (slide < 1){
		slide = 1;
	} else if (slide > slides.length) {
		slide = slides.length;
	}
	document.getElementById("slides").style.backgroundImage = "url(" + slides[slide-1].image + ")";

	if (slide == 1) {
		document.getElementById("previous").style.visibility = "hidden";
	} else {
		document.getElementById("previous").style.visibility = "visible";
	}

	if (slide == slides.length) {
		document.getElementById("next").style.visibility = "hidden";
	} else {
		document.getElementById("next").style.visibility = "visible";
	}

}

var popupWin;

function popup(o, w, h)
{
	var str = "toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0";
	str += ",height=" + h;
	str += ",width=" + w;
	str += ",left=" + (screen.width - w)/2;
	str += ",top=" + (screen.height - h)/2;
	popupWin = window.open(o.href,"popupWin",str);
	popupWin.focus();
	return false;
}


if (navigator.userAgent.indexOf("Firefox/") != -1)
{

	window.onload = function() {
		
		document.body.style.margin = "-1px";
		document.body.style.padding = "1px";

	}
}
