var htmlResponse = "";
var slideInBlocked = false;
var newLinkId = null;

function navigate(linkId, offset) {
	newLinkId = linkId;
	activeGalleries = new Object();
	htmlResponse = "";
	slideInBlocked = true;
	console.log("navigating to "+linkId);
	ajax_read('ajax.php?id='+linkId, "htmlResponse = xmlhttp.responseText;slideInNewPage()");
	dojo.animateProperty({
		  node:"navigationSlider",
		  easing: function (n){return n*(n-2)*-1;},
		  duration: 750,
		  properties: {
				top: 29*(offset)
		  }
		}).play();
	dojo.animateProperty({
		node:"titleBar",
		easing: function (n){return n*(n-2)*-1;},
		duration: 750,
		properties: {
			height: (29*(offset))+61
		}
	}).play();
	dojo.animateProperty({
		node:"teaserSlider",
		easing: function (n){return n*(n-2)*-1;},
		duration: 750,
		properties: {
			left: -650
		}
	}).play();
	setTimeout("navigatePart2()", 150);
	setTimeout("navigatePart3()", 250);
	setTimeout("cleanup()", 1000);	
}

function cleanup() {
	var node = dojo.byId('contentSlider');
	node.parentNode.removeChild(node);
	var node = dojo.byId('teaserSlider');
	node.parentNode.removeChild(node);
	var node = dojo.byId('captionSlider');
	node.parentNode.removeChild(node);
	
	slideInBlocked = false;
}

function cleanup2() {
	dojo.byId('contentSlider_new').id = 'contentSlider';
	dojo.byId('captionSlider_new').id = 'captionSlider';
	dojo.byId('teaserSlider_new').id = 'teaserSlider';
}

function navigatePart2() {
	dojo.animateProperty({
		node:"captionSlider",
		easing: function (n){return n*(n-2)*-1;},
		duration: 750,
		properties: {
			left: -590
		}
	}).play()
}

function navigatePart3() {
	dojo.animateProperty({
		node:"contentSlider",
		easing: function (n){return n*(n-2)*-1;},
		duration: 750,
		properties: {
			left: -650
		}
	}).play();
}

function slideInNewPage() {
	if (htmlResponse == "" || slideInBlocked) {
		setTimeout("slideInNewPage()", 100);
		console.log("blocked");
		return;
	}
	html = htmlResponse.split("||||");
	dojo.place("<img id='headerImgNew' src='img/header/"+newLinkId+".jpg' style='z-index:-1;'/>", dojo.byId('webcam'));
	dojo.place("<div id='teaserSlider_new' style='position:relative;left:650px;'>"+html[0]+"<div>", dojo.byId('teaser'));
	dojo.place("<div id='captionSlider_new' style='position:relative;left:650px;'>"+html[1]+"<div>", dojo.byId('caption'));
	dojo.place("<div id='contentSlider_new' style='position:relative;left:650px;'>"+html[2]+"<div>", dojo.byId('contentWrapper2'));

	dojo.animateProperty({
		node:"teaserSlider_new",
		easing: function (n){return n*(n-2)*-1;},
		duration: 750,
		properties: {
			left: 0
		}
	}).play();
	
	setTimeout("slideIn2()", 150);
	setTimeout("slideIn3()", 250);
	setTimeout("cleanup2()", 1000);
}

function slideIn2() {
	dojo.animateProperty({
		node:"captionSlider_new",
		easing: function (n){return n*(n-2)*-1;},
		duration: 750,
		properties: {
			left: 0
		}
	}).play();
}

function slideIn3() {
	dojo.animateProperty({
		node:"contentSlider_new",
		easing: function (n){return n*(n-2)*-1;},
		duration: 750,
		properties: {
			left: 0
		}
	}).play();
	
	dojo.animateProperty({
		node:"headerImg",
		easing: function (n){return n*(n-2)*-1;},
		duration: 750,
		onEnd: function() {
    		dojo.byId('headerImg').parentNode.removeChild(dojo.byId('headerImg'));
    		dojo.style("headerImgNew", "z-index", "10");
    		dojo.byId('headerImgNew').id = "headerImg";
    	},
		properties: {
			left: -690
		}
	}).play();
}

function fixPNG(myImage) {
	if (window.ie55up) {
		var imgID = (myImage.id) ? "id='" + myImage.id + "' " : "";
		var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : "";
		var imgTitle = (myImage.title) ? "title='" + myImage.title + "' " : "title='" + myImage.alt + "' ";
		var imgStyle = "display:inline-block;" + myImage.style.cssText;
		var strNewHTML = "<span " + imgID + imgClass + imgTitle;
		strNewHTML += " style=\"" + "width:" + myImage.width + "px; height:" + myImage.height + "px;" + imgStyle + ";";
		strNewHTML += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader";
		strNewHTML += "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>";
		myImage.outerHTML = strNewHTML;
	}
}

function ajax_read(url, callback) {
		if(window.XMLHttpRequest){
			xmlhttp=new XMLHttpRequest();
			if(xmlhttp.overrideMimeType){
				xmlhttp.overrideMimeType('text/xml');
			}
		} else if(window.ActiveXObject){
			try {
				xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e){
					
				}
			}
		}

	if(!xmlhttp) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}

	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			eval(callback);
		}
	}

	xmlhttp.open('GET',url,true);
	xmlhttp.send(null);
	}
