/* DOM */
function id(o){ return document.getElementById(o); }
function tag(t , f ){ if( f == undefined ) f=document;  return f.getElementsByTagName(t); }
function make(e){ return document.createElement(e); }
function add(e , p){ p.appendChild(e); }
function remove(e){ e.parentNode.removeChild(e); }

window.onload = initPage;

function initPage(){
	var c1;

	id('noticias').onclick = function(){ getURL('http://www.escueladavinci.net/noticias'); return false; }
	//id('news').onclick = function(){ getURL('http://www.escueladavinci.net/newsletter/'); return false; }
	
	var thumbs = tag('div' , id('centro'));
	
	var opciones = ['cursos','actividades','carrera','institucional'];
	for(var i in opciones){
		if( id( opciones[i] ) != undefined ){ matchCols( id( opciones[i] ) ); }
	}
//	makeModal(1);
}

function getURL(u){	window.open( u , 'target'); }

function matchCols(ob){
	var c1 = id('columna_izquierda');
	var a = Math.max( parseInt(c1.offsetHeight), parseInt(ob.offsetHeight));
	if(a > 500){ c1.style.height =  ob.style.height = a+'px'; }
}

function isIex(){
	var uA = window.navigator.userAgent;
	return ( uA.indexOf('MSIE') != -1 ) ? true : false ;
}

function makeModal(f,b){
	var show_buttons = ( b == undefined ) ? false : b ;
	var div = make('div');
	var div2 = make('div');
	var div3 = make('div');
	//agregado
	var div4 = make('div');
	var a = make('a');
		a.href= '#';
		a.innerHTML = 'cerrar';
		a.id = 'cerrar';
		div4.id = 'limpiarFlotacion'
		a.onclick = function(){ remove(div); remove(div2); return false; }
		add(a , div2 );
		add(div4 , div2);
		div.id = 'modal';
		div2.id = 'innerModal';
		div.style.width = document.documentElement.clientWidth+'px';
		div.style.height= document.documentElement.clientHeight+'px';
//		if( isIex() ){ div.style.filter = 'Alpha(opacity=50);';	}else{ div.style.opacity = 0.5;	}
		if( isIex() ){ 
			div.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=50);'; 
			div.style.zoom=1;	
		}else{ 
			div.style.opacity = 0.5;
		}
		toFixed( div , 0 );
		toFixed( div2, 10);
		add(div , document.body);
		add(div2 , document.body);
		add(div3 , div2);
		var ancho = 400;
		var ext = f.split('.').reverse()[0];
		switch( ext ){
			case 'jpg': case 'jpeg': case 'gif': case 'bmp': 
				var foto = document.createElement('img'); 
					foto.src = '/'+f; 
					if( show_buttons ){
						var prev = make( 'a' ) ;
						var next = make( 'a' ) ;
						var cont = id('galeria') ;
						var listado = tag('img' ,  cont) ;
						for( var i = 0; i < listado.length; i++ ){
							var curr_file = listado[i].src.split( '/' ).pop();
							var compared_file = f.split( '/' ).pop();
							if( compared_file == curr_file ){ break; }
						}
							prev.id = 'btn_prev';
							next.id = 'btn_next';
							
							prev.href = '#';
							next.href = '#';
							
							prev.innerHTML = 'anterior' ;
							next.innerHTML = 'siguiente' ;
							
							prev.title = 'Ver foto anterior';
							next.title = 'Ver foto siguiente';
							
							prev.onclick = function(){
								i = i-1;
								if( i < 0 ) i = listado.length - 1;
								var source = listado[i].src ;
									source = source.replace( 'thumbs/' , '' );
									foto.src = source; 
							}

							next.onclick = function(){
								i = i+1;
								if( i >= listado.length ) i = 0;
								var source = listado[i].src ;
									source = source.replace( 'thumbs/' , '' );
									foto.src = source; 
							}

							add( prev , div3 ) ;
							add( next , div3 ) ;
					}
					add( foto , div3 ); 
//					ancho = foto.width ;
//					div2.style.height = foto.height + 'px';
					break;
			case 'swf':	case 'flv':
				var file = '';
				if( ext == 'swf' ) {
						file = 'ver_flash';
				}else{
						file = 'ver_flv' ;
						f = f.split('.flv')[0];
				}
				
				var ajax = nuevoAjax();
				ajax.open("POST", '/libs/php/'+file+'.php',true);
				ajax.onreadystatechange=function() { if (ajax.readyState==4){ div3.innerHTML = ajax.responseText;  } }
				ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				ajax.send("file="+f);
				break;
			case 'unity3d':
				if (DetectUnityWebPlayer()) {
					var iHtml='<object id="UnityObject" classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" width="600" height="480">';
						iHtml+='<param name="src" value="/'+f+'" />';
						iHtml+='<embed id="UnityEmbed" width="600" height="480" src="/'+f+'" type="application/vnd.unity" pluginspage="http://www.unity3d.com/unity-web-player-2.x" />';
						iHtml+='</object>';
						div3.innerHTML = iHtml;
				}else {
					var getURL = GetInstallerPath ();
					var lnkDiv = make('div');
					var lnkSi = make('a');
					var lnkNo = make('a');
						lnkSi.href = lnkNo.href = '#';
						lnkSi.innerHTML = ' SI ';
						lnkSi.id = 'unitySi';
						lnkNo.id = 'unityNo';
						lnkDiv.id = 'si_no' ;
						lnkNo.innerHTML = ' NO ';
						lnkSi.onclick = function(){ window.open(getURL,'unity'); remove(div); remove(div2); return false; }
						lnkNo.onclick = function(){ remove(div); remove(div2); return false; }
					var iHtml = '<h3 class="unityWarning">Este trabajo requiere la instalación del plugin "Unity Player"</h3>';
						iHtml+= '<img src="/libs/icons/unity3dLogo_big.jpg" alt="unity logo" />';
						iHtml+= '<p>¿Desea descargarlo gratuitamente?</p>';
						iHtml+= '<small>nota: luego de la instalación deberá recargar esta página</small>';
						div3.innerHTML = iHtml;
						lnkDiv.appendChild(lnkSi);
						lnkDiv.appendChild(lnkNo);
						div3.appendChild(lnkDiv);
				}
			break;
		}
		div2.style.left = (Math.round( (document.documentElement.clientWidth - 800) / 2 ) - 15)+ 'px';
		
		
		//div3.innerHTML = 'ventana de tp';
//	new Ajax.Updater( div2, 'formulario_inscripcion.php', { parameters: { 'file' : f }, method : 'get' } ) ;	
}

function toFixed( e , top ){
if( isIex() ){ e.style.position = 'absolute'; setInterval( function(){ e.style.top = (document.documentElement.scrollTop + top) + 'px';  } , 5 );
	}
}

function nuevoAjax(){ 
	var xmlhttp=false; 
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } }
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); }
	return xmlhttp;
}

