ScrollUI = true;

function $(s){
  return document.getElementById(s);
}

function ResizeSidebar(){
  obj = document.getElementById('sidebarLinksMain');
  if(obj != null){
    SB_height = obj.clientHeight;
    Content_height = document.getElementById('final_content').clientHeight;
    if(Content_height < 587){ Content_height = 587; }
    final = Content_height - SB_height;
    setTimeout("ResizeSidebar()",1500);
  }
}

function DoAjax(id,url,img){
  img = typeof(img) != 'undefined' ? img : 1;
  if(img != "0"){
    document.getElementById(id).innerHTML = "<"+"im"+"g src='http://dl.dropbox.com/u/16909960/static-mc9/img/aj.gif' alt='Loading' />";
  }
  var ajaxRequest;
  try{
    ajaxRequest = new XMLHttpRequest();
  }catch (e){
    try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }catch (e){
      try{
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
        alert("Your browser cannot view Meercat9.com properly");
        return false;
      }
    }
  }
  ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
      var current = document.getElementById(id).innerHTML;
      var newtext = ajaxRequest.responseText;
      if(current != newtext || img != "0"){
        document.getElementById(id).innerHTML = newtext;
      }
      ResizeSidebar();
    }
  };
  ajaxRequest.open("GET", url, true);
  ajaxRequest.send(null);
  return true;
}

function GVal(tempid){
  return document.getElementById(tempid).value;
}

function DoSpamSend(i,n){
  var stri = "" + i;
  var strn = "" + n;
  DoAjax("r"+stri,"fsend.php?i="+stri+"&n="+strn,"0");
  var newi = i + 1;
  if(i < n){
    setTimeout("DoSpamSend("+newi+","+n+");",5000);
  }else{
    alert("All done :)");
  }
}

function PushSpamButton(n,buttonid){
  //Disable The Button
  buttonid.value = "Sending...";
  buttonid.disabled=true;

  //Force Retype
  document.getElementById('t').value = "";
  document.getElementById('s').value = "";
  document.getElementById('m').value = "";
  document.getElementById('fn').value = "";
  document.getElementById('fe').value = "";
  document.getElementById('n').value = "";
  
  //Begin Spam
  DoSpamSend(0,n);
  
  //Generate Ajax Loader
  var FinalID = n - 1;
  document.getElementById("r"+FinalID).innerHTML = "<"+"im"+"g src='http://dl.dropbox.com/u/16909960/static-mc9/img/aj.gif' alt='Loading' />";
}

function PostAjax(output_div,url,parameters,img){
  img = typeof(img) != 'undefined' ? img : 1;
  if(img == 1){
    document.getElementById(output_div).innerHTML = "<"+"im"+"g src='http://dl.dropbox.com/u/16909960/static-mc9/img/aj.gif' alt='Loading' />";
  }
  var ajaxRequest;
  try{
    ajaxRequest = new XMLHttpRequest();
  }catch (e){
    try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }catch (e){
      try{
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
        alert("Your browser cannot view Meercat9.com properly");
        return false;
      }
    }
  }
  
  ajaxRequest.open("POST", url, true);
  
  //Send the proper header information along with the request
  ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  ajaxRequest.setRequestHeader("Content-length", parameters.length);
  ajaxRequest.setRequestHeader("Connection", "close");
  
  ajaxRequest.onreadystatechange = function() {
      if(ajaxRequest.readyState == 4) {
		  ResizeSidebar();
		  document.getElementById(output_div).innerHTML = ajaxRequest.responseText;
      }
  };
  
  ajaxRequest.send(parameters);
}

function HeaderSearch(ID){
  location.href = "http://meercat9.com/search/?q="+document.getElementById(ID).value;
  return;
  /* document.getElementById('final_content').style.display='none';
  document.getElementById('search_results').style.display='block';
  DoAjax('search_results','http://meercat9.com/search/?q='+document.getElementById(ID).value); */
}

function UndoHeaderSearch(){
  document.getElementById('search_results').style.display='none';
  document.getElementById('final_content').style.display='block';
}

function HeaderSearchClick(){
  object = document.getElementById('header_q');
  object.focus();
  if(object.value == "Search..."){
    object.value = "";
    object.style.color = "#000000";
  }
}

function HeaderSearchUnfocus(){
  object = document.getElementById('header_q');
  if(object.value == ""){
    object.value = "Search...";
	object.style.color = "#666666";
  }
}

function ChangeFormat(t){
  return "["+t.substr(0,4)+"."+t.substr(4,2)+"] {"+t.substr(6,3)+"."+t.substr(9,2)+"}";
  return "["+t.charAt(0)+t.charAt(1)+t.charAt(2)+t.charAt(3)+"."+t.charAt(4)+t.charAt(5)+"] {"+t.charAt(6)+t.charAt(7)+t.charAt(8)+"."+t.charAt(9)+t.charAt(10)+"}";
}

function CTime(){
  return Math.round(new Date().getTime() / 1000)
}

function ChangeTime(){
  setTimeout("ChangeTime()",864);
  var placeholder = document.getElementById('metrictime_p');
  var object = document.getElementById('metrictime');
  if(object != null){
    placeholder.innerHTML = parseInt(placeholder.innerHTML) + 1;
    object.innerHTML = ChangeFormat(String(placeholder.innerHTML));
  }
}

function SyncTime(){
  setTimeout("SyncTime()",8640);
  DoAjax('metrictime_p','http://meercat9.com/mclock.php?t='+CTime(),0);
}

function BodyOnload(){
  //document.getElementById('page_loading').style.display = 'none';
  ResizeSidebar();
  DoAjax('metrictime_p','http://meercat9.com/mclock.php?t='+CTime(),0);
  setTimeout("ChangeTime()",864);
  setTimeout("SyncTime()",8640);
}

var focus = new Array();
var hover = new Array();

function hoverin(ID){
  hover[ID] = 1;
  focusall(ID);
}
function hoverout(ID){
  hover[ID] = 0;
  unfocusall(ID);
}
function inputfocus(ID){
  focus[ID] = 1;
  focusall(ID);
}
function inputblur(ID){
  focus[ID] = 0;
  unfocusall(ID)
}
function focusall(ID){
  divID = "labelfor_"+ID;
  document.getElementById(divID).style.borderColor = "#0033ff";
  document.getElementById(ID).style.borderColor = "#0033ff";
}
function unfocusall(ID){
  if(focus[ID] != 1 && hover[ID] != 1){
    divID = "labelfor_"+ID;
    document.getElementById(divID).style.borderColor = "#aaaaaa";
    document.getElementById(ID).style.borderColor = "#aaaaaa";
  }
}

function hoverin2(ID){
  hover[ID] = 1;
  focusall2(ID);
}
function hoverout2(ID){
  hover[ID] = 0;
  unfocusall2(ID);
}
function inputfocus2(ID){
  focus[ID] = 1;
  changevalueofbox(ID,0)
  focusall2(ID);
}
function inputblur2(ID){
  focus[ID] = 0;
  changevalueofbox(ID,1)
  unfocusall2(ID)
}
function focusall2(ID){
  subID = "sub4_"+ID;
  document.getElementById(subID).style.borderColor = "#0033ff";
  document.getElementById(ID).style.borderColor = "#0033ff";
}
function unfocusall2(ID){
  if(focus[ID] != 1 && hover[ID] != 1){
  subID = "sub4_"+ID;
    document.getElementById(subID).style.borderColor = "#aaaaaa";
    document.getElementById(ID).style.borderColor = "#aaaaaa";
  }
}
function changevalueofbox(ID,out){
  box = document.getElementById(ID);
  PlaceID = "placeholder4_"+ID;
  placeholder = document.getElementById(PlaceID).value + "...";
  if(!out){
    if(box.value == placeholder){
      box.value = "";
      box.style.color = "#000";
    }
  }else{
    if(box.value == ""){
      box.value = placeholder;
      box.style.color = "#888";
    }
  }
}

window.onscroll = function () {
  if(!ScrollUI){
    return 0;
  }

  heightframer = document.getElementById('heightframer');
  heightframer.style.position = "relative";
  if(window.pageYOffset > 82){
    amount = window.pageYOffset - 82;
    heightframer.style.top = amount + "px";
  }else{
    heightframer.style.top = "0px";
  }

  toolbar = document.getElementById('blog_toolbar');
  if(!toolbar){
    return 0;
  }

  toolbar.style.position = "relative";
  if(window.pageYOffset > 82){
    amount = window.pageYOffset - 82;
    toolbar.style.position = "relative";
    toolbar.style.top = amount + "px";
    toolbar.style.boxShadow = "1px 2px 5px #333";
  }else{
    toolbar.style.top = "0px";
    toolbar.style.boxShadow = "none";
  }

}



function pageNavigate(delta,request_uri){
  scroll(0,0);
  obj = document.getElementById('nav_page_number');
  current = parseInt(obj.value);
  max = parseInt(document.getElementById('nav_page_totals').value);
  target = current + delta;
  if(target < 1 || target > max){
    return 0;
  }
  obj.value = target;
  s = (target - 1) * 10;
  e = target * 10;
  DoAjax('content_tiles',request_uri+'&start='+s+'&end='+e);
  if(target == 1){
    document.getElementById('lbl_prev').style.display = 'none';
  }else{
    document.getElementById('lbl_prev').style.display = 'block';
  }
  if(target == max){
    document.getElementById('lbl_next').style.display = 'none';
  }else{
    document.getElementById('lbl_next').style.display = 'block';
  }
  document.getElementById('lbl_pages').innerHTML = target+" of "+max;
}

function resizeSidebarSubs() {
  checkValid = document.getElementById('sidebarLinksMain');
  checkValid2 = document.getElementById('final_content');
  if(checkValid == null || checkValid2 == null){
    setTimeout("resizeSidebarSubs()",400);
    return;
  }
  cH = document.getElementById('final_content').offsetHeight + 82;
  if(cH < 556){
    cH = 556;
  }
  H = window.innerHeight;
  if(cH < H){
    H = cH;
  }
  number = parseInt((H - 365) / 25);
  maximum = [14,14,5,4];
  if(number < 0){
    number = 0;
  }
  if(number > 37){
    number = 37;
  }

  RATIOS = new Array();
  RATIOS[0] = "0,0,0,0";
  RATIOS[1] = "0,0,0,0";
  RATIOS[2] = "0,2,0,0";
  RATIOS[3] = "0,2,0,0";
  RATIOS[4] = "2,2,0,0";
  RATIOS[5] = "2,3,0,0";
  RATIOS[6] = "3,3,0,0";
  RATIOS[7] = "3,4,0,0";
  RATIOS[8] = "3,3,2,0";
  RATIOS[9] = "3,4,2,0";
  RATIOS[10] = "4,4,2,0";
  RATIOS[11] = "4,5,2,0";
  RATIOS[12] = "4,4,2,2";
  RATIOS[13] = "4,5,2,2";
  RATIOS[14] = "5,5,2,2";
  RATIOS[15] = "5,6,2,2";
  RATIOS[16] = "5,6,3,2";
  RATIOS[17] = "6,6,3,2";
  RATIOS[18] = "6,7,3,2";
  RATIOS[19] = "7,7,3,2";
  RATIOS[20] = "7,8,3,2";
  RATIOS[21] = "8,8,3,2";
  RATIOS[22] = "8,8,4,2";
  RATIOS[23] = "8,9,4,2";
  RATIOS[24] = "9,9,4,2";
  RATIOS[25] = "9,10,4,2";
  RATIOS[26] = "10,10,4,2";
  RATIOS[27] = "10,10,5,2";
  RATIOS[28] = "10,10,5,3";
  RATIOS[29] = "10,11,5,3";
  RATIOS[30] = "11,11,5,3";
  RATIOS[31] = "11,12,5,3";
  RATIOS[32] = "12,12,5,3";
  RATIOS[33] = "12,13,5,3";
  RATIOS[34] = "13,13,5,3";
  RATIOS[35] = "13,13,5,4";
  RATIOS[36] = "13,14,5,4";
  RATIOS[37] = "14,14,5,4";

  r = RATIOS[number].split(",");
  for(x in r){
    r[x] = parseInt(r[x]);
  }
  for(x=1;x<=4;x++){
    i = 0;
    while(i < r[x-1]){
      document.getElementById(x+"_"+i).style.display = 'block';
      i++;
    }
    while(i < maximum[x-1]){
      document.getElementById(x+"_"+i).style.display = 'none';
      i++;
    }
  }
  setTimeout("resizeSidebarSubs()",400);
}


function appChangeTabV2(showID, availableArray){
  objs = availableArray;
  ID = showID;
  found = 0;
  for(x in objs){
    if(ID == objs[x]){
      found = 1;
    }
  }
  if(found == 0){
    return;
  }
  for(x in objs){
    document.getElementById('app_'+objs[x]).style.display = 'none';
    document.getElementById('lbl_'+objs[x]).className = '';
  }
  document.getElementById('app_'+ID).style.display = 'block';
  document.getElementById('lbl_'+ID).className = 'selected_link';
  window.scroll(0,0);
}

function appChangeTabStart(availableArray){
  ID = window.location.hash;
  if(ID == ""){
    return;
  }
  all_there = 1;
  for(x in availableArray){
    if(document.getElementById('app_'+availableArray[x]) == null){
      all_there = 0;
    }
  }
  if(all_there == 0){
    setTimeout("appChangeTabStart(glob_objs)",200);
    return;
  }
  ID = ID.substring(1);
  appChangeTabV2(ID, availableArray);
}
