var anima = [];
var res = []
var hMin = 813;
	
function screenRes() {
	var winWidth = 0; var winHeight = 0;
	
	if( typeof(window.innerWidth) == 'number') {
		// IE9 + todo os outros browsers.
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		// IE6 a 8.
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		// IE4 ou compativel.
		winWidth = document.body.clientWidth;
		winHeight = document.body.clientHeight;
	}
	res.push(winWidth,winHeight);
	
	return 1;
}

function animaOuNao() {
	var animation = false, animationstring = 'animation', keyframeprefix = '',  
    domPrefixes = 'Webkit Moz O ms Khtml'.split(' '),  pfx  = '';  
  
	if( animation === false ) {  
		for( var i = 0; i < domPrefixes.length; i++ ) {  	
				pfx = domPrefixes[ i ];  
				animationstring = pfx + 'Animation';  
				keyframeprefix = '-' + pfx.toLowerCase() + '-';  
				anima.push("sim");  
				break;  
		}  
	}  
}

function fazLoop() {
	var video = document.getElementById("myVideo");
	video.play();
}

function PlayPause() {
	var video = document.getElementById("myVideo");
	var flashV = document.getElementById("flashVideo");
	var btLink = document.getElementById("playPause");

	var elem = document.createElement('canvas');
		
	if(elem.getContext && elem.getContext('2d')) {
		if(video.paused) {
			video.play(); btLink.innerHTML = "PAUSE"; btLink.className = "boldNeg";
		} else {
			video.pause(); btLink.innerHTML = "PLAY"; btLink.className = "bold";
		}
	} else {
		flashV.SetVariable('ordem', btLink.innerHTML);
		
		if (btLink.innerHTML == "PLAY") {
			btLink.innerHTML = "PAUSE"; 
		} else {
			btLink.innerHTML = "PLAY"; 
		}
	} 
}

function resizeVideo() {
	var videoX = 720; var videoY = 432; // rácio de 1,66666667

	
	if(screenRes()) {
		var winWidth = res[0]; var winHeight = res[1];

		if(winWidth <= 240 || winWidth <= 320) {
			document.getElementById("caixa").style.display = "none";
			document.getElementById("playPauseMenu").style.display = "none";
		} else {
		if(winHeight > hMin)	{
			// SE A ALTURA DA JANELA FOR MAIOR QUE A DO VIDEO:
			// - se as proporções forem inversas às do video:
			if((winWidth / winHeight) < (videoX/videoY)) {
				document.getElementById("myVideo").style.height = winHeight + "px";
				document.getElementById("myVideo").style.width = winHeight * (videoX/videoY) + "px";
				document.getElementById("grelha").style.height = winHeight + "px";
				document.getElementById("grelha").style.width = winHeight * (videoX/videoY) + "px";
				document.getElementById("filtro").style.height = winHeight + "px";
				document.getElementById("filtro").style.width = winHeight * (videoX/videoY) + "px";
				document.getElementById("flashVideo").style.height = winHeight + "px";
				document.getElementById("flashVideo").setAttribute("height", winHeight + "px" )
				document.getElementById("flashVideo").style.width = winHeight * (videoX/videoY) + "px";
				document.getElementById("flashVideo").setAttribute("width", winHeight * (videoX/videoY) + "px");
			} else {
				// ..caso contrário:
				document.getElementById("myVideo").style.height = winWidth * (videoY/videoX) + "px";
				document.getElementById("myVideo").style.width = winWidth + "px";
				document.getElementById("grelha").style.height = winWidth * (videoY/videoX) + "px";
				document.getElementById("grelha").style.width = winWidth + "px";
				document.getElementById("filtro").style.height = winWidth * (videoY/videoX) + "px";
				document.getElementById("filtro").style.width = winWidth + "px";
				document.getElementById("flashVideo").style.height = winWidth * (videoY/videoX) + "px";
				document.getElementById("flashVideo").style.width = winWidth + "px"; 
			}	
		} else {
			// SE A ALTURA DA JANELA FOR MENOR QUE A DO VIDEO:
				document.getElementById("myVideo").style.height = hMin + "px";
				document.getElementById("myVideo").style.width = hMin * (videoX/videoY) + "px";
				document.getElementById("grelha").style.height = hMin + "px";
				document.getElementById("grelha").style.width = hMin * (videoX/videoY) + "px";
				document.getElementById("filtro").style.height = hMin + "px";
				document.getElementById("filtro").style.width = hMin * (videoX/videoY) + "px";				
				document.getElementById("flashVideo").style.height = hMin + "px";
				document.getElementById("flashVideo").setAttribute("height", hMin + "px" )
				document.getElementById("flashVideo").style.width = hMin * (videoX/videoY) + "px";
				document.getElementById("flashVideo").setAttribute("width", hMin * (videoX/videoY) + "px");			
		} 
        } 
	}
	res.pop(); res.pop();
}

function goURL(morada) {
	document.location.assign(morada);
}

function mudaLingua() {
	if(document.getElementById("lanClick").innerHTML == "ENGLISH") {
		goURL("index_en.html");
	} else {
		goURL("index.html");
	}
}

function goURLwork() {

	if(document.title == "SYNTAXE // Design de Comunicação") {
		document.location.assign("syntaxe_work.html");
	} else {
		document.location.assign("syntaxe_work_en.html");
	}

}

function mostraFundo() {
	var fundo = document.getElementById("fundo_pLer")
	var estado = fundo.className;
 
	if (estado == "fundo_pLer") {
		fundo.className = "fundo_pLerANIM";
		if (res[1] < hMin) {
			fundo.style.height = hMin + "px";
		//	alert(res[1]);
		} 
	} else {
		fundo.className = "fundo_pLer";
	}
	
}

window.onload = function () {
	var video = document.getElementById("myVideo");
	var browser = navigator.userAgent;

	screenRes();
	/* Força o loop para html5 video, já que alguns browsers não o fazem. */
	if(video) {
		if(video.addEventListener) {
			video.addEventListener('ended', fazLoop, false);
		} else if (video.attachEvent) {
			video.attachEvent('ended', fazLoop);
		}
	}
	
	resizeVideo();

	document.getElementById("playPause").onclick = PlayPause;
	document.getElementById("lanClick").onclick = mudaLingua;
	document.getElementById("sobreClick").onclick = mostraFundo;

	window.onresize = function() { resizeVideo(); };
	animaOuNao()
};
