var pos = 1;
var currentAnchor = null;


var rquery = '';
var rsection = '';





// On load page, init the timer which check if the there are anchor changes each
// 300 ms
$().ready(function() {
	if (preloadpoint<preloadcount) {
		setTimeout("preloadImages()", 100);
	}
	setInterval("checkAnchor()", 300);
	//document.getElementById(currentmdiv).style.display="inline-block";
	//document.getElementById(currenttdiv).style.display="inline-block";
});




// Function which chek if there are anchor changes, if there are, sends the ajax
// petition
function checkAnchor() {
	// Check if it has changes
	if (currentAnchor != document.location.hash) {
		currentAnchor = document.location.hash;
		// if there is not anchor, the loads the default section
		if (!currentAnchor) {
			query = "section=home";
			section = "home";
			immediate=true;
		} else {

			// Creates the string callback. This converts the url URL/#main&id=2
			// in URL/?section=main&id=2
			var splits = currentAnchor.substring(1).split('&');
			// Get the section
			var section = splits[0];
			delete splits[0];
			// Create the params string
			var params = splits.join('&');
			var query = "section=" + section + params;

		}

		// Send the petition

		$.get("server.php", query + "&position=l", function(data) {
			$("#" + "tlcontent").html(data);
		});
		$.get("server.php", query + "&position=r", function(data) {
			$("#" + "trcontent").html(data);
		});
		
		rquery=query;
		rsection=section;
	
		document.getElementById("tlcontent").style.background = "url('content/backgrounds/"
				+ section + "l.jpg') no-repeat";
		document.getElementById("trcontent").style.background = "url('content/backgrounds/"
					+ section + "r.jpg') no-repeat";

	}


}




function swap() {
	if (pos == 0)
		pos = 1;
	else
		pos = 0;
}

function showimage(s,pos,max) {

	$.get("server.php", "&spinner=on" , function(data) {
		$("#gallery").html(data);
	});
	

	document.getElementById("gallerytrans").style.display = "inline-block";
	document.getElementById("gallerytrans").style.top = "0px";
	document.getElementById("gallery").style.display = "inline-block";
	document.getElementById("gallery").style.top = "0px";
	
	$.get("server.php", "&image=" + s+"&imagepos="+pos+"&imagemax="+max, function(data) {
		$("#gallery").html(data);
	});
}

function showvideo(s) {

	$.get("server.php", "&spinner=on" , function(data) {
		$("#gallery").html(data);
	});
	
	
	document.getElementById("gallery").style.display = "inline-block";
	document.getElementById("gallery").style.top = "0px";
	document.getElementById("gallerytrans").style.display = "inline-block";
	document.getElementById("gallerytrans").style.top = "10px";

	$.get("server.php", "&video=" + s, function(data) {
		$("#gallery").html(data);
	});

}

function hidegallerypanel() {
	var parent = document.getElementById('gallery');
	var child = document.getElementById('youtubeplayer');
	if (child != null) {
		parent.removeChild(child);
	}

	document.getElementById("gallery").style.display = "none";
	document.getElementById("gallerytrans").style.display = "none";

}


function preloadImage(s) {
	pic= new Image(320,469);
	pic.src=s;
}

function rewriteformurl() {

}
