// ****************************************************************
// ********************** AJAX PUBLICATIONS ***********************
// ******************* publications page - AJAX *******************
// **********************c2009 Nick Brennan ***********************
// ****************************************************************

//var selectedArtist = '';
var selectedArray = new Array();
var loadedArray = new Array();
var alpha_open = false;

// in_array function
function oc(a)
{
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}

// remove from array function
function removeByElement(arrayName,arrayElement)
{
for(var i=0; i<arrayName.length;i++ )
 { 
    if(arrayName[i]==arrayElement)
        arrayName.splice(i,1); 
  } 
}
  
/////////// loader gif control ////////////
var artist_loaded = false;

function startLoadingArtist(a) {
//alert(a);
artist_loaded = false;
//window.setTimeout('showLoadingImage()', 100);
showLoadingArtistImage(a);
}

function showLoadingArtistImage(a) {
var e = $(a + '_contents');
if (e && !artist_loaded) {

	/* Add temporary feedback that the request has been sent */   
//	var loadingImg = document.createElement('img');
//	loadingImg.src = 'images/artist-loader.gif';
	
	
//	el.appendChild(loadingImg);

    e.innerHTML = '<div align="center" id="' + a + '_loader"><img src="/images/artist-loader.gif" style="margin-bottom:10px;"/></div>';
    new Effect.Appear(e,{ duration: 0.3 });
}
}

function stopLoadingArtist(a) {
	var e = $(a + '_loader');
    Element.hide(e);
    artist_loaded = true;
  }
  
/////////// load publications for an artist ////////////  
function ajaxLoadArtist(a) {
	var e = $(a + '_contents');
	var thisarrow = $('arrow_' + a);
	// if artist is already selected, hide their list
	if( a in oc(selectedArray) ) {
		thisarrow.src = 'images/grey_arrow_horiz.png';
		thisarrow.width=9;
		thisarrow.height=11;

		Element.hide(e);
		removeByElement(selectedArray,a);
		
		new Effect.Highlight(e.parentNode, { startcolor: '#FFFFFF',	endcolor: '#FFFFFF', restorecolor: '#FFFFFF' }); 
	// artist is not selected
	} else {
		if (a in oc(loadedArray)) {
			// artist is loaded ... just show their list
			thisarrow.src = 'images/orange_arrow_vert.png';
			thisarrow.width=10;
			thisarrow.height=9;
			new Effect.Highlight(e.parentNode, { startcolor: '#e9e9e9',	endcolor: '#e9e9e9', restorecolor: '#e9e9e9' }); 
			e.show(a);
			selectedArray.push(a);			
		} else {
			// artist is not yet loaded, call the ajax load
//			Ajax.Responders.register({ onCreate : function() {startLoadingArtist(a);},onComplete : function() {stopLoadingArtist(a);} });

			thisarrow.src = 'images/orange_arrow_vert.png';
			thisarrow.width=10;
			thisarrow.height=9;

			new Effect.Highlight(e.parentNode, { startcolor: '#e9e9e9',	endcolor: '#e9e9e9', restorecolor: '#e9e9e9' }); 

		  	var url = '/loadArtist.php'; 
			// notice the use of a proxy to circumvent the Same Origin Policy. 
			new Ajax.Request(url, { 
				method: 'post', 
				parameters: { artist: a },
				onCreate : function() {startLoadingArtist(a);},
				onComplete : function() {stopLoadingArtist(a);},
				onSuccess: function(transport) { 
					var response = transport.responseText;
					e.update(response);
				}
			}); 
			selectedArray.push(a);
			loadedArray.push(a);
		}
	}
}

/////////// load group publications ////////////  
function ajaxLoadGroup() {
	var a = 'group';
	var e = $(a + '_contents');
	var thisarrow = $('arrow_' + a);
	// if artist is already selected, hide their list
	if( a in oc(selectedArray) ) {
		thisarrow.src = 'images/orange_arrow_horiz.png';
		thisarrow.width=9;
		thisarrow.height=11;

		Element.hide(e);
		removeByElement(selectedArray,a);
		
		new Effect.Highlight(e.parentNode, { startcolor: '#FFFFFF',	endcolor: '#FFFFFF', restorecolor: '#FFFFFF' }); 
	// artist is not selected
	} else {
		if (a in oc(loadedArray)) {
			// artist is loaded ... just show their list
			thisarrow.src = 'images/orange_arrow_vert.png';
			thisarrow.width=10;
			thisarrow.height=9;
			new Effect.Highlight(e.parentNode, { startcolor: '#e9e9e9',	endcolor: '#e9e9e9', restorecolor: '#e9e9e9' }); 
			e.show(a);
			selectedArray.push(a);			
		} else {
			// artist is not yet loaded, call the ajax load
//			Ajax.Responders.register({ onCreate : function() {startLoadingArtist(a);},onComplete : function() {stopLoadingArtist(a);} });

			thisarrow.src = 'images/orange_arrow_vert.png';
			thisarrow.width=10;
			thisarrow.height=9;

			new Effect.Highlight(e.parentNode, { startcolor: '#e9e9e9',	endcolor: '#e9e9e9', restorecolor: '#e9e9e9' }); 

		  	var url = '/loadGroup.php'; 
			// notice the use of a proxy to circumvent the Same Origin Policy. 
			new Ajax.Request(url, { 
				method: 'post', 
				parameters: { artist: a },
				onCreate : function() {startLoadingArtist(a)},
				onSuccess: function(transport) { 
					var response = transport.responseText;
					e.update(response);
				},
				onComplete : function() {stopLoadingArtist(a);}
			}); 
			selectedArray.push(a);
			loadedArray.push(a);
		}
	}
}

/////////// load other publications ////////////  
function ajaxLoadOther() {
	var a = 'other';
	var e = $(a + '_contents');
	var thisarrow = $('arrow_' + a);
	// if artist is already selected, hide their list
	if( a in oc(selectedArray) ) {
		thisarrow.src = 'images/orange_arrow_horiz.png';
		thisarrow.width=9;
		thisarrow.height=11;

		Element.hide(e);
		removeByElement(selectedArray,a);
		
		new Effect.Highlight(e.parentNode, { startcolor: '#FFFFFF',	endcolor: '#FFFFFF', restorecolor: '#FFFFFF' }); 
	// artist is not selected
	} else {
		if (a in oc(loadedArray)) {
			// artist is loaded ... just show their list
			thisarrow.src = 'images/orange_arrow_vert.png';
			thisarrow.width=10;
			thisarrow.height=9;
			new Effect.Highlight(e.parentNode, { startcolor: '#e9e9e9',	endcolor: '#e9e9e9', restorecolor: '#e9e9e9' }); 
			e.show(a);
			selectedArray.push(a);			
		} else {
			// artist is not yet loaded, call the ajax load
//			Ajax.Responders.register({ onCreate : function() {startLoadingArtist(a);},onComplete : function() {stopLoadingArtist(a);} });

			thisarrow.src = 'images/orange_arrow_vert.png';
			thisarrow.width=10;
			thisarrow.height=9;

			new Effect.Highlight(e.parentNode, { startcolor: '#e9e9e9',	endcolor: '#e9e9e9', restorecolor: '#e9e9e9' }); 

		  	var url = '/loadOther.php'; 
			// notice the use of a proxy to circumvent the Same Origin Policy. 
			new Ajax.Request(url, { 
				method: 'post', 
				parameters: { artist: a },
				onCreate : function() {startLoadingArtist(a)},
				onSuccess: function(transport) { 
					var response = transport.responseText;
					e.update(response);
				},
				onComplete : function() {stopLoadingArtist(a);}
			}); 
			selectedArray.push(a);
			loadedArray.push(a);
		}
	}
}

function openAlpha() {
	var e = $('alpha_master');
	var thisarrow = $('arrow_alpha');
	
	if (!alpha_open) {
		thisarrow.src = 'images/orange_arrow_vert.png';
		thisarrow.width=10;
		thisarrow.height=9;
		Element.show(e);
		alpha_open = true;
	} else {
		thisarrow.src = 'images/orange_arrow_horiz.png';
		thisarrow.width=9;
		thisarrow.height=11;
		Element.hide(e);
		alpha_open = false;
	}
}