//I vow that one day I will go through and clean up my shitty JS

var doAllowUpdate = 1;
var stream = "all";
var searching_for;

function updateStream() { 
    if(doAllowUpdate == 1) { 
        var type = stream;
        $.ajax({
            url: webroot+"/index/loadSection/load/"+type,
            type: "POST",
            dataType: "html",
            beforeSend: function() { 
                flipTabs(type);
                $("#"+type+"_loading").show();
            },
            success: function(data) {
              if(data != null) {
                $("#main").html(data);
                constantlyUpdateStream();
              }
              $("#"+type+"_loading").hide();
            },
            error: function(x,y,z) { 
                $("#"+type+"_loading").hide();
                delayConstantPoll(); //Lower polls per minute because those blank requests suck
                return;
            }
        });
    }

}

function constantlyUpdateStream() {

    doAllowUpdate = 1;
    setTimeout("updateStream()", 60000);

}

function delayConstantPoll() { 

    doAllowUpdate = 1;
    setTimeout("updateStream()", 180000);

}

function pauseStreamUpdating() {

    doAllowUpdate = 0;
    return true;
}

function continueStreamUpdating() {
    
    doAllowUpdate = 1;
    return true;

}

function updateOtherStream(type) {

    stream = type;
    continueStreamUpdating();
    return true;

}


    function openTab(type) { 
        continueStreamUpdating();
        updateOtherStream(type);
        $.ajax({
            url: webroot+"/index/loadSection/load/"+type,
            type: "POST",
            dataType: "html",
            beforeSend: function() { 
                flipTabs(type);
                showLoadingTab(type);
            },
            success: function(data) { 
                $("#main").html(data);
                hideLoadingTab(type);
            },
            error: function(x,y,z) { 
                throwBoxError("There was an error loading the page", y, "Please try again or reload the page");
            }
        });
        
    }
    
    function openFilter(type) { 
        pauseStreamUpdating(); //We do not update filters for now
    
        $.ajax({
            url: webroot+"/index/loadFilter/load/"+type,
            type: "POST",
            dataType: "html",
            beforeSend: function() { 
                flipTabs(type);
                showLoadingTab(type);
            },
            success: function(data) { 
                $("#main").html(data);
                hideLoadingTab(type);
            },
            error: function(x,y,z) { 
                throwBoxError("There was an error loading the page", y, "Please try again or reload the page");
            }
        });
        
    }
    
    function showLoadingTab(type) {
        $("#"+type+"_loading").show();
    }
    function hideLoadingTab(type) {
        $("#"+type+"_loading").hide();
    }

    function flipTabs(to) { 
        $(".sidetab").removeClass("selected").addClass("unselected");
        $("#"+to).addClass("selected");
    }

    function openclosechat() { 
        if ($("#chat_area").is(":hidden")) {  
  
            $("#chat_area").show();  
  
            $.cookie('showTop', 'open');  
  
            return false;  
  
        } else {  
  
            $("#chat_area").hide();    
  
            $.cookie('showTop', 'closed');  
  
            return false;  
  
        }  
    }
    
    function openclosefollowing() { 
        if ($("#following_area").is(":hidden")) {  
  
            $("#following_area").show();  
  
            $.cookie('showfollowing', 'open');  
  
            return false;  
  
        } else {  
  
            $("#following_area").hide();    
  
            $.cookie('showfollowing', 'closed');  
  
            return false;  
  
        }  
    }


    function openclosesidebar() { 
        if ($("#sidebar_left").is(":hidden")) {  
  
            $("#sidebar_left").show(); 
            $("#show_sidebar_button").hide(); 
  
            $.cookie('showsidebar', 'open');  
  
            return false;  
  
        } else {  
  
            $("#sidebar_left").hide();
            $("#show_sidebar_button").show();  
  
            $.cookie('showsidebar', 'closed');  
  
            return false;  
  
        }  
    }

    function tile_stalkers() { 
    
      $("#list_stalkers").hide();
      $("#tile_stalkers").show();
      
      $("#tile_icon").removeClass("unselected_showstalkers").addClass("selected_showstalkers");
      $("#list_icon").addClass("unselected_showstalkers").removeClass("selected_showstalkers");
      
      $.cookie('organize_stalkers', 'tile'); 
      
      return true;
    
    }


    function list_stalkers() { 
    
      $("#list_stalkers").show();
      $("#tile_stalkers").hide();
      
      $("#tile_icon").addClass("unselected_showstalkers").removeClass("selected_showstalkers");
      $("#list_icon").removeClass("unselected_showstalkers").addClass("selected_showstalkers");
      
      $.cookie('organize_stalkers', 'list'); 
    
      return true;
    }

    function dismissMessage() { 
    
        $('#notification_globalized').fadeOut();
        $.ajax({
            url: webroot+"/index/updateGlobals",
            type: "POST",
            dataType: "html",
            success: function(data) { 
                //You are awesome!
            },
            error: function(x,y,z) { 
                throwBoxError("There was an error loading the page", y, "Please try again or reload the page");
            }
        });
    
    }
    
    
    function openListEditor() { 
      
      $.facebox('<div style="padding:10px; text-align:center;"><h2>Loading...</h2></div>', null, null, "yes");
    
      $.ajax({
        url: '/friends/editListsFromHome',
        method: 'get', 
        success: function(d) { 
          $.facebox(d, null, 500, "yes");
        }
      });
    
    }  
    
    
    
    function trunicateStalkersList() { 
    
      var totalStalkers = $("#list_stalkers").find("li").length;
      var maxToShow = 20;
      
      if(totalStalkers > maxToShow) { 
        
        var i = 0;
      
        $('#list_stalkers li').each(function(index) {
          if($(this).hasClass("new_posts")) { 
            $(this).show();
            i++;
          } else {
            if(i < maxToShow) { 
              $(this).show();
              i++;
            } else { 
              $(this).hide();
            }
          }
        });
        
        $("#showmorestalkerscontainer").html('<a href="#" onClick="untrunStalkersList(); return false;">Show everyone</a>');
      }
    
    }
    
    
    
    function searchFromSidebar() { 
    //$("#sidebar_search").val()
      $('#search_tag').show();
      searching_for = $("#sidebar_search").val();
      openSearchTab();
    }
    
    function openSearchTab() { 
      pauseStreamUpdating();
      flipTabs('search_tag');
    }
  
    
    function untrunStalkersList() { 
    
      $('#list_stalkers li').each(function(index) {
        $(this).show();
      });
    
      $("#showmorestalkerscontainer").html('<a href="#" onClick="trunicateStalkersList(); return false;">Shrink list</a>');
    
    }
    
    
    $(document).ready(function() { 
    //Chat
  //  $("#chat_area").hide();
    
    var showTop = $.cookie('showTop');  
   
  
    if (showTop == 'open') {  
  
        $("#chat_area").show();  
  
    }  
    
    //Following
    $("#following_area").show();
    
    var showfollowing = $.cookie('showfollowing');  
   
  
    if (showfollowing == 'closed') {  
  
        $("#following_area").hide();  
  
    }  

    //Sidebar
    $("#sidebar_left").show(); 
    $("#show_sidebar_button").hide(); 
    
    var showsidebar = $.cookie('showsidebar');  
   
  
    if (showsidebar == 'closed') {  
  
        $("#sidebar_left").hide(); 
        $("#show_sidebar_button").show();  
  
    } 
    
    //Stalker organizer
    //Following
    
    var showfollowing = $.cookie('organize_stalkers');  
   
  
    if (showfollowing == 'list') {  
  
        $("#list_stalkers").show();  
        $("#tile_icon").addClass("unselected_showstalkers").removeClass("selected_showstalkers");
        $("#list_icon").removeClass("unselected_showstalkers").addClass("selected_showstalkers");
        
  
    } else { 
    
        $("#tile_stalkers").show();
        $("#tile_icon").removeClass("unselected_showstalkers").addClass("selected_showstalkers");
        $("#list_icon").addClass("unselected_showstalkers").removeClass("selected_showstalkers");
      
    }
    
    //Stalkers
    $(".stalkers_open").hover(function() { 
      $("#stalkers_panel").show();
    }, function() { 
      $("#stalkers_panel").hide();
    }); 
    
    //Stream
    constantlyUpdateStream();
    
    //Chrome dialog if IE.
    
    if(renderEngineDetect() == 'ie') { 
      $("#chromeable").show();
    }
    
    
    });
    
