$.QixTV = {

  jCarouselOptions : {
    scroll : 1
  },

  busy : false,

  scrollDuration : 500,

  Interstitial : {
    Finished : function() {
      $('#BoxPlayer .interstitial').hide();
      $('#BoxPlayer .video').show();
    }
  },

  /**
   * Rating
   */

  Rating : {
    Vote : function(v, r) {
      $.ajax( {
        type : "POST",
        url : "rating.php",
        data : {
          v : v,
          r : r
        },
        dataType : 'json',
        success : function(data) {
          if (data.warning != undefined) {
            alert(data.warning);
          } else {
            if (data.notice != undefined) {
              $.QixTV.Rating.Refresh(v);
              alert(data.notice);
            }
          }
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
          alert(XMLHttpRequest.responseText);
        }
      });
    },

    Refresh : function(v) {
      $('#BoxPlayer .rating').load('rating.php?v=' + v);
    }
  },

  /**
   * Comentário Video
   */

  Indique : {
    SendForm : function() {
      if ($.QixTV.busy)
        return;

      $.QixTV.busy = true;

      var form = $('#form_indique');
      var inp = form.find(':input');

      inp.attr('disabled', 'disabled');
      form.find('.loader').show();

      $.ajax( {
        type : "POST",
        url : "indique.php",
        data : inp,
        dataType : 'json',
        success : function(data) {
          if (data.warning != undefined) {
            alert(data.warning);
          }

          else {
            if (data.notice != undefined) {
              alert(data.notice);
            }

            $('#form_indique [name=r_nome]').val('seu nome');
            $('#form_indique [name=r_email]').val('seu email');
            $('#form_indique [name=d_nome]').val('nome do amigo');
            $('#form_indique [name=d_email]').val('email do amigo');
          }

          $.QixTV.Indique.UnlockForm();
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
          $.QixTV.Indique.UnlockForm();
          alert(XMLHttpRequest.responseText);
        },
        complete : function() {
          $.QixTV.Indique.UnlockForm();
        }
      });
    },

    UnlockForm : function() {
      $('#form_indique .loader').hide();
      $('#form_indique :input').removeAttr('disabled');
      $.QixTV.busy = false;
    }
  },

  /**
   * Comentário Video
   */

  Comentarios : {
    SendForm : function() {
      if ($.QixTV.busy)
        return;

      $.QixTV.busy = true;

      var form = $('#form_comentario');
      var inp = form.find(':input');

      inp.attr('disabled', 'disabled');
      form.find('.loader').show();

      $.ajax( {
        type : "POST",
        url : "comments.php",
        data : inp,
        dataType : 'json',
        success : function(data) {
          if (data.warning != undefined) {
            alert(data.warning);
          }

          else {
            if (data.notice != undefined) {
              alert(data.notice);
            }

            $('#form_comentario [name=nome]').val('seu nome');
            $('#form_comentario [name=comentario]').val('seu comentário');
          }

          $.QixTV.Comentarios.UnlockForm();
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
          $.QixTV.Comentarios.UnlockForm();
          alert(XMLHttpRequest.responseText);
        },
        complete : function() {
          $.QixTV.Comentarios.UnlockForm();
        }
      });
    },

    UnlockForm : function() {
      $('#form_comentario .loader').hide();
      $('#form_comentario :input').removeAttr('disabled');
      $.QixTV.busy = false;
    }
  },

  /**
   * Cadastro Newsletter
   */

  CadastroNewsletter : function() {
    if ($.QixTV.busy)
      return;

    $.QixTV.busy = true;

    var form = $('#form_newsletter');
    var inp = form.find(':input');

    inp.attr('disabled', 'disabled');
    form.find('.loader').show();

    $.ajax( {
      type : "POST",
      url : "newsletter.php",
      data : inp,
      dataType : 'json',
      success : function(data) {
        if (data.warning != undefined) {
          alert(data.warning);
        }

        else {
          if (data.notice != undefined) {
            alert(data.notice);
          }

          $('#form_newsletter [name=nome]').val('seu nome');
          $('#form_newsletter [name=email]').val('seu email');
        }

        $.QixTV.UnlockNewsForm();
      },
      error : function(XMLHttpRequest, textStatus, errorThrown) {
        $.QixTV.UnlockNewsForm();
        alert(XMLHttpRequest.responseText);
      },
      complete : function() {
        $.QixTV.UnlockNewsForm();
      }
    });
  },

  UnlockNewsForm : function() {
    $('#form_newsletter .loader').hide();
    $('#form_newsletter :input').removeAttr('disabled');
    $.QixTV.busy = false;
  },

  /**
   * Menu do vídeo
   */

  BoxPlayer : {
    Show : function(a, id) {
      $('#BoxPlayer .menu a').removeClass('active');
      $(a).addClass('active');

      $('#BoxPlayer .content .item').each(function(k, item) {
        if ($(item).attr('id') == id) {
          $(item).show();
        } else {
          $(item).hide();
        }
      });

      $('.carousel').jcarousel({ size: $.QixTV.CountRelated, scroll:1, easing:false });

      $.scrollTo('#BoxPlayer', $.QixTV.scrollDuration);
    }
  },

  /**
   * Programas
   */

  Programas : {
    Show : function(a, id) {
      $('#BoxProgramas ul li a').removeClass('active');
      $(a).addClass('active');

      $.QixTV.Programas.Load('programas.php?c=' + id);

      $.scrollTo('#BoxPlayer .menu', $.QixTV.scrollDuration);
    },

    Load : function(url) {
      $('#BoxProgramas .loader').show();
      $('#BoxProgramas .content').load(url, null, function() {
        $('#BoxProgramas .loader').hide();
      });
    }
  },

  /**
   * Sidebar
   */

  Sidebar : {
    Show : function(id, c, scroll) {
      $('#Sidebar .videos .menu a').removeClass('active');
      $('#Sidebar .videos .menu a.' + id).addClass('active');

      $.QixTV.Sidebar.Load('sidebar.php?f=' + id + '&c=' + c, scroll);
    },

    Load : function(url, scroll) {
      $('#Sidebar .videos .content').load(url);

      if (scroll !== false)
        $.scrollTo('#Sidebar .videos', $.QixTV.scrollDuration);
    }
  },

  /**
   * Página de cadastro de usuário/vídeo
   */

  Cadastro : {
    init : function() {
      $('#v_code').blur($.QixTV.Cadastro.LoadVideoData);
      $('#v_source').change($.QixTV.Cadastro.LoadVideoData);
    },

    LoadVideoData : function() {
      cod = String($('#v_code').val());
      src = String($('#v_source').val());

      if (cod.toString().length > 0 && src.toString().length > 0) {
        $('#loader').show();
        $.QixTV.Cadastro.lockForm();

        $.ajax( {
          type : "POST",
          url : "loadvideodata.php",
          data : {
            source : src,
            code : cod
          },
          dataType : 'json',
          success : function(data) {
            if (data.error != undefined) {
              alert('Código (embed) ou url inválido');
            } else {
              $('#v_titulo').val(data.title);
              $('#v_descricao').val(data.description);
              $('#v_dur_min').val(data.duration.minutes);
              $('#v_dur_sec').val(data.duration.seconds);
            }

            $('#loader').hide();
            $.QixTV.Cadastro.unlockForm();
          },
          error : function(XMLHttpRequest, textStatus, errorThrown) {
            $('#loader').hide();
            $.QixTV.Cadastro.unlockForm();
            alert(XMLHttpRequest.responseText);
          },
          complete : function() {
            $('#loader').hide();
            $.QixTV.Cadastro.unlockForm();
          }
        });
      }
    },

    lockForm : function() {
      $('#cadastros input, #cadastros textarea').attr('disabled', 'disabled');
    },

    unlockForm : function() {
      $('#cadastros input, #cadastros textarea').removeAttr('disabled');
    }
  },

  /**
   * Arquivo
   */

  SelectDate : function(dateText, inst) {
    window.location.href = "search.php?d=" + dateText;
    ;
  },

  /**
   * Enquete
   */

  Enquete : {
    state : 'vote',

    Vote : function(e) {
      if ($.QixTV.Enquete.state == 'vote') {
        $('#BoxEnquete .loader').show();

        var v = $('#BoxEnquete :checked').val();

        if (v == undefined) {
          alert('Selecione um vídeo para votar');
          return;
        }

        $.ajax( {
          type : "POST",
          url : "enquete.php?a=vote",
          data : {
            e : e,
            v : v
          },
          dataType : 'json',
          success : function(data) {
            if (data.warning != undefined) {
              alert(data.warning);
            } else if (data.notice != undefined) {
              alert(data.notice);
            }

            $('#BoxEnquete .loader').hide();
          },
          error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.responseText);

            $('#BoxEnquete .loader').hide();
          }
        });
      } else {
        $.QixTV.Enquete.state = 'vote';
        $('#BoxEnquete .legenda').show();
        $('#BoxEnquete .results').hide();
      }
    },

    GetResults : function(e) {
      $.QixTV.Enquete.state = 'results';

      $('#BoxEnquete .loader').show();

      $.ajax( {
        type : "POST",
        url : "enquete.php?a=result",
        data : {
          e : e
        },
        dataType : 'json',
        success : function(data) {
          if (data.warning != undefined) {
            alert(data.warning);
          } else {
            for ( var i in data.result) {
              $('#enq_opt_' + i + ' .results').text(
                Math.round(data.result[i] / data.total * 100) + '%');
            }

            $('#BoxEnquete .results').show();
            $('#BoxEnquete .legenda').hide();
          }

          $('#BoxEnquete .loader').hide();
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
          alert(XMLHttpRequest.responseText);

          $('#BoxEnquete .loader').hide();
        }
      });
    }
  }

}