/*
Criado por: Oscar G. Neto
Data: 05/04/2010
Obs: midiaBox v.1 Browsers: Internet Explorer 5,6,7 e 8
	                        Mozilla Firefox 3.5
				            Google Chrome 4.1
				            Safari 4.0
				            Opera 10.10

Alterado por: 
Data: 00/00/0000
Obs: 
*/

/*function preload(images) {
	if (document.images) {
		var i = 0;
		var imageArray = new Array();
		imageArray = images.split(',');
		var imageObj = new Image();
		for(i=0; i<=imageArray.length-1; i++) {
			imageObj.src=imageArray[i];
			alert('url: '+ imageArray[i] +'\nwidth: '+ imageObj.width +'\nheight: '+ imageObj.height);
		}
	}
}
preload('http://cientistasdoamanha.br.dev.sangari.com/multimidia_center/arquivoteca/103.jpg,http://cientistasdoamanha.br.dev.sangari.com/multimidia_center/arquivoteca/105.jpg,http://cientistasdoamanha.br.dev.sangari.com/multimidia_center/arquivoteca/101.jpg,http://cientistasdoamanha.br.dev.sangari.com/multimidia_center/arquivoteca/102.jpg');
*/

/*var imgPreloader = function(imgArr){
	var imgObj = new Image();
	imgArr.forEach(function(elm){
		imgObj.src = elm;
	});
}

if (!Array.prototype.forEach) {
	Array.prototype.forEach = function(fn){
		if (imgArr.constructor.toString().indexOf("Array") != -1){ // Checando se é um array
			for ( var i = 0; i < this.length; i++ ) {
				fn( this[i] );
			}
		}
	};
}

var imagens = new Array('http://cientistasdoamanha.br.dev.sangari.com/multimidia_center/arquivoteca/103.jpg,http://cientistasdoamanha.br.dev.sangari.com/multimidia_center/arquivoteca/105.jpg,http://cientistasdoamanha.br.dev.sangari.com/multimidia_center/arquivoteca/101.jpg,http://cientistasdoamanha.br.dev.sangari.com/multimidia_center/arquivoteca/102.jpg');
imgPreloader(imagens);

for(x in anchor){
	alert(x+': '+anchor[x]);
}

for(x in anchor['attributes']){
	alert(x+': '+anchor['attributes'][x]);
}

*/


var mbox = {
	GetXMLHttp: function ()
	{
		//alert('Start: GetXMLHttp');
		if(navigator.appName == "Microsoft Internet Explorer") {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else {
			xmlHttp = new XMLHttpRequest();
		}
		return xmlHttp;
	},
	
	addLoadEvent: function(func)
	{	
		var oldonload = window.onload;
		if (typeof window.onload != 'function')
		{
			window.onload = func;
		} 
		else
		{
			window.onload = function()
			{
				oldonload();
				func();
			}
		}
	
	},

	initMidiabox: function()
	{	
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName("a");
	
		// Loop de todas as âncoras
		for (var i=0; i<anchors.length; i++)
		{
			var anchor = anchors[i];
	
			if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "midiabox"))
			{			
				if (document.images)
				{
				  preload_image = new Image(5,5); 
				  preload_image.src=anchor.getAttribute("href"); 
				}
				
				anchor.onclick = function () {mbox.showMidiabox(this,0); return false;}
			}
		}
		
		var objBody = document.getElementsByTagName("body").item(0);
		
		// Criando overlay da página
		var objEscurecer = document.createElement("div");
		objEscurecer.setAttribute('id','overlay');
		objEscurecer.style.position = 'absolute';
		objEscurecer.onclick = function () {mbox.hideMidiabox(); return false;}
		objEscurecer.style.display = 'none';
		objEscurecer.style.zIndex = '1000';
		objBody.insertBefore(objEscurecer, objBody.firstChild);	
			
		var imgPreloader = new Image();
		
		// Criando o loading
		imgPreloader.onload=function(){
	
			var objLoadingImageLink = document.createElement("a");
			objLoadingImageLink.setAttribute('href','#');
			objLoadingImageLink.onclick = function () {mbox.hideMidiabox(); return false;}
			objEscurecer.appendChild(objLoadingImageLink);
	
			var objLoadingImage = document.createElement("img");
			objLoadingImage.src = loadingImage;
			objLoadingImage.setAttribute('id','loadingImage');
			objLoadingImage.style.position = 'absolute';
			objLoadingImage.style.zIndex = '1001';
			objLoadingImage.style.border = '0';
			objLoadingImageLink.appendChild(objLoadingImage);
	
			imgPreloader.onload=function(){};
			return false;
		}
		imgPreloader.src = loadingImage;
		
		// Criando o box da página
		var objBox = document.createElement("div");
		objBox.setAttribute('id','boxmidia');
		//objBox.style.position = 'absolute';
		objBox.style.display = 'none';
		objBox.style.zIndex = '1001';	
		objBody.insertBefore(objBox, objEscurecer.nextSibling);

		// Criando o toolbar
		var objMidiaboxToolbar = document.createElement("div");
		objMidiaboxToolbar.setAttribute('id','midiaboxToolbar');
		objMidiaboxToolbar.onclick = function () {mbox.hideMidiabox(); return false;}
		objMidiaboxToolbar.style.position = 'relative';
		objMidiaboxToolbar.style.float = 'left';
		objMidiaboxToolbar.style.display = 'none';
		objMidiaboxToolbar.style.zIndex = '1002';	
		objBox.appendChild(objMidiaboxToolbar);

		// Criando o title
		var objMidiaboxTitle = document.createElement("div");
		objMidiaboxTitle.setAttribute('id','midiaboxTitle');
		objMidiaboxTitle.onclick = function () {mbox.hideMidiabox(); return false;}
		objMidiaboxTitle.style.position = 'relative';
		objMidiaboxTitle.style.float = 'left';
		objMidiaboxTitle.style.display = 'none';
		objMidiaboxTitle.style.zIndex = '1001';	
		objBox.appendChild(objMidiaboxTitle);

		// Criando o midiabox da página
		var objMidiabox = document.createElement("div");
		objMidiabox.setAttribute('id','midiabox');
		objMidiabox.style.position = 'relative';
		objMidiabox.style.float = 'left';
		objMidiabox.style.display = 'none';
		objMidiabox.style.zIndex = '1001';	
		//objBody.insertBefore(objMidiabox, objEscurecer.nextSibling);
		objBox.appendChild(objMidiabox);
			
		// Criando a legenda
		var objMidiaboxCaption = document.createElement("div");
		objMidiaboxCaption.setAttribute('id','midiaboxCaption');
		objMidiaboxCaption.style.position = 'relative';
		objMidiaboxCaption.onclick = function () {mbox.hideMidiabox(); return false;}
		objMidiaboxCaption.style.float = 'left';
		objMidiaboxCaption.style.display = 'none';
		objMidiaboxCaption.style.zIndex = '1001';	
		//objBody.insertBefore(objMidiaboxCaption, objEscurecer.nextSibling);
		objBox.appendChild(objMidiaboxCaption);
		
		// Criando link do botão fechar no box
		var objCloseLink = document.createElement("a");
		objCloseLink.setAttribute('id','closeLink');
		objCloseLink.setAttribute('href','#');
		objCloseLink.setAttribute('title','');
		objCloseLink.onclick = function () {mbox.hideMidiabox(); return false;}
		objMidiaboxToolbar.appendChild(objCloseLink);

		// Carregando a imagem close buttom no box
		var imgPreloadCloseButton = new Image();
	
		imgPreloadCloseButton.onload=function(){
	
			// Criando o botão fechar no box
			var objCloseButton = document.createElement("img");
			objCloseButton.src = closeButton;
			objCloseButton.setAttribute('id','closeButton');
			objCloseButton.style.position = 'absolute';
			objCloseButton.style.display = 'none';
			objCloseButton.style.zIndex = '1002';
			objCloseButton.style.border = '0';
			objCloseLink.appendChild(objCloseButton);
	
			return false;
		}
		imgPreloadCloseButton.src = closeButton;			
		
		// Criando link na imagem
		/*try 
		{ 
			var objLinkRemove = document.getElementById('closedLink');
			objMidiabox.removeChild(objLinkRemove);
		}
		catch(err)
		{
			// Faz nada
		}*/
		
		// Criando link do botão fechar na imagem
		var objClosedLink = document.createElement("a");
		objClosedLink.setAttribute('id','closedLink');
		objClosedLink.setAttribute('href','#');
		objClosedLink.setAttribute('title','');
		objClosedLink.onclick = function () {mbox.hideMidiabox(); return false;}
		objMidiabox.appendChild(objClosedLink);

		// Carregando a imagem close buttom na imagem
		var imgPreloadClosedButton = new Image();
	
		imgPreloadClosedButton.onload=function(){
	
			// Criando o botão fechar da imagem
			var objClosedButton = document.createElement("img");
			objClosedButton.src = closedButton;
			objClosedButton.setAttribute('id','closedButton');
			objClosedButton.style.position = 'absolute';
			objClosedButton.style.display = 'none';
			objClosedButton.style.zIndex = '1002';
			objClosedButton.style.border = '0';
			objClosedLink.appendChild(objClosedButton);
			
			return false;
		}
		imgPreloadClosedButton.src = closedButton;	
		
		// Criando o objeto img
		var objImage = document.createElement("img");
		objImage.setAttribute('id','midiaboxImage');
		objMidiabox.appendChild(objImage);
		
		itemResize = 0;
	},

	showMidiabox: function(objLink,resize){
		//alert(objLink);

		//if (navigator.appVersion.indexOf("MSIE")!=-1){
			//mbox.pause(250);
		//} 

		//scroll(0,0);
		clearInterval(mbox.INTERVAL);
		
		var clientWidth = mbox.getWidth().toString();
		var clientHeight = mbox.getHeight().toString();
		
		midiaboxTop = 0;
		midiaboxLeft = 0;
		midiaboxWidth = 0;
		midiaboxHeight = 0;
		
		viewBox = true;
		
		if (resize == 0)
		{
			attributeHref = objLink.href;
			attributeTitle = objLink.getAttribute('title');
			attributeCaption = objLink.getAttribute('caption');
			attributeWidth = objLink.getAttribute('width');
			attributeHeight = objLink.getAttribute('height');
			
			/*alert(attributeHref);
			alert(attributeTitle);
			alert(attributeCaption);
			alert(attributeWidth);
			alert(attributeHeight);*/
		}
		else
		{
			attributeHref = objLink.href;
			attributeTitle = objLink.title;
			attributeCaption = objLink.caption;
			attributeWidth = objLink.width;
			attributeHeight = objLink.height;
			
			/*alert(attributeHref);
			alert(attributeTitle);
			alert(attributeCaption);
			alert(attributeWidth);
			alert(attributeHeight);*/
		}
		
		/*xmlRequest.open("GET",attributeHref,true);    
		xmlRequest.onreadystatechange = mbox.ajaxStatus;
		xmlRequest.send(null);*/
		
		itemResize = 1;
		
		// get objeto habilitando o overlay
		var objEscurecer = document.getElementById('overlay');
		objEscurecer.style.display = 'block';
			
		try 
		{ 
		// get objeto habilitando o loading
		var objLoadingImage = document.getElementById('loadingImage');
		objLoadingImage.style.display = 'block';
		}
		catch(err)
		{
			// Faz nada
		}
		
		// get objeto habilitando o box
		var objBox = document.getElementById('boxmidia');
		
		// get objeto habilitando o midiabox
		var objMidiabox = document.getElementById('midiabox');
				
		// Centralizando o loading
		if (objLoadingImage) {
			objLoadingImage.style.top = parseInt((clientHeight - objLoadingImage.height)/2) + 'px';
			objLoadingImage.style.left = parseInt((clientWidth - objLoadingImage.width)/2) + 'px';
			objLoadingImage.style.display = 'block';
		}
		
		// get objeto habilitando o midiaboxToolbar
		var objMidiaboxToolbar = document.getElementById('midiaboxToolbar');
		
		// get objeto habilitando title
		var objMidiaboxTitle = document.getElementById('midiaboxTitle');
		
		// get objeto habilitando caption
		var objMidiaboxCaption = document.getElementById('midiaboxCaption');
	
		// get objeto habilitando o botão close do box
		var objCloseButton = document.getElementById('closeButton');
	
		// get objeto habilitando o botão close da imagem
		var objClosedButton = document.getElementById('closedButton');
		
		
		//Remove objetos
		// Removendo e criando a imagem no link dentro do midiabox
		/*try 
		{ 
			var objImageRemove = document.getElementById('midiaboxImage');
			objMidiabox.removeChild(objImageRemove);
		}
		catch(err)
		{
			// Faz nada
		}*/
		
		// Removendo o objeto movie no midiabox, caso ela tenha sido criado
		try 
		{ 
			var objImageRemove = document.getElementById('movie');
			objMidiabox.removeChild(objImageRemove);
		}
		catch(err)
		{
			// Faz nada
		}
				
		// Removendo o objetoiframe no midiabox, caso ela tenha sido criado
		try 
		{ 
			var objImageRemove = document.getElementById('ipage');
			objMidiabox.removeChild(objImageRemove);
		}
		catch(err)
		{
			// Faz nada
		}

									
		// Recupera extenção
		var wholeString = attributeHref.toString();
		var parsedObject = mbox.xtractFile(wholeString);
		var path = parsedObject.path;
		var file = parsedObject.file;
		var extension = parsedObject.extension;
		
		/*alert(extension);
		extensao = file.split(".");
		_extensao = extensao[1];
		alert(_extensao);
		
		if(_extensao!=undefined)
		{
			_extensao2 = _extensao.split("?",1);
			var extension = "."+ _extensao2.toString();
		}
		else
			var extension = parsedObject.extension;*/
			
		
		// Válida extenção
		var validExtensions = new Array(".jpg",".png",".jpeg",".gif",".bmp",".swf",".flv",".mp3",".doc",".docx",".xls",".xlsx",".ppt",".pptx",".pdf",".txt",".wmv",".avi",".html",".htm",".dhtml",".shtml",".cfm",".asp",".aspx",".php",".jsp",".all");
		
		var countExt = 0;
		for (var i = 0; i < validExtensions.length; i++) 
		{
			// Verificar se a extensão é apropriada
			if (validExtensions[i] == extension) 
				countExt ++;
		}
		
		execute=true;
		if (countExt == 0)
		{
			objEscurecer.style.display = 'none';
			objMidiabox.style.display = 'none';
			objMidiaboxToolbar.style.display = 'none';
			objMidiaboxTitle.style.display = 'none';
			objMidiaboxCaption.style.display = 'none';
			//alert('Extensão não configurada no midiabox');
			execute=false;
			
			objNewLink = { href: "http://noticias.sangari.com/images/midiabox/error.jpg", title: "Erro", caption: "", width: "400", height: "200" };
			mbox.showMidiabox(objNewLink,1);
		}
			
		if (execute==true)
		{
			// Montamos o midiabox de acordo com o tipo
			switch(extension) {
				case ".jpg":
				case ".png":
				case ".jpeg":
				case ".gif":
				case ".bmp":
					//alert('Debug: .jpg,.png,.jpeg,.gif ou .bmp');
					
					var objImage = document.getElementById('midiaboxImage');
					objImage.style.display = 'block';
					
					var objLoadingImage = document.getElementById('loadingImage');
					objLoadingImage.style.display = 'block';
									
					// Carregando a imagem
					imgPreload = new Image();
						
					imgPreload.onload=function()
					{
						objImage.src = attributeHref;	
			
						return false;
					}
					imgPreload.src = attributeHref;
					
					// Verifica se existe um largura e altura fixo setado no href
					if (attributeWidth != null && attributeHeight != null){
						imgPreloadWidth = attributeWidth;
						imgPreloadHeight = attributeHeight;
						clientSizeWidth = imgPreloadWidth;
						clientSizeHeight = imgPreloadHeight;
					}
					else
					{
						imgPreloadWidth = imgPreload.width;
						imgPreloadHeight = imgPreload.height;
						clientSizeWidth = Math.abs(parseInt(clientWidth - 100));
						clientSizeHeight =Math.abs( parseInt(clientHeight - 100));
					}
								
					// Resize da imagem	
					OriginalWidth = imgPreloadWidth;
					OriginalHeight = imgPreloadHeight;
				
					// Só faz o resize da imagem se a largura da imagem for maior ou igual que a largura do browser ou se a altura da imagem for maior ou igual que a altura do browser
					if (OriginalWidth >= clientSizeWidth || OriginalHeight >= clientSizeHeight)
						if (OriginalWidth > OriginalHeight)
						{
							setNewWidth = clientSizeWidth;
							setNewHeight = parseInt((OriginalHeight / OriginalWidth ) * clientSizeWidth);
							
							// Se o height da imagem for maior que o height do browser refaz o resize
							if (setNewHeight > clientSizeHeight)
							{
								setNewHeight = clientSizeHeight;
								setNewWidth = parseInt((OriginalWidth / OriginalHeight) * clientSizeHeight);
							}
						}
						else
						{
							setNewHeight = clientSizeHeight;
							setNewWidth = parseInt((OriginalWidth / OriginalHeight) * clientSizeHeight);
							
							// Se o width da imagem for maior que o width do browser refaz o resize
							if (setNewWidth > clientSizeWidth)
							{
								setNewWidth = clientSizeWidth;
								setNewHeight = parseInt((OriginalHeight / OriginalWidth ) * clientSizeWidth);
							}
						}
					else
					{
						setNewWidth = imgPreloadWidth;
						setNewHeight = imgPreloadHeight;	
					}
																	
					// Setando o resize
					objImage.width = setNewWidth;
					objImage.height = setNewHeight;
								
					objCloseButton.style.display = 'block';
					objClosedButton.style.display = 'none';
					
					if (objLoadingImage) {objLoadingImage.style.display = 'none';}
					
					clearInterval(mbox.INTERVAL);  
				break;
		
				case ".flv":
				case ".swf":
				case ".mp3":
					//alert('Debug: .flv,.swf');
					
					var objImage = document.getElementById('midiaboxImage');
					objImage.style.display = 'none';
					
					var objLoadingImage = document.getElementById('loadingImage');
					objLoadingImage.style.display = 'block';
					
					// Resize do player	
					OriginalWidth = attributeWidth;
					OriginalHeight = attributeHeight;
					
					if((OriginalWidth == null || OriginalHeight == null) || (OriginalWidth == 0 || OriginalHeight == 0))
					{
						if(extension==".flv" || extension==".swf")
						{
							OriginalWidth = 500;
							OriginalHeight = 400;
						}
						else
						{
							OriginalWidth = 300;
							OriginalHeight = 40;
						}
					}
					
					/*if (extension==".flv")
						var videoPlayer = "player/Flvplayer.swf?src=";
					else if (extension==".mp3")
						var videoPlayer = "player/Mp3player.swf?src=";
					else
						var videoPlayer = "";*/
						
					var videoPlayer = "";
					
					
					clientSizeWidth =Math.abs( parseInt(clientWidth - 100));
					clientSizeHeight = Math.abs(parseInt(clientHeight - 100));
				
					// Só faz o resize do player se a largura do player for maior ou igual que a largura do browser ou se a altura do player for maior ou igual que a altura do browser
					if (OriginalWidth >= clientSizeWidth || OriginalHeight >= clientSizeHeight)
						if (OriginalWidth > OriginalHeight)
						{
							setNewWidth = clientSizeWidth;
							setNewHeight = parseInt((OriginalHeight / OriginalWidth ) * clientSizeWidth);
							
							// Se o height da imagem for maior que o height do browser refaz o resize
							if (setNewHeight > clientSizeHeight)
							{
								setNewHeight = clientSizeHeight;
								setNewWidth = parseInt((OriginalWidth / OriginalHeight) * clientSizeHeight);
							}
						}
						else
						{
							setNewHeight = clientSizeHeight;
							setNewWidth = parseInt((OriginalWidth / OriginalHeight) * clientSizeHeight);
							
							// Se o width da imagem for maior que o width do browser refaz o resize
							if (setNewWidth > clientSizeWidth)
							{
								setNewWidth = clientSizeWidth;
								setNewHeight = parseInt((OriginalHeight / OriginalWidth ) * clientSizeWidth);
							}
						}
					else
					{
						setNewWidth = OriginalWidth;
						setNewHeight = OriginalHeight;	
					}
				
					// Removendo o objeto movie no midiabox, caso ela tenha sido criado
					try 
					{ 
						var objImageRemove = document.getElementById('movie');
						objMidiabox.removeChild(objImageRemove);
					}
					catch(err)
					{
						// Faz nada
					}
	
					var objMovie = document.createElement("div");
					objMovie.setAttribute('id','movie');
					objMovie.style.zIndex = '1001';
					objMovie.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="'+setNewWidth+'" height="'+setNewHeight+'"><param name="movie" value="'+ videoPlayer + attributeHref+'" /><param name="quality" value="high" /><embed src="'+ videoPlayer + attributeHref+'" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="'+setNewWidth+'" height="'+setNewHeight+'" allowFullScreen="true"></embed></object>';
					objMidiabox.appendChild(objMovie);
						
					setNewWidth = setNewWidth;
					setNewHeight = setNewHeight;	
					
					objCloseButton.style.display = 'block';
					objClosedButton.style.display = 'none';
					
					if (objLoadingImage) {objLoadingImage.style.display = 'none';}
					
					clearInterval(mbox.INTERVAL);  
				break;
		
				case ".doc":
				case ".docx":	
					//alert('Debug: .doc,.docx');
				
					scroll(0,0);
					
					var objImage = document.getElementById('midiaboxImage');
					objImage.style.display = 'none';
					
					var objLoadingImage = document.getElementById('loadingImage');
					objLoadingImage.style.display = 'block';
					
					var clientWidth = mbox.getWidth().toString();
					var clientHeight = mbox.getHeight().toString();
								
					// Resize
					OriginalWidth = attributeWidth;
					OriginalHeight = attributeHeight;
					clientSizeWidth =Math.abs( parseInt(clientWidth - 100));
					clientSizeHeight = Math.abs(parseInt(clientHeight - 100));
	
					setNewWidth = OriginalWidth;
					setNewHeight = OriginalHeight;
					
					viewBox = false;
					
					//mbox.INTERVAL = setTimeout('mbox.rBrowser("'+attributeHref+'")',1500); 
					clearInterval(mbox.INTERVAL);  
					mbox.INTERVAL = setInterval('mbox.rBrowser("'+attributeHref+'")',1500); 
				break;
	
				case ".xls":
				case ".xlsx":
					//alert('Debug: .xls,.xlsx');
				
					scroll(0,0);
								
					var objImage = document.getElementById('midiaboxImage');
					objImage.style.display = 'none';
					
					var objLoadingImage = document.getElementById('loadingImage');
					objLoadingImage.style.display = 'block';
	
					var clientWidth = mbox.getWidth().toString();
					var clientHeight = mbox.getHeight().toString();
				
					// Resize
					OriginalWidth = attributeWidth;
					OriginalHeight = attributeHeight;
					clientSizeWidth =Math.abs( parseInt(clientWidth - 100));
					clientSizeHeight = Math.abs(parseInt(clientHeight - 100));
	
					setNewWidth = OriginalWidth;
					setNewHeight = OriginalHeight;
					
					viewBox = false;
					
					//mbox.INTERVAL = setTimeout('mbox.rBrowser("'+attributeHref+'")',1500); 
					clearInterval(mbox.INTERVAL);  
					mbox.INTERVAL = setInterval('mbox.rBrowser("'+attributeHref+'")',1500); 
				break;
	
				case ".ppt":
				case ".pptx":
					//alert('Debug: .ppt,.pptx');
					
					scroll(0,0);
								
					var objImage = document.getElementById('midiaboxImage');
					objImage.style.display = 'none';
					
					var objLoadingImage = document.getElementById('loadingImage');
					objLoadingImage.style.display = 'block';
				
					var clientWidth = mbox.getWidth().toString();
					var clientHeight = mbox.getHeight().toString();
					
					// Resize
					OriginalWidth = attributeWidth;
					OriginalHeight = attributeHeight;
					clientSizeWidth =Math.abs( parseInt(clientWidth - 100));
					clientSizeHeight = Math.abs(parseInt(clientHeight - 100));
	
					setNewWidth = OriginalWidth;
					setNewHeight = OriginalHeight;
					
					viewBox = false;
					
					//mbox.INTERVAL = setTimeout('mbox.rBrowser("'+attributeHref+'")',1500); 
					clearInterval(mbox.INTERVAL);  
					mbox.INTERVAL = setInterval('mbox.rBrowser("'+attributeHref+'")',1500); 
				break;
				
				case ".pdf":
					//alert('Debug: .pdf');
					
					scroll(0,0);
					
					var objImage = document.getElementById('midiaboxImage');
					objImage.style.display = 'none';
					
					var objLoadingImage = document.getElementById('loadingImage');
					objLoadingImage.style.display = 'block';
				
					var clientWidth = mbox.getWidth().toString();
					var clientHeight = mbox.getHeight().toString();
								
					// Resize
					OriginalWidth = attributeWidth;
					OriginalHeight = attributeHeight;
					clientSizeWidth =Math.abs( parseInt(clientWidth - 100));
					clientSizeHeight = Math.abs(parseInt(clientHeight - 100));
	
					setNewWidth = OriginalWidth;
					setNewHeight = OriginalHeight;
					
					viewBox = false;
					
					//mbox.INTERVAL = setTimeout('mbox.rBrowser("'+attributeHref+'")',1500); 
					clearInterval(mbox.INTERVAL);  
					mbox.INTERVAL = setInterval('mbox.rBrowser("'+attributeHref+'")',1500); 
				break;
	
				case ".txt":
					//alert('Debug: .txt');
				
					scroll(0,0);
					
					var objImage = document.getElementById('midiaboxImage');
					objImage.style.display = 'none';
					
					var objLoadingImage = document.getElementById('loadingImage');
					objLoadingImage.style.display = 'block';
					
					var clientWidth = mbox.getWidth().toString();
					var clientHeight = mbox.getHeight().toString();
								
					// Resize
					OriginalWidth = attributeWidth;
					OriginalHeight = attributeHeight;
					clientSizeWidth =Math.abs( parseInt(clientWidth - 100));
					clientSizeHeight = Math.abs(parseInt(clientHeight - 100));
	
					setNewWidth = OriginalWidth;
					setNewHeight = OriginalHeight;
					
					viewBox = false;
					
					//mbox.INTERVAL = setTimeout('mbox.rBrowser("'+attributeHref+'")',1500); 
					clearInterval(mbox.INTERVAL);  
					mbox.INTERVAL = setInterval('mbox.rBrowser("'+attributeHref+'")',1500); 
				break;
				
				case ".wmv":
					//alert('Debug: .wmv');
				
					scroll(0,0);
					
					var objImage = document.getElementById('midiaboxImage');
					objImage.style.display = 'none';
					
					var objLoadingImage = document.getElementById('loadingImage');
					objLoadingImage.style.display = 'block';
					
					var clientWidth = mbox.getWidth().toString();
					var clientHeight = mbox.getHeight().toString();
								
					// Resize
					OriginalWidth = attributeWidth;
					OriginalHeight = attributeHeight;
					clientSizeWidth =Math.abs( parseInt(clientWidth - 100));
					clientSizeHeight = Math.abs(parseInt(clientHeight - 100));
	
					setNewWidth = OriginalWidth;
					setNewHeight = OriginalHeight;
					
					viewBox = false;
					
					//mbox.INTERVAL = setTimeout('mbox.rBrowser("'+attributeHref+'")',1500); 
					clearInterval(mbox.INTERVAL);  
					mbox.INTERVAL = setInterval('mbox.rBrowser("'+attributeHref+'")',1500); 
				break;
				
				case ".avi":
					//alert('Debug: .avi');
					
					scroll(0,0);
					
					var objImage = document.getElementById('midiaboxImage');
					objImage.style.display = 'none';
					
					var objLoadingImage = document.getElementById('loadingImage');
					objLoadingImage.style.display = 'block';
					
					var clientWidth = mbox.getWidth().toString();
					var clientHeight = mbox.getHeight().toString();
									
					// Resize
					OriginalWidth = attributeWidth;
					OriginalHeight = attributeHeight;
					clientSizeWidth =Math.abs( parseInt(clientWidth - 100));
					clientSizeHeight = Math.abs(parseInt(clientHeight - 100));
	
					setNewWidth = OriginalWidth;
					setNewHeight = OriginalHeight;
					
					viewBox = false;
					
					//mbox.INTERVAL = setTimeout('mbox.rBrowser("'+attributeHref+'")',1500); 
					clearInterval(mbox.INTERVAL);  
					mbox.INTERVAL = setInterval('mbox.rBrowser("'+attributeHref+'")',1500); 
				break;
				
				case ".html":
				case ".htm":
				case ".dhtml":
				case ".shtml":
				case ".cfm":
				case ".asp":
				case ".aspx":
				case ".php":
				case ".jsp":
				case ".all":
					//alert('Debug: .html,.htm,.dhtml,.shtml,.cfm,.asp,.aspx,.php,.jsp,.all');
					
					var objImage = document.getElementById('midiaboxImage');
					objImage.style.display = 'none';
					
					try 
					{ 
						var objLoadingImage = document.getElementById('loadingImage');
						objLoadingImage.style.display = 'block';
					}
					catch(err)
					{
						// Faz nada
					}
					
					// Resize do player	
					OriginalWidth = attributeWidth;
					OriginalHeight = attributeHeight;
					clientSizeWidth =Math.abs( parseInt(clientWidth - 100));
					clientSizeHeight = Math.abs(parseInt(clientHeight - 100));
				
					// Só faz o resize do player se a largura do player for maior ou igual que a largura do browser ou se a altura do player for maior ou igual que a altura do browser
					if (OriginalWidth >= clientSizeWidth || OriginalHeight >= clientSizeHeight)
						if (OriginalWidth > OriginalHeight)
						{
							setNewWidth = clientSizeWidth;
							setNewHeight = parseInt((OriginalHeight / OriginalWidth ) * clientSizeWidth);
							
							// Se o height da imagem for maior que o height do browser refaz o resize
							if (setNewHeight > clientSizeHeight)
							{
								setNewHeight = clientSizeHeight;
								setNewWidth = parseInt((OriginalWidth / OriginalHeight) * clientSizeHeight);
							}
						}
						else
						{
							setNewHeight = clientSizeHeight;
							setNewWidth = parseInt((OriginalWidth / OriginalHeight) * clientSizeHeight);
							
							// Se o width da imagem for maior que o width do browser refaz o resize
							if (setNewWidth > clientSizeWidth)
							{
								setNewWidth = clientSizeWidth;
								setNewHeight = parseInt((OriginalHeight / OriginalWidth ) * clientSizeWidth);
							}
						}
					else
					{
						setNewWidth = OriginalWidth;
						setNewHeight = OriginalHeight;	
					}
				
					var objIFrame = document.createElement("div");
					objIFrame.setAttribute('id','ipage');
					objIFrame.style.zIndex = '1001';
					objIFrame.innerHTML = '<iframe id="ipframe" name="ipframe" src = "'+attributeHref+'" height= "'+setNewHeight+'" width = "'+setNewWidth+'" scrolling = "auto" frameborder = "0" class="ipageframe">Frame content</iframe> ';
					objMidiabox.appendChild(objIFrame);
						
					setNewWidth = setNewWidth;
					setNewHeight = setNewHeight;	
					
					try 
					{ 
					objCloseButton.style.display = 'block';
					objClosedButton.style.display = 'none';
					}
					catch(err)
					{
						// Faz nada
					}
					
					if (objLoadingImage) {objLoadingImage.style.display = 'none';}
					
					clearInterval(mbox.INTERVAL);  
				break;
			}
		
			// Setando posição, largura e altura do midiabox
			//midiaboxTop = parseInt((clientHeight - setNewWidth)/2);
			midiaboxTop = parseInt((clientHeight - setNewHeight)/2);
			midiaboxLeft = parseInt((clientWidth - setNewWidth)/2);
			midiaboxWidth = parseInt(setNewWidth);
			//midiaboxHeight = parseInt(setNewWidth);
			midiaboxHeight = parseInt(setNewHeight);		
			
			// get objeto midiabox setando posição, largura e altura midiabox
			//objMidiabox.style.top = midiaboxTop +"px";
			//objMidiabox.style.left =  midiaboxLeft +"px";
			//objMidiabox.style.width = midiaboxWidth +"px";
			//objMidiabox.style.height =  midiaboxHeight +"px";
			
			// Setando valores do midiaboxToolbar
			objMidiaboxToolbar.style.width = parseInt(midiaboxWidth +2) +'px';
			
			// Setando valores do midiaboxTitle
			objMidiaboxTitle.style.width = parseInt(midiaboxWidth -2) +'px';
			//objMidiaboxTitle.style.top =  parseInt(midiaboxTop - 30) +"px";
			//objMidiaboxTitle.style.left =  parseInt(midiaboxLeft +1) +"px";
							
			sizeMidiaboxTitleHeight = 20;
			if(attributeTitle){
				var lengthTitle = parseInt((midiaboxWidth -25) /8)
				var textTitle = attributeTitle;
				objMidiaboxTitle.style.display = 'block';
				
				if (textTitle.length > lengthTitle)
					objMidiaboxTitle.innerHTML = mbox.Left(textTitle,lengthTitle) +'...';
				else
					objMidiaboxTitle.innerHTML = textTitle;
	
			} else {
				objMidiaboxTitle.style.display = 'block';
				objMidiaboxTitle.innerHTML = '&nbsp;';
			}
			
			// Setando valores do midiaboxCaption
			objMidiaboxCaption.style.width = parseInt(midiaboxWidth +2) +'px';
			//objMidiaboxCaption.style.top =  parseInt(midiaboxHeight + 30) +"px";
			//objMidiaboxCaption.style.left =  parseInt(midiaboxLeft +1) +"px";
			
			sizeMidiaboxCaptionHeight = 10;
			if(attributeCaption){
				var lengthCaption = parseInt((midiaboxWidth-2) /5.5)
					lengthCaption = parseInt(lengthCaption + lengthCaption)
				var textCaption = attributeCaption;
				objMidiaboxCaption.style.display = 'block';
				
				if (textCaption.length > lengthCaption)
					objMidiaboxCaption.innerHTML = mbox.Left(textCaption,lengthCaption) +'...';
				else
					objMidiaboxCaption.innerHTML = textCaption;
				
			} else {
				objMidiaboxCaption.style.display = 'block';
				objMidiaboxCaption.innerHTML = '&nbsp;';
			}
	
			// Pega o tamanho da pagina
			var documento = document;
			var heightPage = Math.max(
				Math.max(documento.body.scrollHeight, documento.documentElement.scrollHeight),
				Math.max(documento.body.offsetHeight, documento.documentElement.offsetHeight),
				Math.max(documento.body.clientHeight, documento.documentElement.clientHeight)
			);
	
			// Setando novos valores para o overlay		
			objEscurecer.style.height = heightPage +'px';
			objEscurecer.style.display = 'block';
			
			// Ajustando box
			objBox.style.top = parseInt(midiaboxTop - sizeMidiaboxTitleHeight - sizeMidiaboxCaptionHeight)  +"px";
			objBox.style.left =  midiaboxLeft +"px";
			objBox.style.width = midiaboxWidth +"px";
			objBox.style.height =  midiaboxHeight +"px";
					
			// Habilitando o box
			if(viewBox)
			{
				objBox.style.display = 'block';
				objMidiabox.style.display = 'block';
				objMidiaboxToolbar.style.display = 'block';
				objMidiaboxTitle.style.display = 'block';
				objMidiaboxCaption.style.display = 'block';
			}
			else
			{
				objBox.style.display = 'none';
				objMidiabox.style.display = 'none';
				objMidiaboxToolbar.style.display = 'none';
				objMidiaboxTitle.style.display = 'none';
				objMidiaboxCaption.style.display = 'none';
			}
			
			// Desabilitando o loading
			//if (objLoadingImage) {objLoadingImage.style.display = 'none';}
				
			// close midiabox via listenKey
			mbox.listenKey();
			
			// Caso for imagem e o width ou height forem igual a zero faz um resize
			switch(extension) {
				case ".jpg":
				case ".png":
				case ".jpeg":
				case ".gif":
				case ".bmp":
					if(objImage.width == 0 || objImage.height ==0)
					{
						objBox.style.display = 'none';			
						objMidiabox.style.display = 'none';					
						objMidiaboxToolbar.style.display = 'none';					
						objMidiaboxTitle.style.display = 'none';					
						objMidiaboxCaption.style.display = 'none';					
						objLoadingImage.style.display = 'none';
						objCloseButton.style.display = 'none';					
						objClosedButton.style.display = 'none';	
						objLoadingImage.style.display = 'block';
	
						//self.setTimeout('mbox.resizeImage()',2000);
						
						clearInterval(mbox.INTERVAL);  
						mbox.INTERVAL = setInterval('mbox.resizeImage()',1500); 
					}
				break;
			}
		}
		
		//mbox.existArquivo(attributeHref);
	},
	
	hideMidiabox: function()
	{
		// get objeto desabilitando o overlay
		objEscurecer = document.getElementById('overlay');
		objEscurecer.style.display = 'none';
		
		// get objeto desabilitando o midiabox
		objBox = document.getElementById('boxmidia');
		objBox.style.display = 'none';	
		
		// get objeto desabilitando o midiabox
		objMidiabox = document.getElementById('midiabox');
		objMidiabox.style.display = 'none';	
		
		// get objeto desabilitando o midiaboxToolbar
		objMidiaboxToolbar = document.getElementById('midiaboxToolbar');
		objMidiaboxToolbar.style.display = 'none';	
		
		// get objeto desabilitando o title
		var objMidiaboxTitle = document.getElementById('midiaboxTitle');
		objMidiaboxTitle.style.display = 'none';
		
		// get objeto desabilitando o caption
		var objMidiaboxCaption = document.getElementById('midiaboxCaption');
		objMidiaboxCaption.style.display = 'none';
		
		var objImage = document.getElementById('midiaboxImage');
		objImage.style.display = 'none';
		
		// Removendo a imagem no link dentro do midiabox
		/*try 
		{ 
			var objImageRemove = document.getElementById('midiaboxImage');
			objMidiabox.removeChild(objImageRemove);
		}
		catch(err)
		{
			// Faz nada
		}*/
		
		// Removendo o objeto movie no midiabox, caso ela tenha sido criado
		try 
		{ 
			var objImageRemove = document.getElementById('movie');
			objMidiabox.removeChild(objImageRemove);
		}
		catch(err)
		{
			// Faz nada
		}
		
		itemResize = 0;
		
		clearInterval(mbox.INTERVAL);
	},
	
	getWidth: function()   
	 {   
		return window.innerWidth && window.innerWidth > 0 ? window.innerWidth : /* Non IE */
		       document.documentElement.clientWidth && document.documentElement.clientWidth > 0 ? document.documentElement.clientWidth : /* IE 6+ */
		       document.body.clientWidth && document.body.clientWidth > 0 ? document.body.clientWidth : -1; /* IE 4 */
	 },
	   
	getHeight: function()   
	 {   
		return window.innerHeight && window.innerHeight > 0 ? window.innerHeight : /* Non IE */
		       document.documentElement.clientHeight && document.documentElement.clientHeight > 0 ? document.documentElement.clientHeight : /* IE 6+ */
		       document.body.clientHeight && document.body.clientHeight > 0 ? document.body.clientHeight : -1; /* IE 4 */
	 } ,
	 
	getKey: function(e)
	{
		if (e == null) 
		{ // ie
			keycode = event.keyCode;
		} 
		else 
		{ // mozilla
			keycode = e.which;
		}
		key = String.fromCharCode(keycode).toLowerCase()
		
		/*alert(keycode);
		alert(key);*/
		
		if(key == 'x' || keycode == 0  || keycode == 8 || keycode == 13 || keycode == 27){ mbox.hideMidiabox(); }
	},

	listenKey: function()
	{
		document.onkeypress = mbox.getKey;
	},
	
	xtractFile: function(data)
	{
		var m = data.match(/(.*)\/([^\/\\]+)(\.\w+)$/);
		if (m == null)
			return {path: data, file: data, extension: '.all'};
		else
			return {path: m[1], file: m[2], extension: m[3]};
	},
	
	Left: function(str, n){
		if (n <= 0)
			return "";
		else if (n > String(str).length)
			return str;
		else
			return String(str).substring(0,n);
	},
	
	Right: function(str, n){
		if (n <= 0)
		   return "";
		else if (n > String(str).length)
		   return str;
		else {
		   var iLen = String(str).length;
		   return String(str).substring(iLen, iLen - n);
		}
	},
		
	onresize: function(){
		if (itemResize == 1)
		{
			// Recriando o objeto link
			objNewLink = { href: attributeHref, title: attributeTitle, caption: attributeCaption, width: attributeWidth, height: attributeHeight };
			mbox.showMidiabox(objNewLink,1);
		}
	},
	
	resizeImage: function () 
	{
		itemResize = 1;
		mbox.onresize();
		
		// get objeto
		var objBox = document.getElementById('boxmidia');
		objBox.style.display = 'block';
		
		// get objeto
		var objMidiabox = document.getElementById('midiabox');
		objMidiabox.style.display = 'block';
		
		// get objeto
		var objMidiaboxToolbar = document.getElementById('midiaboxToolbar');
		objMidiaboxToolbar.style.display = 'block';
		
		// get objeto
		var objMidiaboxTitle = document.getElementById('midiaboxTitle');
		objMidiaboxTitle.style.display = 'block';
		
		// get objeto
		var objMidiaboxCaption = document.getElementById('midiaboxCaption');
		objMidiaboxCaption.style.display = 'block';
		
		// get objeto
		var objLoadingImage = document.getElementById('loadingImage');
		objLoadingImage.style.display = 'block';
		
		// get objeto
		var objCloseButton = document.getElementById('closeButton');
		objCloseButton.style.display = 'block';
		
		// get objeto
		var objClosedButton = document.getElementById('closedButton');
		objClosedButton.style.display = 'none';	
	},
	
	pause: function(numberMillis) {
		var now = new Date();
		var exitTime = now.getTime() + numberMillis;
		while (true) {
			now = new Date();
			if (now.getTime() > exitTime)
				return;
		}
	},
	
	rBrowser: function(item) 
	{
		mbox.hideMidiabox();
		//top.location=item;
		window.open(item, '_blank'); 
		
		// get objeto
		var objLoadingImage = document.getElementById('loadingImage');
		objLoadingImage.style.display = 'none';
		
		return false; 
	},

	existArquivo: function(url){
		xmlRequest.open("GET",url,true);    
		xmlRequest.onreadystatechange = mbox.ajaxStatus;
		xmlRequest.send(null);

		// Quando estiver carregando...
		/*if (xmlRequest.readyState == 1)
		{
			// carregando...
		}*/
	},
	
	ajaxStatus: function(){
		// Quando estiver tudo pronto.
		if (xmlRequest.readyState == 4 || xmlRequest.status == 200)
		{
			attributeHref = attributeHref;
			attributeTitle = attributeTitle;
			attributeCaption = attributeCaption;
			attributeWidth = attributeWidth;
			attributeHeight = attributeHeight;
		}
		else
		{
			attributeHref = 'http://noticias.sangari.com/images/midiabox/error.jpg';
			attributeTitle = '';
			attributeCaption = '';
			attributeWidth = 400;
			attributeHeight = 200;
		}
	},
	
	replaceAll: function(string, token, newtoken){
		while (string.indexOf(token) != -1) {
			string = string.replace(token, newtoken);
		}
		return string;
	}
	
}
var loadingImage = 'http://noticias.sangari.com/images/midiabox/loading.gif';		
var closeButton = 'http://noticias.sangari.com/images/midiabox/closed_azul.png';	
var closedButton = 'http://noticias.sangari.com/images/midiabox/closed_azul.png';	
var moreButton = 'http://noticias.sangari.com/images/midiabox/more.png';	
var lessButton = 'http://noticias.sangari.com/images/midiabox/less.png';
var xmlRequest = mbox.GetXMLHttp();
mbox.addLoadEvent(mbox.initMidiabox);

window.onresize = function (){
	mbox.onresize();
}
