// JavaScript page methods for pctsWeb
// PAGE = main_index

// --- namespace variables -------
page.singleRotatorThumb = new Array();
page.allRotatorThumbs = new Array();
page.rotatorPaused = false;

// --- page functions -------

page.navigateTo = function(theDestination) {
	// navigate page to a give URL
	window.location.href = theDestination + "?frameworkState=" + pctsFramework.orphanMode;
	
}

page.handleRotatorButtons = function(theObject, theAction) {
	// handle mouse actions to the main page rotatorAssembly buttons
	
	// if the mouse action is 'in' we change the className to the moused state
	if (theAction == 'in') {
		var theNewClass = theObject.className + 'Moused';
		theObject.className = theNewClass;

	} else {
		// this is a mouse out
		var theClass = theObject.className;
		var theClassParts = theClass.split('_');
		theClass = theClassParts[0] + '_btn';
		theObject.className = theClass;
	}
		
}

page.initialOperations = function() {
	// functions that will be called when the page ready state is true
	
	if (pcts.rotatorThumbsOn) {
		// get thumb image info if the rotator is active
		page.getThumbImageInfo();
	}
	
	// set the rotator thumbnail assemblies to display:none
	if(pcts.rotatorThumbsOn) {
		document.getElementById('rotatorThumbsLeft').style.top = '-120px';
		document.getElementById('rotatorThumbsRight').style.top = '-120px';

		// set opacity on the rotator balloon
		pcts.setOpacity(0, 'rBalloon');
	}
	
}

page.getThumbImageInfo = function() {

	var theRan = pctsFramework.randomNumber();

	// get and return HTML for a rotatorThumbImage.
	var theThumbURL = "../accessors/getAllRotatorThumbnails.php?browser=" + pctsFramework.browserPath + '&contentRegion=' + pctsFramework.contentRegion + '&anticache=' + theRan;;
	
	// alert(theThumbURL);

	pctsFramework.getData(theThumbURL);

	// stop propagation
	pcts.setNoPropogate(window.event);
	
	// alert('exited getThumb');
}

page.constructRotatorThumbs = function() {
	// construct the rotator thumbnail bar and display it
	alert('constructing rotator thumbnails by golly');
	alert(pcts.allRotatorThumbs);
}

page.handleThumbMouseAction = function(theId, theAction) {
	// fade a thumb image in to 100% when moused
	if (pcts.thumbsVisible == true) {
		// first, get the parent id of the thumbnail - that is the container we want to change.
		var theParentItem = document.getElementById(theId).offsetParent.id;
		
		// now conduct actions based on theAction type
		if (theAction == 'in') {
			// we have moused in - set the opacity and show the balloon for this item
		 
			// first, set opacity
			pcts.setOpacity(100, theParentItem);

			// now get the content for the ballon and show it
			// we do this by accessing the DB by thumb name, so we need this name.  We get it from the id.
			var thumbNameParts = theId.split('_');
			var thumbName = thumbNameParts[0];
			
			// determine the top and left of the moused item
			// we will use this to position the balloon
			var theLeft = pctsFramework.GetElementLeft(document.getElementById(theId));
			var theTop = pctsFramework.GetElementTop(document.getElementById(theId));
			
			// call an accessor function to get the content for a balloon, and then show that balloon
			// first, construct the accessor strong
			var theString = "../accessors/getThumbnailBalloonInfo.php?id=" + theId + "&thumbName=" + thumbName + "&left=" + theLeft + "&top=" + theTop;
			
			// show the balloon FIRST with a loading icon
			page.showThumbInfo(theId, theLeft, theTop);

			// now get the data the function
			pctsFramework.getData(theString);
	
		} else {
			pcts.setOpacity(52, theParentItem);
			
			// hide the ballon if shown
			document.getElementById('rBalloon').style.visibility = 'hidden';
			document.getElementById('balloonBody').style.visibility = 'hidden';
			document.getElementById('balloonHead').style.visibility = 'hidden';
			
			// clear any timrouts for the fade function
			window.clearTimeout(pcts.fadeTimer);
			
			pcts.setOpacity(0, 'rBalloon');
			document.getElementById('rBalloon').style.top = '-120px';
			
		}
	}
}

page.showThumbInfo = function(theId, theLeft, theTop) {
	// show a balloon as the result of a content call to an accessor
	
	// if it is  IE, we initially set opacity to 100%
	if (pctsFramework.browserPath == 'ie') {
		// pcts.setOpacity(100, 'rBalloon');

	}

	// set the content to the spinner before we do anything else.
	// this effectively erases any old values in the balloon
	document.getElementById('balloonHeadRow').style.display = 'block';
	document.getElementById('balloonHead').innerHTML = pctsFramework.balloonSpinner;
	document.getElementById('balloonBodyRow').style.display = 'none';

	// determine how high the balloon is
	var theHeight = document.getElementById('rBalloon').offsetHeight;
	
	// calculate display offsets based upon the actual size of the balloon
	var theAdjustedTop = theTop - (theHeight - 12);
	var theAdjustedLeft = (theLeft * 1) + 21;
	
	// set the balloon top and left
	document.getElementById('rBalloon').style.top = theAdjustedTop + 'px';
	document.getElementById('rBalloon').style.left = theAdjustedLeft + 'px';

	// now display the balloon
	
	// if this is not IE, we set the opacity to zero so we can fade it in
	if (!pctsFramework.browserPath == 'ie') {
			pcts.setOpacity(0, 'rBalloon');

	}
	
	if (document.getElementById('rBalloon').style.visibility != 'visible') {
		document.getElementById('rBalloon').style.visibility = 'visible';
		
		// if it is not IE, we fade in
		if (!pctsFramework.browserPath == 'ie') {
			// pcts.fadeIn('rBalloon', 0, 100, 160);
			jQuery('#rBalloon').fadeTo("normal", 1.00);

		} else {
			// ie can't deal with fading text on top of a PNG and it is always transparent
			// so for IE, we don't fade but we do set the opacity on the text elements
			jQuery('#rBalloon').fadeTo("normal", 1.00);
			//pcts.fadeIn('rBalloon', 0, 100, 160);
			pcts.setOpacity(100, 'balloonHead');
			pcts.setOpacity(100, 'balloonBody');
			
		}
		
	}

	document.getElementById('balloonBody').style.visibility = 'visible';
	document.getElementById('balloonHead').style.visibility = 'visible';	
	
}

page.handleThumbClick = function(theId) {
	// handle clicks to a thumbnail item
	
	// first, split the ID because it contains both the image name and te actual sequence for the slide
	var theData = theId.split('_');
	var theSequence = theData[2] * 1;
	
	pcts.goToSpecificRotator(theSequence);
}

page.handlePlayControlClick = function(theId) {
	// handle clicks to the play / pause button
	
	// if it is paused, we set the className appropriately and engage play
	if (page.rotatorPaused) {
		// we need to resume, so mark that we are not paused
		page.rotatorPaused = false;

		// and pause the rotator
		pcts.resumeRotator();

	} else {
		// we need to pause, so mark that we are paused
		page.rotatorPaused = true;

		// and pause the rotator
		pcts.pauseRotator();
	}
}

page.rotatorGoPrev = function() {
	// go to the previous image in the rotator sequence

	// first cancel the current rotator callback
	var int = window.clearInterval(pcts.rotateTrigger);

	// determine what the previous rotator image should be when we back up
	var thePreviousImage = pcts.prevRotatorIndex - 1;

	if (thePreviousImage < 0 ) {
		thePreviousImage = pcts.maxRotatorIndex;
	}

	// set the background slide to the previous rotator index
	theImageRef = 'rotatorSlide' + thePreviousImage;

	theImage = document.getElementById(theImageRef).innerHTML;

	// now load the background image
	document.getElementById('pctsWebMainContent').innerHTML = theImage;

	// fade out the foreground slide to reveal the next slide in the sequence
	// before fade, hide the rotatorTitleAssembly
	// it will be shown again after the fade completes
	document.getElementById('rotatorTitleBlock').style.visibility = 'hidden';
	
	// if this is IE, we also need to hide the titleBlock BG
	if (pctsFramework.browserPath == 'ie') {
		document.getElementById('rotatorTitleBlockBG').style.visibility = 'hidden';
	}
	
	// determine what the previous rotator ought to be
	var thePrev = pcts.prevRotatorIndex - 1;
	var theCurrent = pcts.currentRotatorIndex - 1;

	if (thePrev < 0 ) {
		thePrev = pcts.maxRotatorIndex;
	}

	if (theCurrent < 0 ) {
		theCurrent = pcts.maxRotatorIndex;
	}

	// pcts.fadeOut('pctsWebMainContent2', 100, 0, pcts.rotatorFadeDuration);
	pcts.fadeSlideToPrevious();

	var theAccessor = '../accessors/getRotatorData.php?imageName=' + pcts.rotatorImages[thePrev] + '&contentRegion=' + pctsFramework.contentRegion + '&anticache=' + theRan;
	pctsFramework.getData(theAccessor);

	// restore the rotator sequence
	// pcts.rotateTrigger = setInterval("pcts.updateRotatorSequence()", 30000);
	
}

page.rotatorGoNext = function() {
	// first cancel the current rotator callback
	var int = window.clearInterval(pcts.rotateTrigger);

	// fade out the foreground slide to reveal the next slide in the sequence
	// before fade, hide the rotatorTitleAssembly
	// it will be shown again after the fade completes
	document.getElementById('rotatorTitleBlock').style.visibility = 'hidden';
	
	// if this is IE, we also need to hide the titleBlock BG
	if (pctsFramework.browserPath == 'ie') {
		document.getElementById('rotatorTitleBlockBG').style.visibility = 'hidden';
	}
	
	// pcts.fadeOut('pctsWebMainContent2', 100, 0, pcts.rotatorFadeDuration);
	pcts.fadeSlide("pcts.advanceRotator()");

	var theAccessor = '../accessors/getRotatorData.php?imageName=' + pcts.rotatorImages[pcts.currentRotatorIndex] + '&contentRegion=' + pctsFramework.contentRegion + '&anticache=' + theRan;
	pctsFramework.getData(theAccessor);

	// restore the rotator sequence
	pcts.rotateTrigger = setInterval("pcts.updateRotatorSequence()", 30000);
	
}

page.goToSub = function(theURLSet, theID) {
	// navigate to a specified sub page of an item
	
	// the URLSet will be a complex of the URL plus the page token
	// we split it apart
	var theParts = theURLSet.split('|');
	var theURL = theParts[0];
	var theID = theParts[1];
	
	// alert('go to sub: ' + theURL + '?frameworkState=true');
	window.location.href = theURL + "?frameworkState" + pctsFramework.orphanMode;
	
	if (theID != 'undefined') {
		// set the clicked ID
		pctsFramework.currentPage = theID;
	} else {
		pctsFramework.currentPage = null;
	}

}

page.handlePressItemRetrieval = 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 = "/unified/pressRelease.php?releaseID=" + theId;
	var theAccessor = theAccessor + "&anticache=" + theRan;
	
	// go to the press release
	page.goToURL(theAccessor);
}

page.goToURL = function(theURL, theID) {
	// navigate to a specified URL
	// alert('go to URL: ' + theURL + '?frameworkState=true');
	window.location.href = theURL + "?frameworkState" + pctsFramework.orphanMode;
	if (theID != 'undefined') {
		// set the clicked ID
		pctsFramework.currentPage = theID;
	} else {
		pctsFramework.currentPage = null;
	}

}
	
// --- 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", page.getPageContent);

} else {
	// attached events for other browsers
	// window.addEventListener('load', page.getPageContent, false);

}