var clubArray = generateContextItemArray(new Array("queanbeyan","canberra","newcastle","moruya","kuala_lumpur","oslo","grimstad"), "-club-menu");
var globalLocationArray = generateContextItemArray(new Array("malaysia","australia","norway"), "-context-menu");
var contextSensitiveObjectArray = globalLocationArray; //clubArray.concat(globalLocationArray);

function generateContextItemArray(clubNames, contextSuffix) {
    if(clubNames == null) {
	return null;
    }
    var bClubArray = new Array(clubNames.length);
    for(i=0; i<clubNames.length; i++) {
	bClubArray[i] = new Array(2);
	    // string to search for in URL
	bClubArray[i][0] = clubNames[i];
	    // matching div id to enable/disable
	bClubArray[i][1] = clubNames[i] + contextSuffix;
    }
    return bClubArray;
}

function resetIFrameHeight() {
    var innerFrameRef = getIFrameRef();
    if(	innerFrameRef 
	&& innerFrameRef.contentWindow
	&& innerFrameRef.contentWindow.document
	&& innerFrameRef.contentWindow.document.body) {
		// reset the height immediately;
	    innerFrameRef.height = 0;
		// find the height of the internal page once loaded;
		// this function should be called on an onLoad event
		// after body has been loaded
	    var new_height = innerFrameRef.contentWindow.document.body.scrollHeight;
		// change the height of the iframe with a 50px buffer
		// so that the scrollbar is guaranteed not to appear
	    innerFrameRef.height = new_height + 40;
    }
}

/* Toggles the club-specific menus based on the current URL parameter.
 * If the URL contains the club name (or, is exactly the club name),
 * that club menu will appear in the context menu; else it is hidden.
 *
 */ 
function toggleMenu(menuURL) {
    if(menuURL == null) {
	return;
    }
	// search through all clubs
    for(i=0; i<contextSensitiveObjectArray.length; i++) {
	var objectIDRef = getGlobalTopObjectByID(contextSensitiveObjectArray[i][1]);
	if(objectIDRef == null) {
	    // alert('Failed to locate menu object with ID: ' + contextSensitiveObjectArray[i][1]);
	    continue;
	}
	var displayProperty = 'none';
	if(menuURL.indexOf(contextSensitiveObjectArray[i][0]) >= 0) {
	    displayProperty = 'block';
	} else {
	    displayProperty = 'none';
	}
	    // do the toggle
	if(objectIDRef) {
	    objectIDRef.style.display = displayProperty;
	}
    }
};

// current context menu height for this browser; defined later
var cmHeight=-1;

function resetContextMenuYPosition(forceReturn) {
	// reset the context sensitive object positions based on the current 
	// and previous offsets
    var yOffsetNow = window.pageYOffset;
	// look at all context-sensitive objects
    for(i=0; i<contextSensitiveObjectArray.length; i++) {
	var objectIDRef = getGlobalTopObjectByID(contextSensitiveObjectArray[i][1]);
	    // ignore if not present, or hidden
	if(objectIDRef == null || objectIDRef.style.display == 'none') {
	    continue;
	}
	    // if the static current context menu height has 
	    // not yet been computed for this browser, do it
	if(cmHeight < 0) {
	    var el = objectIDRef;
	    var ct = 0;
	    if(el.offsetParent) {
		do {
		    ct+=el.offsetTop;
		} while(el=el.offsetParent);
	    }
	   cmHeight = ct; 
	}
	    // reposition the object down the menu pane, but only 
	    // if we have scrolled an extent past the current 
	    // position; this keeps the item 'static' relative
	    // to motion
	if(objectIDRef && yOffsetNow && yOffsetNow > cmHeight) {
	    var newOffsetPos = yOffsetNow - cmHeight;
	    if(forceReturn) {
		newOffsetPos = 0;
	    }
	    objectIDRef.style.marginTop = newOffsetPos + "px";
	}
    }
}

/* Locates reference to a top level object by ID.
 */
function getGlobalTopObjectByID(id) {
    var globalRef = window.top.document.getElementById(id);
    if(globalRef == null && window.top.document.all != null) {
	globalRef = window.top.document.all[id];
    } 
    return globalRef; 
}

/* Locates reference to a local level object by ID.
 */
function getLocalObjectByID(id) {
    var localRef = document.getElementById(id);
    if(localRef == null && document.all != null && document.all[id] != null) {
	localRef = document.all[id];
    } 
    return localRef;
}

/* Returns reference to the topmost iFrame reference, if
 * unset. Maintains a constant reference once located.
 *
 */ 
var globalContentIFrame = getIFrameRef();
function getIFrameRef() {
    if(globalContentIFrame != null) {
	return globalContentIFrame;
    } else {
	globalContentIFrame = getGlobalTopObjectByID("inner-iframe")
    }
    return globalContentIFrame;
}

/* Replace the content of the main iFrame with the specified
 * URL parameter.
 */
function replaceIFrameContent(pageHREF) {
	// obtain reference to the main content frame
    var innerFrame = getIFrameRef();
	// replace the content of the iframe
    innerFrame.src = pageHREF;
	// the frame change may have made the outer window
	// scroll without firing an event; resposition 
	// all the context-sensitive objects now
    resetContextMenuYPosition(true);
}

/* @param newLocIDString: String identifying the current location, or null.
 *			  Should match one of the clubs in the first array
 *			  if anything, as the parameter is passed to the 
 *			  menu processor to enable/disable club-specific
 *			  menus based on the current content.
 *
 * This method is called when a page is reloaded to disable the right-click
 * context menu on all images, and to display/hide the club-specific menus
 * in the top window margin.
 *
 */ 
function resetCM(newLocIDString) {
	// set global image protection against right-click 
	// (doesn't work for opera :)
    protectImages();
	// test if a club menu need showing/hiding
    toggleMenu(newLocIDString);
	// perform any countdown-redirects
    redirElem();
}

/* set_iframe_content_by_url(): Inspects the current URL for a 
 * parameter iframe=URL and sets the inner iFrame content to
 * be equal to URL, if located; else, URL is assigned to intro.html.
 *
 */

function set_iframe_content_by_url() {
    set_iframe_content_by_url(null);
}

function set_iframe_content_by_url(override_url) {
    var curr_url = '' + window.location;
    var curr_url_split = curr_url.split('?');
    var new_url = '';
    if(override_url != null) {
	curr_url = override_url;
    }
    if(curr_url_split.length > 1) {
	var all_params = curr_url_split[1].split('&');
	if(all_params.length >= 1) {
	    for(i=0; i<all_params.length; i++) {
		var pair = all_params[i].split('=');
		if(pair.length == 2 && pair[0] == 'iframe') {
		    new_url = pair[1];
		}
	    }
	}
    }
    if(new_url == '') {
	new_url = 'intro.html';
    }
    replaceIFrameContent(new_url);

	// protect images on container...
    protectImages();
}

function redirElem() {
	// look for any item on the page corresponding to a 5-sec redir
    var redirElem = getLocalObjectByID('redir');
    var redirTarget = getLocalObjectByID('redir_target');
    if(redirElem != null && redirTarget != null) {
	var startTime = parseInt(redirElem.innerHTML);
	if(startTime == null) {
	    startTime = 3;
	}
	    // perform redirect later
	setTimeout(function() {
		// reset top position
	    top.window.location.href = redirTarget;
	}, startTime * 1000);
    }
};

/* Function disables the right-click context menu on all images
 * in the document, if possible. Works for most all browsers 
 * with the known exception of Opera.
 *
 *	    Written by: david.ratcliffe.webdesign@gmail.com
 */
var debugResetCM = false;
function protectImages() {
    if(debugResetCM) {
        alert('Now applying image protection settings...');
    }
    for(i=0; i<document.images.length; i++) {
	if(document.images[i] != null) {
	    document.images[i].oncontextmenu=new Function("return false");
	    document.images[i].onmousedown=new Function("return false");
	    document.images[i].onmouseup=new Function("return false");
	    if(debugResetCM) {
		alert('Reset click behaviour on image: ' + document.images[i].src);
	    }
	}
    }
    if(debugResetCM) {
        alert('...complete.');
    }
}

function toggleMapElementDisplay(obj, id) {
    var idRef = getLocalObjectByID(id);
    if(idRef != null) {
	    // display the div element and map within
	var disP = idRef.style.display;
	if(disP != 'block') {
	   idRef.style.display = 'block';	
	    if(obj != null) {
		obj.innerHTML = "Click here to hide the map below."
	    }
	} else {
	    idRef.style.display = 'none';
	    if(obj != null) {
		obj.innerHTML = "Click here to show a map of the location."
	    }
	}
	    // reload the entire map content to force a 
	    // centering of the map based on the new 
	    // div extents (since it was previously 0x0, 
	    // or hidden from view, and was 'centered'
	    // incorrectly at the top left hand portion
	    // of the map...
	var gmapDIVRef = getLocalObjectByID('gmap0');
	var innerContent = gmapDIVRef.innerHTML;
	gmapDIVRef.innerHTML = innerContent;
    }
	// resize the iframe as the map may have made
	// it longer
    resetIFrameHeight();
}

window.onscroll = function (e) {
    resetContextMenuYPosition(false);
};
