// JavaScript page methods for pctsWeb
// PAGE = solutions.php

// declare a page namespace
var news = {};

// --- namespace variables -------
news.readyState = '';

// --- page functions -------
news.getMoreCurrentYear = function() {
	// get the secondary press releases for the current year
	
	// first get a random number so as to prevent IE from using a cached accessor return
	var theRan = pctsFramework.randomNumber();
		
	// Create an accessor
	var theAccessor = "../accessors/getMoreNews.php?year=" + pctsFramework.currentYear;
	var theAccessor = theAccessor + "&anticache=" + theRan;
		
	// set the target
	var theTarget = document.getElementById('secondaryList');
	
	pctsFramework.getContent(theAccessor, theTarget, true);
	
	// update the control assembly
	document.getElementById('currentYear').className = 'newsLinkInactive';
	document.getElementById('pastYear').className = 'newsLink';
	
}

news.getMorePreviousYear = function() {
	// get the secondary press releases for the current year
	
	// first get a random number so as to prevent IE from using a cached accessor return
	var theRan = pctsFramework.randomNumber();
		
	// Create an accessor
	var theAccessor = "../accessors/getMoreNews.php?year=" + pctsFramework.lastYear;
	var theAccessor = theAccessor + "&anticache=" + theRan;
		
	// set the target
	var theTarget = document.getElementById('secondaryList');
	
	pctsFramework.getContent(theAccessor, theTarget, true);
	
	// update the control assembly
	document.getElementById('currentYear').className = 'newsLink';
	document.getElementById('pastYear').className = 'newsLinkInactive';
	
}

news.switchToPreviousYear = function() {
	// get the secondary press releases for the current year
	
	// first get a random number so as to prevent IE from using a cached accessor return
	var theRan = pctsFramework.randomNumber();
	
	// Create an accessor
	var theAccessor = "../accessors/getMoreNews.php?year=" + pctsFramework.lastYear;
	var theAccessor = theAccessor + "&anticache=" + theRan;
	
	// set the target
	var theTarget = document.getElementById('secondaryList');
	
	pctsFramework.getContent(theAccessor, theTarget, true);
	
	// update the control assembly
	document.getElementById('pastYear').className = 'newsLinkInactive';
	
}

news.updateYearSelector = function() {
	// update the year selector when a user navigates between past and current years
	
}

news.moreMouseIn = function(theId) {
	// handle mouse in to a table row
	document.getElementById(theId).className = 'newsRowOver';
}	

news.moreMouseOut = function(theId) {
	// handle mouse out from a table row
	
	// first determine what sort of row this is, odd or even.
	var theParts = theId.split("_");
	
	var theType = theParts[0];
	
	if(theType == 'newsRowOdd') {
		document.getElementById(theId).className = 'newsRowOdd';
		
	} else {
		document.getElementById(theId).className = 'newsRowEven';
		
	}
	
}	

news.moreControlMouseIn = function(theClass, theId) {
	// handle mouse in to a table row
	
	if(theId == 'pastYear' && theClass == 'newsLink') {
		document.getElementById(theId).className = 'newsLinkMoused';
		
	}
	
	if(theId == 'currentYear' && theClass == 'newsLink') {
		document.getElementById(theId).className = 'newsLinkMoused';
		
	}
	
}	

news.moreControlMouseOut = function(theClass, theId) {
	// handle mouse out from a table row
	// alert(theClass);
	
	if(theId == 'pastYear' && theClass == 'newsLinkMoused') {
		document.getElementById(theId).className = 'newsLink';
		
	}
	
	if(theId == 'currentYear' && theClass == 'newsLinkMoused') {
		document.getElementById(theId).className = 'newsLink';
		
	}
	
}

news.fadeThumbs = function() {
	// fade out the thumbnail images for the thumbs so that they can get a nice mouseover effect when a user hovers the mouse over a primary block
	
	// we iterate through the thumb collection to do this
	for(i = 1; i <= 6; i++) {
		
		if (pctsFramework.browser != 'IE') {
			// its not IE so we address the entire primary block
			var theThumb = 'pressRelease' + i;
			// set the opacity of the thumb
			$('#' + theThumb).fadeTo("normal", 0.79);

		} else {
			if (pctsFramework.browserVersion >= 7) {
				// its IE7 + so we address the image by itself, and set opacity directly

				var theThumb = 'pressThumb' + i;
				var theThumbParent = 'pressThumbParent' + i;
				// set the opacity of the thumb to 100% because natve IE rendering of PNG images SUCKS, 
				// But alpha loader ALSO sucks at page load.  So, we rewrite the image now using an imageloader, after load
				news.reWriteClass(theThumb, theParent);
				$('#' + theThumbParent).fadeTo("normal", 0.79);
				
			} else {
				// it's IE 6 or older so we address the thumb image itself
				var theThumb = 'pressThumbParent' + i;
				// set the opacity of the thumb
				// page.setOpacity(88, theThumb);
				$('#' + theThumb).fadeTo("normal", 0.79);
			}
		}
		
	}
	
}

news.reWriteClass = function(theThumb, theParent) {
	// rewrite the class
	var theClass = document.getElementById(theThumb).className;
	theClass = theClass + 'Alpha';
	document.getElementById(theThumb).className = theClass;
	
}

news.primaryMouseIn = function(theId) {
	// handle mouse in to a primary block
	
	// first, determine the iterator
	var theThumbNum = theId.split('pressRelease');
	i = theThumbNum[1];
	
	// now handle the fade
	if (pctsFramework.browser != 'IE') {
		  // its not IE so we address the entire primary block
		  var theThumb = 'pressRelease' + i;
		  // set the opacity of the thumb
		  page.setOpacity(100, theThumb);
		  //$('#' + theThumb).fadeTo("normal", 1.0);

	  } else {
		  // it's IE so we address the thumb image itself
		  var theThumb = 'pressThumbParent' + i;
		  page.setOpacity(100, theThumb);
		  
		  //theThumb = 'IEPressBlockBody' + i;
		  //page.setOpacity(100, theThumb);	  
	
	  }
}

news.primaryMouseOut = function(theId) {
	// handle mouse out from a primary block
	
	// first, determine the iterator
	var theThumbNum = theId.split('pressRelease');
	i = theThumbNum[1];
	
	// now handle the fade
	if (pctsFramework.browser != 'IE') {
		  // its not IE so we address the entire primary block
		  var theThumb = 'pressRelease' + i;
		  // set the opacity of the thumb
		  // $('#' + theThumb).fadeTo("normal", .79);
		  page.setOpacity(79, theThumb);

	  } else {
		  // it's IE so we address the thumb image itself
		  var theThumb = 'pressThumbParent' + i;
		  page.setOpacity(79, theThumb);
		  
		  //theThumb = 'IEPressBlockBody' + i;
		  //page.setOpacity(88, theThumb);
		  
	  }
}

news.handlePressItemClick = function(theId) {
	// present a press release
	
	// first get a random number so as to prevent IE from using a cached accessor return
	var theRan = pctsFramework.randomNumber();
		
	// Create an accessor
	var theAccessor = "pressRelease.php?releaseID=" + theId;
	var theAccessor = theAccessor + "&anticache=" + theRan;
	
	// go to the press release
	page.goToURL(theAccessor);
	
	// record the access
	var theAccessor = "../accessors/insertResourceRetrieval.php?source=" + theId + "&ip=" + pctsFramework.currentIP + "&user=" + pctsFramework.registeredUser + "&type=press";
	
	// add a random number so as to prevent IE from using a chached accessor return
	var theRan = pctsFramework.randomNumber();
	theAccessor = theAccessor + "&anticache=" + theRan;
	
	pctsFramework.getData(theAccessor);
}

news.checkForOldPress = function() {
	// check to see if there are any news items from last year
	
	// first get a random number so as to prevent IE from using a cached accessor return
	var theRan = pctsFramework.randomNumber();
	var pastYear = 	(pctsFramework.currentYear * 1) - 1;
	
	// set previous year in framework (hack)
	pctsFramework.lastYear = pastYear;
	
	// Create an accessor
	var theAccessor = "../accessors/checkPress.php?year=" + pastYear + "&mode=past";
	var theAccessor = theAccessor + "&anticache=" + theRan;
	
	// call the accessor
	page.getData(theAccessor);
	
}

news.checkForCurrentPress = function() {
	// check to see if there are any news items from last year
	
	// first get a random number so as to prevent IE from using a cached accessor return
	var theRan = pctsFramework.randomNumber();
		
	// Create an accessor
	var theAccessor = "../accessors/checkPress.php?year=" + pctsFramework.currentYear + "&mode=current";
	var theAccessor = theAccessor + "&anticache=" + theRan;
	
	// call the accessor
	page.getData(theAccessor);
	
}

news.handleReturnedEventNumber = function(theNumber, theMode) {
	// check to see if there are any records for a given year, and if not, turn off that year in the paging control
	
	var theNumber = theNumber * 1;
	// alert("the number is: " + theNumber + " and the mode is: " + theMode);
	if(theNumber <= 5) {
		// no records were found
		var theElement = theMode + 'Year';
		document.getElementById(theElement).style.display = 'none';
		document.getElementById('pageControlSeparator').style.display = 'none';
		
		// if the mode is current, then get the previous year's news
		if (theMode == 'current') {
			// we wound up here because the current year returned 0 items
			
			news.switchToPreviousYear();
		}
		
	}
}

news.setReadyStateInterval = function() {
	// set up a ready state interval that will keep firing until the document has completely loaded
	// check every 100MS
	news.readyState = setInterval("news.readyMonitor()", 100);
}

news.readyMonitor = function() {
	// check to see if the page has fully loaded / rendered in IE
	// in mozilla, onload doesn't fire until the DOM is loaded.
	var theState = document.readyState;
	
	if (pctsFramework.browserPath == 'ie') {
		if (theState == "complete") {
			// clear the readystate interval
			clearInterval(news.readyState);
	
			// do some other IE related thing here
			news.getMoreCurrentYear();
			news.fadeThumbs();
			news.checkForOldPress();
			news.checkForCurrentPress();
		}
	} else {
		clearInterval(news.readyState);
		news.getMoreCurrentYear();
		news.fadeThumbs();
		news.checkForOldPress();
		news.checkForCurrentPress();
	}
}

news.twitterLinkClick = function(theToken) {
	// navigate to a designated twitter page
	var uri = 'http://www.twitter.com/' + theToken;
	window.location.href = uri;
	
}

// --- attached events -------

// we attach inside an if statement because IE handles event listeners differently from other browsers.

if (window.attachEvent) {
	// attached events for IE
	window.attachEvent("onload", news.setReadyStateInterval);
	// window.attachEvent("onload", news.getMoreCurrentYear);
	// window.attachEvent("onload", news.fadeThumbs);
	// window.attachEvent("onload", news.checkForOldPress);
	// window.attachEvent("onload", news.checkForCurrentPress);

} else {
	// attached events for other browsers
	window.addEventListener('load', news.setReadyStateInterval, false);
	// window.addEventListener('load', news.getMoreCurrentYear, false);
	// window.addEventListener('load', news.fadeThumbs, false);
	// window.addEventListener('load', news.checkForOldPress, false);
	// window.addEventListener('load', news.checkForCurrentPress, false);

}