
var rotateGraphics = false;
var totalHeight = 0;
var totalImages = 0;
var rotaterToLoad = 0;
var prevRotater = 0;
var nextRotater = 0;
var direction = null;
var d = new Date();
/* 
 * Creates and returns an XMLhttp object that can be used for AJAX control
 * (if it can't, it'll return null)
 *
 * Returns:
 *    XMLhttp - An XMLhttp request object that can be used to make AJAX
 *      calls.  If the object could not be instantiated this function will
 *      return null
 */
function createRequestObject() {
	// Default our return value to null in case the HTTP object can't be
	// initialized
	var toReturn = null;
	
	// Get the XML http object which will be used to make requests to the server
	// If in any browser but IE, just use the XMLhttp object
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		toReturn = new XMLHttpRequest();
		if (toReturn.overrideMimeType) {
			// Reset the mime type, in case Firefox breaks because the content
			// returned is not valid XML
			toReturn.overrideMimeType('text/xml');
		}
	} 
	// Otherwise, use the Microsoft ActiveX control
	else if (window.ActiveXObject) { // IE
		try {
			toReturn = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				toReturn = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}
	
	// Return the created XMLHTTP object
	return toReturn;
}

// Create a global variable, which represents the XMLhttp object we are passing
// back and forth with the server to answer our queries
//var http = createRequestObject();

/* 
 * Use this function to send an AJAX request -- this function
 * can be called with multiple different requests at the same time
 * and they won't collide.
 * 
 * Parameters:
 *    URL - A string URL that the AJAX submits to
 *    method - A string (either 'GET' or 'POST') that describes how
 *      this request should be processed.  Note that it must be given
 *      in all caps to work
 *    responseFunction - The name of the response function WITHOUT
 *      any parathesis.  The sendAJAX function will send a single
 *      parameter to this function which will be the XMLhttp object
 *      it used to make the request (so that the function can ask
 *      onReadyState etc).  Make sure that the function given accepts
 *      a single parameter.
 *      For example: 'wooba' should correspond to a function declared
 *      like the following: function wooba(http) { ... }
 *    variables - The variables to send along with this request in
 *      standard URL format: 'arg1=val1&arg2=val2&arg3=val3'
 *    asyncronous - True if this function should submit the request
 *      and continue executing (without being sure if it's been sent
 *      yet) or False if it should at least wait until the request is
 *      processed.  Most of the time you can get away with asyncronous
 *      unless this page is somehow going to go away before the request
 *      can be processed (like if you're doing AJAX on a window close
 *      event)
 *
 * Returns:
 *    boolean - True if the operation appears to have succeeded false if
 *      it failed
 */
function sendAJAX(URL, method, responseFunction, variables, asyncronous) {
	
	// Get our http object that we'll use for our requests
	var http = createRequestObject();
	
	// Make sure that our method is upper case (if not it will
	// break in FireFox)
	method = method.toUpperCase();
	// Whether or not AJAX appears to have succeeded
	var succesful = false;
	
	if(http != null) {
		//alert('Despite your best efforts, your file -- ' + window.checkinPath + ' -- is still going  to be checked in.  Nice try sucka!');
		// Use AJAX to ask the server to checkin the file
		http.abort();
		// Open a location (to reset the Session timer)
		http.open(method, URL, asyncronous);
		//alert('I\'ve submitted your checkin request to ' + window.sitePath + '/CMCMS/checkinFile.php');
		// We don't need to specify a return function since we won't still be here
		// when he gets back
		eval('http.onreadystatechange = function() { ' + responseFunction + '(http); };');
		// This line must be included for POST to work, otherwise the Server will
		// drop the variables
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		// Make sure the browser does not cache our request and stop sending
		// AJAX every time we request it
		http.setRequestHeader('Cache-Control', 'no-cache');
		// Variables are in standard form: 'arg1=val1&arg2=val2&arg3=val3'
		http.send(variables);
		// Note the apparent success
		succesful = true;
	}
	
	// Return the success or failure of this AJAX operation
	return succesful;
}

	menuOff = new Image();
	menuOff.src = '/pix/main_bg_off.png';
	menuOn = new Image();
	menuOn.src = '/pix/main_bg_on.png';
	
	function newTitle() {
		document.title = "Code-Monkeys :: Where one stop mobile development meets bacon!";
	}
	
	setTimeout("newTitle()",5000);

	function loadRotater(rotater_id) {
			document.getElementById("txtNew").style.left = 0 + "px";
			document.getElementById("rotater_contentANew").style.left = 0 + "px";
			document.getElementById("rotater_contentBNew").style.left = 0 + "px";
		rotaterToLoad = rotater_id
		rotateGraphics = false;
		sendAJAX("/scripts/generateText.cfm?token=" + d.getTime(), "POST", "processText", 'rotater_id=' + rotaterToLoad, true);
	}
	function loadPrev() {
		if (rotateGraphics) {
			direction = "LEFT";
			document.getElementById("txtNew").style.left = 247 + "px";
			document.getElementById("rotater_contentANew").style.left = 247 + "px";
			document.getElementById("rotater_contentBNew").style.left = 247 + "px";
			rotaterToLoad = prevRotater;
			rotateGraphics = false;
			sendAJAX("/scripts/generateText.cfm?token=" + d.getTime(), "POST", "processText", 'rotater_id=' + rotaterToLoad, true);
		}
	}
	function loadNext() {
		if (rotateGraphics) {
			direction = "RIGHT";
			document.getElementById("txtNew").style.left = -247 + "px";
			document.getElementById("rotater_contentANew").style.left = -247 + "px";
			document.getElementById("rotater_contentBNew").style.left = -247 + "px";
			rotaterToLoad = nextRotater;
			rotateGraphics = false;
			sendAJAX("/scripts/generateText.cfm?token=" + d.getTime(), "POST", "processText", 'rotater_id=' + rotaterToLoad, true);
		}
	}
	
	function processImages(http) {
		if (http.readyState==4 && http.status==200) {
			document.getElementById("rotater_contentANew").innerHTML=http.responseText;
			document.getElementById("rotater_contentBNew").innerHTML=http.responseText;
			sendAJAX("/scripts/generateRotater.cfm", "POST", "processRotater", 'rotater_id=' + rotaterToLoad, true);
		}
	}
	
	function processText(http) {
		if (http.readyState==4 && http.status==200) {
			document.getElementById("txtNew").innerHTML=http.responseText;
			sendAJAX("/scripts/generateImages.cfm", "POST", "processImages", "rotater_id=" + rotaterToLoad, true);
		}
	}
	
	function processRotater(http) {
		if (http.readyState==4 && http.status==200) {
			eval(http.responseText);
			document.getElementById("rotater_contentANew").style.top = 0 + "px";
			document.getElementById("rotater_contentBNew").style.top = totalHeight + "px";
			document.getElementById("rotater_contentANew").style.display = "block";
			document.getElementById("rotater_contentBNew").style.display = "block";
			document.getElementById("txtNew").style.display = "block";
			//rotateGraphics = true;
			setTimeout("showNewContent()", 50);
		}
	}
	
	function showNewContent() {
		goodToGo = false;
		modifier = 0;
		if (direction == "LEFT") {
			modifier = -10;
		}
		if (direction == "RIGHT") {
			modifier = 10;
		}
		// Move text in
		
		txtNewLeft = document.getElementById("txtNew").offsetLeft;
		txtLeft = document.getElementById("txtContent").offsetLeft;

		txtNewLeft = txtNewLeft + modifier;
		txtLeft = txtLeft + modifier;
		
		if (Math.abs(txtNewLeft) < 15) {
			txtNewLeft = 0;
			txtLeft = txtLeft + modifier;
			goodToGo = true;
		}
		
		//alert("New:" + txtNewLeft + " Old:" + txtLeft);
		
		document.getElementById("txtNew").style.left = txtNewLeft + "px";
		document.getElementById("txtContent").style.left = txtLeft + "px";
		
		txtNewLeft = document.getElementById("rotater_contentANew").offsetLeft;
		txtLeft = document.getElementById("rotater_contentA").offsetLeft;
		txtNewLeft = txtNewLeft + modifier;
		txtLeft = txtLeft + modifier;
		
		if (Math.abs(txtNewLeft) < 15) {
			txtNewLeft = 0;
			txtLeft = txtLeft + modifier;
			goodToGo = true;
		}
		
		document.getElementById("rotater_contentANew").style.left = txtNewLeft + "px";
		document.getElementById("rotater_contentA").style.left = txtLeft + "px";
		document.getElementById("rotater_contentBNew").style.left = txtNewLeft + "px";
		document.getElementById("rotater_contentB").style.left = txtLeft + "px";
		
		
		if (goodToGo == false) {
			setTimeout("showNewContent()", 20);
		} else {
			document.getElementById("rotater_contentA").innerHTML = document.getElementById("rotater_contentANew").innerHTML;
			document.getElementById("rotater_contentB").innerHTML = document.getElementById("rotater_contentBNew").innerHTML;
			document.getElementById("txtContent").innerHTML = document.getElementById("txtNew").innerHTML;
			document.getElementById("rotater_contentA").style.display = "none";
			document.getElementById("rotater_contentB").style.display = "none";
			document.getElementById("txtContent").style.display = "none";
			document.getElementById("rotater_contentA").style.top = 0 + "px";
			document.getElementById("rotater_contentB").style.top = totalHeight + "px";
			setTimeout("startSwap()", 50);
		}
		
	}
	
	function startSwap() {
		document.getElementById("rotater_contentA").style.left = 0 + "px";
		document.getElementById("rotater_contentB").style.left = 0 + "px";
		document.getElementById("txtContent").style.left = 0 + "px";
		setTimeout("continueSwap()", 50);
	}
	
	function continueSwap() {	
		document.getElementById("rotater_contentA").style.display = "block";
		document.getElementById("rotater_contentB").style.display = "block";
		document.getElementById("txtContent").style.display = "block";
		setTimeout("endSwap()", 50);
	}
	
	function endSwap() {
		document.getElementById("rotater_contentANew").style.display = "none";
		document.getElementById("rotater_contentBNew").style.display = "none";
		document.getElementById("txtNew").style.display = "none";
		rotateGraphics = true;
		setTimeout("rotateUm()", 50);
	}
	
	function rotateUm() {
		if (rotateGraphics) {
			currentTop = document.getElementById("rotater_contentA").offsetTop;
			nextTop = currentTop - 1;
			if (nextTop < (0 - totalHeight)) {
				nextTop = totalHeight;
			}
			document.getElementById("rotater_contentA").style.top = nextTop + "px";
			currentTop = document.getElementById("rotater_contentB").offsetTop;
			nextTop = currentTop - 1;
			if (nextTop < (0 - totalHeight)) {
				nextTop = totalHeight;
			}
			document.getElementById("rotater_contentB").style.top = nextTop + "px";
			setTimeout("rotateUm()", 50);
		}
	}
	
	
