var globalFLFrameID = 'api';
var globalApiPath = 'http://www.fixtureslive.com/api_simple/';
var now = new Date();
var documentHeadElement = document.getElementsByTagName("head")[0];

document.getElementById(globalFLFrameID).innerHTML = '<p></p><img src="' + globalApiPath + 'waiting.gif" />'; // Add initial Loading screen.

function pagePanel() {
    // These variables are st by the various javascript pages that are loaded.
    this.content='';
    this.currentURL='';
    this.previousURL='';
    this.display=display;
    this.historyMode='';
    this.arrHistory = new Array(); // This will hold the last 10 pages.
}
function display() {
    outputStr = '';
    outputStr = outputStr + '<div class="fcontent">' + this.content + '<div style="clear:both"><!-- --></div></div>';
    document.getElementById(globalFLFrameID).innerHTML = outputStr; // Display Content
    
    if (typeof this.content == "undefined") {
        alert("invalid data");
    }
    if (this.content == "") {
        if (this.previousURL != '') {

            currentPage.content = "<div class=\"fl_api_error_message\">Sorry - there is a problem loading this information. Please <a href=\"javascript:navigate('" + currentPage.currentURL + "')\">try again</a> or <a href=\"javascript:navigateHistory('" + currentPage.previousURL + "')\">go back</a> or contact info@fixtureslive.com if the problem persists.</div>";
        } else {
            currentPage.content = "<div class=\"fl_api_error_message\">Sorry - there is a problem loading this information. Please <a href=\"javascript:navigate('" + currentPage.currentURL + "')\">try again</a> or contact info@fixtureslive.com if the problem persists.</div>";
        }
        currentPage.display();
    } else {
        if (this.historyMode == 'back') {
            this.arrHistory.pop(); // remove last element from the array
            this.historyMode = ''; // turn off go back mode.
        } else {
            if (this.historyMode != 'none') {
                if (this.previousURL != "") {
                    if (this.arrHistory.length == 10) {
                        this.arrHistory.shift(); // Remove first element from array
                    } else {
                        this.arrHistory.push(this.previousURL); // Add the preoviously loaded page to history list.
                    }
                }
            } else {
                this.historyMode = ''; // Reset history Mode
            }
        }
        
        if(document.getElementById("apiGoBack")) {       
            if (this.arrHistory.length > 0) { // If there is a history show the back links
                document.getElementById("apiGoBack").innerHTML = " <a href=\"javascript:navigateHistory('" + this.arrHistory[this.arrHistory.length-1] + "')\">Back</a> |&nbsp;"; //class=\"fback_link\"
            }
        }
    }
}

var currentPage = new pagePanel();

function navigate(file) {
    // Display loading icon.
    if (file.substr(0,7) == "http://") {
        window.document.location = file;
    } else if (file != '') {
        currentPage.previousContent = currentPage.content; // This is used in the event of the user clicking cancel when a page is loading.
        currentPage.content = '';
        if (currentPage.historyMode != 'none') {
            if (currentPage.currentURL != file) { // Only update previous page, if the user has moved onto a new page.
                currentPage.previousURL=currentPage.currentURL;
            }
            currentPage.currentURL = file;
        }
        document.getElementById(globalFLFrameID).innerHTML = '<p>Receiving Data, please wait.</p><img src="' + globalApiPath + 'waiting.gif" />';
        // Record current page, so that you can impelement back button.
        loadPanel(globalApiPath + file)
    }
    
}
function navigateHistory(file) {
    // Display loading icon.
    currentPage.historyMode = 'back';
    navigate(file);
}

function loadPanel(apiUrl) {
	var now = new Date();
	globalPanelHTML = '';
	var documentHeadElement = document.getElementsByTagName("head")[0];
	var newScriptElement=document.createElement("script");
	newScriptElement.type="text/javascript";
	newScriptElement.charset="utf-8";
	newScriptElement.src = apiUrl + ((apiUrl.indexOf('?')==-1)?'?':'&') + 'nocache=' + now;
	var onLoadFunc = function() {
	//alert(apiUrl);
		var documentHeadElement=newScriptElement.parentNode;
		documentHeadElement.removeChild(newScriptElement);
		delete newScriptElement;
		currentPage.display();		
	};
	var onReadyFunc=function(documentHeadElement){
		var docObj=(documentHeadElement?documentHeadElement:window.event).target?(documentHeadElement?documentHeadElement:window.event).target:(documentHeadElement?documentHeadElement:window.event).srcElement;
		if(docObj.readyState=="loaded"||docObj.readyState=="complete"){
			onLoadFunc()
		}
	};
	if(navigator.product=="Gecko"){
		newScriptElement.onload=onLoadFunc
	} else {
		newScriptElement.onreadystatechange=onReadyFunc
	}
	documentHeadElement.appendChild(newScriptElement);
}


if (typeof fStartPageFile == "undefined") {
    
    if (typeof fStartPage.split(":")[1] == "undefined") {
        pageID = 0;
    } else {
        pageID = fStartPage.split(":")[1]
    }
    
    if (fStartPage == "") {
         // No page specified. Goto default page
        currentPage.currentURL = 'default_page.ashx';

    } else if (fStartPage.toUpperCase() == "SAMPLE_PAGE1") {
        currentPage.currentURL = 'sample_page1.ashx';
        loadPanel(globalApiPath + currentPage.currentURL);
        
    } else if (fStartPage.toUpperCase().split(":")[0] == "SAMPLE_PAGE2") {
        currentPage.currentURL = 'sample_page2.ashx?sample_param=' + pageID;
        loadPanel(globalApiPath + currentPage.currentURL);
    } else if (fStartPage.toUpperCase().split(":")[0] == "EPUB1") {
        currentPage.currentURL = 'flDemo_cal.ashx?volorgid=' + pageID;
        loadPanel(globalApiPath + currentPage.currentURL);
	//alert(globalApiPath + currentPage.currentURL);
} 
else if (fStartPage.toUpperCase().split(":")[0] == "EPUBT1") {
        currentPage.currentURL = 'tmp_flDemo_cal3.ashx?volorgid=' + pageID;
        loadPanel(globalApiPath + currentPage.currentURL);
	//alert(globalApiPath + currentPage.currentURL);
}
else if (fStartPage.toUpperCase().split(":")[0] == "EPUB") {

 pageID = fStartPage.split(":")[1]
	                            letter = fStartPage.split(":")[2]
	                            currentPage.currentURL = 'flDemo_Prev.ashx?sectionid=' + pageID + '&volorgid=' + letter;		                    

		                    loadPanel(globalApiPath + currentPage.currentURL);

 
}
} else {
    currentPage.currentURL = fStartPageFile;    
}



// Load common JS file.
var commonJS=document.createElement("script");
commonJS.type="text/javascript";
commonJS.charset="utf-8";
commonJS.src = globalApiPath + "common.js?nocache=" + now;
documentHeadElement.appendChild(commonJS);

loadPanel(globalApiPath + currentPage.currentURL);