var id = 0;
var inMotion = 0;
var idsQueue = new Array();

function backUp(transport) {
	var content = this.transport.responseText;
	
}
function getInfo(i_aid) {
	
	var l = navigator.language;
	var suffix = "";
	
	if(l.substring(0,2).toLowerCase()== "de")
	{
		suffix = "_de";
	}

	if(l.substring(0,2).toLowerCase()== "fr")
	{
		suffix = "_fr";
	}
	
	var aid = i_aid + suffix;
	
	if(inMotion == 1) {
		idsQueue[idsQueue.length] = aid;
		return false;
	} else if(aid == id) {
		return false; 
	} else {		
		inMotion = 1;
	}
	
	id = aid;
	
	//fade('__loading', '0', '100', 20);
	fade('footer', '100', '0', 20);
	fade('ajax_content', '100', '0', 20, function() {
		if($('backup_' + id)) {
			var content = $('backup_' + id).innerHTML; 
			$('ajax_content').innerHTML = content;
			
			//fade('__loading', '100', '0', 20);
			fade('footer', '0', '100', 20);
			fade('ajax_content', '0', '100', 20, function() {
				inMotion = 0;
			
				if (idsQueue.length > 0) {
					getInfo(idsQueue[0]);
					idsQueue.splice(0, 1);
				}
			});
			var anchors = document.getElementsByTagName('a');

			// loop through all anchor tags
			for (var i=0; i<anchors.length; i++){
				var anchor = anchors[i];

				var relAttribute = String(anchor.getAttribute('rel'));

				// use the string.match() method to catch 'lightbox' references in the rel attribute
				if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox'))){
					anchor.onclick = function () {myLightbox.start(this); return false;}
				}
			}
			
		} else {
			var xhr;
		    if (window.XMLHttpRequest) xhr = new XMLHttpRequest();
		    else if (window.ActiveXObject) xhr = new ActiveXObject('Microsoft.XMLHTTP');
		    else
		    {
		        alert('Error...');
		        return;
		    }
		    xhr.open('get','content/' + id + '.html',true);
		    xhr.onreadystatechange = function()
		    {
		        if (xhr.readyState == 4)
		        {
		            var content = xhr.responseText;
					$('ajax_content').innerHTML = content;
					var item = document.createElement("div");
					item.innerHTML = content;
					item.id = 'backup_' + id;

					$('back_up').appendChild(item);
					//fade('__loading', '100', '0', 20);
					fade('footer', '0', '100', 20);
					fade('ajax_content', '0', '100', 20, function() {
						inMotion = 0;

						if (idsQueue.length > 0) {
							getInfo(idsQueue[0]);
							idsQueue.splice(0, 1);
						}
					});
					var anchors = document.getElementsByTagName('a');

					// loop through all anchor tags
					for (var i=0; i<anchors.length; i++){
						var anchor = anchors[i];

						var relAttribute = String(anchor.getAttribute('rel'));

						// use the string.match() method to catch 'lightbox' references in the rel attribute
						if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox'))){
							anchor.onclick = function () {myLightbox.start(this); return false;}
						}
					}
		        }
		    }
		    xhr.send(null);
		    
		}
	});
}

function fade(el, from, to, interval, callBack) {
    if (el == new String(el)) el = document.getElementById(el);
	if(el.style.display == 'none') { setOpacity('0', el); el.style.display = 'block'; }

    var dif = Math.abs(to - from);
    var pi = Math.PI;
    var i = 0;
    
    var opacs = [];
    
    for (var theta=0; theta < 90; theta+=8) { 
    	if (to > from) 
			var step = Math.sin((theta*pi)/180);
		else 
			var step = Math.cos((theta*pi)/180);
		
		opacs.push(dif*step);
    }
        
	var inter = setInterval(
		function() {
			if (++i < opacs.length) {
				setOpacity(Math.max(0.01, opacs[i]), el);
			} else {
				setOpacity(to, el);
				if(to == 0) { el.style.display = 'none'; }
				clearInterval(inter);
				if (callBack != null) callBack();
			}
		}, interval);
}

function setOpacity(opacity, el) { 
    if (el == new String(el)) el = document.getElementById(el);

	if (navigator.userAgent.indexOf("Firefox") != -1) {
		if (opacity == 100) { opacity = 99.999; }
	}
	el.style.filter = "alpha(opacity=" + opacity + ")";
	el.style.KhtmlOpacity = (opacity / 100);
	el.style.MozOpacity = (opacity / 100);
	el.style.opacity = (opacity / 100);
}

