﻿// function called on links
function pageLoad(page) {
	// jQuery ajax call to load page
    $("#scrollContent").load(page);
}

function appendLoad(page, table) {
	// jQuery ajax call to load page
    $(table).load(page);
}

function xmlLoad() {
	//TB_WORKID from thickbox js file
	var InventoryID = TB_WORKID;
	
	// jQuery ajax call
    $.ajax({
        type: "GET",
        url: "Inventory_desc.xml",
        dataType: "xml",
        success: function(xml) {
            $(xml).find('Cell').each(function() {
                var findID = $(this).text();
                if (findID == InventoryID) {
					$(this).parent().children().each(function(i) {
                        var WorkInfo = $(this).text();
						// insert title
						if($(this).attr('ID') == '1') { 
							$('#WorkImageSrc').attr('alt',WorkInfo);
							$('#workTitle').html(WorkInfo);
						}
						// insert year
						if($(this).attr('ID') == '5') {
							$('#workYear').html(WorkInfo);
						}
						// insert dimension
						if($(this).attr('ID') == '3') {
							$('#workDimension').html(WorkInfo);
						}
						// insert medium
						if($(this).attr('ID') == '4') {
							$('#workMedium').html(WorkInfo);
						}
						// check for private collection status
						if($(this).attr('ID') == '6') {
							//alert(WorkInfo);
							if(WorkInfo == 'yes') {
								$('#workPC').text('Private Collection');
							} else if(WorkInfo == 'no') {
								$('#workPC').css('display','none');
								$('#workContact').css('display','inline');
							}
						}
						// call image from directory
						$('#WorkImageSrc').attr('src','Images/TheWork/' + InventoryID + '.jpg');
						
						// check for abstract
						//if($(this).attr('ID') == '11') {
						//}
                    });
                }
            });
        }
    });
}


function xmlSearch(keywords, start) {
	var keywordArray = new Array();
	
	if(keywords == "Search Hadi's work") {
		keywords = "NoSearchWord";
	}
	
	// Specific Search page
	var SearchPage = keywords;
	
	if(keywords == "TNW") {
		SearchPage = "TheNaturalWorld";	
	} else if (keywords == "2009,2008,2007") {
		SearchPage = "NewWork";	
	}
	
	if(keywords == "TNW" || keywords == "2010,2009,2008,2007" || keywords == "JTS" || keywords == "Abstract" || keywords == "Figures" || keywords == "Villages" || keywords == "NewWork") {
		SearchPage = SearchPage;	
	} else {
		SearchPage = "TheWork";
	}
	
	keywordArray = keywords.split(new RegExp( "[,+ ]{1}", "g" ));

	// jQuery ajax call
	$.ajax({
	    type: "GET",
	    url: "Inventory_desc.xml",
	    dataType: "xml",
		error: function() {alert('err');},
	    success: function(xml) {
	        // variable for thumbnails on html
	        var k = 0;
			var l = 0;
			
			// reset images to blank
			if(start != 0) {
				for(i=0;i<12;i++) {
					$('#searchLink' + i).attr('href', 'SeeWorkSelectAnother.html?height=700&width=960');
					$('#searchImage' + i).attr('src', 'Images/TheWork/blank_thumbnail.gif');
					$('#searchImage' + i).attr('alt', ' ');
				}
			}
			
			// variable for results
			var FoundResults = false;
			
	        // loop for each keyword
	        for (j = 0; j < keywordArray.length; j++) {
	            // set variables to a single result from each XML <Row>
				var lastID = '';
				var uniqueID = '';
	            // find tag <Cell>
				$(xml).find('Cell').each(function() {
				    // variable to find keyword
	                var getText = $(this).text().toLowerCase();
					// var findKeyword = $(this).text().indexOf(keywordArray[j]);
	               	var findKeyword = getText.search(keywordArray[j].toLowerCase());
	               	//var findKeyword = $(this).text().toLowerCase().indexOf(keywordArray[j].toLowerCase());
					
	                // if true
	                if (findKeyword >= 0) {
						// found results
						FoundResults = true;
	                    // declare variables
	                    var insertID = '';
	                    var insertAlt = '';
	                    var insertCategory = '';
						
	                    // look at siblings
						$(this).parent().children().each(function(i) {
							// variable for the text in tag
	                        var WorkInfo = $(this).text();
	                        // var for id into link and image
							if($(this).attr('ID') == '0') {
								insertID = WorkInfo; 
								if(insertID == lastID) {
									uniqueID = false;
									return false;
								} else {
									lastID = WorkInfo; 
									uniqueID = true;
									return true;
								}
							}
	                        // var for title into Alt tag info
							if($(this).attr('ID') == '1') { insertAlt = WorkInfo; }
	                        // var for category from directory
							if($(this).attr('ID') == '11') { insertCategory = WorkInfo; }
							
	                    });
	                    // insert into html
						if(uniqueID && k>=start && k<(k+12)) {
							//alert(k);
							if(insertID != "Inventory ID") {
								$('#searchLink' + l).attr('href', 'SeeWork.html?workID=' + insertID + '&height=700&width=960');
								$('#searchImage' + l).attr('src', 'Images/TheWork/Thumbnails/' + insertID + '.jpg');
								$('#searchImage' + l).attr('alt', insertAlt);
							}
							
							// advance place in html
							k = k+1;
							l = l+1;
							//alert(keywordArray[j] + " id=" + insertID + " cat=" + insertCategory + " alt=" + insertAlt);
							
						} else if(uniqueID) {
							// advance place in html
							k = k+1;
						}
	                }
	                // go on to next Cell
	            });
				// go to next keyword
	        }
			
			// show or hide a no results message on page
			if(FoundResults) {
				$("#SearchNoResults").css("display","none");
				
				// Produce links on right side.
				for(m=0;m<=k;m++) {
					if(m==0) {
						$("#contLink0").attr("href", SearchPage + "Search.php?searchWord=" + keywords + "&start=" + 0);
						$("#contLink0").css("display","block");
					}
					if(m==12) {
						$("#contLink12").attr("href", SearchPage + "Search.php?searchWord=" + keywords + "&start=" + 12);
						$("#contLink12").css("display","block");
					}
					if(m==24) {
						$("#contLink24").attr("href", SearchPage + "Search.php?searchWord=" + keywords + "&start=" + 24);
						$("#contLink24").css("display","block")
					}
					if(m==36) {
						$("#contLink36").attr("href", SearchPage + "Search.php?searchWord=" + keywords + "&start=" + 36);
						$("#contLink36").css("display","block")
					}
					if(m==48) {
						$("#contLink48").attr("href", SearchPage + "Search.php?searchWord=" + keywords + "&start=" + 48);
						$("#contLink48").css("display","block")
					}
					if(m==60) {
						$("#contLink60").attr("href", SearchPage + "Search.php?searchWord=" + keywords + "&start=" + 60);
						$("#contLink60").css("display","block")
					}
					if(m==72) {
						$("#contLink72").attr("href", SearchPage + "Search.php?searchWord=" + keywords + "&start=" + 72);
						$("#contLink72").css("display","block")
					}
					if(m==84) {
						$("#contLink84").attr("href", SearchPage + "Search.php?searchWord=" + keywords + "&start=" + 84);
						$("#contLink84").css("display","block")
					}
					if(m==96) {
						$("#contLink96").attr("href", SearchPage + "Search.php?searchWord=" + keywords + "&start=" + 96);
						$("#contLink96").css("display","block")
					}
					if(m==108) {
						$("#contLink108").attr("href", SearchPage + "Search.php?searchWord=" + keywords + "&start=" + 108);
						$("#contLink108").css("display","block")
					}
				}
			} else {
				// set keyword output
				var keywordOutput = "";
				for(i=0;i<keywordArray.length;i++) {
					keywordOutput = keywordOutput + keywordArray[i] + " ";
					
					if(keywordOutput == "NoSearchWord ") {
						$("#SearchNoResults").html("Please enter a word to search Hadi's work.")
					}
				}
				// display message and keywords on page
				$("#SearchNoResults").css("display","block");
				$("#SeeAllTable").css("display","none");
				$("#keyword").html(keywordOutput);
			}
	    }
	});
}

function xmlRetrieveAll(start) {
	
	// jQuery ajax call
	$.ajax({
	    type: "GET",
	    url: "Inventory_desc.xml",
	    dataType: "xml",
	    success: function(xml) {
	        var InventoryCount = $(xml).find('Row').length;	
			
			var AdjustCount = InventoryCount + 3;	
			
			if(start > AdjustCount) {
				start = AdjustCount;	
			}
			
			// variable for thumbnails on html
	        var k = 0;
			
			// find tag <Row>
			$(xml).find('Row').each(function() {
				// declare variables
				var insertID = '';
				var insertAlt = '';
				var insertCategory = '';
				// convert ID of <Row> and Search querystring into an integer
				var xmlRowID = parseFloat($(this).attr('ID'));
				var startID = parseFloat(start);
				var endID = startID - 10;
				
				//alert("row=" + xmlRowID + " start= " + startID);
				
				if(xmlRowID <= startID) {
					// if search is out of range
					if(xmlRowID >= endID) {
						// look at siblings
						// alert($(this).attr('ID'));
						$(this).children().siblings().each(function(i) {
							
							// variable for the text in tag
							var WorkInfo = $(this).text();
							
							// var for id into link and image
							if($(this).attr('ID') == '0') { insertID = WorkInfo; }
							// var for title into Alt tag info
							if($(this).attr('ID') == '1') { insertAlt = WorkInfo; }
							// var for category from directory
							if($(this).attr('ID') == '11') { insertCategory = WorkInfo; }
						});
						
						// insert into html
						$('#searchLink' + k).attr('href', 'SeeWork.html?workID=' + insertID + '&height=700&width=960');
						$('#searchImage' + k).attr('src', 'Images/TheWork/Thumbnails/' + insertID + '.jpg');
						$('#searchImage' + k).attr('alt', insertAlt);
						// advance place in html
						k = k+1;
						//alert(keywordArray[j] + " id=" + insertID + " cat=" + insertCategory + " alt=" + insertAlt);
						
						// go on to next row	
						return true;
					} else {
						// end loop
						return false;
					}
				}
			});
			
			// Produce links on right side.
			for(m=0;m<=AdjustCount-1;m++) {
				if(m==0) {
					$("#contLink0").attr("href", "SeeAllSearch.php?searchWord=" + AdjustCount);
					$("#contLink0").css("display","block");
				}
				if(m==12) {
					$("#contLink12").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 12));
					$("#contLink12").css("display","block");
				}
				if(m==24) {
					$("#contLink24").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 24));
					$("#contLink24").css("display","block")
				}
				if(m==36) {
					$("#contLink36").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 36));
					$("#contLink36").css("display","block")
				}
				if(m==48) {
					$("#contLink48").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 48));
					$("#contLink48").css("display","block")
				}
				if(m==60) {
					$("#contLink60").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 60));
					$("#contLink60").css("display","block")
				}
				if(m==72) {
					$("#contLink72").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 72));
					$("#contLink72").css("display","block")
				}
				if(m==84) {
					$("#contLink84").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 84));
					$("#contLink84").css("display","block")
				}
				if(m==96) {
					$("#contLink96").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 96));
					$("#contLink96").css("display","block")
				}
				if(m==108) {
					$("#contLink108").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 108));
					$("#contLink108").css("display","block")
				}
				if(m==120) {
					$("#contLink120").attr("href", "SeeAllSearch.php?searchWord=" + AdjustCount - 120);
					$("#contLink120").css("display","block");
				}
				if(m==132) {
					$("#contLink132").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 132));
					$("#contLink132").css("display","block");
				}
				if(m==144) {
					$("#contLink144").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 144));
					$("#contLink144").css("display","block")
				}
				if(m==156) {
					$("#contLink156").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 156));
					$("#contLink156").css("display","block")
				}
				if(m==168) {
					$("#contLink168").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 168));
					$("#contLink168").css("display","block")
				}
				if(m==180) {
					$("#contLink180").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 180));
					$("#contLink180").css("display","block")
				}
				if(m==192) {
					$("#contLink192").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 192));
					$("#contLink192").css("display","block")
				}
				if(m==204) {
					$("#contLink204").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 204));
					$("#contLink204").css("display","block")
				}
				if(m==216) {
					$("#contLink216").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 216));
					$("#contLink216").css("display","block")
				}
				if(m==228) {
					$("#contLink228").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 228));
					$("#contLink228").css("display","block")
				}
				if(m==240) {
					$("#contLink240").attr("href", "SeeAllSearch.php?searchWord=" + AdjustCount - 240);
					$("#contLink240").css("display","block");
				}
				if(m==252) {
					$("#contLink252").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 252));
					$("#contLink252").css("display","block");
				}
				if(m==264) {
					$("#contLink264").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 264));
					$("#contLink264").css("display","block")
				}
				if(m==276) {
					$("#contLink276").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 276));
					$("#contLink276").css("display","block")
				}
				if(m==288) {
					$("#contLink288").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 288));
					$("#contLink288").css("display","block")
				}
				if(m==300) {
					$("#contLink300").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 300));
					$("#contLink300").css("display","block")
				}
				if(m==312) {
					$("#contLink312").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 312));
					$("#contLink312").css("display","block")
				}
				if(m==324) {
					$("#contLink324").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 324));
					$("#contLink324").css("display","block")
				}
				if(m==336) {
					$("#contLink336").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 336));
					$("#contLink336").css("display","block")
				}
				if(m==348) {
					$("#contLink348").attr("href", "SeeAllSearch.php?searchWord=" + (AdjustCount - 348));
					$("#contLink348").css("display","block")
				}
			}
		 }
	});
}
