	/**
	* Functions for the main ECE page, controlling story retrieval and image transition.
	* April 15, 2008
	*/
	
	/**
	* Calls the functions that transition both images and the HTML text,
	* then calls the function that wipes the old information.
	*
	* @param whichOne The binary value that describes whether the div fades in/out.
	*/
	function trans(whichOne){		
		var duration = 550;
		mainTransition(whichOne, duration);
		titleTransition(whichOne, duration);
		textTransition(whichOne, duration);
		linksTransition(whichOne, duration);		
	}
	
	/**
	* The transition function for the main image.
	*
	* @param whichOne The binary value that describes whether the div fades in/out.
	* @param dur The duration of the transition function.
	*/
	function mainTransition(whichOne, dur){
		
		if(whichOne == 1){
			tDiv = "image1";
			vDiv = "image0";
		}else{
			tDiv = "image0";
			vDiv = "image1";
		}
		if($(tDiv).fx){$(tDiv).fx.stop();}
		if($(vDiv).fx){$(vDiv).fx.stop();}
		$(tDiv).fx = $(tDiv).effect('opacity', {duration: dur}).start(1);
		$(vDiv).fx = $(vDiv).effect('opacity', {duration: dur}).start(0);	
	}
	
	/**
	* The transition function for the title image.
	*
	* @param whichOne The binary value that describes whether the div fades in/out.
	* @param dur The duration of the transition function.
	*/
	function titleTransition(whichOne, dur){
		if(whichOne == 1){
			tDiv = "text1";
			vDiv = "text0";
		}else{
			tDiv = "text0";
			vDiv = "text1";
		}
		if($(tDiv).fx){$(tDiv).fx.stop();}
		if($(vDiv).fx){$(vDiv).fx.stop();}
		$(tDiv).fx = $(tDiv).effect('opacity', {duration: dur}).start(1);
		$(vDiv).fx = $(vDiv).effect('opacity', {duration: dur}).start(0);	
	}
	
	/**
	* The transition function for the HTML story text.
	*
	* @param whichOne The binary value that describes which div should fade in/out.
	* @param dur The duration of the transition function.
	*/
	function textTransition(whichOne, dur){
		var tDiv = "story" + (1 - whichOne);
		var vDiv = "story" + whichOne;
		
		if($(vDiv).fx){$(vDiv).fx.stop();}
		if($(tDiv).fx){$(tDiv).fx.stop();}
		$(tDiv).fx = $(tDiv).effect('opacity', {
									duration: (dur / 2),
									onComplete: function(){
										removeText(whichOne);
										$(vDiv).fx = $(vDiv).effect('opacity', {duration: (dur / 2)}).start(1);
									}}).start(0);
	}
	
	/**
	* The transition function for the story links.
	*
	* @param whichOne The binary value that describes which div should fade in/out.
	* @param dur The duration of the transition function.
	*/
	function linksTransition(whichOne, dur){
		var tDiv = "links" + (1 - whichOne);
		var vDiv = "links" + whichOne;
		
		if($(vDiv).fx){$(vDiv).fx.stop();}
		if($(tDiv).fx){$(tDiv).fx.stop();}
		$(tDiv).fx = $(tDiv).effect('opacity', {
									duration: (dur / 2),
									onComplete: function(){
										removeLink(whichOne);
										$(vDiv).fx = $(vDiv).effect('opacity', {duration: (dur / 2)}).start(1);
									}}).start(0);
		
	}
	
	/**
	* The function that removes the main image.
	*
	* @param whichOne The binary value that describes which image to remove.
	*/
	function removePic(whichOne){
		var img = "image" + (1 - whichOne);
		$(img).removeChild($(img).getElementsByTagName("img")[0]);
	}
	
	/**
	* The function that removes the text image.
	*
	* @param whichOne The binary value that describes which image to remove.
	*/
	function removeTitle(whichOne){
		var txt = "text" + (1 - whichOne);
		$(txt).removeChild($(txt).getElementsByTagName("img")[0]);
	}
	
	/**
	* The function that removes the story text.
	*
	* @param whichOne The binary value that describes which text to erase.
	*/
	function removeText(whichOne){					
//		var str = "story" + (1 - whichOne);
//		$(str).innerHTML = "";
		var str0 = "story" + (1 - whichOne);
		$(str0).style.display = "none";
		var str1 = "story" + (whichOne);
		$(str1).style.display = "";

		var str = "story_span" + (1 - whichOne);
		$(str).innerHTML = "";		

		
	}
	
	
	function removeLink(whichOne){					
		var str0 = "links" + (1 - whichOne);
		$(str0).style.display = "none";
		var str1 = "links" + (whichOne);
		$(str1).style.display = "";

		var stra = "homepage_seephotoslinkspan" + (1 - whichOne);
		var strb = "homepage_readmorelinkspan" + (1 - whichOne);
		var strc = "homepage_watchvideoslinkspan" + (1 - whichOne);
	}
	
	/**
	* The function in charge of loading the new story information
	* and calling the transition functions for the two images
	* and the story text.
	*
	* @param direction The direction of the next requested story, either 1 or -1.
	*/
	function nextPic(direction){
		
		var oldindex = indexkey;
		indexkey = indexkey + direction;
		if(indexkey > (array_length - 1)) indexkey = 0;
		if(indexkey < 0) indexkey = array_length - 1;
		var params = "storyID=" + indexkey;
		var ajaxRequest;
				
		try{
			// Opera 8.0+, Firefox, Safari
			ajaxRequest = new XMLHttpRequest();
		} catch (e){
			// Internet Explorer Browsers
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					// Something went wrong
					alert("Your browser broke!");
					return false;
				}
			}
		}
		
		/**
		* The ajax request in charge of retrieving and setting
		* the correct story information from the PHP script.
		*/
		ajaxRequest.onreadystatechange = function(){
		
			if(ajaxRequest.readyState == 4){
				if (ajaxRequest.status == 200) {
					var ret = ajaxRequest.responseXML;
					var dv = divcounter % 2;
					
					var n = document.createElement("img");
					n.id = "homepage_main_imageLocal" + (dv);
					
					var m = document.createElement("img");
					m.id = "homepage_main_image_textLocal" + (dv);
					
					var imagenext = document.getElementById("homepage_main_imageLocal" + (dv));
					imagenext.src = ret.getElementsByTagName("main")[0].firstChild.data;
					imagenext.alt = ret.getElementsByTagName("alt")[0].firstChild.data;
					if(ret.getElementsByTagName("link")[0].firstChild){
						imagenext.longdesc = ret.getElementsByTagName("link")[0].firstChild.data;
					}
					
					var textimagenext = document.getElementById("homepage_main_image_textLocal" + (dv));
					textimagenext.src = ret.getElementsByTagName("titleimage")[0].firstChild.data;
					textimagenext.alt = ret.getElementsByTagName("second")[0].firstChild.data;
					if(ret.getElementsByTagName("link")[0].firstChild){
						textimagenext.longdesc = ret.getElementsByTagName("link")[0].firstChild.data;		
					}
					
					var boxcolor = document.getElementById("feature_txtbox");					
					boxcolor.style.background = "#" + ret.getElementsByTagName("boxcolor")[0].firstChild.data;
					

					var dvlinks = dv;
					if(ret.getElementsByTagName("album")[0].firstChild){
						var albumlink = document.getElementById("homepage_seephotoslink"+(dvlinks));						
						
						var albumlink_length = ret.getElementsByTagName("album")[0].firstChild.data.length;
						if(albumlink_length>1){
							var albumlink_span = document.getElementById("homepage_seephotoslinkspan" +(dvlinks));

							albumlink_span.style.display = "";
							albumlink_span.style.display = "inline";
							albumlink_span.style.visibility = "visible";
							albumlink_span.innerHTML = "<a href=\""+ ret.getElementsByTagName("album")[0].firstChild.data +"\">"+"<img src=\"images/triangle_red.gif\" alt=\"\" /> SEE PHOTOS </a>";							
							//albumlink.href  = ret.getElementsByTagName("album")[0].firstChild.data;
						} else {
							var albumlink_span = document.getElementById("homepage_seephotoslinkspan" +(dvlinks));
							
							albumlink_span.style.display = "none";
							albumlink_span.style.visibility = "hidden";
							albumlink_span.innerHTML = "";
						}
					} else {
							var albumlink_span = document.getElementById("homepage_seephotoslinkspan" +(dvlinks));
							albumlink_span.style.display = "none";
							albumlink_span.style.visibility = "hidden";
							albumlink_span.innerHTML = "";
					}
					
					if(ret.getElementsByTagName("link")[0].firstChild){
						var readmorelink = document.getElementById("homepage_readmorelink");						
						var readmorelink_length = ret.getElementsByTagName("link")[0].firstChild.data.length;
						
						if(readmorelink_length>1){
							var readmorelink_span = document.getElementById("homepage_readmorelinkspan" +(dvlinks));
							readmorelink_span.style.display = "";
							readmorelink_span.style.display = "inline";
							readmorelink_span.style.visibility = "visible";
							readmorelink_span.innerHTML = "<a href=\""+ ret.getElementsByTagName("link")[0].firstChild.data +"\">"+"<img src=\"images/triangle_red.gif\" alt=\"\" /> READ MORE</a>";													
						} else {
							var readmorelink_span = document.getElementById("homepage_readmorelinkspan" +(dvlinks));
							readmorelink_span.style.display = "none";
							readmorelink_span.style.visibility = "hidden";
							readmorelink_span.innerHTML = "";
						}
					} else {
							var readmorelink_span = document.getElementById("homepage_readmorelinkspan" +(dvlinks));
							readmorelink_span.style.display = "none";
							readmorelink_span.style.visibility = "hidden";
							readmorelink_span.innerHTML = "";
					}
	
					if(ret.getElementsByTagName("video")[0].firstChild){
						var watchvideolink = document.getElementById("homepage_watchvideoslink");						
						var watchvidelink_length = ret.getElementsByTagName("video")[0].firstChild.data.length;
						
						if(watchvidelink_length>1){						
							var watchvideolink_span = document.getElementById("homepage_watchvideoslinkspan" +(dvlinks));
							watchvideolink_span.style.display = "inline";
							watchvideolink_span.style.visibility = "visible";
							
							var newline_span = document.getElementById("homepage_newLine" +(dvlinks));
			
							if(((readmorelink_length>1)|(albumlink_length>1))&(watchvidelink_length>1)){
								watchvideolink_span.innerHTML = "<a href=\""+ ret.getElementsByTagName("video")[0].firstChild.data +"\">"+"<img src=\"images/triangle_red.gif\" alt=\"\" /> VIDEO : "+ ret.getElementsByTagName("videotext")[0].firstChild.data+"</a>";	
								newline_span.style.display = "inline";
							} else {								
								watchvideolink_span.innerHTML = "<a href=\""+ ret.getElementsByTagName("video")[0].firstChild.data +"\">"+"<img src=\"images/triangle_red.gif\" alt=\"\" /> VIDEO : "+ ret.getElementsByTagName("videotext")[0].firstChild.data+"</a>";
								newline_span.style.display = "none";
							}

						} else {
							var watchvideolink_span = document.getElementById("homepage_watchvideoslinkspan" +(dvlinks));
							watchvideolink_span.style.display = "none";
							watchvideolink_span.style.visibility = "hidden";
							watchvideolink_span.innerHTML = "";
						}
					} else {
						var watchvideolink_span = document.getElementById("homepage_watchvideoslinkspan" +(dvlinks));
						watchvideolink_span.style.display = "none";
						watchvideolink_span.style.visibility = "hidden";
						watchvideolink_span.innerHTML = "";
					}
					//var story = document.getElementById("story" + (dv));
					//story.style.display = "";
					//story.style.display = "inline";
					//story.style.visibility = "visible";
					//story.innerHTML  = ret.getElementsByTagName("text")[0].firstChild.data;
					
					var story_span = document.getElementById("story_span" + (dv));
					story_span.style.display = "";
					story_span.style.display = "inline";
					story_span.style.visibility = "visible";
					story_span.innerHTML  = ret.getElementsByTagName("text")[0].firstChild.data; 

					
					var count_track = document.getElementById("count_track");
					indexkeypp = indexkey + 1;
					var count_track_string = "Featured Story: " + indexkeypp + " of " + array_length;
					count_track.innerHTML  = count_track_string;	
					
					trans(dv);
					divcounter = divcounter + 1;
				}
			}
		}
		
		//The actual ajax call.
		ajaxRequest.open("POST", '/home_info/getStory.php', true);
		ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajaxRequest.setRequestHeader("Content-length", params.length);
		ajaxRequest.setRequestHeader("Connection", "close");
		ajaxRequest.send(params);
	}