/*
    json.js
    2006-04-28

    This file adds these methods to JavaScript:

        object.toJSONString()

            This method produces a JSON text from an object. The
            object must not contain any cyclical references.

        array.toJSONString()

            This method produces a JSON text from an array. The
            array must not contain any cyclical references.

        string.parseJSON()

            This method parses a JSON text to produce an object or
            array. It will return false if there is an error.
*/
(function () {
    var m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        s = {
            array: function (x) {
                var a = ['['], b, f, i, l = x.length, v;
                for (i = 0; i < l; i += 1) {
                    v = x[i];
                    f = s[typeof v];
                    if (f) {
                        v = f(v);
                        if (typeof v == 'string') {
                            if (b) {
                                a[a.length] = ',';
                            }
                            a[a.length] = v;
                            b = true;
                        }
                    }
                }
                a[a.length] = ']';
                return a.join('');
            },
            'boolean': function (x) {
                return String(x);
            },
            'null': function (x) {
                return "null";
            },
            number: function (x) {
                return isFinite(x) ? String(x) : 'null';
            },
            object: function (x) {
                if (x) {
                    if (x instanceof Array) {
                        return s.array(x);
                    }
                    var a = ['{'], b, f, i, v;
                    for (i in x) {
                        v = x[i];
                        f = s[typeof v];
                        if (f) {
                            v = f(v);
                            if (typeof v == 'string') {
                                if (b) {
                                    a[a.length] = ',';
                                }
                                a.push(s.string(i), ':', v);
                                b = true;
                            }
                        }
                    }
                    a[a.length] = '}';
                    return a.join('');
                }
                return 'null';
            },
            string: function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            }
        };

    Object.prototype.toJSONString = function () {
        return s.object(this);
    };

    Array.prototype.toJSONString = function () {
        return s.array(this);
    };
})();

String.prototype.parseJSON = function () {
    try {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
            eval('(' + this + ')');
    } catch (e) {
        return false;
    }
};

/*

Functions Start Here

*/

var xmlHttp;

function createXMLHttpRequest() 
{
    if (window.ActiveXObject) 
	{
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) 
	{
        xmlHttp = new XMLHttpRequest();
    }
}

function cycleImage(linkID, imageID, controlParam, previousID) 
{	
    createXMLHttpRequest();
	
	var url = "resources/modules/image_cycle/IC_cycleImage.php?timeStamp=" + new Date().getTime();
	
	var queryString = "linkID=" + linkID + "&imageID=" + imageID + "&controlParam=" + controlParam + "&previousID=" + previousID;
	//var queryString = "linkID=" + linkID + "&imageID=" + imageID + "&controlParam=" + controlParam;
	
	//alert(queryString);
	
    xmlHttp.open("POST", url, true);
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");    		
    xmlHttp.send(queryString);	
}

function handleStateChange()
{
    if(xmlHttp.readyState == 4) 
	{
        if(xmlHttp.status == 200) 
		{           	
            parseResults();
        }
    }
}

var preImage = new Array();

preImage["difference_link"] = new Image();
preImage["accommodation_link"] = new Image();
preImage["conference_link"] = new Image();

var requestObjects = new Array();


// Deal with the PHP results
function parseResults() 
{    
	var responseText = xmlHttp.responseText;	
	
	if(responseText != 'null')
	{		
		var myObject = responseText.parseJSON();	

		// Pull out object variables
		var style = myObject[0]['style'];
		var link = myObject[0]['link'];
		var link_name = myObject[0]['link_name'];
		var linkID = myObject[0]['linkID'];		
		var imageID = myObject[0]['imageID'];
		var newID = myObject[0]['newID'];
		
		requestObjects[linkID] = myObject[0];
		preImage[linkID].src = style; 
		setTimeout("doSwitch('"+linkID+"');", 1000);
	}
}

function doSwitch(objectID){
	if (requestObjects[objectID] != null){
		// Pull out the link and wipe it's href and name
		//var target_image = document.getElementById(requestObjects[objectID]['imageID']);			
		//target_image.style.backgroundImage = "url('" + preImage[requestObjects[objectID]['linkID']].src + "')";	
						
		
		// Pull out the link and set its href
		var target_link = document.getElementById(requestObjects[objectID]['linkID']);
		target_link.href = requestObjects[objectID]['link'];
		target_link.title = requestObjects[objectID]['newID'];
		
		if(target_link.firstChild.firstChild.nodeType == 3)
		{
			target_link.firstChild.firstChild.nodeValue = requestObjects[objectID]['link_name'];
			
			//Now we want to replace the src of the image itself
	
			if(requestObjects[objectID]['controlParameter'] == 1)
			{
				var img = document.getElementById('differenceimage');
				img.alt = requestObjects[objectID]['link_name'];
				img.src = requestObjects[objectID]['style'];
				img.width = '266';
			}
			else if(requestObjects[objectID]['controlParameter'] == 2)
			{
				var img = document.getElementById('accommodationimage');
				img.alt = requestObjects[objectID]['link_name'];
				img.src = requestObjects[objectID]['style'];
				img.width = '266';
				
			}			
			else if(requestObjects[objectID]['controlParameter'] == 3)
			{
				var img = document.getElementById('conferenceimage');
				img.alt = requestObjects[objectID]['link_name'];
				img.src = requestObjects[objectID]['style'];
				img.width = '266';
				
			}				
		}
	} else {
		window.status = "Invalid objectID: "+objectID;
	}
}

function pausecomp(millis) 
{
	var date = new Date();
	var curDate = null;
	
	do { curDate = new Date(); } 
	while(curDate-date < millis);
}

/*
* This function will not return until (at least)
* the specified number of milliseconds have passed.
* It does a busy-wait loop.
*/
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

function waitForLoad(style, link, link_name, linkID, imageID, newID)
{
	if (!preImage.complete)
	{
		setTimeout('waitForLoad("'+style+'", "'+link+'", "'+link_name+'", "'+linkID+'", "'+imageID+'", "'+newID+'")', 500);
		return;
	}

	// Pull out the link and wipe it's href and name
	var target_image = document.getElementById(imageID);			
	target_image.style.backgroundImage = "url(/" + style + ")";	
	
	// Pull out the link and set its href
	var target_link = document.getElementById(linkID);
	target_link.href = link;
	target_link.title = newID;
	
	if(target_link.firstChild.firstChild.nodeType == 3)
	{
		target_link.firstChild.firstChild.nodeValue = link_name;
	}		
	
}