var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		pre_arrowlft_over = newImage('images/arrowlft-over.png');
		pre_circle_over = newImage('images/circle-over.png');
		pre_arrowrgt_over = newImage('images/arrowrgt-over.png');
		pre_pausebtn_over = newImage('images/pausebtn-over.png');
		pre_scrollarrow_rgt_over = newImage('images/scrollarrow-rgt-over.png');
		pre_scrollarrow_lft_over = newImage('images/scrollarrow-lft-over.png');
		preloadFlag = true;
	}
}
	

function setCircle (c,count,index)
{
		for ( i  = 1 ; i <= count ; i++ )
		{
			circlename = c+'circle'+i;
			if (	i == index )
			{
				changeImages(circlename,'images/circle-over.png');
				document.getElementById(circlename).name = "selected";
			}
			else 
			{
				changeImages(circlename,'images/circle.png');
				document.getElementById(circlename).name = document.getElementById(circlename).id;
			}
		}
		
}

var currentDisolver = null;

function IDisolver (disolverObject)
{
	this.currentDisolver = disolverObject;
}

IDisolver.prototype.intervalID;
IDisolver.prototype.currentDisolver;

IDisolver.prototype.changeOpacityOff = function (c,source,i)
{
	control = document.getElementById(c);
	if ( i == 10 ) control.style.opacity = "1";
	else control.style.opacity = "0."+i;
	p = i *10;
	control.style.filter="alpha(opacity='"+ p + "');";
	
	if ( i >= 0 )
	{
		i = i - 1;
		this.intervalID = setTimeout(this.currentDisolver+".changeOpacityOff('"+c+"','"+source+"',"+i+")",50);
	}
	else
	{
		clearInterval (this.intervalID);
		document.getElementById(c).innerHTML = document.getElementById(source).innerHTML ;
		this.changeOpacityOn(c,0);
	}
}

IDisolver.prototype.changeOpacityOn = function (c,i)
{
	control = document.getElementById(c);
	if ( i == 10 ) control.style.opacity = "1";
	else control.style.opacity = "0."+i;
	p = i *10;
	control.style.filter="alpha(opacity='"+ p + "');";
	
	if ( i < 10 )
	{
		i = i + 1;
		this.intervalID =setTimeout(this.currentDisolver+".changeOpacityOn('"+c+"',"+i+")",50);
	}
	else clearInterval (this.intervalID);
}

IDisolver.prototype.changeDivContnetWithDisolve = function (source,dest,cname,count,index)
{
//	currentDisolver = this;
	this.changeOpacityOff (dest,source,10);
	if ( count > 1 ) setCircle (cname,count,index);
}



var currentSlider = null;

function SlidingBox (_varname)
{
	this.max_height=0;
	this.userbox=0;
	this.resizeDelta = 10;
	this.resizeSleep = 2;
	this.chookie_name = "";
	this.varname = _varname;
}

SlidingBox.prototype.userbox;
SlidingBox.prototype.max_height;
SlidingBox.prototype.intervalID;
SlidingBox.prototype.resizeDelta;
SlidingBox.prototype.resizeSleep;
SlidingBox.prototype.cookie_name;
SlidingBox.prototype.domain_name;
SlidingBox.prototype.varname;


SlidingBox.prototype.setCookieName = function (_cookie_name)
{
	this.cookie_name = _cookie_name;
}

SlidingBox.prototype.setDomainName = function (_domain_name)
{
	this.domain_name = _domain_name;
}

SlidingBox.prototype.slideBoxHide = function(c,i)
{

//	if ( c == 'GROUP_COMMUNITY_NAVIGATION_BOX_TITLE8' ) alert (c+","+i+","+this.resizeDelta+","+this.resizeSleep);
	if ( i >= 1 )
	{
		document.getElementById(c).style.height = i;
		if ( i > 1 )
		{
			i = i  - this.resizeDelta ;
			if ( i < 1 ) i  = 1 ;
			this.intervalID = setTimeout(this.varname+".slideBoxHide('"+c+"',"+i+")",this.resizeSleep);
		}
//		else clearInterval(this.intervalID);
	}
	else clearInterval(this.intervalID);
}

SlidingBox.prototype.slideBoxShow =function  (c,max,i)
{
	if ( i <= max )
	{
		document.getElementById(c).style.height = i;
		if ( i < max )
		{
			i = i  +  this.resizeDelta;
			if ( i > max ) i  = max ;
			this.intervalID = setTimeout(this.varname+".slideBoxShow('"+c+"',"+max+","+i+")",this.resizeSleep);
		}
		else
		{
			document.getElementById(c).style.height = "100%";
			clearInterval(this.intervalID);
		}
	}
	else clearInterval(this.intervalID);
}

SlidingBox.prototype.closed = function (c)
{
	if (this.userbox == 0 ) return false;
	else return true;

}

SlidingBox.prototype.boxshowhide = function (c)
{
	currentSlider = this;
	if (this.userbox == 0 )
	{
		if ( this.max_height == 0 )
		{
			this.max_height = document.getElementById(c).offsetHeight;
			this.resizeDelta = Math.ceil(this.max_height/5);
		}
		this.userbox = document.getElementById(c).offsetHeight;
//		document.getElementById(c).style.opacity = 0;
		this.slideBoxHide (c,this.userbox);
		if (this.cookie_name!="") Set_Cookie (this.cookie_name,"hidden",0, "/",this.domain_name );
	}
	else 
	{
//		document.getElementById(c).style.opacity = 1;
		this.slideBoxShow (c,this.userbox,0);
		this.userbox = 0;
		if (this.cookie_name!="") Set_Cookie (this.cookie_name,"visible",0, "/",this.domain_name);
	}
}



var currentScroller = null;

function Scroller ()
{
	this.mousedown = false;
}

Scroller.prototype.mousedown ;
Scroller.prototype.intervalID ;

Scroller.prototype.continousScrollLeft = function(control)
{
	this.acquire();
	currentScroller = this;
	this.scrollLeft(control);
	this.intervalID = setInterval("currentScroller.scrollLeft('"+control+"')",100);
}

Scroller.prototype.continousScrollRight = function(control)
{
	this.acquire();
	currentScroller = this;
	this.scrollRight(control);
	this.intervalID = setInterval("currentScroller.scrollRight('"+control+"')",100);
}

Scroller.prototype.scrollLeft = function(control)
{
		document.getElementById(control).scrollLeft -= 20;
}
Scroller.prototype.scrollRight= function(control)
{
		document.getElementById(control).scrollLeft += 20;
}

Scroller.prototype.acquire= function()
{
	this.mousedown = true;
	clearInterval(this.intervalID);
}

Scroller.prototype.release= function()
{
	this.mousedown = false;
	clearInterval(this.intervalID);
}

function switchTab (common_name,selected_tab,tab_count)
{
//	document.getElementById('divAction').style.height = "";
	for ( i = 0 ; i < tab_count;i++)
	{
		tab_content_id = common_name+"_content_"+i;
		element_id = common_name+""+i;
		link_element_id = "link_text_"+common_name+""+i;

		if ( i == selected_tab )
		{
			document.getElementById(element_id).style.backgroundImage = 'url(images/greenon-bkgd.gif)';
			document.getElementById(link_element_id).className = 'main';
			document.getElementById(tab_content_id).style.visibility = "visible";
			document.getElementById(tab_content_id).style.opacity = 1;
			document.getElementById(tab_content_id).style.zIndex = 1;
			document.getElementById(tab_content_id).style.height = '100%';
		}
		else
		{
			document.getElementById(element_id).style.backgroundImage = 'url(images/greenoff-bkgd.gif)';
			document.getElementById(link_element_id).className = 'tabtitle_unactive';

			document.getElementById(tab_content_id).style.visibility = "hidden";
			document.getElementById(tab_content_id).style.opacity = 0;
			document.getElementById(tab_content_id).style.zIndex = -1;
			document.getElementById(tab_content_id).style.height = '1px';
		}
	}
}

function reloadTab (common_name,selected_tab,tab_count)
{
}


function changeImages (control,img)
{
	document.getElementById(control).src = img;
}


function clearOrganization (oForm)
{
	oForm.organization_id.value=0;
	oForm.organization_name.value='';
}

function setOrganization (oForm,id,oname)
{
	oForm.organization_id.value=id;
	oForm.organization_name.value=oname;

}



function appendRow(table_name,content1,content2,content3,content4,content5,id){
    
	if (document.getElementById(table_name).rows.length >= 6)return;
	var currentTime = new Date();
	var myid = currentTime.getTime();;
    var x=document.getElementById(table_name).insertRow(-1);
    x.id= myid;
    var y=x.insertCell(0);
    var z=x.insertCell(1);
    var m=x.insertCell(2);
    var n=x.insertCell(3);
    var l=x.insertCell(4);
/*	if (document.getElementById(table_name).rows.length ==1 )
	{
		z.innerHTML=content1;
		z.className='blue_title';
		m.innerHTML=content1;
		m.className='blue_title';
	}*/
    y.vAlign='top';
    m.vAlign='top';
    n.vAlign='top';
    z.vAlign='top';
    l.vAlign='top';
    y.innerHTML=content1;
    z.innerHTML=content2;
    m.innerHTML=content3;
    n.innerHTML=content4;
    l.innerHTML="<a class='main_link' href='javascript:removeRow(\""+table_name+"\",\""+myid+"\")'>"+content5+"</a>";
}
function remRow(table_name,index){
    document.getElementById(table_name).deleteRow(index);
}


function removeRow(table_name,id)
{
    var oRow = document.getElementById(id);
    document.all(table_name).deleteRow(oRow.rowIndex);
/*	if (document.getElementById(table_name).rows.length == 1)
		document.all(table_name).deleteRow(0);*/
}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function resetCheckBoxes ()
{
	var reset_flag = false;
	var checkboxCollection = document.getElementsByTagName("input");
	for(var i=0;i<checkboxCollection.length;i++)
	{
		if ( checkboxCollection[i].id.indexOf("region_id",0) != -1 || checkboxCollection[i].id.indexOf("theme_id",0) != -1 || checkboxCollection[i].id.indexOf("theme_tag_id",0) != -1 || checkboxCollection[i].id.indexOf("country_id",0) != -1) 
		{
			checkboxCollection[i].checked=false;
			reset_flag = true;
		}
	}
	return reset_flag;
}
function setThemeSearchKey (field_name,suffix,control)
{
	var form_field_name  = field_name+"_"+suffix;
	var formsCollection = document.getElementsByTagName("form");
	var target_form = "";
	for(var i=0;i<formsCollection.length;i++)
	{
		if ( formsCollection[i].name.indexOf("ADAPTABLE_WORKING_PAPER_SERIES_LIBRARY_DOCUMENTS_LISTING",0) != -1  || formsCollection[i].name.indexOf("ADAPTABLE_GDN_LIBRARY_DOCUMENTS_LISTING",0) != -1  || formsCollection[i].name.indexOf("ADAPTABLE_DOCUMENTS_LISTING",0) != -1  || formsCollection[i].name.indexOf("ADAPTABLE_RESEARCHERS_LISTING",0) != -1 || formsCollection[i].name.indexOf("ADAPTABLE_ORGANIZATIONS_LISTING",0) != -1)
		{
			for ( j = 0 ; j < formsCollection[i].elements.length ; j ++ )
			{
				if ( formsCollection[i].elements[j].name  == form_field_name )
				{
					if ( control.checked ) formsCollection[i].elements[j].value = 1;
					else formsCollection[i].elements[j].value = 0;
				}
				else if (formsCollection[i].elements[j].name  == 'stamp' )
				{
					target_form = "postAjaxPageRefresh_"+ formsCollection[i].elements[j].value+" (0,1,0,'');";
				}
			}
		}
	}
	globalEval(target_form);
	window.scroll(0,0);
}



function popup_video(vid)
{
	serveGetRequest('/component_gen.php','resource_id=VIDEO_POPUP&tag_type=PHP_FILE&video_id='+vid+'&top_position='+document.body.scrollTop,'slide1');
	showDiv('slide1')

}

