/*
 * This class if there is an existing cookie name "tapaus_lang" with the selected language
 * If the cookie exists, then go to homepage with content
 * If not, then go select a language 
 */

/* actual language */
var lang;
var first = true;

/* pages to load languagues */
var homePage = 'home.html';
var languagePage = 'language.html';
var titelPage = 'lang/titels.php?lang=';
var productPage = 'lang/product.php?lang=';

/* Function to be called, when the user comes the first time */
function startPage() {
	
	//get actual language throw cookie
	lang = getCookie('tapaus_lang');

	if(lang != "") {
		/* AJAX with prototype
		 * load page homepage with saved language
		 * load in body tag
		 */
		loadToTagFirst(homePage, 'body', lang); // load page
	}
	else {	
		/* AJAX with prototype
		 * load page to select language
		 * load only in body tag
		 */
		loadToTag(languagePage, 'body'); // load page
	}
	
	getClock();
}

function loadPage(language) {
	setCookie('tapaus_lang',language, 365); //set cookie
	lang = language;
	loadToTag(homePage, 'body'); // load page
	
	//load language
	loadTitel(titelPage + language);
	loadProducts(productPage + language);
	loadToTag('pages/others.php?lang=' + language, 'subInfoCenter2');
	loadToTag('pages/text.php?lang=' + language + '&p=home', 'colum2');
}

function loadLanguage(language) {	
	//load language
	loadTitel(titelPage + language);
	loadProducts(productPage + language);
	loadToTag('pages/others.php?lang=' + language, 'subInfoCenter2');
	loadToTag('pages/text.php?lang=' + language + '&p=home', 'colum2');
}

function loadToTag(page, tag) {
	/* AJAX with prototype
	 * load page to select language
	 * load only in body tag
	 */
	new Ajax.Request(page, {
    	method:'post',
    	
    	onLoading: function() {
    		document.getElementById(tag).innerHTML = '';
    		loading();
    	},
    	
    	onSuccess: function(transport) {
      		var response = transport.responseText || "no response text";
      		document.getElementById(tag).innerHTML = response;
      		loading();
    	},
    
    	onFailure: function() {
    		errorMessage(tag);
    	}
	});
}

function loadToTagFirst(page, tag, language) {
	setCookie('tapaus_lang',language, 365); //set cookie
	lang = language;

	/* AJAX with prototype
	 * load page to select language
	 * load only in body tag
	 */
	new Ajax.Request(page, {
    	method:'post',
    	
    	onLoading: function() {
    		document.getElementById(tag).innerHTML = '';
    		loading();
    	},
    	
    	onSuccess: function(transport) {
      		var response = transport.responseText || "no response text";
      		document.getElementById(tag).innerHTML = response;
      		loadLanguage(lang);
      		loading();
    	},
    
    	onFailure: function() {
    		errorMessage(tag);
    	}
	});
}

function loadToTagLan(page, tag) {
	/* AJAX with prototype
	 * load page to select language
	 * load only in body tag
	 */
	new Ajax.Request(page + "&lang=" + lang, {
    	method:'post',
    	
    	onLoading: function() {
    		document.getElementById(tag).innerHTML = '';
    		loading();
    	},
    	
    	onSuccess: function(transport) {
      		var response = transport.responseText || "no response text";
      		document.getElementById(tag).innerHTML = response;
      		loading();
    	},
    
    	onFailure: function() {
    		errorMessage(tag);
    	}
	});
}

function loadInfo(page) {
	new Ajax.Request(page + "?lang=" + lang, {
    	method:'post',
    	
    	onLoading: function() {
    		document.getElementById(tag).innerHTML = '';
    		//loading();
    	},
    	
    	onSuccess: function(transport) {
      		var response = transport.responseText || "no response text";
      		document.getElementById('colum2').innerHTML = response;
      		//loading();
    	},
    
    	onFailure: function() {
    		errorMessage(tag);
    	}
	});
}

function loadNavigation(lang) {
	
}

/* function to write a error message on the seleted part */
function errorMessage(tag) {
	document.getElementById(tag).innerHTML = "Sorry, we are having some problems, please try later!";
	//TODO send email an administrator with error description
}

var isLoading = false;
function loading() {
	isLoading = !isLoading;
	if(isLoading) {
		document.getElementById('loading').style.top =  window.innerHeight/2 - 30 + "px";
		document.getElementById('loading').style.left = window.innerWidth/2 - 75 + "px";
		document.getElementById('loading').style.visibility = "visible";
		(lang == 'es')?document.getElementById('loading').innerHTML = 'cargando ...':nothing();
		(lang == 'en')?document.getElementById('loading').innerHTML = 'loading ...':nothing();
		(lang == 'de')?document.getElementById('loading').innerHTML = 'laden ...':nothing();
	}
	else {
		document.getElementById('loading').style.visibility = "hidden";
	}
}

function nothing() {
}
