var CURRENT_PAGE = 0;

/*************************************

* JSON ScriptBuilder Function 

**************************************/

	function JSONscriptRequest(fullUrl) {

	  this.fullUrl = fullUrl;

	  this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();

	  this.headLoc = document.getElementsByTagName("head").item(0);

	  this.scriptId = 'azScriptId' + JSONscriptRequest.scriptCounter++;

	}

	JSONscriptRequest.scriptCounter = 1;

	JSONscriptRequest.prototype.buildScriptTag = function () {

	  this.scriptObj = document.createElement("script");

	  this.scriptObj.setAttribute("type", "text/javascript");

	  this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);

	  this.scriptObj.setAttribute("id", this.scriptId);

	}

	JSONscriptRequest.prototype.removeScriptTag = function () {

	  this.headLoc.removeChild(this.scriptObj);

	}

	JSONscriptRequest.prototype.addScriptTag = function () {

	  this.headLoc.appendChild(this.scriptObj);

	}



/******************************************************************

*Callback function referenced by the JSON script, This teases the

* JSON object out into DOM Nodes, and appends them to 'items' DIV) but before it appends them

* it goes through an animation, so that you get each div one by one

*********************************************************************/

	var theDivs = new Array();

	var amzJSONCallback = function(tmpData)

	{

		//makes sure to reset the array

		theDivs = new Array();

		var count = 0;
		var imgDiv = document.getElementById('loadImg');
		imgDiv.setAttribute('src',' ');
		
		var gDiv = document.getElementById('items');

		gDiv.innerHTML="";

		

		//for all of the searched dvd results

		//(gets the  formatting from the .xsl file)

		for(var i=0; i<tmpData.ItemSet.Item.length-1; i++){

			count++;

			//the image
			var newImg = "url(http://images.amazon.com/images/P/" + tmpData.ItemSet.Item[i].asin + ".01._AA150_SCMZZZZZZZ_.jpg)";

			var tmpItem = tmpData.ItemSet.Item[i];

			//makes the title shorter if it is too long
			var myTitle;

				if(tmpItem.title.length > 20)

				{

					var myTitlet = tmpItem.title.substr(0,20) + " ...";

					myTitle= myTitlet;

		

				}

				else

				{

					myTitle = tmpItem.title;

				}

			var newDiv = document.createElement('div');

			newDiv.setAttribute('class','item');

			

		

			//makes the title of the div attribute for the tooltips

			newDiv.setAttribute("title",tmpItem.title + " :: " + "<b>Actor: </b>" + tmpItem.Actor + "   " + "<b>Rating: </b>" + tmpItem.AudienceRating);

			

			newDiv.style.backgroundImage = newImg;

				

			//makes the id the actor and the audience rating, splitting them up with '666666' so that we can use it later

			newDiv.setAttribute('id',tmpItem.Actor + "666666" + tmpItem.AudienceRating);

			

			//the title

			var newSpan = document.createElement('span');

			newSpan.setAttribute('id','spanText');

			newSpan.appendChild(document.createTextNode(myTitle));

			

			//the link

			var a = document.createElement('a');

			a.setAttribute('href',tmpItem.url);

			a.appendChild(newSpan);

			

			newDiv.appendChild(a);

			

			theDivs.push(newDiv);

			

			

		}

		if(count!=0)

		{

			

			

			

		

			//this is the chained periodical animation

			var myChain = new Chain();

				theDivs.each(function(aDiv) { myChain.chain( function(){ addDiv(aDiv); } ); });

				var runChain = function() { 

					myChain.callChain();

					if (myChain.chains.length == 0) { runChain = $clear(timer); } 

				}

				var timer = runChain.periodical(500);

				

			



		}

		else

		{

			
			var imgDiv = document.getElementById('loadImg');
			imgDiv.setAttribute('src',' ');
		
		
			//deletes the old text
			var newD = document.getElementById('log');
			if(newD.childNodes.length > 0)
			{
				newD.removeChild(newD.firstChild);

			}
			
			//makes text stating that we couldn't find the results
			var newH = document.createElement('h2');
			newD.appendChild(newH.appendChild(document.createTextNode("sorry, no results found for '" + document.getElementById('srch').value + "'")));

			document.getElementById('col1_2').appendChild(newD);

		

		

		

			

		

	//makes the 'flash' on the div, alerting the user that we couldn't find results

	var fx = new Fx.Styles(document.getElementById('log'), {

	duration: 2000,

	wait: false,

	transition: Fx.Transitions.Quad.easeOut

});

	fx.start({

			'background-color': ['#66B8CC', '#F8F2E3'],
			

			'opacity': [1, 0]

		})

	











		

		}

	}

	

	

	// Search function - creates, builds, and adds remote JSON script

	// to the page, which in turn calls amzJSONCallback

	var amazonSearch = function(x){

		if(x){

			

		//switches the page over to "home"	
		if(CURRENT_PAGE ==1)
		{
		var url = "home.htm";

		

		new Ajax(url, {

			method: 'get',

			update: $('col1_2')

		}).request();

		$('li_1_about').setAttribute('class', '');

		$('navigation_1_about').setAttribute('class', '');

		$('li_1_home').setAttribute('class', 'nav_current_li_1');

		$('navigation_1_home').setAttribute('class', 'nav_current_1');
		CURRENT_PAGE = 0;
		}

		
		
		//the loading image
		var imgDiv = document.getElementById('loadImg');
		imgDiv.setAttribute('src','images/ajax-loader.gif');
			

		

		//checks for the search word
		var myForm = document.getElementById("options_form");

		var searchWord;

		for(var i = 0;i<myForm.leftcol.length;i++)

		{

			if(myForm.leftcol[i].checked == true){

			searchWord = myForm.leftcol[i].value;

			

			}

		}

			//the xml request to amazon

			var request = 'http://xml-us.amznxslt.com/onca/xml?Service=AWSECommerceService&SubscriptionId=1RR8389AFCSBNQ06NA82&AssociateTag=jessdav-20&Operation=ItemSearch&Style=http://people.rit.edu/jld4460/amazonstuff/ajsonCategorySearch.xsl&ContentType=text/javascript&ItemPage=2&SearchIndex=DVD&ResponseGroup=ItemAttributes,Images,Small,Offers&MerchantId=ATVPDKIKX0DER&Availability=Available&Condition=All&'+ searchWord + '=' + x + '&CallBack=amzJSONCallback'

			

			//Json stuff

			aObj = new JSONscriptRequest(request);

			aObj.buildScriptTag();

			aObj.addScriptTag();

				



		}

	}

	

	

	

	

	

	///////////////////////////////////
