//declaracion de variables
ventanaAbierta = false;

function mensaje(msg){
  if(!ventanaAbierta){
      crearCortina();
      var pos = window.getSize();
      ypos = (pos.y/2)-100;
      xpos = (pos.x/2)-200;
      var popupWin = new Element('div',{
        id:'popupWin',
        styles:{
          'width':'461px',
          'height':'200px',
          'top':ypos+'px',
          'left':xpos+'px',
          'position':'fixed',
          'backgroundColor':'transparent',
          'zIndex':'1001',
          'overflow':'hidden'
        }
      });
      popupWin.inject(document.body,'top');
      $('popupWin').makeDraggable();
      var req = new Request({
        method:'get',
        url:'pages/mensaje.php?msg='+msg,
        onSuccess:function(texto,xml){
            $('popupWin').set('html',texto);
            $('popupWin').getElementById('headerSmall').setStyles({
              'backgroundImage':'url(images/popup/headerSmall.png)',
              'backgroundRepeat':'no-repeat',
              'width':'461px',
              'height':'37px',
              'lineHeight':'37px',
              'fontFamily':'Georgia, Bookman Old Style, Times New Roman, Tahoma',
              'fontSize':'10pt',
              'color':'#333',
              'textShadow':'1px 1px 2px #fff',
              'cursor':'move'
            });
            $('popupWin').getElementById('cuerpoSmall').setStyles({
              'backgroundImage':'url(images/popup/bodySmall.png)',
              'backgroundRepeat':'repeat-y',
              'width':'461px',
              'fontFamily':'Bookman Old Style, Georgia, Times New Roman, Tahoma',
              'fontSize':'10pt',
              'color':'#333'
            });
        }
      }).send();
      ventanaAbierta = true;
  }
}

function cerrarPopup(){
  /*$('popupDiv').set('tween',{
    duration: 300
  });
  $('popupDiv').tween('opacity',0);
  setTimeout('$(\'popupDiv\').destroy()',1000);*/
  $('popupDiv').destroy();

  /*$('cortina').set('tween',{
    duration: 500
  });
  $('cortina').tween('opacity',0);
  setTimeout('$(\'cortina\').destroy()',1000);*/
  $('cortina').destroy();

}

function cerrarMensaje(){
  /*$('popupWin').set('tween',{
    duration: 300
  });
  $('popupWin').tween('opacity',0);
  setTimeout('$(\'popupWin\').destroy()',1000);*/
  $('popupWin').destroy();

  /*$('cortina').set('tween',{
    duration: 500
  });
  $('cortina').tween('opacity',0);
  setTimeout('$(\'cortina\').destroy()',1000);*/
  $('cortina').destroy();
  ventanaAbierta = false;
}

function crearCortina(){
  var tam = window.getScrollSize();
  var cortina = new Element('div',{
    id:'cortina',
    styles:{
      'width': tam.x + 'px',
      'height': tam.y + 'px',
      'top': '0px',
      'left':'0px',
      'position':'absolute',
      'backgroundColor':'#000',
      'zIndex':'1000'
    }
  });
  cortina.inject(document.body,'top');
  $('cortina').setOpacity(0.2);
  /*$('cortina').set('tween',{
    duration:300,
    link:'chain'
  });
  $('cortina').tween('opacity',0.5);*/
}

function detallesProyecto(id){
  crearCortina();
  //xpos = lanzador.offsetLeft;
  //ypos = lanzador.offsetTop;
  wpos = window.getSize();
  ypos = (wpos.y/2) - 250;
  xpos = $('body').getPosition();
  xpos = xpos.x + 80;
  var popup = new Element('div',{
    id:'popupDiv',
    styles:{
      'width':'701px',
      'height':'600px',
      'top':ypos+'px',
      'left':xpos+'px',
      'position':'fixed',
      'backgroundColor':'transparent',
      'zIndex':'1001',
      'overflow':'hidden'
    }
  });
  popup.inject(document.body,'top');
  $('popupDiv').makeDraggable();
  /*$('popupDiv').setOpacity(0);
  $('popupDiv').set('tween',{
    duration: 500
  });
  setTimeout('$(\'popupDiv\').tween(\'opacity\',1)',1000);*/
  var req = new Request({
    method: 'get',
    url: 'pages/popupStyle.php?id='+id,
    onSuccess: function(texto,xml){
      $('popupDiv').set('html',texto);
      $('popupDiv').getElementById('headerPopup').setStyles({
        'color':'#333',
        'backgroundImage':'url(images/popup/header.png)',
        'backgroundRepeat':'no-repeat',
        'width':'701px',
        'height':'37px',
        'lineHeight':'37px',
        'fontFamily':'Georgia, Bookman Old Style, Times New Roman, Tahoma',
        'fontSize':'10pt',
        'textShadow':'1px 1px 2px #fff',
        'cursor':'move',
        'float':'left'
      });
      $('popupDiv').getElementById('cuerpo').setStyles({
        'backgroundImage':'url(images/popup/body.png)',
        'backgroundRepeat':'repeat-y',
        'width':'701px',
        'float':'left',
        'position':'relative',
        'top':'0px',
        'margin':'0'
      });

      $('popupDiv').getElementById('informacion').setStyles({
        'color':'#444',
        'fontFamily':'Bookman Old Style, Georgia, Times New Roman, Tahoma',
        'fontSize':'10pt'
      });

      //iframe
      var paginaLoader = new Element('iframe',{
        id:'cargador',
        src:'pages/popup.php?id='+id,
        width:'675',
        height:'343',
        styles:{
          'border':'none',
          'position':'relative',
          'backgroundColor':'transparent',
          'overflow':'hidden'
        },
        scrolling:'no',
        frameborder:'0',
        marginwidth:'0',
        marginheight:'0'
      });
      paginaLoader.inject($('iframeDiv'),'top');
      //$('cargador').makeDraggable();
      //iframe fin

    },
    onFailure: function(){
      alert('no se encontro la direccion');
    }
  }).send();


}

function showDesc(id){
  obj = document.getElementById('desc'+id);
  obj.style.display = 'block';
  $('desc'+id).setOpacity(0.8);
  //alert(obj);
}

function hideDesc(id){
  obj = document.getElementById('desc'+id);
  obj.style.display = 'none';
}

function cargarImagenes(arreglo,lista_imagenes){
  for(var i in arreglo){
    lista_imagenes[i] = new Image();
    lista_imagenes[i].src = arreglo[i];
  }
}

function cambiarPest(opc,capa){
  capa.style.backgroundImage = "url(images/folio_pest"+opc+".png)";
  switch(opc){
    case 1: $('infoWeb').style.display = 'block';$('infoPrint').style.display = 'none';$('infoMedia').style.display = 'none';break;
    case 2: $('infoWeb').style.display = 'none';$('infoPrint').style.display = 'block';$('infoMedia').style.display = 'none';break;
    case 3: $('infoWeb').style.display = 'none';$('infoPrint').style.display = 'none';$('infoMedia').style.display = 'block';
  }

  var altura = document.getElementById('body').clientHeight-130;
  document.getElementById('fondoShapesFoot').style.top = altura+"px";
}

//checamos el formulario y lo lanzamos
function sendForm(formulario){
  if(validado=validar(formulario)){
    formulario.submit();
  }
}
//funcion para validar cualquier formulario
function validar(formulario){
  var valido = true;
  var frmEl = formulario.elements;
  var contChk = 0;
  var hayChk = false;
  for(var i=0; i<frmEl.length; i++){
    var obj = frmEl[i];
    switch(obj.type){
      case 'text':{
        if(obj.value == '' || obj.value == obj.name){
          //obj.setStyle('border','1px solid red');
          if(obj.className != 'nulo'){
            obj.style.border = "1px solid red";
            mensaje("Es necesario llenar todos los campos");
  		    valido = false;
          }
        }
        else{
		  if(obj.name == 'correo'){
			if(!(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(obj.value))){
			  //alert("direccion de correo invalida");
              mensaje("Dirección de correo inválida");
              valido = false;
			  //obj.setStyle('border','1px solid red');
              obj.style.border = "1px solid red";
			}else{
			  //obj.setStyle('border','1px solid #aaaaaa');
              obj.style.border = "1px solid #aaaaaa";
            }
		  }else{
		    //obj.setStyle('border','1px solid #aaaaaa');
            obj.style.border = "1px solid #aaaaaa";
		  }
          if(obj.name == 'anti-spam'){
            if(formulario.code.value!=obj.value){
              alert("No Coincide el texto del la imagen, intentelo nuevamente");
              valido = false;
              //obj.setStyle('border','1px solid red');
              obj.style.border = "1px solid red";
            }
            else{
                //obj.setStyle('border','1px solid #aaaaaa');
                obj.style.border = "1px solid #aaaaaa";
            }
          }
        }
        break;
      }
      case 'file':{
        if(obj.value==''){
          //obj.setStyle('border','1px solid red');
          obj.style.border = "1px solid red";
          valido = false;
        }else{
          //obj.setStyle('border','1px solid #aaaaaa');
          obj.style.border = "1px solid #aaaaaa";
        }
        break;
      }
      case 'password':{
        if(obj.value == ''){
		  //obj.setStyle('border','1px solid red');
          obj.style.border = "1px solid red";
          valido = false;
        }
        else{
          if(obj.name=='confirmacion'){
            if(frmEl[i-1].value!=obj.value){
              alert("los campos de clave y confirmación deben coincidir, verifiquelo e intentelo nuevamente");
              //obj.setStyle('border','1px solid red');
              obj.style.border = "1px solid red";
              valido = false;
            }
            else{
              //obj.setStyle('border','1px solid #aaaaaa');
              obj.style.border = "1px solid #aaaaaa";
            }
          }else{
             //obj.setStyle('border','1px solid #aaaaaa');
             obj.style.border = "1px solid #aaaaaa";
          }
        }
		break;
      }
	 case 'textarea':{
		if(obj.value == '' || obj.value==obj.name){
		  //obj.setStyle('border','1px solid red');
          if(obj.className!='nulo'){
              obj.style.border = "1px solid red";
    		  valido = false;
          }
		}
		else{
		  //obj.setStyle('border','1px solid #aaaaaa');
          obj.style.border = "1px solid #aaaaaa";
		}
        break;
	 }
     case 'checkbox':{
       hayChk = true;
       if(obj.checked) contChk++;

       if(obj.name == 'changePass') hayChk = false;

       if(obj.name == 'changeImg') hayChk = false;

       if(obj.className == 'nulo') hayChk = false;

       break;
     }
     case 'select-one':{
       if(obj.value==0){
         //obj.setStyle('border','1px solid red');
         obj.style.border="1px solid red";
         valido = false;
       }else{
         //obj.setStyle('border','1px solid #aaaaaa');
         obj.style.border="1px solid #aaaaaa";
       }
     }
    }
  }
   if(contChk==0 && hayChk){
      valido = false;
      alert('Debe seleccionar al menos un elemento de la lista');
    }
  return valido;
}

window.addEvent('domready',function(){
  //----------------------------------------------------------------
  //configuracion de los campos del formulario
  $$('.texto').addEvent('focus',function(){
    //this.setOpacity(0.6);
    if(this.value==this.name){
      this.value='';
    }
  });
  $$('.texto').addEvent('blur',function(){
    //this.setOpacity(0.4);
    if(this.value==''){
      this.value=this.name;
    }
  });
  //----------------------------------------------------------------

  
  //SAMPLE 4 (walk to item)
  var nS4 = new noobSlide({
  	box: $('box4'),
  	items: $$('#box4 div'),
  	size: 850,
  	autoPlay: true,
    interval: 7000,
    fxOptions: {
        duration: 1000,
        transition: Fx.Transitions.Bounce.easeOut,
        wait: false
    },
  	handles: $$('#handles4 span'),
  	onWalk: function(currentItem,currentHandle){
  		$('info4').set('html',currentItem.getFirst().innerHTML);
  		this.handles.removeClass('active');
  		currentHandle.addClass('active');
  	}
  });




  //----------------------------------------------------------------
  //precarga de imagenes
  var imagenes = new Array();
  imagenes[0] = 'images/presentaciones/portofolio.jpg';
  imagenes[1] = 'images/folio_pest1.png';
  imagenes[2] = 'images/folio_pest2.png';
  imagenes[3] = 'images/folio_pest3.png';
  imagenes[4] = 'images/popup/header.png';
  imagenes[5] = 'images/popup/body.png';
  imagenes[6] = 'images/popup/foot.png';
  imagenes[7] = 'images/popup/headerSmall.png';
  imagenes[8] = 'images/popup/bodySmall.png';
  imagenes[9] = 'images/popup/footSmall.png';
  imagenes[10] = 'images/popup/btn_close.png';
  imagenes[11] = 'images/popup/prev.png';
  imagenes[12] = 'images/popup/play.png';
  imagenes[13] = 'images/popup/stop.png';
  imagenes[14] = 'images/popup/fwd.png';

  var images = new Array();

  cargarImagenes(imagenes,images);

  //$('#handles4').setOpacity(0.6);
});

window.addEvent('load',function(){
  var altura = document.getElementById('body').clientHeight-60;
  document.getElementById('fondoShapesFoot').style.top = altura+"px";
});

