// Set up namespace
var FoCoProLo = FoCoProLo || {};

// Logging functions
FoCoProLo.log = function(x) {
	// no logging!
};
FoCoProLo.logError = function(funcName, error) {
	FoCoProLo.log("An unhandled error occurred in " + funcName);
	FoCoProLo.log(error);
};

// Stylesheet registration and  loading

FoCoProLo.stylesheets = [];
FoCoProLo.noStylesheets = NaN;
FoCoProLo.noStylesheetsLoaded = 0;

FoCoProLo.registerStylesheetForLoading = function(stylesheet) {
	try {
		FoCoProLo.stylesheets.push(stylesheet);
	}
	catch(e) {
		FoCoProLo.logError("registerStylesheetForLoading", e);
		throw e;
	}
};

FoCoProLo.loadStylesheets = function() {
	try {
		FoCoProLo.noStylesheets = FoCoProLo.stylesheets.length;
		if(FoCoProLo.noStylesheets == 0) {
			FoCoProLo.allStylesheetsLoaded();
		}
		else {
			for(var i = 0; i < FoCoProLo.noStylesheets; i++) {
				FoCoProLo.getStylesheet(FoCoProLo.stylesheets[i], FoCoProLo.stylesheetLoadedCallback);
			}
		}
	}
	catch(e) {
		FoCoProLo.logError("loadStylesheets", e);
		throw e;
	}
};

FoCoProLo.getStylesheet = function(url, callback) {
	try {
		jQuery.get(url, function(data, status) {
			try {
				if(status == "success") {
					jQuery("head").append("<style type=\"text/css\">" + data + "</style>").get(0);
				}
				callback(data, status);
			}
			catch(e) {
				FoCoProLo.logError("getStylesheet callback", e);
				throw e;
			}
		});
	}
	catch(e) {
		FoCoProLo.logError("getStylesheet", e);
		throw e;
	}
};

FoCoProLo.stylesheetLoadedCallback = function(data, status) {
	try {
		FoCoProLo.noStylesheetsLoaded++;
		FoCoProLo.log("Loaded " + FoCoProLo.noStylesheetsLoaded + " stylesheets of " + FoCoProLo.noStylesheets);
		if(FoCoProLo.noStylesheetsLoaded >= FoCoProLo.noStylesheets) {
			FoCoProLo.allStylesheetsLoaded();
		}
	}
	catch(e) {
		FoCoProLo.logError("stylesheetLoadedCallback", e);
		throw e;
	}
};

// Script registration and loading

FoCoProLo.scripts = [];
FoCoProLo.noScripts = NaN;
FoCoProLo.noScriptsLoaded = 0;

FoCoProLo.registerScriptForLoading = function(script) {
	try {
		FoCoProLo.scripts.push(script);
	}
	catch(e) {
		FoCoProLo.logError("registerScriptForLoading", e);
		throw e;
	}
};

FoCoProLo.loadScripts = function() {
	try {
		FoCoProLo.noScripts = FoCoProLo.scripts.length;
		if(FoCoProLo.noScripts == 0) {
			FoCoProLo.allScriptsLoaded();
		}
		else {
			for(var i = 0; i < FoCoProLo.noScripts; i++) {
				jQuery.getScript(FoCoProLo.scripts[i], FoCoProLo.scriptLoadedCallback);
			}
		}
	}
	catch(e) {
		FoCoProLo.logError("loadScripts", e);
		throw e;
	}
};

FoCoProLo.scriptLoadedCallback = function(data, status) {
	try {
		FoCoProLo.noScriptsLoaded++;
		FoCoProLo.log("Loaded " + FoCoProLo.noScriptsLoaded + " scripts of " + FoCoProLo.noScripts);
		if(FoCoProLo.noScriptsLoaded >= FoCoProLo.noScripts) {
			FoCoProLo.allScriptsLoaded();
		}
	}
	catch(e) {
		FoCoProLo.logError("scriptLoadedCallback", e);
		throw e;
	}
};

// Popup
FoCoProLo.createPopup = function() {
	try {
		// Create prompt window
		jQuery("body").append("<div id=\"facebookconnectprompt\" class=\"loading\" style=\"display: none;\"><div class=\"content\"><div class=\"inner\"></div></div></div>");
		if(!jQuery.support.opacity) {
			jQuery("#facebookconnectprompt div.content").addClass("faux-borders");
			jQuery("#facebookconnectprompt div.content").wrapInner("<div class=\"caramel\"></div>");
			jQuery("#facebookconnectprompt div.content").append("<div class=\"corner top-left\"></div>");
			jQuery("#facebookconnectprompt div.content").append("<div class=\"corner top-right\"></div>");
			jQuery("#facebookconnectprompt div.content").append("<div class=\"corner bottom-left\"></div>");
			jQuery("#facebookconnectprompt div.content").append("<div class=\"corner bottom-right\"></div>");
			jQuery("#facebookconnectprompt div.content").append("<div class=\"border top\"></div>");
			jQuery("#facebookconnectprompt div.content").append("<div class=\"border right\"></div>");
			jQuery("#facebookconnectprompt div.content").append("<div class=\"border bottom\"></div>");
			jQuery("#facebookconnectprompt div.content").append("<div class=\"border left\"></div>");
		}
	}
	catch(e) {
		FoCoProLo.logError("createPopup", e);
		throw e;
	}
};

FoCoProLo.showPopup = function() {
	try {
		jQuery("#facebookconnectprompt").jqm({
			overlay: 5, // overlay is transparent-ish
			toTop: true
		});
		jQuery("#facebookconnectprompt").jqmAddClose("#facebookconnectprompt"); // close when position div is clicked
		jQuery("#facebookconnectprompt div.content").bind("click", function(ev) { // don't click when content of popup is clicked
			ev.stopPropagation();
		});
		jQuery("#facebookconnectprompt").jqmShow();
	}
	catch(e) {
		FoCoProLo.logError("showPopup", e);
		throw e;
	}
};

// Check document state

FoCoProLo.checkDocumentStateHandle = null;

FoCoProLo.checkDocumentState = function() {
	try {
		if(document.readyState == "complete" || document.body) {
			clearInterval(FoCoProLo.checkDocumentStateHandle);
			FoCoProLo.documentStateComplete();
		}
	}
	catch(e) {
		FoCoProLo.logError("checkDocumentState", e);
		throw e;
	}
};

// All loaded callbacks
FoCoProLo._allStylesheetsLoaded = false;
FoCoProLo._allScriptsLoaded = false;
FoCoProLo._documentStateComplete = false;

FoCoProLo.allStylesheetsLoaded = function() {
	try {
		FoCoProLo._allStylesheetsLoaded = true;
		if(FoCoProLo._allScriptsLoaded) {
			FoCoPro.fillPrompt();
		}
	}
	catch(e) {
		FoCoProLo.logError("allStylesheetsLoaded", e);
		throw e;
	}
};

FoCoProLo.allScriptsLoaded = function() {
	try {
		FoCoProLo._allScriptsLoaded = true;
		if(FoCoProLo.everythingLoaded()) {
			FoCoPro.fillPrompt();
		}
	}
	catch(e) {
		FoCoProLo.logError("allScriptsLoaded", e);
		throw e;
	}
};

FoCoProLo.documentStateComplete = function() {
	try {
		FoCoProLo._documentStateComplete = true;
		if(FoCoProLo.everythingLoaded()) {
			FoCoPro.fillPrompt();
		}
	}
	catch(e) {
		FoCoProLo.logError("documentStateComplete", e);
		throw e;
	}
};

FoCoProLo.everythingLoaded = function() {
	try {
		return FoCoProLo._allScriptsLoaded && FoCoProLo._allStylesheetsLoaded && FoCoProLo._documentStateComplete;
	}
	catch(e) {
		FoCoProLo.logError("everythingLoaded", e);
		throw e;
	}
};

FoCoProLo.getVersion = function() {
	try {
		var version = null;
		jQuery("script").each(function() {
			var src = jQuery(this).attr("src");
			if(src) {
				var matches = /\?v=(.*)$/.exec(src);
				if(matches.length == 2) {
					version = matches[1];
					return false; // break out of jQuery loop
				}
			}
		});
		return version;
	}
	catch(e) {
		FoCoProLo.logError("getVersion", e);
		throw e;
	}
};

FoCoProLo.init = function() {
	try {
		if(/PromptToConnect=[^;]/.test(document.cookie)) {
			// Get the query string used to request this file..
			var version = FoCoProLo.getVersion();
			
			// Register prompt stylesheet for loading
			FoCoProLo.registerStylesheetForLoading("/static/css/modules/facebookplaceholder.css?v=" + version);
			
			// If it hasn't already been loaded, load the 99designs Facebook script
			if(typeof Facebook != "object") {
				FoCoProLo.registerScriptForLoading("/static/js/facebook.js?v=" + version);
			}
			
			if(document.location.protocol == "https:") {
				FoCoProLo.registerScriptForLoading("https://ssl.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php");
				FoCoProLo.registerScriptForLoading("/facebook/connect/prompt-ssl.js?v=" + version);
			}
			else {
				FoCoProLo.registerScriptForLoading("http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php");
				FoCoProLo.registerScriptForLoading("/facebook/connect/prompt.js?v=" + version);
			}
			
			// Start loading of stuff
			FoCoProLo.loadStylesheets();
			FoCoProLo.loadScripts();
			
			// Start checking document state
			FoCoProLo.checkDocumentStateHandle = setInterval(FoCoProLo.checkDocumentState, 100);
			
			FoCoProLo.createPopup();
			FoCoProLo.showPopup();
		}
	}
	catch(e) {
		FoCoProLo.logError("init", e);
	}
};

jQuery(document).ready(FoCoProLo.init);
