
/*
 * Namespace definition
 */
var org;
if (!org) {
	// we run the initialize function
	org = {};
	if (!org.resoa)
	org.resoa = {};
	if (!org.resoa.data)
		org.resoa.data = {};
	
	// some static links
	org.resoa.HOME = "/content/resoa.html";
	org.resoa.AUTH = "/content/enter";
	org.resoa.DOWNLOAD = "/content/download/index.html";
	org.resoa.AUTOHIDE = 1500;
	
	// create a option object for our ajax call
	org.resoa.getAJAXOptions = function(successHandler, errorHandler) {
		var options = {
			contentType : "application/json; charset=utf-8",
			dataType : "json",
			url : "/service",
			cache : false,
			global : false,
			processData : false,
			type : "POST",
			complete : successHandler,
			error : errorHandler
		};
		return options;
	};

	
	
	org.resoa.pageload = function(url, div, callback) {
		// we first hide any message
		org.resoa.hideMessage();
		if (url == null || url.length == 0)
			url = org.resoa.HOME;
		if (div == null)
			div = "#container";
		if (callback == null)
			callback = function() {
			$(div + " a.catch").click(org.resoa.catchURL);
		};
		org.resoa.ACTIVEURL = org.resoa.getInnerURL(url);
		$(div).load(url, null, callback);
	}
	
	/*
	 * function for catching navigation links by AJAX
 	*/
	org.resoa.catchURL = function(event) {
		var url = org.resoa.getCatchURL(event);
		if (url == null) 
			// we do not catch and continue normal url processing
			return;
		event.preventDefault();
		try {
			// we have inner domain, prepare url and load
			$.historyLoad(url);
		} catch (e) {
			org.resoa.showMessage(e.message, "Error", "OK");
		}
	};
			

	/*
	 * analyze and adapt url for ajax based processing 
 	*/
	org.resoa.getCatchURL = function(event) {
		if (event == null)
			return null;
		var url = event.target.href;
		if (url == null) 
			// we ignore
			return null;
		else if (url.indexOf(location.host) < 0) 
			// any remote url, we process normal
			return null;
		return org.resoa.getInnerURL(url);
	}

	/*
	 * Prepare the URL for AJAX div loading.
 	*/
	org.resoa.getInnerURL = function(url) {
		if (url == null || url == "" || url == "/")
			return null;
		// we create string
		var innerURL = url.toString().replace(/^.*\.[A-Za-z0-9]+:?[0-9]*\//, '');
		// we remove leading anchor
		if (innerURL.indexOf("#") == 0)
			innerURL = innerURL.substring(1);
		// we set it as root
		if (innerURL.indexOf("/") != 0)
			innerURL = "/" + innerURL;
		if (innerURL.indexOf(".html") < 0)
			innerURL += ".html";
		return innerURL;
	}
	
	/*
	 * validates the Cookie setting. IE might have problems with processing Cookies
	 * issued by Resoa Restlet/Jetty architecture, it accepts if the values are not
	 * wrapped by double quotes, so we remove them and set cookie again.
	 */
	org.resoa.checkCookieSet = function(xmlReq) {
		if (xmlReq == null)
			return;
		var cs = xmlReq.getResponseHeader("Set-Cookie");
		if (cs != null) {
			// we explicit set cookies (for IE, Chrome, Safari) by removing double quotes
			document.cookie = cs.replace(/"/g, "");
		}
	}
	
	/*
	 * This pops up our message box div
	*/
	org.resoa.showMessage = function(message, header, button, autohide) {
		var box = $("#dialog_wrapper");
		var boxmsg = $("#dialog_message");
		if (header != null)
			$("#dialog_header p.dialog_title").html(header);
		else
			$("#dialog_header p.dialog_title").html("Info");
		// we prepare html for box and first add the close click
		if (message != null)
			boxmsg.html(message);
		// if we have a button, we show
		box.css("visibility", "visible");
		if (button == null)
			$("#dialog_close").css("visibility", "hidden");
		else {
			$("#dialog_close").css("visibility", "visible");
			$("#dialog_close input").attr("value", button);
		}
		if (autohide != null)
			setTimeout(org.resoa.hideMessage, autohide);
	}

	/*
	 * This hides our message box
 	*/
	org.resoa.hideMessage = function() {
		$("#dialog_close").css("visibility", "hidden");
		$("#dialog_wrapper").css("visibility", "hidden");
	}
			
	org.resoa.showTerms = function(event) {
		var box = $("#container_box");
		if (box){
			box.load("/content/terms_txt.html");
			box.css("visibility", "visible");
		}
		event.preventDefault();
	}
	
	org.resoa.hideTerms = function() {
		$("#container_box").css("visibility", "hidden");
	}
	
	org.resoa.getCountryList = function() {

		if (org.resoa.data.CountryList != null)
			return org.resoa.data.CountryList;
		// we request from server
		var cl = new org.resoa.website.CountryList();
		cl.setLanguage("*");
		var ajaxOptions = org.resoa.getAJAXOptions(null, null);
		ajaxOptions.async = false;
		ajaxOptions.data = cl.toJson("get");
		var xmlReq = $.ajax(ajaxOptions);
		var clr = new org.resoa.website.CountryList(xmlReq.responseText);
		if (clr.getCountry() != null) {
			// succeeded
			org.resoa.data.CountryList = clr;
			return clr;
		} else {
			// any error
			return null;
		};

	}
	
	org.resoa.fillCountrySelect = function(response, partner) {
		// we directly received a JSON object, not a String.
		var ctl = null;
		if (response != null && response.CountryList != null) {
			// we have a CountryList object and wrap
			ctl = org.resoa.example.CountryList.prototype
					.wrap(response.CountryList);
		} else
			ctl = org.resoa.getCountryList();
		if (ctl == null)
			return;
		// we first check for language/default country
		var userLang = window.navigator.language;
		// for IE we need to ask for browserLanguage
		if (userLang == null)
			userLang = window.navigator.browserLanguage;
		// if we got a default, we switch
		if (partner != null && partner.country != null)
			userLang = partner.country.id;
		// in case we have a fumm specification, we remove the second part
		if (userLang != null)
			userLang = userLang.replace(/-.*/, '');
		// we select element not by jquery, as 1.3 does css selector chooses
		// options instead of select
		var fc = document.forms[0].elements["Partner_country"];
		
		for (var i in ctl.country) {
			var optn = document.createElement("option");
			optn.text = ctl.country[i].name;
			// we set full JSON Country representation as value
			optn.value = "{\"Country\":" + JSON.stringify(ctl.country[i]) + "}";
			// we preselect the country from default or language
			fc.options.add(optn);
			if (userLang != null && ctl.country[i].id == userLang)
				optn.selected = true;
		}
	}
	
	/*
	 * Ready function, adding click event
	 */
	$(document).ready(function() {
		
		// we init history manager
		$.historyInit(org.resoa.pageload);
		if (org.resoa.ACTIVEURL == null){
			org.resoa.ACTIVEURL = org.resoa.HOME;
			org.resoa.pageload (org.resoa.ACTIVEURL);
		}	
		// we adapt links
		$("#navi_top a.catch").click(org.resoa.catchURL);
		$("#terms a.catch").click(org.resoa.showTerms);
	});
};
