/*
Version Date			Developer			Comment
----------------------------------------------------------------------
d1		16-Jun-2003		James Home			Created
d2		06-Mar-2006		Martin Wilson		Changed popupViewImage
d3		23-Oct-2006		Adam Bones			Created popupCropImage
d4		23-Jan-2007		Kevin Bennett		Added popupTerms
d5		23-Feb-2007		Steve Bryan			Added functions for popup help link event handling
d6		28-Feb-2007		Matt Stevenson		Added audio popup
d7		28-Feb-2007		Matt Stevenson		Added popupPreview
d8		02-Apr-2008		Matt Woollard		Created	popupAssetAudit
d9 		02-May-2008		Matt Woollard		Added versionNumber to popupAssetAudit
d10		10-Jul-2008		Matt Woollard		Added popupViewAgreement
d11		10-Jul-2008		Matt Woollard		Added popupAddAgreement
d12		10-Oct-2008		Matt Woollard		Added popupAssetWorkflowAudit
d13		20-Oct-2008		Ben Browning		Changed to bright-util.js and reorganised
d14		10-Nov-2008		Ben Browning		Added 
d15		22-Jan-2009		Matt Woollard		Added moveAssetInLightbox
d15		07-May-2009		Matt Woollard		Added preventTimeout
d16		08-Jun-2009		Matt Woollard		Changed add to lightbox so counter isn't updated until OK response received
----------------------------------------------------------------------
*/

/* 
======================================================================
JAVASCRIPT STYLES
----------------------------------------------------------------------
Anonymous function that dynamically inserts a stylesheet link to 
js-enabled.css
Useful for initially hiding expanding content without any flicker
*/
(function () {
	var head = document.getElementsByTagName("head")[0];
	if (head) {
		var scriptStyles = document.createElement("link");
		scriptStyles.rel = "stylesheet";
		scriptStyles.type = "text/css";
		scriptStyles.href = "../css/standard/js-enabled.css";
		head.appendChild(scriptStyles);
	}
}());

/* 
======================================================================
POPUP WINDOWS
----------------------------------------------------------------------
*/

function popupGeneric(elms,options,href,windowRef) 
/*
----------------------------------------------------------------------
	d1		20-October-2008		Ben Browning		Created
----------------------------------------------------------------------
Notes:
Generic popup window function. 

Arguments:
elms(required): is an array of elements for which the popup will be triggered onclick. 
	This means the function should be called once the page has loaded (so 
	the elements exist in the DOM). It can only contain one element, but it must be an array.
options(optional): for opening e.g. "width=x, height=y"
href(optional): specify the url (for links uses href value)
windowRef(optional): specify the reference to the window

Example Usage:
<script type="text/javascript">
	Event.observe(window, 'load', function() {
		// Listen for clicks on my popup links <a id="myPopup">
		var myLink = $$('#myPopup');
		popupGeneric(myLink);
	});
</script>
*/
{
	//var elms = $(elm);
	var i = 0;
	while (i < elms.length){
		elms[i].onclick = function(){
			var newWindow = window.open(href || this.href, windowRef || "newWindow",options || "width=500,height=500,toolbar=no,location=no,status=no,menubar=no,resizable=yes,scrollbars=yes,titlebar=no");
			if(newWindow.blur){newWindow.focus();};
			return false;
		};
		i++;
	};
}



function displayPopup(a_sURL)
/*
----------------------------------------------------------------------
	d1		16-Jun-2003		James Home			Created
----------------------------------------------------------------------
*/
{	
	mywindow=open(a_sURL ,'myname','resizable=no,scrollbars=yes,width=460,height=290');
	mywindow.location.href = a_sURL;
	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}

function popupViewAgreement(a_lAgreementId)
/*
----------------------------------------------------------------------
	d10		10-Jul-2008		Matt Woollard		Created
----------------------------------------------------------------------
*/
{
	mywindow=open('../action/viewAgreement?id=' + a_lAgreementId,'viewAgreement','resizable=yes,scrollbars=yes,toolbar=no,width=500,height=500');

	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}

function popupAddAgreement()
/*
----------------------------------------------------------------------
	d11		11-Jul-2008		Matt Woollard		Created
----------------------------------------------------------------------
*/
{
	mywindow=open('../action/addAgreementPopup','viewAgreement','resizable=yes,scrollbars=no,toolbar=no,width=800,height=600');

	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}


function popupEditAgreement(a_lAgreementId)
/*
----------------------------------------------------------------------
	d11		11-Jul-2008		Matt Woollard		Created
----------------------------------------------------------------------
*/
{
	mywindow=open('../action/editAgreementPopup?copy=1&id='+a_lAgreementId,'viewAgreement','resizable=yes,scrollbars=no,toolbar=no,width=800,height=600');

	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}

function popupViewImage(a_lImageId)
/*
----------------------------------------------------------------------
	d1		23-May-2005		James Home			Created
----------------------------------------------------------------------
*/
{
	mywindow=open('../action/viewFullSizedImage?id=' + a_lImageId,'viewImage','resizable=yes,scrollbars=no,toolbar=no,width=500,height=500');
	
	// Is this required? If so for which browsers and can we only run it when necessary?
	// mywindow.location.href = '../action/viewFullSizedImage?id=' + a_lImageId;
	
	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}

function popupViewImage(a_lImageId, imageWidth, imageHeight, hasMultipleLayers)
/*
----------------------------------------------------------------------
	d1		08-Jun-2005		James Home			Created
	d2		06-Mar-2006	Martin Wilson		Changed to specify size to viewFullSizedImage
----------------------------------------------------------------------
*/
{

	var screenWidth = imageWidth;
	var screenHeight = imageHeight;

	// Work out the size (largest of 2 dimensions):
	var iSize = imageWidth;

	if (imageHeight > imageWidth)
	{
		iSize = imageHeight;
	}
	// Limit the size of the popup window
	if(screenWidth>750)
	{
		screenWidth=750;
	}
	if(screenHeight>550)
	{
		screenHeight=550;
	}
	else if(hasMultipleLayers)
	{
		screenHeight+=60;
	}
	
	//screenWidth = 500;
	//screenHeight = 500;

	mywindow=open('../action/viewFullSizedImage?id=' + a_lImageId + "&size=" + iSize + '&layer=1','viewImage','resizable=yes,scrollbars=yes,toolbar=no,width=' + screenWidth + ',height=' + screenHeight);
	
	// Is this required? If so for which browsers and can we only run it when necessary?
	// mywindow.location.href = '../action/viewFullSizedImage?id=' + a_lImageId;
	
	mywindow.focus();

	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}


function popupFormatHelp()
/*
----------------------------------------------------------------------
	d1		20-Jun-2006		Ben Browning		Created
----------------------------------------------------------------------
*/
{
	mywindow=open('../action/viewFormatHelp','HighResHelp','resizable=yes,scrollbars=yes,toolbar=no,width=500,height=500');

	mywindow.location.href = '../action/viewFormatHelp';
	
	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}

function popupConditions(a_sAction)
{
	return (popupConditions(a_sAction,'false'));
}

function popupConditions(a_sAction,a_sExtra)
/*
----------------------------------------------------------------------
	d1		07-Jun-2005		James Home			Created
----------------------------------------------------------------------
*/
{
	mywindow=open('../action/' + a_sAction + '?extra=' + a_sExtra,'viewConditions','resizable=yes,scrollbars=yes,toolbar=no,width=500,height=500');
	
	mywindow.location.href = a_sAction + '?extra='+ a_sExtra;
	
	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}

function popupAssetUse(a_lAssetId)
/*
----------------------------------------------------------------------
	d1		24-Jun-2005		James Home			Created
----------------------------------------------------------------------
*/
{
	mywindow=open('../action/viewAssetUsage?id=' + a_lAssetId,'viewAssetUsage','resizable=yes,scrollbars=yes,toolbar=no,width=590,height=500');

	mywindow.location.href = '../action/viewAssetUsage?id=' + a_lAssetId;
	
	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}

function popupAssetAudit(a_lAssetId, a_lVersionNumber)
/*
----------------------------------------------------------------------
	d8		02-Apr-2008		Matt Woollard		Created	
	d9 		02-May-2008		Matt Woollard		Added versionNumber to popupAssetAudit
----------------------------------------------------------------------
*/
{
	mywindow=open('../action/viewAssetAudit?id=' + a_lAssetId + '&versionNumber=' + a_lVersionNumber,'viewAssetUsage','resizable=yes,scrollbars=yes,toolbar=no,width=590,height=500');

	mywindow.location.href = '../action/viewAssetAudit?id=' + a_lAssetId + '&versionNumber=' + a_lVersionNumber;
	
	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}

function popupAssetWorkflowAudit(a_lAssetId)
/*
----------------------------------------------------------------------
	d12		10-Oct-2008		Matt Woollard		Created	
----------------------------------------------------------------------
*/
{
	mywindow=open('../action/viewWorkflowAudit?id=' + a_lAssetId, 'viewWorkflowAudit','resizable=yes,scrollbars=yes,toolbar=no,width=590,height=500');

	mywindow.location.href = '../action/viewWorkflowAudit?id=' + a_lAssetId;
	
	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}

function popupHelp(section_name)
/*
----------------------------------------------------------------------
	d1		07-Jun-2005		James Home			Created
	d2		23-Feb-2007		Steve Bryan			I think this is redundant now - see handler at top of file
----------------------------------------------------------------------
*/
{
	mywindow=open('../action/viewHelp?helpsection=' + section_name,'viewHelp','resizable=yes,scrollbars=yes,toolbar=no,location=no,width=500,height=500');

	mywindow.location.href = '../action/viewHelp?helpsection=' + section_name;
	
	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}

function popupTerms(option_id)
/*
----------------------------------------------------------------------
	d1		07-Jun-2005		James Home			Created
----------------------------------------------------------------------
*/
{
	mywindow=open('../action/viewTerms?id=' + option_id,'viewTerms','resizable=yes,scrollbars=yes,toolbar=no,location=no,width=500,height=500');

	mywindow.location.href = '../action/viewTerms?id=' + option_id;
	
	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}


function attributePopup(id)
/*
----------------------------------------------------------------------
	d1		28-Feb-2007		Matt Stevenson			Created
----------------------------------------------------------------------
*/
{
	mywindow=open('../action/viewAttributeValue?id=' + id,'AttributeValue','resizable=yes,scrollbars=yes,toolbar=no,location=no,width=500,height=250');

	mywindow.location.href = '../action/viewAttributeValue?id=' + id;
	
	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}


function videoPopup(a_sURL)
/*
----------------------------------------------------------------------
	d1		11-Aug-2006		Ben Browning			Created
----------------------------------------------------------------------
*/
{	
	mywindow=open(a_sURL ,'myname','toolbar=no,location=no,resizable=yes,scrollbars=yes,width=558,height=400');
	mywindow.location.href = a_sURL;
	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}



function audioPopup(a_sURL)
/*
----------------------------------------------------------------------
	d6		28-Feb-2007		Matt Stevenson		Created
----------------------------------------------------------------------
*/
{	
	mywindow=open(a_sURL ,'myname','toolbar=no,location=no,resizable=yes,scrollbars=yes,width=420,height=200');
	mywindow.location.href = a_sURL;
	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}

function popupPreview(a_sURL,a_iWidth,a_iHeight)
/*
----------------------------------------------------------------------
	d7		05-Feb-2008		James Home		Created
----------------------------------------------------------------------
*/
{	
	if(a_iWidth<=0)
	{
		a_iWidth = 800;
	}
	if(a_iHeight<=0)
	{
		a_iHeight = 600;
	}
	
	mywindow=open(a_sURL ,'myname','toolbar=no,location=no,resizable=yes,scrollbars=yes,width=' + (a_iWidth+50) + ',height=' + (a_iHeight+80));
	mywindow.location.href = a_sURL;
	mywindow.focus();
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}


/* 
======================================================================
Show and Hide
----------------------------------------------------------------------
*/
function expand_content(item)
/*
----------------------------------------------------------------------
	d1		03-Jan-2007		Ben Browning			Created
----------------------------------------------------------------------
*/
{
	if( document.getElementById && document.getElementsByTagName ){
  		if( document.getElementById(item) ){
   		var check = document.getElementById(item);
   			document.getElementById('expandLink').style.display = 'none';
				document.getElementById('collapseLink').style.display = 'block';
   			check.style.display = 'block';
   			return false;
   		}
 	}
}
	
function collapse_content(item)
/*
----------------------------------------------------------------------
	d1		03-Jan-2007		Ben Browning			Created
----------------------------------------------------------------------
*/
{
	if( document.getElementById && document.getElementsByTagName ){
  		if( document.getElementById(item) ){
    		var check = document.getElementById(item);
   			document.getElementById('expandLink').style.display = 'block';
   			check.style.display = 'none';
   			return false;
   		}
	}
}

function showHide(id,imgId)
/*
----------------------------------------------------------------------
	d1		???				James Home				Created			
	d2		11-May-2009		Ben Browning			Moved into bright_util.js
----------------------------------------------------------------------
This function is used to show and hide attribute groups on asset detail pages
*/
{
	var element = document.getElementById(id);
	var image = document.getElementById(imgId);
	
	if(element)
	{
		if(element.style.display!='block')
		{
			element.style.display='block';
			image.src='../images/standard/icon/subtract.gif';
		}
		else
		{
			element.style.display='none';
			image.src='../images/standard/icon/add.gif';
		}
	}
}
/* 
======================================================================
Stripey table rows
----------------------------------------------------------------------
*/
function stripe(trClass)
/*
----------------------------------------------------------------------
	d1		11-May-2009		Ben Browning			Moved into bright_util.js
----------------------------------------------------------------------
*/
{
	//dynamically make the metadata table stripey
	var tables = $$('table.stripey');
	for(var i=0; i < tables.length; i++)
		{
			stripeRows(tables[i]);
		}
}

function stripeRows(table)
/*
----------------------------------------------------------------------
	d1		???				James Home				Created		
	d2		11-May-2009		Ben Browning			Moved into bright_util.js
----------------------------------------------------------------------
*/
{
	if(table!=null)
	{
		var rows = table.getElementsByTagName("tr");  
		
		//iterate through table rows
		for(var i = 0; i < rows.length; i++)
		{          
	 		//manipulate rows
			if(i % 2 == 0)
			{
				rows[i].className+=rows[i].className?' even':'even';
			}    
		}
	}
}

/* 
======================================================================
NAVIGATION MENU JS
----------------------------------------------------------------------
Functions to control behaviour of the main menu.
We now manipulates class names instead of style properties so that 
the presentation of the drop downs is customisable through css alone.

NB: Makes use of prototype.js library methods
*/

//global variable
var toggleDownArrow = 1;


function showMenu(menu_id) {
	var menu = $(menu_id);
	var link = $(menu_id + '_link');
	if ((menu.style.display == 'none') || (menu.style.display == ''))
	{
		menu.style.display='block';
		if (toggleDownArrow) {
			link.addClassName('open');
			link.removeClassName('openHover');
		}

	}
	else
	{
		menu.style.display='none';
		link.removeClassName('open');
		link.addClassName('openHover');
	}
}

function hoverMenu(menu_id,toggleVisibility) {
	var menu = $(menu_id);
	var link = $(menu_id + '_link');
	var admin = $('adminPage') || $('importPage');
	
	if (!(admin)) {
		if ((menu.style.display == 'none') || (menu.style.display == '')) {
			
			if (toggleVisibility && toggleDownArrow) {
				link.addClassName('openHover');
			}
			else {
				link.removeClassName('openHover');
			}
		}
	}
}




/* 
======================================================================
AJAX FUNCTIONS
----------------------------------------------------------------------
Misc UI enhancing functions
*/
/*
----------------------------------------------------------------------
Image Hover
----------------------------------------------------------------------
	d1		???				Ben Browning			Created		
	d2		11-May-2009		Ben Browning			No longer use an 
													ajax call and position 
													image so it doesn't run 
													off the page
----------------------------------------------------------------------

NOTES:
The following functions dynamically create a preview image, alter its 
position if necessary and display it.
NB: Makes use of prototype.js library methods
*/

// global variable for timeout timer so can cancel it in hide function
var timedFunction;
// global variables for current popup image dimensions
var imageWidth;
var imageHeight;
var thumbheight;
//global variable for a unique id to identify the currrent asset being hovered over
var uniqueId;
// flag for whether image is loaded (used to eliminate flicker of loading graphic)
var loaded=0;

function showPreview(thumb,thisId,imgSrc) 
{
	//set global variable for current thumbnail height
	thumbHeight = thumb.height;
	//set global variable for id of this asset
	uniqueId = thisId;
	//show popup image after a short delay
	timedFunction = showPreviewSpan.delay(0.4, imgSrc);
}

function showPreviewSpan(imgSrc) 
{	
	//initialise flag for whether popup is loaded
	loaded = 0;
	// set up image using the passed in src
	var srcAttribute = '../servlet/display?file='+imgSrc;
	var fetchedImg;
	fetchedImg = new Element('img'); 
	fetchedImg.id = 'image_'+uniqueId;
	$('id_'+uniqueId).update(fetchedImg);
	//listener for when image is loaded
	fetchedImg.observe('load', positionImage); 
	fetchedImg.src = '../servlet/display?file='+imgSrc;
	//show loading gif (if image has not already loaded)
	if (loaded == 0){
		$('load_'+uniqueId).setStyle({
			display:'block'
		});
	}	
}

function positionImage(event)
{
	loaded=1;
	//hide loading gif
	$('load_'+uniqueId).setStyle({
		display:'none'
	});
	//show span containing popup
	$('id_'+uniqueId).setStyle({
		display:'block'
	});
	//get reference to popup image on which this onload event has fired
	var img;
	img = event.findElement();
	//update global variables
	imageWidth = img.width;
	imageHeight = img.height;
	//check position of image (check it still exists as hidePreview() may have deleted it)
	if ($(img)) {
		//initialise vertical position
		$(img).up('span.inner').setStyle({
			top:'0px'
		});
		//get position of containing span
		var imgPosition = $(img).up('span.larger').viewportOffset();
		//get size of window
		var browserDimensions = document.viewport.getDimensions();
		
		//set position of image if it is too close to right hand edge or bottom of browser
		//alert(browserDimensions.width - imgPosition[0]+','+imageWidth);
		if ((browserDimensions.width - imgPosition[0]) < imageWidth) {
			$(img).up('span.inner').setStyle({
				right:'0px'
			});
		}
		if ((browserDimensions.height - imgPosition[1]) < imageHeight) {
			// want to shift image up by its height + the height of the thumbnail above it
			offsetY = imageHeight + thumbHeight + 5;
			$(img).up('span.inner').setStyle({
				top:'-'+offsetY+'px'
			});
		}	
		//having set the position now show the image (using visibility so IE still can calculate
		//image height and width).
		
		$(img).setStyle({
			visibility:'visible'
		});

	}
		
}
function hidePreview(uniqueId) 
{
	//cancel timeout
	window.clearTimeout(timedFunction);
	var popupSpanId = 'id_'+uniqueId;
	//get reference to popup image
	var imageId = 'image_'+uniqueId;
	if ($(imageId)) {
		//delete image
		$(popupSpanId).removeChild($(imageId));
	}	
	//hide popup span
	$(popupSpanId).setStyle({
		display:'none'
	});
	//hide loading image
	$('load_'+uniqueId).setStyle({
		display:'none'
	});
}


/*
----------------------------------------------------------------------
Add to lightbox
----------------------------------------------------------------------
NOTES:
Uses Ajax to dynamically add assets to lightbox.
*/
function addToLightBox(assetId, numAssets, forwardParams) 
/*
----------------------------------------------------------------------
	d1		18-Aug-2008		Matt Woollard			Created
	d2		26-Mar-2009		Ben Browning			Modified to work without the invalid hidden fields
													(Makes use of prototype)
	d16		08-Jun-2009		Matt Woollard			Changed add to lightbox so counter isn't updated until OK response received
----------------------------------------------------------------------
*/
{
	var divId = 0;
	var url = 'addToAssetBox?id=' + assetId + '&type=Ajax&'+forwardParams;
	
	document.getElementById("ajaxAddToLightBox"+assetId).removeAttribute("href");
	document.getElementById("ajaxAddToLightBox"+assetId).removeAttribute("onclick");
	document.getElementById("ajaxAddToLightBox"+assetId).innerHTML = "adding...";
	
	new Ajax.Request(url, 
	{
		method: 'get',
		onSuccess: function(transport) 
		{
			var notice = $('notice');
			
			if (transport.responseText.match("OK"))
			{
			
				//get reference to html element containing the current number of assets in the lightbox
				var displayNumItemsIn = $$('#lbText strong').first();
			
				var numItems = displayNumItemsIn.innerHTML.stripTags();
			
				// if there are currently 0 items, now there's 1 so use different text (which is got from a hidden span on the page)
				if (numItems==0)
				{
					document.getElementById("itemsInLightbox").innerHTML = document.getElementById("itemInLightBoxText").innerHTML;
				}
				else
				{
					document.getElementById("itemsInLightbox").innerHTML = document.getElementById("itemsInLightBoxText").innerHTML;
				}
				
				// update the number of items in the lightbox
				var displayNumItemsOut = $$('#lbText strong').first();
				displayNumItemsOut.update(eval(numItems)+1);
			
				// update the link to show that it is in the lightbox
				document.getElementById("ajaxAddToLightBox"+assetId).className = "in";
				document.getElementById("ajaxAddToLightBox"+assetId).removeAttribute("href");
				document.getElementById("ajaxAddToLightBox"+assetId).removeAttribute("onclick");
				document.getElementById("ajaxAddToLightBox"+assetId).innerHTML = document.getElementById("inLightBoxText").value;
			}
		}
	});
	
	
}


/*
----------------------------------------------------------------------
Reordering Assets
----------------------------------------------------------------------
NOTES:
Uses Ajax to dynamically move assets in lightbox.
*/
function moveAssetInLightbox(assetId, assetIdInfrontOf) 
/*
----------------------------------------------------------------------
	d15		22-Jan-2009		Matt Woollard		Created
----------------------------------------------------------------------
*/
{	
	var divId = 0;
	var url = 'moveAssetInAssetBox?id=' + assetId + '&moveTo='+assetIdInfrontOf;
	var myAjax = new Ajax.Updater(divId, url, {method: 'get'});
	
}


/*
----------------------------------------------------------------------
Dynamic bulk upload
----------------------------------------------------------------------
NOTES:
Uses Ajax to dynamically update the bulk import page so we no longer have 
to rely on an ugly meta refresh.
*/
function ajaxUpdate(url,url2) 
/*
----------------------------------------------------------------------
	d1		1-Apr-2009		Ben Browning		Created
	d2		22-May-2009		Ben Browning		Removed 'decay' feature (i.e. set to 1)
----------------------------------------------------------------------
*/
{
	new Ajax.PeriodicalUpdater('importProgress', url, {
		// make the call every 2 seconds
		frequency: 2, decay: 1,
		onSuccess: function(myRequest) {
			//check if it contains the text 'Import Complete'
			if (myRequest.responseText.match("Import Complete")){
				// if it does, force page reload
				window.location = url2;
			}
		}
	});
}

/*
----------------------------------------------------------------------
Prevent timeout
----------------------------------------------------------------------
NOTES:
This prevents users seeing a timeout message 
It accesses the homepage every 60 seconds 
*/
function preventTimeout() 
/*
----------------------------------------------------------------------
	d15		07-May-2009		Matt Woollard		Created
----------------------------------------------------------------------
*/
{
	new Ajax.PeriodicalUpdater('container', 'refreshSession', {
		// make the call every 60 seconds
		frequency: 60,
		onSuccess: function(myRequest) {
		}
	});
}

/*
----------------------------------------------------------------------
Set up Autocompleter fields
----------------------------------------------------------------------
NOTES:
Requires prototype and scriptaculous to initialise autocompleter fields.
http://wiki.github.com/madrobby/scriptaculous/ajax-autocompleter
*/
function addSpace(delimiters,delimSet,input,li)
 /*
----------------------------------------------------------------------
	d1		18-Sep-2009		Ben Browning		Created
----------------------------------------------------------------------
This function is called just after the autocomplete request is made.
*/
{
	//only add a delimter if a delimeter has been defined
	if (delimSet) {
		//convert to array
		var myArray = $A(delimiters);
		input.value = input.value + myArray[0];
	}	
}


function initAutocompleter(inputId,divId,delimiters,attrId,searchEdit,langCode)
 /*
----------------------------------------------------------------------
	d1		07-Sep-2009		Ben Browning		Created
----------------------------------------------------------------------
Note: attrId is an optional parameter
*/
{
	var delimSet = true;
	if (typeof delimiters == "undefined") {
		delimiters = " ";
		var delimSet = false;
	} 
	// pass an attributeId parameter to do auto-completion for a particular attribute ID. Don't pass an
	// attributeId parameter to do auto-completion for all attributes (e.g. for the quick search)
	if (typeof attrId == "undefined") {
		attrId = "";
	}	
	// set default behaviour to 'search'
	if (typeof searchEdit == "undefined") {
		searchEdit = "search";
	}	
	// check for language code, if not set as empty string
	if (typeof langCode == "undefined") {
		langCode = "";
	}	
	//set up autocompleter options	
	var options = {
	 minChars: 2,
	 frequency: 0.2,
	 paramName: "prefix",
	 parameters: "attributeId="+attrId+"&completeType="+searchEdit+"&languageCode="+langCode,
	 tokens: delimiters,
	 afterUpdateElement : addSpace.bind(this,delimiters,delimSet)
	 };
	//initialise autocompleter
	new Ajax.Autocompleter(inputId, divId, "../action/autoComplete", options);
	//new Ajax.Autocompleter(inputId, divId, "../jsp/standard/en/inc/autocomplete_results.jsp", options);
	//IE fix for issue with initial delay in loading autocomplete suggestions:
	try
	{
		Position.clone($(inputId), $(divId),
			{ setHeight: false, offsetTop: $(inputId).offsetHeight});  
	}
	catch(e){}
}
/* 
======================================================================
PAGE FUNCTIONS
----------------------------------------------------------------------
Less commonly used functions that are still worth putting into an include
*/

function selectCheckboxes(allOrNone, cssSelector) {
/*
Switches an array of checkboxes all on or all off
*/
	var checkBoxArray = $$(cssSelector);
	//Iterate through checkboxes on the page
	for ( var i = 0; i < checkBoxArray.length; i++){
		if (allOrNone == 'all') {
			checkBoxArray[i].checked = true;

		} else if (allOrNone == 'none') {
			checkBoxArray[i].checked = false;
		}

	}
}


function toggleState(thisCheckbox) {
/*
Toggles a checkbox between checked and unchecked.
*/
	if ( thisCheckbox.checked == true ) {
		thisCheckbox.checked = false;
	}
	else {
		thisCheckbox.checked = true;
	}
}
/*
Toggles class of containing <li> for extra visual feedback
*/
function toggleClass(wrappingLi,force) {
	if ( wrappingLi.hasClassName('selected')) {
		wrappingLi.removeClassName('selected');
	}
	else {
		wrappingLi.addClassName('selected');
	}
}



function toggleControls()
{
	//toggle visibility of control panel
	$$("#comparePanel div.inner").invoke("toggle");
	$("controlsToggle").toggleClassName("expand");
	
	//toggle the css class of the link
	if ($("controlsToggle").hasClassName("collapse_white")) 
	{	
		$("controlsToggle").removeClassName("collapse_white");
		$("controlsToggle").addClassName("expand_white");
	} 
	else 
	{
		$("controlsToggle").removeClassName("expand_white");
		$("controlsToggle").addClassName("collapse_white");
	} 
}

