/* 	Ajax loading functions for Kate Schutt site
	Barking Dog Studios
	David Lachapelle
	Last Modified: February 20th, 2007
*/

/* 	Global Variables 
*/
var dynamicContent = false;
var jsCache = new Array();

var enableCache = true;

var counter = 0;

/*	Main ajax loading function
	Called to load each panel into its proper location
*/
function ajax_loadContent(divId, pathToFile) {
	if(enableCache && jsCache[pathToFile]) {
		document.getElementById(divId).innerHTML = jsCache[pathToFile];
		return;
	}
	
	dynamicContent = new sack();
	dynamicContent.requestFile = pathToFile;
	dynamicContent.onCompletion = function() { ajax_preload(divId, pathToFile); };
	dynamicContent.onError = function() { return false; alert(dynamicContent.response); }
	dynamicContent.runAJAX();
}

/*	Preload function called by the initial ajax completion call
	Loads the ajax response into the hidden loading div for preloading of images
*/
function ajax_preload(divId, pathToFile) {
	document.getElementById('loader').innerHTML = dynamicContent.response;

	//IE 7 is a pain in my side and doesn't fire onload events properly so I'm disabling preloading
	//graphics.  Safari is as well (apparently).
	if((BrowserDetect.browser != "Explorer" && BrowserDetect.version != 7) && BrowserDetect.browser != "Safari" && (BrowserDetect.browser != "Firefox" && BrowserDetect.version != 2) && BrowserDetect.browser != "Opera" && BrowserDetect.browser != "Camino")
		preloadImages(divId, pathToFile);
	else
		ajax_showContent(divId, pathToFile);
}

/*	Called by the onload callback from the preloadImages function to display the
	panel once the panel's background image has been loaded.
	Moves the contents from the hidden loading div into its final location
*/
function ajax_showContent(divId, pathToFile) {
	var regex = /panel/;
	
	if(divId.match(regex)) {
		//Safari is VERY unforgiving if you have two elements with the same ID in the dom registry.
		var foo = document.getElementById('loader').innerHTML;
		document.getElementById('loader').innerHTML = "";
		document.getElementById(divId).innerHTML = foo;
		
		//If the cache is enabled, place the item in it
		if(enableCache) {
			jsCache[pathToFile] = dynamicContent.response;
		}
		dynamicContent = false;
	
		ajax_removeLoadingImage(divId);
	
		//increment the loading counter
		counter++;
		//if we haven't loaded all the dynamic elements, then load the next one
		if(counter < containers.length) {
			ajax_loadContent(containers[counter][0], containers[counter][1]);
		} else {
			document.getElementById('loadingscreen').style.display = "none";
			document.getElementById('contentpane').style.display = "block";
			isLoading = false;
		}
	} else {
		document.getElementById(divId).innerHTML = dynamicContent.response;
	}
}

/*	Simple function to remove the loading overlay from the navigation
*/
function ajax_removeLoadingImage(divId) {
	var regex = /container/;
	document.getElementById(divId.replace(regex, "loading")).style.display = "none";
}

/*	Image preloader
	Preloads the background image for each panel before the panel is displayed
*/
function preloadImages(divId, pathToFile) {
	var regex = /.*\((.*)\).*/;
	var pics = Array();
    // get images on the page to pre-load them
	var imgcol = getElementsByClassName(document.getElementById('loader'), "div", "panel");

	if(imgcol == "") {
		ajax_showContent(divId, pathToFile);
	}

	for(var i = 0; i < imgcol.length; i++) {
		pics[i] = new Image();
		pics[i].src = imgcol[i].style.background.replace(regex, "$1");
		pics[i].onload = function() { ajax_showContent(divId, pathToFile); };
	}
} 

/*	Quote loader
	Loads in quotes for the buzz section
*/
function loadQuote(quoteID) {
	ajax_loadContent('p5_quote','getquote.php?id='+quoteID);
	return;
}

/*	Login function
	Logs a user in to view the exclusive media
*/
function loginMedia() {
	//p7_street-team-list
	ajax_loadContent('p7_street-team-list', 'login.php?id='+document.getElementById('p7_loginemail').value);
	showObj('p7_street-team-list');
}

/*	Get element by class function
	Note: I got this function from:
		http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
*/
function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}
