function resaltarBoton(opc,obj){
  if(opc=='over'){
    /*obj.css('background-color','#8E8D8C');*/
    obj.animate({
       'background-color':'#8E8D8C',
       'width':'150px',
       'padding-left':'13px'
    },300,'easeOutExpo');
  }
  else{
    /*obj.css('background-color','#B2B2B2');*/
    obj.animate({
        'background-color':'#B2B2B2',
        'width':'158px',
        'padding-left':'5px'
    },100,'easeOutExpo');
  }
}

function ajaxLoader(capa){
    capa.html('<img src="../image/admin/ajax-loader.gif" align="center" id="ajax-loader" />');
}

function loadSection(address,capa){
    $.ajax({
        type: "GET",
        url: address,
        async: true,
        context: capa,
        dataType: 'html',
        success:function(datos){
          //$(this).css('opacity',0);
          $(this).html(datos);
          /*$(this).animate({
            opacity: 1
          },300,'easeInExpo');*/
        }

    });
}

function crearCortina(){
  if($('#cortina')!=null && $('#cortina')!=undefined){
    $('#cortina').remove();
  }
  var cortina = $("<div>",{id: 'cortina'}).appendTo("body");
  var cssObj = {
    'background-color':'#000000',
    'position':'fixed',
    'top':'0',
    'left':'0',
    'width': $(document).width()+'px',
    'height': $(document).height()+'px',
    'z-index':'100',
    'opacity':0.5
  }
  cortina.css(cssObj);
}

//generamos popus con el tamaño y la dirección
function popup(ancho,url){
  if($('#nuevoPopup')!=null && $('#nuevoPopup')!=undefined){
    $('#nuevoPopup').remove();
  }
  var path = "../views/admin/popups/";
  var direccion = path+url+".php";
  var nuevaVentana = $("<div>",{id:'nuevoPopup'}).appendTo($('#container'));
  var cssObj = {
    'position':'absolute',
    'width': ancho +'px',
    'z-index':'101',
    'opacity':0
  }
  nuevaVentana.css(cssObj);

  $.ajax({
      type: "GET",
      url: direccion,
      async: true,
      context: nuevaVentana,
      dataType: 'html',
      success:function(datos){
        $(this).html(datos);
        var popWidth = $(this).width();
        var popHeight = $(this).height();
        xPos = $(document).width()/2 - popWidth/2;
        yPos = $(document).height()/2 - popHeight/2;
        $(this).css({
          'left':xPos
        });
        $(this).animate({
          opacity: 1,
          'top':yPos
        },300,'easeOutElastic');
      }
  });
}


//funcion para cerrar el popup
function closePopup(){
  $('#nuevoPopup').remove();
  $('#cortina').remove();
}
//formularios ajax
function popupForm(formulario,modulo,capa,opcion,capaRefresh,destino){
  if(validado=validar(formulario)){

    if($('#enviarBtn')){
      $('#enviarBtn').html('Enviando...');
      $('#enviarBtn').attr('href', '#');
    }

    variables = formulario.serialize();
    path = "../action/act."+modulo+".php";
    direccion = path+"?"+variables+"&accion="+opcion;

    $.ajax({
        type: "GET",
        url: direccion,
        async: true,
        context: capa,
        dataType: 'html',
        success:function(datos){
          $(this).html(datos);
          loadSection(destino,capaRefresh);
        }
    });

  }
}

//formulario simple
function submitForm(formulario,destino,capa){
  if(validar(formulario)){
    variables = formulario.serialize();
    direccion = destino+'?'+variables;

    $.ajax({
        type: "GET",
        url: direccion,
        async: true,
        context: capa,
        dataType: 'html',
        success:function(datos){
          $(this).html(datos);
        }
    });

  }
}

//checamos el formulario y lo lanzamos
function formulario(formulario){
  if(validado=validar(formulario)){
    document.getElementById(formulario.attr('id')).submit();
  }
}

//funcion para validar cualquier formulario
function validar(formulario){
  var valido = true;
  var frmEl = document.getElementById(formulario.attr('id')).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.setStyle('border','1px solid red');
          if(obj.className != 'nulo'){
            obj.style.border = "1px solid red";
  		    valido = false;
          }
        }
        else{
		  if(obj.name == 'correo'){
			if(!(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(obj.value))){
			  alert("direccion de correo invalida");
              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 == 'txtCaptcha'){
            if($('#code').attr('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 'hidden':{
        if(obj.className == 'imagen' && obj.value==''){
          valido = false;
          alert("Debe seleccionar una imágen para el pronostico");
        }
        if(obj.name == 'idLocutor' && obj.value==''){
          valido = false;
          alert("Debe seleccionar un locutor");
        }
      }
      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.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.className!='nulo'){
         //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).ready(function(){
    //configuramos los botones
    $('.menuBtn').mouseover(function(){
      resaltarBoton('over',$(this));
    });
    $('.menuBtn').mouseout(function(){
      resaltarBoton('out',$(this));
    });

    //--------
    //configuramos la capa del contenido al tamaño de la pantalla

    altura = $(window).height() - 50;
    ancho = $(window).width() - 179;
    if($('#contenido')!=null){
      $('#contenido').css('height',altura+'px');
      $('#contenido').css('width',ancho+'px');
      $('#contenido').css('position','fixed');
    }

});

$(window).resize(function(){
  altura = $(window).height() - 50;
  ancho = $(window).width() - 179;
  if(altura > 400 && $('#contenido')!=null){
    $('#contenido').css('height',altura+'px');
  }
  if(ancho > 700 && $('#contenido')!=null){
    $('#contenido').css('width',ancho+'px');
  }

  /*if($('#cortina')!=null && $('cortina')!='undefined'){
    $('#cortina').css('height',screen.height);
    $('#cortina').css('width',screen.width);
  }*/

  if($('#nuevoPopup')!=null && $('#nuevoPopup')!=undefined){
    py = $('#nuevoPopup').height();
    px = $('#nuevoPopup').width();
    newYpos = $(window).height()/2 - py/2;
    newXpos = $(window).width()/2 - px/2;

    $('#nuevoPopup').animate({
            top:newYpos,
            left:newXpos
        }, 500, function(){
          //animacion completa
        }
    );
  }
});
