$(function(){
   
   
   $('.texto-destaque').eq(0).fadeIn();
   $('.destaques').scrollable({
       size:1,
       items:'.items a',
       speed:900,
       circular:true,
       onSeek:function(e,i){
           $('.texto-destaque').hide().eq(i).fadeIn();
       }
   }).autoscroll({
       autoplay:true,
       interval:6000
   }).navigator({
       navi:'.nav-destaques',
       activeClass:'sel-destaque'
   }).parent().find('.nav-destaques li').eq(0).addClass('sel-destaque');
   
   $(".lightbox").fancybox({
       titlePosition: 'inside'
   });

    /*inline labels*/
   $("label:not([class*=infield])").inFieldLabels();
   
      /*CONFIGUE OVERLAY*/
   $.extend($.tools.overlay.conf, {
       
       expose:"#000",
       closeOnClick:true,
       top:"25%",
       oneInstance:false,
       speed:"fast",
       api:"true",
       fixed:false,
       onBeforeLoad:function(){this.getOverlay().appendTo('body')}
       
   });
   
   /*CONFIGURE DIALOG*/
    $.extend($.ui.dialog.prototype.options, {
        modal: true,
        zIndex:99999,
        show: 'slide',
        hide: 'blind'
    });

   $('#tr-login').click(function(){
      
      $('.login').overlay().load();
      
      return false;
   });
   
  $('.tr-tr-login').click(function(){
      
      $('.login').overlay().load();
      
      return false;
   });
   
    $('#newsletter').click(function(){
      
      $('.newsletter').overlay().load();
      
      return false;
   });
   
   
   
    var hash = location.hash.replace('#','');

    if( hash != '' ){
        
        $('#listen').load('/listen/sons/'+hash);
        $('#listen').overlay().load();

      }
      
   
   $('.listen').click(function(){
      
      $('#listen').load(this.href);
      $('#listen').overlay().load();
      location.hash = this.rel;
      
      //alert(location);
      
   return false;
   }); 
   
   $('.view-video').click(function(){
      
      $('#view-video').load(this.href);
      $('#view-video').overlay().load();
      
   return false;
   });
   
   $('.containsEvent').each(function(){
       
       var rel = $(this).find('.day').text();
       $(this).tooltip({
        tip: '.output[rel='+rel+']',
        effect: 'slide',
        position: "left"
       });
       
   });
   
   function createAlert(title,type,text)
   {
       var html = '<div title="'+title+'">'; 
       switch (type)
       {
           case 'success':
               html += '<img style="float:right" src="/assets/images/grafs/success.png" alt="Sucess"/>';
               break;
           case 'error':
               html += '<img style="float:right" src="/assets/images/grafs/error.png" alt="Error"/>';
               break;
           case 'info':
               html += '<img style="float:right" src="/assets/images/grafs/information.png" alt="Information"/>';
               break;
       }
       html += text;
       html += '</div>';
    return html;
   }
   
   function createConfirm(title,text)
   {
       var html = '<div id="dialog-confirm" title="'+title+'">'; 

       html += text;
       html += '</div>';
    return html;
   }
   
   
    $('.formAjax').submit(function(){

        $.post(this.action, $(this).serialize(),function(r){
            
         var result = $.parseJSON(r);
           
          if(result.type == 'no_error')
          {
                foo = createAlert(result.title,'success',result.alert);
                if(result.title == 'Login')
                    window.location.replace('/area/perfil');
                else
                    $(foo).dialog({close: function(event, ui){window.location.reload();}});
          }
          else
          {
              
              $.each(result.errors, function(key, value) {

                $('.formAjax').find(':input[name='+key+']').addClass('error-border').keypress(function(){
                    $(this).removeClass('error-border');
                });

              });
              
              foo = createAlert(result.title,'error',result.alert);
              $(foo).dialog();
          }

        });

    return false;
    });
    
    $('.lista-mensagens tr:nth-child(odd)').addClass('odd');
    
    $('.lista-mensagens .delete a').click(function(){
        
        var element = this;
        
        foo = createConfirm('Atenção!','Dejesa mesmo eliminar este registo?');
        $(foo).dialog({
            buttons:{
                        "Confirmar": function() {
                            $( this ).dialog('close');
                            $.ajax({
                               url: element.href,
                               type: 'POST',
                               data:({'id':element.rel}),
                               success: function(a){
                                   $(element).parents('tr').css('background-color','#ff0000').fadeOut();
                               }
                            });
                        },
                        "Cancelar": function() {
                                $( this ).dialog( "close" );
                        }
                    }
        });
    return false;
    });
    
    $('.formMensagem').submit(function(){
       
       $.post(this.action, $(this).serialize(), function(r){
           
          var result = $.parseJSON(r);
           
          if(result.type == 'no_error')
          {
                foo = createAlert(result.title,'success',result.alert);
                $(foo).dialog({close: function(event, ui){window.location.reload();}});
          }
          else
          {
              
                foo = createAlert(result.title,'error',result.alert);
                $(foo).dialog();
          }
           
       });
       
    return false;
    });
    
});
