$.qixtv = $.extend({
  baseUrl: null,

  layout: {
    leftHeight: 0,
    rightHeight: 0,

    ApplyCorners: function() {
      $('.corner-8').corner('8px cc:#fff');
      $('.corner-8-top').corner('8px cc:#fff top');
      $('.corner-8-bottom').corner('8px cc:#fff bottom');
    },

    Draw: function() {
      $.qixtv.layout.ApplyCorners();

      $('.videos-h li').mouseover(function() {
        $(this).find('.flip').show();
      });

      $('.videos-h li').mouseout(function() {
        $(this).find('.flip').hide();
      });

      $.qixtv.layout.leftHeight = 0;
      $.qixtv.layout.rightHeight = 0;

      $('#left-col .box, #left-col .banner').each(function() {
        $.qixtv.layout.leftHeight += $(this).height() + 10;
      });

      $('#right-col .box, #right-col .banner').each(function() {
        if ($(this).attr('id') != 'sidebar') {
          $.qixtv.layout.rightHeight += $(this).height() + 10;
        }
      });

      $.qixtv.sidebar.height = Math.max($.qixtv.layout.leftHeight - $.qixtv.layout.rightHeight - 10, $.qixtv.sidebar.itemHeight + $.qixtv.sidebar.padding);

      $('#sidebar').animate({ height: $.qixtv.sidebar.height }, function() {
        var count = Math.floor(($.qixtv.sidebar.height - $.qixtv.sidebar.padding) / $.qixtv.sidebar.itemHeight);

        if (count != $.qixtv.sidebar.count) {
          $.qixtv.sidebar.Load(0, count);
          $.qixtv.sidebar.count = count;
        }
      });
    }
  },

  sidebar: {
    current: null,
    count: 0,
    height: 0,
    itemHeight: 155,
    offset: 0,
    padding: 114,
    request: null,

    Init: function() {
      $.qixtv.sidebar.current = $('#sidebar .box-menu .active a').attr('id').substr(String('sidebar-').length);
      $('#sidebar .box-menu a').click($.qixtv.sidebar.MenuClick);
    },

    MenuClick: function() {
      var name = $(this).attr('id').substr(String('sidebar-').length);

      if (name != $.qixtv.sidebar.current) {
        $('#sidebar .box-menu li').removeClass('active');

        $(this).parent().addClass('active');

        $.qixtv.sidebar.offset = 0;
        $.qixtv.sidebar.current = name;
        $.qixtv.sidebar.Reload();
      }
    },

    AbortRequest: function() {
      if ($.qixtv.sidebar.request) {
        // does not work with asynchronous requests...
        //$.qixtv.sidebar.request.abort();
      }

      if ($.qixtv.sidebar.timer) {
        clearTimeout($.qixtv.sidebar.timer);
      }
    },

    ScheduleRequest: function() {
      $.qixtv.sidebar.CancelUpdate();

    },

    Load: function(offset, count) {
      if (offset != undefined) $.qixtv.sidebar.offset = offset;
      if (count != undefined) $.qixtv.sidebar.count = count;

      $.qixtv.sidebar.AbortRequest();
      $.qixtv.sidebar.timer = setTimeout($.qixtv.sidebar.Reload, 1000);
    },

    Reload: function() {
      $('#sidebar .content').html('').addClass('box-loader');
      $('#sidebar .loading').show();

      $.qixtv.sidebar.request = $('#sidebar .content').load($.qixtv.baseUrl + '/guia/sidebar/' + $.qixtv.sidebar.current + '/' + $.qixtv.sidebar.offset + '/' + $.qixtv.sidebar.count, function() {
        $('#sidebar .content').removeClass('box-loader');
        $('#sidebar .loading').hide();
      });
    }
  },

  ShowErrors: function(data, onError) {
    if (data.errors) {
      $.qixtv.Warning(data.errors);

      if (typeof onError == 'function') {
        onError(data.errors);
      }

      return true;
    }
    else if (data.exception) {
      alert(data.exception.message + "\n" + data.exception.trace + "\n" + data.exception.file + ' (' + data.exception.line + ')');
      return true;
    }

    return false;
  },

  Warning: function(s) {
    if (s == null) return;

    var html = '<div class="popup popup-warning corner-8">'
      + '<a class="close" href="javascript:" onclick="$.fn.colorbox.close();">fechar</a>'
      + '<span class="content">';

    if (typeof s == 'object') {
      html += '<ul>';
      for (var i in s) {
        html += '<li>' + s[i] + '</li>';
      }
      html += '</ul>';
    } else {
      html += s;
    }

    html += '</span></div>';

    $.fn.colorbox({ html: html, close:false, scrolling:false });
    $.qixtv.layout.ApplyCorners();
  },

  Notice: function(s) {
    if (s == null) return;

    var html = '<div class="popup popup-notice corner-8">'
      + '<a class="close" href="javascript:" onclick="$.fn.colorbox.close();">fechar</a>'
      + '<span class="content">';

    if (typeof s == 'object') {
      html += '<ul>';
      for (var i in s) {
        html += '<li>' + s[i] + '</li>';
      }
      html += '</ul>';
    } else {
      html += s;
    }

    html += '</span></div>';

    $.fn.colorbox({ html: html, close:false, scrolling:false });
    $.qixtv.layout.ApplyCorners();
  }
}, $.qixtv || {});
