// JavaScript Document

function cleanQuery(strInput) {
	return strInput.replace("&", "");
}

//************************************************************
  
function processMailForm() { 
	
	var strMailing = ($('#MailingList').is(':checked') == true ? '1' : '0');

	doJqueryAjax('', '', 'op=mailing&email=' + cleanQuery($("#Email").val()) + 
				'&MailingList=' + strMailing +
				'&Forename=' + cleanQuery($("#Forename").val()) +
				'&Surname=' + cleanQuery($("#Surname").val()) +
				'&Interest=' + cleanQuery($("#Interest").val()),
				'GET', 0);
}

//************************************************************
  
function getArtistArchive(intID) {
	doJqueryAjax('', '', 'op=archive&ref=' + intID + '&archive=1', 'GET', 0);

}

//************************************************************
  
function getArtistPublications(intID) {
	doJqueryAjax('', '', 'op=publicationlist&ref=' + intID, 'GET', 0);

}


//************************************************************
  
function getArtistExhibitions(intID) {
	doJqueryAjax('', '', 'op=exhibitionlist&ref=' + intID, 'GET', 0);

}

//************************************************************
  
function getNextPageThumbsWork(intPage, intArtist, intSection, intMedia, intArchive) {
	doJqueryAjax('', '', 'op=artistthumb&ref=' + intArtist + '&page=' + intPage + '&media=' + intMedia + '&section=' + intSection + '&archive=' + intArchive, 'GET', 0);
}

//************************************************************
  
function getNextPageThumbsExhibition(intPage, intExhibition, $intArtist) {
	doJqueryAjax('', '', 'op=exthumb&ref=' + intExhibition + '&page=' + intPage + '&artist=' + $intArtist, 'GET', 0);
}

//************************************************************

function getArtistBiog(intID) {
	
	doJqueryAjax('', '', 'op=biog&ref=' + intID, 'GET', 0);
	
}

//************************************************************
  
function adjustHeights(arrDivs) {
	maxHeight = 0;
	var i = 0;
	for (i=0; i<arrDivs.length; i++) { 
		if ($('#' + arrDivs[i]).height() > maxHeight) {
			maxHeight = $('#' + arrDivs[i]).height();
		}
	}
	for (i=0; i<arrDivs.length; i++) { 
		if ($('#' + arrDivs[i]).height() < maxHeight) {
			$('#' + arrDivs[i]).height(maxHeight);
		}
	}
}

//************************************************************

function getExhibitionArtists(intExhibition) {
	doJqueryAjax('', '', 'op=exhibitionartists&ref=' + intExhibition, 'GET', 0);
}

//************************************************************

function newImageExhibitions(strElement, strImage, strDesc, strCredit, strTitle, intExhibition, strArtistLink, intRef) {
	$("#" + strElement).html('<a href="javascript: setDescription(' + intRef + ', 1);"><img src="' + strImage + '" alt="' + strTitle + '" title="' + strTitle + '" /></a>');
	$("#imageCredit").html(strCredit);
	$("#artistDetails").html(strArtistLink + strDesc);
}

//************************************************************

function setDescription(intRef, intFull) {
	if ( intFull === undefined ) {
      intFull = 0;
   }

	doJqueryAjax('', '', 'op=getPictureDescription&ref=' + intRef + '&section=' + intFull, 'GET', 0);
}

//************************************************************

function newImage(strElement, strImage, strDesc, strCredit, strTitle, intRef) {
	$("#" + strElement).html('<a href="javascript: setDescription(' + intRef + ');"><img src="' + strImage + '" alt="' + strTitle + '" title="' + strTitle + '" /></a>');
	$("#imageCredit").html(strCredit);
	$("#artistDetails").html(strDesc);
}

//************************************************************

function swapImage(strReplace, strImage) {
	$("#" + strReplace).attr("src", strImage);
}

//************************************************************

function changePortfolioThumb(strReplace, strImage, strDescription) {
	
	$("#" + strReplace).html('');
	$("#" + strReplace).css('display', 'none');
	$("#" + strReplace).html('<img src="/images/site/' + strImage + '" alt="' + strDescription + '" title="' + strDescription + '" /><p style="text-align: left; padding-top: 10px;">' + strDescription + '</p>');
	$("#" + strReplace).fadeIn("slow");
	
}

//************************************************************

function replaceImage(strReplace, strImage, strDescription) {
	$("#" + strReplace).html('');
	$("#" + strReplace).css('display', 'none');
	$("#" + strReplace).html('<img src="/images/site/' + strImage + '" alt="' + strDescription + '" title="' + strDescription + '" /><p style="text-align: right; padding-top: 10px;">' + strDescription + '</p>');
	$("#" + strReplace).fadeIn("slow");
	
	$("#showProjectContent").fadeIn("slow");
	
	var arrDivColumns = new Array('microLeftMenu', 'microLeftContent', 'microLeftRight'); 
	adjustHeights(arrDivColumns);
}

//************************************************************

function doJqueryAjax(strShow, strHide, strQueryData, strMethod, intReplace)  {
	
	var intDebug = 0;
	var strDataType = 'json';
	var strData = '';
	var strQuery = '';
	
	if (strMethod == 'POST') {
		strData = $("#" + strForm).serialize(); 
		strQuery = "?" + strQueryData;
	} 
	else {
		strData = strQueryData;
	}
	
	var strURL = "/php/object-austin-desmond-ajax.php" + strQuery;
	
	var strDebug =  "<strong>doJqueryAjaxM</strong>" + 
					" <br />strShow = " + strShow + 
					" <br />strHide = " + strHide + 
					"<br /> strQueryData = " + strQueryData + 
					"<br /> strMethod = " + strMethod + 
					"<br />";
	
	
	//$("#debug").html(strDebug);
	
	$.ajax({  
		type: 		strMethod,   
		url: 		strURL,  
		data: 		strData,
		cache: 		false,  
		dataType: 	'json',
		
		beforeSend: function() { 
		
			if (strHide.length > 0) {
				$("#" + strHide).hide(); //hide
			} 
			if (strShow.length > 0) {
				$("#waiting").show("slow"); //hide
				$("#" + strShow).hide(); //hide
				$("#" + strShow).html("");
			}
			if (intDebug > 0) {
				$("#ajaxurl").html("<p>" + strURL + " (" + strQueryData + ")</p>");
			}
			//alert(strURL);
		}, //show loading just when link is clicked
		
		success: function(objReturn, textStatus) {  
			//alert('returned ok');
			switch (strDataType) {
				case 'json': // JSON
					 
					if (objReturn.result == 'success') {
						strDebug += "objReturn.result = success" + " - " + objReturn.op + "<br />";
						
						switch (objReturn.op) {
							
							case 'mailing':
								
								$("#mailContainer").html(objReturn.html);
							
							break;
							
							case 'replace': // replace table
							
								var strLinks = '' + objReturn.links;
								if (strLinks.length > 0) {
									$("#imageLinks").html(objReturn.links);
								}
							
								$("#" + strShow).html(objReturn.html); //show the html inside
								$("#" + strShow).fadeIn('slow');
								
								strDebug += "<strong>Action:</strong> <br />HTML = " + strShow + "<br />";
											
							break;
							
							case 'biog':
								$("#featureHolder").html(objReturn.html);
								$("#artistDetails").html(objReturn.htmlSecond);
							break;
							
							case 'exthumb':
								$("#thumbHolder").html(objReturn.html);
								$("#imageThumbNav").html(objReturn.htmlSecond);
								$("#featureHolder").html(objReturn.htmlFull);
							break;
							
							case 'artistthumb':
								$("#featureHolder").html(objReturn.htmlFull);
								$("#thumbHolder").html(objReturn.html);
								$("#imageThumbNav").html(objReturn.htmlSecond);
								$("#artistDetails").html(objReturn.htmlLeft);
							break;
							
							case 'exhibitionlist':
								$("#artistDetails").html(objReturn.html);
								$("#artistDetails").fadeIn('slow');
							break;
							
							case 'publicationlist':
								$("#artistDetails").html(objReturn.html);
								$("#artistDetails").fadeIn('slow');
							break;
							
							case 'exhibitionartists':
								$("#artistDetails").html(objReturn.html);
								$("#artistDetails").fadeIn('slow');
							break;
							
							case 'getPictureDescription':
								$("#artistDetails").html(objReturn.html);
							break;
						}
					}
				break;
			}
			
			$("#waiting").hide("slow"); //hide
			//$("#questionHolder").show("slow"); //animation
			
			
			if (intDebug > 0) {
				$("#ajax").html(html2entity(objReturn.html)); //animation
				$("#debug").html(strDebug);
				$("#debugSuccess").html(objReturn.debug);
			}
		},
		
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(textStatus + " " + errorThrown);
		}
		
	}); 
}
