// JavaScript Document



function dataLoader() {
	
	//xmlObj = CreateXmlHttp();
	apiPath = "madison_projects.aspx";
	projects = [];
	slideshows = [];
	searchProjectsArray=[];
	
	
	return {
		
		getXML: function() {
			

			$.get(apiPath+'?clearcache=true',this.buildLoader);
			/*if (xmlObj) {
				
				//xmlObj.onreadystatechange = checkDataStatus(xmlObj);
				
				xmlObj.onreadystatechange = function () {
					if (xmlObj.readyState == 4) {
						if (xmlObj.status == 200) {
							alert(constructor);
							constructor.dataHolder.buildLoader(xmlObj);		
						}
					}
				}

			} else {
				xmlObj = CreateXmlHttp();
			}
			
			xmlObj.open("GET",apiPath,true);
			xmlObj.send(null); */

		},
	
		buildLoader: function(xmlObj) {
	
				
				//loop through the XML, building the project thumbnail links
						//var projectXML = xmlObj.responseXML;
						
						//get the projects node
						var projectNodes = xmlObj.getElementsByTagName("projects")[0].childNodes;
						
						//loop through
						for (i=0;i<projectNodes.length;i++) {
							
							//check for the nodeType
							var projectNode = projectNodes[i];
							
							if (projectNode.nodeType==1) {
							
								//increment the number of images for the thumbnail slider
								//constructor.slider.imageNum++;
								
								var projectArray = [];
								//get props of this proj node
								projectArray["id"] = projectNode.getElementsByTagName("id")[0].firstChild.nodeValue;
								projectArray["name"] = projectNode.getElementsByTagName("name")[0].firstChild.nodeValue;
								projectArray["title"] = projectNode.getElementsByTagName("title")[0].firstChild.nodeValue;
								
								//create object to push to project array
								var projObj = {id:projectArray["id"], name:projectArray["name"],title:projectArray["title"]};
								searchProjectsArray.push(projObj);
								
							}
	
						}		
						
												//end for loop
							$("#projectSearchBox").autocomplete(searchProjectsArray,{minChars:2, scrollHeight:500, multiple:false, matchContains: true, autoFill: false, selectFirst: false,
							formatItem: function(row, i, max) {return row["name"]+": "+row["title"]}});
							$('input#projectSearchBox').result(function(event, data, formatted) {dataHolder.loadProject(data.id);});
							$("input#projectSearchBox").attr("disabled",this.value == 'no');
			},
			
			//called when you click a thumbnail. This gets the Flash to completely UNLOAD the current slideshow.  It will then call the 
			//load project to load it in new
			loadProject:function(whichProj) {
				
				$("#ps_btn").click(function() {
					window.location.href='ourwork.html?p='+whichProj;														 
				 });
				
			}

}};


function CreateXmlHttp()

{
    //Creating object of XMLHTTP in IE
    try
    {
        var XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            var XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
           var  XmlHttp = null;
        }
    }
    //Creating object of XMLHTTP in Mozilla and Safari
    if(!XmlHttp && typeof XMLHttpRequest != "undefined")
    {
       var  XmlHttp = new XMLHttpRequest();
    }
		
		return XmlHttp;
		
}