// JavaScript Document

var grid = new Array(25);
var image = Array();
var comment_n = Array();
var comment_o = Array();
var language = 0;

var layout = "00000.00000.00000.00000.00000.00000.00000.00000.00000.00000.00000.00000.";
var newlayout = "00000.00000.00000.00000.00000.00000.00000.00000.00000.00000.00000.00000.";
var transparency = "00000.00000.00000.00000.00000.00000.00000.00000.00000.00000.00000.00000.";

var faderInterval = 0;
var animateInterval = 0;

var key = '';
var fadeId = '';
var fade = 0;
var opacity = 0;
var wait = 0;

var fadeCommentOpacity = 0;
var fadeCommentInterval = 0;

var cx = 0;
var cy = 0;
var nx = 0;
var ny = 0;

var ajaxObj = createXMLHttp();

function animate() {
	
	if ((fadeId == '') && (wait == 0)) {

		if (newlayout == layout) {

			var url = 'fileadmin/template/nextlayout.php?L='+language+'&y='+layout;
			ajaxObj.open("GET", url, true);
	
			ajaxObj.onreadystatechange = function() { ajaxObj.processRequest(); };
			ajaxObj.send(null);
			
			wait = 15;
	
		} else {
			
			x1 = 0;
			y1 = 0;

			if (nx > 0) {
				cx = nx; 
				cy = ny;
				nx = 0; 
				ny = 0; 
			} else {
				cx = Math.floor(Math.random()*4)+1;
				cy = Math.floor(Math.random()*3)+1;
			}

			if (GetXY(layout,cx,cy) == 0) {
				
				var ok = true;
				
				// Check if all required units are blank

				for (x1 = 1; x1 <= 4; x1++) {

					for (y1 = 1; y1 <= 3; y1++) {

						if ((GetXY(newlayout,x1,y1) == GetXY(newlayout,cx,cy)) && (GetXY(layout,x1,y1) != 0)) {

							var zap = GetXY(layout,x1,y1);
							
							for (x2 = 1; x2 <= 4; x2++) {

								for (y2 = 1; y2 <= 3; y2++) {

									if (GetXY(layout,x2,y2) == zap) {

										if (fadeId == '') { 
											fade = -10; 
											opacity = 100; 
											fadeId = '' + x2 + '' + y2; 
											faderInterval = setInterval('fader()',40);
										}
		
										layout = SetXY(layout,x2,y2,0);

									}

								}

							}

							ok = false;
							x1 = 10;
							y1 = 10;

						}

					}

				}

				// If all are OK then replace

				if (ok) {

					for (x1 = 1; x1 <= 4; x1++) {

						for (y1 = 1; y1 <= 3; y1++) {

							if (GetXY(newlayout,x1,y1) == GetXY(newlayout,cx,cy)) {

								if (fadeId == '') { 
									fade = 10; 
									opacity = 0; 
									fadeId = '' + x1 + '' + y1; 
									faderInterval = setInterval('fader()',40);
								}

								layout = SetXY(layout,x1,y1,GetXY(newlayout,cx,cy));

							}

						}

					}

				}
				
			} else if (GetXY(layout,cx,cy) != GetXY(newlayout,cx,cy)) {
				
				var zap = GetXY(layout,cx,cy);
				
				for (x1 = 1; x1 <= 4; x1++) {

					for (y1 = 1; y1 <= 3; y1++) {

						if (GetXY(layout,x1,y1) == zap) {

							if (fadeId == '') { 
								fade = -10; 
								opacity = 100; 
								fadeId = '' + x1 + '' + y1; 
								faderInterval = setInterval('fader()',40);
							}

							layout = SetXY(layout,x1,y1,0);

						}

					}

				}
	
			} else {
				
				for (x1 = 1; x1 <= 4; x1++) {

					for (y1 = 1; y1 <= 3; y1++) {

						if ((GetXY(layout,x1,y1) == 0) || (GetXY(layout,x1,y1) != GetXY(newlayout,x1,y1))) {

							nx = x1; 
							ny = y1; 
							x1 = 10;
							y1 = 10;
							
						}

					}

				}

			}
			
			document.getElementById('l1').innerHTML = layout.substr(0,24);
			document.getElementById('l2').innerHTML = layout.substr(24,24);
			document.getElementById('l3').innerHTML = layout.substr(48,24);
	
		}

	} else if (wait > 0) { 

		if (wait < 100) { wait--; };
		document.getElementById('wait').innerHTML = wait;

	}
	
}

function fader() {
	
	if ((fadeId != '') && (wait == 0)) {
		
		if ((opacity == 0) && (fade > 0)) {
			
			// New image
			
			v = "00000"+GetXY(layout,fadeId.substr(0,1),fadeId.substr(1,1))+".jpg";
			
			document.getElementById('grid'+fadeId).style.opacity = 0;
			document.getElementById('grid'+fadeId).style.MozOpacity = 0;
			document.getElementById('grid'+fadeId).style.filter = "alpha(opacity=0)";
			document.getElementById('grid'+fadeId).src = image[GetXY(layout,fadeId.substr(0,1),fadeId.substr(1,1))*1].src;
			document.getElementById('grid'+fadeId).style.display = "block";

			opacity += fade;
			
		} else {
			
			opacity += fade;
			
			document.getElementById('grid'+fadeId).style.opacity = (opacity / 100);
			document.getElementById('grid'+fadeId).style.MozOpacity = (opacity / 100);
			document.getElementById('grid'+fadeId).style.filter = "alpha(opacity=" + opacity + ")";

			if (opacity == 0) { document.getElementById('grid'+fadeId).style.display = "none"; };

			if ((opacity == 0) || (opacity == 100)) {
				fade = 0;
				fadeId = 0;	
				clearInterval(faderInterval);
			}
			
		}
		
	}
	
}

function createXMLHttp() {

	if (typeof XMLHttpRequest != 'undefined') {

		return new XMLHttpRequest();

	} else if (window.ActiveXObject) {

		var avers = ["Microsoft.XmlHttp", "MSXML2.XmlHttp",
			"MSXML2.XmlHttp.3.0", "MSXML2.XmlHttp.4.0",
			"MSXML2.XmlHttp.5.0"];

		for (var i = avers.length -1; i >= 0; i--) {

			try {

				httpObj = new ActiveXObject(avers[i]);
				return httpObj;

			} catch(e) {}

		}

	}

	throw new Error('XMLHttp (AJAX) not supported');

}

ajaxObj.processRequest = function() {

	if (this.readyState == 4) {

		if (this.status != 200) {
			
			alert('Error : Status '+this.status+' returned.');

		} else {
			
			newlayout = this.responseText;
			
			document.getElementById('n1').innerHTML = newlayout.substr(0,24);
			document.getElementById('n2').innerHTML = newlayout.substr(24,24);
			document.getElementById('n3').innerHTML = newlayout.substr(48,24);
			
		}

	}

	var filename = newlayout.split(".");

	for (var i = 0; i < filename.length; i++) {

		image[filename[i]*1] = new Image();
		image[filename[i]*1].src = "fileadmin/images/index/"+filename[i]+".jpg";

		comment_n[filename[i]*1] = new Image();
		comment_n[filename[i]*1].src = "fileadmin/images/index/"+filename[i]+"n.png";
		
		comment_o[filename[i]*1] = new Image();
		comment_o[filename[i]*1].src = "fileadmin/images/index/"+filename[i]+"o.png";
		
	}
	
	wait = 10;

}

function GetXY(layout, x, y) { return (1*layout.substr(((x-1)*6) + ((y-1)*24),5)); };

function SetXY(layout, x, y, v) { 

	v = "00000"+v;
	v = v.substr(v.length-5,5);
	
	var i = ((x-1)*6) + ((y-1)*24);
	
	layout = layout.substr(0,i) + v + layout.substr(i+5);

	return layout;

}

function showcomment(image, grid) {
	
	if (wait < 100) {
	
		wait = 1000;

		key = (1*image.substr(image.length-9,5));

		document.getElementById('imagecomment').src = comment_n[key].src;
		document.getElementById('imagecomment_over').src = comment_o[key].src;

		document.getElementById('imagecomment').style.top = "0px";
		document.getElementById('imagecomment').style.left = "0px";
	
		fadeCommentOpacity = 100;
		fadeCommentInterval = setInterval("fadeComment('"+image+"')",15);

	}

}

function fadeComment(image) {

	fadeCommentOpacity -= 2; 

	var ay1 = 7; 
	var ay2 = 0; 
	var ax1 = 7; 
	var ax2 = 0;

	for (x1 = 1; x1 <= 4; x1++) {
	
		for (y1 = 1; y1 <= 3; y1++) {

			if (GetXY(layout,x1,y1) != 0) {

				if ((1*image.substr(image.length-9,5)) != (1*GetXY(layout,x1,y1))) {
				
					document.getElementById('grid'+x1+y1).style.opacity = (fadeCommentOpacity/100);
					document.getElementById('grid'+x1+y1).style.MozOpacity = (fadeCommentOpacity/100);
					document.getElementById('grid'+x1+y1).style.filter = "alpha(opacity="+fadeCommentOpacity+")";

				} else {
				
					if ( x1 < ax1 ) { ax1 = x1; };				
					if ( y1 < ay1 ) { ay1 = y1; };				
					if ( x1 > ax2 ) { ax2 = x1; };				
					if ( y1 > ay2 ) { ay2 = y1; };				

				}
				
			}

		}

	}

	if (document.getElementById('imagecomment').style.top == "0px") {
		
		var w = (1+(ax2-ax1));
		var h = (1+(ay2-ay1));
		
		switch(""+w+h) {
			
		case "11":
		case "12":

			if (ax1 == 1) {
				var y = ay1+((h-1)/2);
				var x = ax1 + 1;
			} else if (ax1 == 4) {
				var y = ay1+((h-1)/2);
				var x = ax1 - 2;
			} else {
				if (ay1 > 1) {
					var y = ay1 -1;
					var x = ax1 - 0.5;
				} else {
					var y = ay2+1;
					var x = ax1 - 0.5;
				}
			}

			break;
			
		case "13":
		
			var y = ay1+((h-1)/2);

			if (ax1 < 3) {
				var x = ax1 + 1;
			} else {
				var x = ax1 - 2;
			}

			break;
			
		case "21":
		case "31":
		case "41":
		
			var x = ax1+((w-2)/2);

			if (ay1 < 2) {
				var y = ay1 + 1;
			} else {
				var y = ay1 - 1;
			}

			break;
			
		case "22":
		case "32":
		case "42":
		
			var x = ax1+((w-2)/2);

			if (ay1 == 1) {
				var y = 3;
			} else {
				var y = 1;
			}

			break;
			
		case "23":
		
			var y = 2;

			if (ax1 == 1) {
				var x = 3;
			} else if (ax1 == 2) {
				var x = 2;
			} else {
				var x = ax1 - 2;
			}

			break;
			
		case "33":
		
			var y = 1;

			if (ax1 == 1) {
				var x = 3;
			} else {
				var x = 1;
			}

			break;
			
		}
		
		document.getElementById('imagecomment').style.left = ((x-1)*188)+"px";
		document.getElementById('imagecomment').style.top = ((y-1)*146)+"px";
		document.getElementById('imagecomment_over').style.left = ((x-1)*188)+"px";
		document.getElementById('imagecomment_over').style.top = ((y-1)*146)+"px";
		
	}

	if (fadeCommentOpacity < 10) { 
		
		document.getElementById('imagecomment').style.display = 'block';

		clearInterval(fadeCommentInterval);

		setTimeout("fadeCommentInterval=setInterval(\"resumeAfterComment('"+image+"')\",15);",7500);

	}

}

function resumeAfterComment(image) {
	
	fadeCommentOpacity += 2;

	document.getElementById('imagecomment').style.opacity = ((100-fadeCommentOpacity)/100);
	document.getElementById('imagecomment').style.MozOpacity = ((100-fadeCommentOpacity)/100);
	document.getElementById('imagecomment').style.filter = "alpha(opacity="+(100-fadeCommentOpacity)+")";

	for (x1 = 1; x1 <= 4; x1++) {
	
		for (y1 = 1; y1 <= 3; y1++) {

			if (GetXY(layout,x1,y1) != 0) {

				if ((1*image.substr(image.length-9,5)) != (1*GetXY(layout,x1,y1))) {
				
					document.getElementById('grid'+x1+y1).style.opacity = (fadeCommentOpacity/100);
					document.getElementById('grid'+x1+y1).style.MozOpacity = (fadeCommentOpacity/100);
					document.getElementById('grid'+x1+y1).style.filter = "alpha(opacity="+fadeCommentOpacity+")";

				}
				
			}

		}

	}

	if (fadeCommentOpacity >= 100) { 

		clearInterval(fadeCommentInterval); 

		document.getElementById('imagecomment').style.display = 'none';
		document.getElementById('imagecomment').style.opacity = 1;
		document.getElementById('imagecomment').style.MozOpacity = 1;
		document.getElementById('imagecomment').style.filter = "alpha(opacity=100)";
	
	}

	wait = 2;
	
}

function goToUrl() {
	
	window.location.href = "redirect.php?key=" + key;	

}
	
