// Global js for non-redesign pages 
// 
// Globals 
//
var toolbarPathPrefix = ''; 
var divCheckIntervalId; 
var alertCount = 0; 


// 
// Functions 
//
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function checkLoaded() { 
    // Check files dynamically added are loaded, and body exists 
    if (alertCount < 30) { 
	//        if ((document.body != null) && (jQuery) && (window.toolbarInit)) { 
        if ((document.body != null) && (typeof jQuery != 'undefined') && (window.toolbarInit)) { 
	    // Create toolbar container div 
            var toolbarContainerDiv = document.createElement('div'); 
            toolbarContainerDiv.id = 'global_toolbar_container'; 
            toolbarContainerDiv.innerHTML = ''; 
            document.body.appendChild(toolbarContainerDiv); 
            clearInterval(divCheckIntervalId); 
	    // Call toolbar start function from toolbar js file 
	    toolbarInit('/includes/toolbar/' + toolbarPathPrefix + 'toolbar.php'); 
        } 
        else { 
            alertCount++; 
        } 
    } 
    else { 
        alert("Hmm, looks like things didn't completely load.  Please......\n\n- check the toolbar file prefix you chose on the development page\n\n- then send this info to Aaron:\ndocument: " + (document.body != null) + ", jquery: " + (typeof jQuery != 'undefined') + ", js: " + (window.toolbarInit) + ";\n\n- then try reloading"); 
        clearInterval(divCheckIntervalId); 
    } 
} 


// 
// Main processing 
//
if (readCookie('ex_develop_toolbar') == 'on') { 
    // Check for toolbar files path prefix 
    if (readCookie('ex_develop_toolbar_path') != null) { 
        toolbarPathPrefix = readCookie('ex_develop_toolbar_path'); 
    } 

    // Bootstrap jquery and toolbar files 
    var jqueryScriptElement=document.createElement('script'); 
    jqueryScriptElement.setAttribute("type","text/javascript"); 
    jqueryScriptElement.setAttribute("src", "/shared/js/jquery-1.4.2.min.js"); 
    document.getElementsByTagName("head")[0].appendChild(jqueryScriptElement); 

    var toolbarCssLinkElement=document.createElement("link"); 
    toolbarCssLinkElement.setAttribute("rel", "stylesheet"); 
    toolbarCssLinkElement.setAttribute("type", "text/css"); 
    toolbarCssLinkElement.setAttribute("href", "/css/toolbar/" + toolbarPathPrefix + "toolbar.css"); 
    document.getElementsByTagName("head")[0].appendChild(toolbarCssLinkElement); 

    var toolbarScriptElement=document.createElement('script'); 
    toolbarScriptElement.setAttribute("type","text/javascript"); 
    toolbarScriptElement.setAttribute("src", "/shared/js/toolbar/" + toolbarPathPrefix + "toolbar.js"); 
    document.getElementsByTagName("head")[0].appendChild(toolbarScriptElement); 

    // Check files loaded 
    divCheckIntervalId = window.setInterval(checkLoaded, 100); 
} 

