/*
  Adding anchoring funcs here to be called later.
  (Easier to maintain in a single file than within HAML views.)
*/

var ChannelView = {
  Values : {Cursor:false, Interval:60000, to:false},
  // Render : {
  //   Row : function(item) {
  //   },
  //   Grid : function(item) {
  //   }
  // },
  Stream : function() {
    var url = location.href.replace(/^(.*)(\#)(.*)$/i, '$1').replace(/^(.*)(\?)(.*)$/i, '$1');
    this.to = setInterval(function() {
      $.ajax({url:url+'.json?cursor='+ChannelView.Values.Cursor, dataType:'json', method:'GET', data: {}, 
        success : function(result) {
          if (ChannelView.Values.Cursor && ChannelView.Values.Cursor > 0) {
            if (result.cursor != ChannelView.Values.Cursor) {
              var vids = $.evalJSON(result.videos), html = (vids.length > 1 ? 'New videos have' : 'A new video has') +' been added to this channel. <a href="'+ url +'">Click here</a> to refresh this page.';
              if ($('#channel_videos_new_videos').size() > 0) {
                $('#channel_videos_new_videos').animate({'opacity':0}, 250, function() {$(this).html(html).animate({'opacity':'100'}, 250);});
              } else {
                $('#channel_videos').before('<div id="channel_videos_new_videos" style="display: none;"></div>');
                $('#channel_videos_new_videos').html(html).animate({'height':'show'}, 250);
              }
            }
          }
          ChannelView.Values.Cursor = result.cursor;
        }, error : function() {}
      });
    }, this.Values.Interval);
  }
};

var Anchoring = {
  Channel : {
    Func : function(item) {
      var url = (item && item.href && item.href != '' ? item.href : item), cv_box = $('#channel_videos');
      if (url) {
        //cv_box.addClass('loading').html('&nbsp;');
        var $sort = $('.menu.sorts .items'), $intv = $('.menu.intervals .items'), $modes = $('.view_modes');

        // Get sorting method
        if ($sort.length > 0) {
          var sort = (/(sort\=)([A-Z0-9\-\_]+)/i.test(url)) ? url.replace(/^(.*)(sort\=)([A-Z0-9\-\_]+)(.*)$/i, '$3') : false;
          url = url.replace(/(&?)(sort\=)([A-Z0-9\-\_]+)/i, '').replace(/(.*)(\?)$/, '$1');

          if (!sort || sort == '') sort = ($sort.find('.item.selected a').length > 0 ? $sort.find('.item.selected a') : $sort.find('.item:eq(0) a')).attr('rel').replace(/(.*)(\s)([A-Z0-9\-\_]+)$/i, '$3');
          if (sort && sort != '') {
            $sort.find('.item a[rel*='+sort+']').menuDropClick(true);
            url += (url.indexOf('?') >= 0 ? '&' : '?') + 'sort='+ sort;
          }
        }

        // Get interval method
        if ($intv.length > 0) {
          var intv = (/(interval\=)([A-Z0-9\-\_]+)/i.test(url)) ? url.replace(/^(.*)(interval\=)([A-Z0-9\-\_]+)(.*)$/i, '$3') : false;
          url = url.replace(/(&?)(interval\=)([A-Z0-9\-\_]+)/i, '').replace(/(.*)(\?)$/, '$1');

          if (!intv || intv == '') intv = ($intv.find('.item.selected a').length > 0 ? $intv.find('.item.selected a') : $intv.find('.item:eq(0) a')).attr('rel').replace(/(.*)(\s)([A-Z0-9\-\_]+)$/i, '$3');
          if (intv && intv != '') {
            $intv.find('.item a[rel*='+sort+']').menuDropClick(true);
            url += (url.indexOf('?') >= 0 ? '&' : '?') + 'interval='+ intv;
          }
        }

        // Get grid view
        if ($modes.length > 0) {
          var view = (/(view\=)([A-Z0-9\-\_]+)/i.test(url)) ? url.replace(/^(.*)(view\=)([A-Z0-9\-\_]+)(.*)$/i, '$3') : false;
          url = url.replace(/(&?)(view\=)([A-Z0-9\-\_]+)/i, '').replace(/(.*)(\?)$/, '$1');
          if (!view || view == '') view = $.cookie('_view_mode') || ($modes.find('a.icon.selected').length > 0 ? $modes.find('a.icon.selected') : $modes.find('a.icon:eq(0)')).attr('rel').replace(/(.*)(\s)([A-Z0-9\-\_]+)$/i, '$3');
          $modes.find('a.icon').removeClass('selected').css({'font-weight':'normal'});
          if (view && view != '') {
            $modes.find('a.icon[rel*='+ view +']').addClass('selected').css({'font-weight':'bold'});
            url += (url.indexOf('?') >= 0 ? '&' : '?') + 'view='+ view;
          } else {
            $modes.find('a.icon:eq(0)').addClass('selected').css({'font-weight':'bold'});
          }
          $.cookie('_view_mode', view);
        }
        
        if (item) $.anchoring.set(url);

        // Need to simplify this somehow. :/
        if (!cv_box.hasClass('loading')) {
          cv_box.animate({'height':'hide'}, 500, 'swing', function() {
            $(this).html('&nbsp;').addClass('loading').show();
            $.anchoring.retrieve({dataType:'HTML', url:url, success : function(response) {
              cv_box.removeClass('loading').hide().html(response).animate({'height':'show'}, 500, 'swing');
              $(window).trigger('channel.load', [cv_box]);
            }});
          });
        } else {
          $.anchoring.retrieve({dataType:'HTML', url:url, success : function(response) {
            cv_box.removeClass('loading').hide().html(response).animate({'height':'show'}, 500, 'swing');
            $(window).trigger('channel.load', [cv_box]);
          }});
        }
      }
    },
    Default : function(item) {
      $.anchoring.funcs.page(item ? item : $.anchoring.settings.location);
    }
  },
  VideoStats : {
    Func : function(item, data) {
      var url = (item && item.href && item.href != '' ? item.href : item);
      if (item && url) $.anchoring.set(url);
      if (url) {
        $('#video_details_area').addClass('loading').children().hide().empty();

        $.anchoring.retrieve({dataType:'HTML', url:url, success : function(response) {
          if (data) {
            $(item).sTabs({tabs:'#tab_video_details', func : function() {
              $('#video_details_area .tab_block').hide();
              $('#video_stats_'+data).show();
            }});
            // Must show first before adding HTML, otherwise breaks sparklines.
            $('#video_stats_'+data).html(response);
            $('#video_details_area').removeClass('loading');
          } else {
            // debug purposes (DELETE LATER or make better msg prompt!)
            alert('no data!');
          }
        }});
      }
    },
    Default : function(item) {$('#tab_video_details_views').click();}
  },
  Dashboard : {
    Func : function(item) {
      var url = (item && item.href && item.href != '' ? item.href : item), n_box = $('#n_area');
      if (url) {
        if (!n_box.hasClass('loading')) n_box.animate({'height':'hide'}, 500, 'swing', function() {$(this).html('&nbsp;').addClass('loading').show();});

        /*
          // If there are menu items, select that one.
          var sort = (/(sort\=)([A-Z0-9\-\_]+)/i.test(url)) ? url.replace(/^(.*)(sort\=)([A-Z0-9\-\_]+)(.*)$/i, '$3') : false;
          var view = (/(view\=)([A-Z0-9\-\_]+)/i.test(url)) ? url.replace(/^(.*)(view\=)([A-Z0-9\-\_]+)(.*)$/i, '$3') : false;
          url = url.replace(/(&?)(sort\=)([A-Z0-9\-\_]+)/i, '').replace(/(&?)(view\=)([A-Z0-9\-\_]+)/i, '').replace(/(.*)(\?)$/, '$1');

          // Get sorting method
          if (!sort || sort == '') sort = ($('.menu .items .item.selected a').length > 0 ? $('.menu .items .item.selected a') : $('.menu .items .item:eq(0) a')).attr('rel').replace(/(.*)(\s)([A-Z0-9\-\_]+)$/i, '$3');
          if (sort && sort != '') {
            $('.menu .items .item a[rel*='+sort+']').menuDropClick(true);
            if (sort != 'added') url += (url.indexOf('?') >= 0 ? '&' : '?') + 'sort='+ sort;
          }

          // Get grid view
          if (!view || view == '') view = ($('.view_modes a.icon.selected').length > 0 ? $('.view_modes a.icon.selected') : $('.view_modes a.icon:eq(0)')).attr('rel').replace(/(.*)(\s)([A-Z0-9\-\_]+)$/i, '$3');
          $('.view_modes a.icon').removeClass('selected').css({'font-weight':'normal'});
          if (view && view != '') {
            $('.view_modes a.icon[rel*='+ view +']').addClass('selected').css({'font-weight':'bold'});
            if (view != 'list') url += (url.indexOf('?') >= 0 ? '&' : '?') + 'view='+ view;
          } else {
            $('.view_modes a.icon:eq(0)').addClass('selected').css({'font-weight':'bold'});
          }
        */

        if (item) $.anchoring.set(url);

        $.anchoring.retrieve({dataType:'HTML', url:url, success : function(response) {
          n_box.removeClass('loading').hide().html(response).animate({'height':'show'}, 500, 'swing');
          n_box.find('.video, #video_icons').vbuttons();
          n_box.find('a.click2play').click2play();
          n_box.find('.loctime').locTime();
          n_box.find('a.flag').click(flagVideo);
        }});
      }
    },
    Default : function(item) {
      $.anchoring.funcs.dashboard(item ? item : $.anchoring.settings.location);
    }
  }
};