// Javascript functions for basket/lightbox adding withut requiring page reload
// ------------------------------------------------------------------------------------------------------

var timerID = false;		// to store ID of the timer (so we can cancel it)
var saveDelay = 2000;		// delay in milliseconds after last click that the save will take place

var saveActive = false;		// flag to show if we are in the middle of a save

var basketItemsToAdd = new Object;			// object to use an associative array to store basket items to add
var basketItemsToRemove = new Object;		// object to use an associative array to store basket items to remove

var lightboxItemsToAdd = new Object;		// object to use an associative array to store lightbox items to add
var lightboxItemsToRemove = new Object;	// object to use an associative array to store lightbox items to remove

var saveHelper;		// var that will eventually hold the image object used to access the server

document.forcePK_LB = 0;

var basketRefreshRequired = false;
var lightboxRefreshRequired = false;

var overlayActive;


// make sure the user cannot exit the page without an impending save being skipped
window.onbeforeunload = makeSureSavedOnExit;

function makeSureSavedOnExit() {
	// check if timer is running
	if (timerID) {
		// stop the timer
		clearTimeout(timerID);
		// do the save
		saveBasketAndLightbox();
		// wait a second before leaving the page (seemed to be necessary on some browsers to make sure save complete)
		date = new Date();
		var curDate = null;
		do { var curDate = new Date(); } while(curDate-date < 1000);	
	}
}

function startSaveCountdown() {
	// clear previous timer if it is running
	if (timerID) clearTimeout(timerID);
	// set the save to run after the defined delay
	timerID = setTimeout('saveBasketAndLightbox();', saveDelay);
}


function setAssetBasketStatus( gridName, assetPK, state) {
	// only do it if we aren't in the middle of a save
	if (!saveActive) {
		// update the indicator in the grid
		setAssetGridBasketIndicatorStatus( gridName, assetPK, state );
		// update Add/Remove arrays as appropriate
		if (state) {
			basketItemsToAdd[assetPK] = true;
			basketItemsToRemove[assetPK] = false;
		} else {
			basketItemsToAdd[assetPK] = false;
			basketItemsToRemove[assetPK] = true;
		}
		// start countdown for save
		startSaveCountdown();
		// deal with the overlay if it is active
		basketRefreshRequired = true;
		if ( overlayActive && (window.frames[0].setAssetGridBasketIndicatorStatus) ) window.frames[0].setAssetGridBasketIndicatorStatus('overlayGrid', assetPK, state);
	}
}

function setMultipleAssetBasketStatus( gridName, assetList, state ) {
	assets = assetList.split(',');
	for (i=0; i<assets.length; i++) {
		setAssetBasketStatus( gridName, assets[i], state);
	}
}


function setAssetLightboxStatus( gridName, assetPK, state) {
	// only do it if we aren't in the middle of a save
	if (!saveActive) {
		// update the indicator in the grid
		setAssetGridLightboxIndicatorStatus( gridName, assetPK, state );
		// update Add/Remove arrays as appropriate
		if (state) {
			lightboxItemsToAdd[assetPK] = true;
			lightboxItemsToRemove[assetPK] = false;
		} else {
			lightboxItemsToAdd[assetPK] = false;
			lightboxItemsToRemove[assetPK] = true;
		}
		// start countdown for save
		startSaveCountdown();
		// deal with the overlay if it is active
		lightboxRefreshRequired = true;
		if ( overlayActive && (window.frames[0].setAssetGridLightboxIndicatorStatus) ) window.frames[0].setAssetGridLightboxIndicatorStatus('overlayGrid', assetPK, state);
	}
}

function setMultipleAssetLightboxStatus( gridName, assetList, state ) {
	assets = assetList.split(',');
	for (i=0; i<assets.length; i++) {
		setAssetLightboxStatus( gridName, assets[i], state);
	}
}



function saveBasketAndLightbox() { 
	// mark the save as active and clear the timerID
	saveActive = true;
	timerID = false;
	
	bskAdd = "";
	bskRem = "";
	lbxAdd = "";
	lbxRem = "";
		
	// do the save
	saveHelper = new Image;
	saveDate = new Date;
	saveDate = saveDate.getTime();
	saveURL = "doLightboxBasketUpdate.php";
	saveURL += "?t=" + saveDate;		// add unix datestamp to URL to make it unique (prevent caching)
	
	// get basket Items to add
	for (var a in basketItemsToAdd) {
		if (basketItemsToAdd[a]) bskAdd += ((bskAdd=="")?"":",") + a;
	}
	
	// get basket Items to remove
	for (var a in basketItemsToRemove) {
		if (basketItemsToRemove[a]) bskRem += ((bskRem=="")?"":",") + a;
	}
	
	// get lightbox Items to add
	for (var a in lightboxItemsToAdd) {
		if (lightboxItemsToAdd[a]) lbxAdd += ((lbxAdd=="")?"":",") + a;
	}
	
	// get basket Items to remove
	for (var a in lightboxItemsToRemove) {
		if (lightboxItemsToRemove[a]) lbxRem += ((lbxRem=="")?"":",") + a;
	}
	
	saveURL += "&ba=" + bskAdd + "&br=" + bskRem + "&la=" + lbxAdd + "&lr=" + lbxRem;
	
	// force adding to a specified lightbox if we've been given one
	if (document.forcePK_LB != 0) saveURL += "&lb=" + document.forcePK_LB;
		
	// send request to server
	saveHelper.src = saveURL;
	//do {} while (!(saveHelper.complete));
	
	// clear the arrays
	basketItemsToAdd = new Array();
	basketItemsToRemove = new Array();	
	lightboxItemsToAdd = new Array();
	lightboxItemsToRemove = new Array();
	
	// refresh the overlay if necessary
	if (overlayActive) {
		// wait a second
		date = new Date();
		var curDate = null;
		do { var curDate = new Date(); } while(curDate-date < 1000);	
		ovMode = window.frames[0].overlayMode;
		if (basketRefreshRequired && (ovMode=='Basket')) window.frames[0].location.href = "overlay.php";
		if (lightboxRefreshRequired && (ovMode=='Lightbox')) window.frames[0].location.href = "overlay.php";
	}
	basketRefreshRequired = false;
	lightboxRefreshRequired = false;
	
	// mark save no longer active
	saveActive = false;
}


// Overlay asset and basket adding functions
// ------------------------------------------------------------------------------------

function overlaySetAssetBasketStatus(PK_Asset, state) {

	switch (overlayMode) {
		// Lightbox mode
		case "Lightbox":
			setAssetBasketStatus('overlayGrid', PK_Asset, state);
			if (window.parent.setAssetGridBasketIndicatorStatus) window.parent.setAssetGridBasketIndicatorStatus('results', PK_Asset, state);
			break;
			
		// Basket mode
		case "Basket":
			setAssetBasketStatus('overlayGrid', PK_Asset, state);
			if (window.parent.setAssetGridBasketIndicatorStatus) window.parent.setAssetGridBasketIndicatorStatus('results', PK_Asset, state);
			saveBasketAndLightbox();
			date = new Date();
			do { var curDate = new Date(); } while(curDate-date < 1000);
			document.location.href = "overlay.php";
			break;
			
	}

}


function overlaySetAssetLightboxStatus(PK_Asset, state) {

	switch (overlayMode) {
		// Lightbox mode
		case "Lightbox":
			setAssetLightboxStatus('overlayGrid', PK_Asset, state);
			if (window.parent.setAssetGridLightboxIndicatorStatus) window.parent.setAssetGridLightboxIndicatorStatus('results', PK_Asset, state);
			saveBasketAndLightbox();
			date = new Date();
			do { var curDate = new Date(); } while(curDate-date < 1000);
			document.location.href = "overlay.php";
			break;
			
		// Basket mode
		case "Basket":
			setAssetLightboxStatus('overlayGrid', PK_Asset, state);
			if (window.parent.setAssetGridLightboxIndicatorStatus) window.parent.setAssetGridLightboxIndicatorStatus('results', PK_Asset, state);
			break;
					
	}

}