// JavaScript page methods for pctsWeb
// Gallery control

// declare a gallery control namespace
var gallery = {};

// --- namespace variables -------
gallery.galleryTop = 97;
gallery.galleryLeft = '';
gallery.slideCollection = new Array();
gallery.slidePositioner = 0;

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

gallery.setSlideLeft = function() {
	// set the left position of slides on this page for an image gallery 
	
	try {// first, get the slide array
		// do this only if slidePositioner is zero.  A higher number indicates we have already positioned slides.
		
		if(gallery.slidePositioner < 1) {
			var theSlides = document.getElementById('slideArray').value;
		
			// now get the left of the imageGallery object
			gallery.galleryLeft = pctsFramework.GetElementLeft(document.getElementById('imageGallery'));
			
			// split the slide list into parts, each representing one slide sequence number
			var slideArray = theSlides.split('@');
			
			// set a spacer to handle absolute positioning of the sldes in a stacked order
			var slideSpacer = 0;
			
			// check to see if we are fully loaded
			// this is for an IE6 bug
			var theLeft = gallery.galleryLeft + 20;
			
			// if galleryLeft is 20, we are not loaded, so don't process further
			if(theLeft > 20) {
				// now iterate through the slides array and set the slide top and left for each slide
				var maxIteration = slideArray.length - 1;
				
				for(i = 0; i <= maxIteration; i++) {
					var theSlide = 'slide' + slideArray[i];
					document.getElementById('slide' + slideArray[i]).style.top = gallery.galleryTop + slideSpacer + 'px';
					document.getElementById('slide' + slideArray[i]).style.left = gallery.galleryLeft + 20 + 'px';
					
					// add to the slide spacer slide height plus padding
					slideSpacer = slideSpacer + 111;
					
					document.getElementById('slide' + slideArray[i]).style.visibility = 'visible';
					
					// set the opacity of the slide
					if (pctsFramework.browser != 'IE') {
						$('#' + theSlide).fadeTo("normal", 0.70);
					} else {
						var theSlide = theSlide + '_image';
						$('#' + theSlide).fadeTo("normal", 0.70);
					}
				}
			}
			
			gallery.slidePositioner = 1;
		}

	} catch(err) {
		
	}
	
}

gallery.getGalleryAssembly = function(theToken) {
	// get the gallery assembly when the page loads
	
	// first create an accessor
	var theAccessor = "../accessors/getGalleryPane.php?token=" + theToken + '&mode=' + pctsFramework.browserPath + '&left=' + 3000;
	
	// add a random number so as to prevent IE from using a chached accessor return
	var theRan = pctsFramework.randomNumber();
	theAccessor = theAccessor + "&anticache=" + theRan;
	
	// set the target
	var theTarget = document.getElementById('rightContentBlock');

	gallery.getContentPlusData(theAccessor, theTarget, true);
	
}

gallery.getSlideAssembly = function(theToken) {
	// get a single slide assembly when the page loads
	
	// first create an accessor
	var theAccessor = "../accessors/getSlidePane.php?token=" + theToken + '&mode=' + pctsFramework.browserPath;
	
	// add a random number so as to prevent IE from using a chached accessor return
	var theRan = pctsFramework.randomNumber();
	theAccessor = theAccessor + "&anticache=" + theRan;
	
	// set the target
	var theTarget = document.getElementById('rightContentBlock');

	gallery.getContentPlusData(theAccessor, theTarget, true);
	
}

gallery.getGalleryAssemblyPage = function(theToken, thePage) {
	// get the gallery assembly when the page loads
	
	// set the page correctly
	var thePage = (thePage * 1) - 1;
	
	// check for a negative value
	if (thePage < 0) {
		thePage = 0;
	}
	
	// create an accessor
	var theAccessor = "../accessors/getGalleryPanePage.php?token=" + theToken + "&page=" + thePage + '&mode=' + pctsFramework.browserPath + '&left=' + pctsFramework.GetElementLeft(document.getElementById('imageGallery'));

	// add a random number so as to prevent IE from using a chached accessor return
	var theRan = pctsFramework.randomNumber();
	theAccessor = theAccessor + "&anticache=" + theRan;
	
	// set the target
	var theTarget = document.getElementById('imageGalleryBG');

	gallery.getContentPlusData(theAccessor, theTarget, true);
	
}

gallery.getContentPlusData = function(theContentSource, theTarget, showSpinner) {
	// ajax some content asynchronously along with a data packet.  Return content to a target
	// container after return AND process data.

	// first, set up a request object.  We have to do this so it degrades gracefully for IE6
	if (window.XMLHttpRequest) {
		// this is IE7, safari, etc.

		var xmlhttp =  new XMLHttpRequest();

	} else {
		// this is probably IE6, so see if we can instantiate an activeX obejct
		if (window.ActiveXObject) {

     		var xmlhttp = new ActiveXObject('MSXML2.XMLHTTP.3.0');

  		}
		
		// if we get here and have no request object this is some funky browser and we are totally out of luck.
	}

	// inject a loading indicator into the target object
	if (showSpinner != false) {
		// we do not show the spinner in IE6 because it will cause a crash
		// interacts with Ajax activeX object
		if (pctsFramework.browserVersion != '6.0') {
			pctsFramework.injectSpinner(theTarget);
		}

	}
	
	// wire up the callBack handler
	xmlhttp.onreadystatechange = function() {  
  		if(xmlhttp.readyState == 4)  
		var theReturn = xmlhttp.responseText;
		
		// populate the target only if it is not 'undefined'
		if (theReturn != undefined) {
			
			
			// break the return into parts.  The first part will be the script block returned, the second will be the content
			var theParts = theReturn.split('#content');
			var theData = theParts[0];
			
			// evaluate the returned data packet
			// and populate the innerHTML
			
			try {
				theTarget.innerHTML = theParts[1];
				
				eval(theData);
				
			} catch(err) {
				
			}

		} else {
			// alert(theReturn);

		}
		
	};  

    // send the ajax request after the desired data
	xmlhttp.open('GET', theContentSource, true);
	xmlhttp.send(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", gallery.setSlideLeft);
	window.attachEvent("onresize", gallery.setSlideLeft);
	
} else {
	// attached events for other browsers
	window.addEventListener('load', gallery.setSlideLeft, false);
	window.addEventListener('resize', gallery.setSlideLeft, false);

}