// once loaded, an iframe triggers this function to reset contents 
// of the main display block
var predisplace = 0;
function replxcb() {
    // break out of a loop if we're stuck here.
    if(predisplace > 1) {
        predisplace = 0;    
        return;
    }
    // get reference to the hidden iframe on this page
    var iframe = document.getElementById('hidden-iframe');
    if(iframe) {
        // locate the page container
        var container = document.getElementById('rhs-content');
        if(container) {
            // locate the iframe content block (must exist)
            var contentblock = null;
            if(iframe.contentDocument) {
                contentblock = iframe.contentDocument.getElementById('contentblock');
            } else if(iframe.contentWindow.document) {
                contentblock = iframe.contentWindow.document.getElementById('contentblock');
            } else {
                alert('replxcb(): No document in iframe located!');
            }
            // check if we found the content block
            if(contentblock) {
                // unify the inner html content of the dynamically loaded 
                // iframe content block into the main RHS page table
                container.innerHTML = contentblock.innerHTML;
                // the return is crucial here; we don't want an infinite loop
                return;
            } else {
                // else there was an error; no content block to be seen. continue 
                // to pass through to replace the content with the under_construction content.
            }
        } else {
            alert('replxcb(): No page container \'rhs-content\' located!');
        }
    } else {
        alert('replxcb(): No iframe located!');
    }
    // default to setting the content to the 'under construction' page if an error occurred.
    predisplace += 1;
    replx('under_construction.html');
}

// reset the iframe to the given local URL
function replx(url) {
    // get reference to the hidden iframe on this page
    var iframe = document.getElementById('hidden-iframe');
    // if found, replace it's contents with the URL specified.
    if(iframe) {
        iframe.src = url;
    }
}

function encapsulate_with_iframe() {
    if(top.window.location.href.indexOf('index.html') > -1) {
        // don't modify the index page location (only page that doesn't require an embedding)
        return;
    }
    if(top.window.location.pathname.replace(/\//gi, "").length < 1) {
        // don't modify the default top level location
        return;
    }
    // determine a '?' component
    var resource_path = top.window.location.search;
    // if no search component, or one that does not indicate index.html?iframe=... then
    if(!resource_path || resource_path.length < 1 || resource_path.indexOf("c=") < 0) {
        // extract the whole path name 
        var path_name = top.window.location.pathname;
        var iind1 = path_name.lastIndexOf("/");
        if(iind1 > -1) {
            // extract the path prefix and final target resource
            var pre_target = path_name.substring(0, iind1);
            var f_target = path_name.substring(iind1 + 1);
            // construct the new target URI
            var repl_target = top.window.location.protocol + '//' + top.window.location.host + pre_target + '/index.html?c=' + f_target;
            // redirect immediately
            top.window.location.href = repl_target;
        }
    }
}

// execute immediately
encapsulate_with_iframe();

/* 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;
}

/* @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_content_by_url(): Inspects the current URL for a 
 * parameter c=URL and sets the inner div content to
 * be equal to URL, if located; else, URL is assigned to welcome.html.
 *
 */

function set_content_by_url() {
    set_content_by_url(null);
}

function set_content_by_url(override_url) {
    var curr_url = top.window.location.href;
    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] == 'c') {
		            new_url = pair[1];
		        }
	        }
	    }
    }
    if(new_url == '' || new_url == 'index.html') {
	new_url = 'welcome.html';
    }
    replx(new_url);

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

/* 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();
}

