var xmlHttp1; //get Categories;
var xmlHttp2; //top 10 top level
var xmlHttp3; //top 10 category specific
var xmlHttp4; //open link
var xmlHttp5; //getSID
var xmlHttp6; //reload links
var xmlHttp7; //stats
var xmlHttp8; //movers
var xmlHttp9; //scroll
var xmlHttp10; //loadAllCategories
var x1;
var x2;//get number of links in category;



function getScrollNum(cat){
  x2=GetXmlHttpObject();
  
  if (x2==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 

  var url="getScrollNum.php?cat="+cat+"";
  x2.onreadystatechange=numLinks; 
  x2.open("GET",url,true);
  x2.send(null);

}

function numLinks(){
  if (x2.readyState==4 || x2.readyState=="complete"){ 
    scrollNum = x2.responseText;
  }
}


function puvinInfo(){
  info.innerHTML = "<div style=\"text-align:center;font-family:arial;font-size:12px;\"><strong>About puvin.com</strong><p>puvin is the only bookmark you need, puvin provides quick, easy access to all of the best places on the web.</p><p>puvin is free, puvin recieves no payment from any listed site nor does puvin endorse any listed site.</p><p>All categories and site lists on puvin are positioned based on puvin user inputs and actions ensuring that you see the most popular user defined sites within each category first. Our fastest climbing sites are arranged based on their rapid growth in popularity. All remaining sites within a category are available on the ticker screen.</p></div>"; 
  info.style.visibility = "visible";
	
}

function puvinLoading(){


  info.innerHTML = "<table style=\"filter:alpha(opacity=50);-moz-opacity:.5;opacity:.5\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"550\"><!-- fwtable fwsrc=\"LOADING.png\" fwbase=\"loading.gif\" fwstyle=\"Generic\" fwdocid = \"1087829663\" fwnested=\"1\" -->  <tr>   <td><img name=\"loading_1\" src=\"images/loading_1.gif\" width=\"33\" height=\"180\" border=\"0\" id=\"loading_1\" alt=\"The Citrus Stand Media Group Inc\" /></td>   <td><table align=\"left\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"517\">	  <tr>	   <td><img name=\"loading_2\" src=\"images/loading_2.gif\" width=\"517\" height=\"39\" border=\"0\" id=\"loading_2\" alt=\"The Citrus Stand Media Group Inc\" /></td>	  </tr>	  <tr>	   <td><table align=\"left\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"517\">		  <tr>		   <td><img name=\"loading_3\" src=\"images/loading-anim.gif\" width=\"102\" height=\"95\" border=\"0\" id=\"loading_3\" alt=\"The Citrus Stand Media Group Inc\" /></td>		   <td><img name=\"loading_4\" src=\"images/loading_4.png\" width=\"415\" height=\"95\" border=\"0\" id=\"loading_4\" alt=\"The Citrus Stand Media Group Inc\" /></td>		  </tr>		</table></td>	  </tr>	  <tr>	   <td><img name=\"loading_5\" src=\"images/loading_5.gif\" width=\"517\" height=\"46\" border=\"0\" id=\"loading_5\" alt=\"The Citrus Stand Media Group Inc\" /></td>	  </tr>	</table></td>  </tr></table>";
  info.style.visibility = "visible";
	
}


function hidePuvinInfo(){
	info.style.visibility = "hidden";
}

function send(){
	x1=GetXmlHttpObject();
  
  if (x1==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 

  email = document.getElementById('email').value;
  name = document.getElementById('name').value;
  bod = document.getElementById('bod').value;


  var url="send.php?email="+email+"&name="+name+"&body="+bod+"";
  x1.onreadystatechange=sent; 
  x1.open("GET",url,true);
  x1.send(null);
	
}

function sent(){
	if (x1.readyState==4 || x1.readyState=="complete"){ 
		document.getElementById('contact').innerHTML= "Your message has been received.";
	}
	else{
		document.getElementById('contact').innerHTML= "Sending Message...";
	}
	
	
}

function contact(){
	window.open('contact.html', '_blank', 'directories=0,fullscreen=0,height=400,location=0,menubar=0,resizable=1,scrollbars=0,status=0,titlebar=0,toolbar=0,width=450');
}



var clicksArrLen = 5000;
var clicksArr = new Array(clicksArrLen);

function init_array(){
	for(i=0;i<clicksArrLen;i++){
		clicksArr[i] = 0;
	}
	
}
 



function load(){
  info= document.getElementById('info');
  info_td = document.getElementById('info_td');
  puvinInfo();
  //puvinLoading();
  startTimer();
  document.getElementById('parent').value= "0";
  loadTop10();
  cats();
  allCats();
  loadMovers();
  loadScroll();
  getScrollNum(0);
  scroll();
  init_array();  
  document.getElementById('bod').onload = reload;

}

function reload(){
  reloadCategory();
  loadCats();
}

var clearTimer;
var setTimer;
var time = 0;
var done = true;

function startTimer(){
  
  clearInterval(clearTimer);
  setTimer=setInterval("logTime()",2000);
}

function logTime(){
  if(done){
   time = time+2;
   document.getElementById("time").value = time;
   arr = new Array("timer.php","timer","time","sid");
   XHRTimer(arr);
  }
}

function cats(){
  xmlHttp1=GetXmlHttpObject();

  if (xmlHttp1==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 

  var url="getCategories.php";
  xmlHttp1.onreadystatechange=catsLoaded; 
  xmlHttp1.open("GET",url,true);
  xmlHttp1.send(null);
}



function catsLoaded() 
{ 
if (xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete")
  {
    catsText = xmlHttp1.responseText;
    loadCats();
  } 
}

function loadCats(){
    thisElement = document.getElementById("categories");
    thisElement.style.width = "220px";
    thisElement.innerHTML= catsText;
    document.getElementById("catBG").style.visibility = "hidden";
}



function allCats(){
  xmlHttp10=GetXmlHttpObject();

  if (xmlHttp10==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 

  var url="getAllCategories.php";
  xmlHttp10.onreadystatechange=allCatsLoaded; 
  xmlHttp10.open("GET",url,true);
  xmlHttp10.send(null);
}



var allCatsText = "";
var catsText = "";


function allCatsLoaded(){
  if (xmlHttp10.readyState==4 || xmlHttp10.readyState=="complete"){ 
     allCatsText=xmlHttp10.responseText;
  }
}

function loadAllCategories(){
    thisElement = document.getElementById("categories");
    thisElement.style.width = "660px";
    thisElement.innerHTML = allCatsText;
    document.getElementById("catBG").style.visibility = "visible";
}






function loadTop10(){
  xmlHttp2=GetXmlHttpObject();

  if (xmlHttp2==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 

  var url="top10.php?parent=0";
  xmlHttp2.onreadystatechange=top10Changed; 
  xmlHttp2.open("GET",url,true);
  xmlHttp2.send(null);
}



var info; 
var info_td; 


function show_desc(id){
  thisDiv = document.getElementById(id);
  info.innerHTML = thisDiv.innerHTML; 
  info.style.visibility = "visible";
  //document.getElementById("puvin-bounce-large").style.visibility = "hidden";
}

function hide_desc(id){
  info.style.visibility = "hidden";
  //document.getElementById("puvin-bounce-large").style.visibility = "visible";
}


function reloadCategory(){
  var str = document.getElementById('parent').value;

  xmlHttp6=GetXmlHttpObject();
  if (xmlHttp6==null){
    alert ("Browser does not support HTTP Request")
    return;
  }
 
  var url="top10.php";
  url=url+"?parent="+str+"&sid=0";
  xmlHttp6.onreadystatechange=reloadCatDone; 
  xmlHttp6.open("GET",url,true);
  xmlHttp6.send(null);
}

function reloadCatDone(){ 
  if (xmlHttp6.readyState==4 || xmlHttp6.readyState=="complete"){ 
    document.getElementById("top10").innerHTML=xmlHttp6.responseText; 
    loadMovers();
    loadScroll();
    
  } 
}


function loadCategory(serial){
  document.getElementById('parent').value= serial;
  var sid = document.getElementById('sid').value;

  getScrollNum(serial);  

  xmlHttp3=GetXmlHttpObject();
  if (xmlHttp3==null){
    alert ("Browser does not support HTTP Request")
    return;
  }
 
  var url="top10.php";
  url=url+"?parent="+serial+"&sid="+sid+"";
  xmlHttp3.onreadystatechange=top10CatChanged; 
  xmlHttp3.open("GET",url,true);
  xmlHttp3.send(null);
}



function top10CatChanged() { 
  if (xmlHttp3.readyState==4 || xmlHttp3.readyState=="complete"){ 
    document.getElementById("top10").innerHTML=xmlHttp3.responseText;
    var cat_name = document.getElementById('cat_name'); 
    var str = cat_name.value;
    string = "puvin's Top 10 in "+str;
    document.getElementById('top_10_text').innerHTML = string;
    loadMovers();
    loadScroll();
    
  } 
} 



function loadMovers(){
  var str = document.getElementById('parent').value;

  xmlHttp8=GetXmlHttpObject();
  if (xmlHttp8==null){
    alert ("Browser does not support HTTP Request")
    return;
  }
 
  var url="movers.php?parent="+str;
  xmlHttp8.onreadystatechange=moversChanged; 
  xmlHttp8.open("GET",url,true);
  xmlHttp8.send(null);
}
function moversChanged() { 
  if (xmlHttp8.readyState==4 || xmlHttp8.readyState=="complete"){ 
    document.getElementById("movers").innerHTML=xmlHttp8.responseText; 
    var cat_name = document.getElementById('mov_cat_name'); 
    var str = cat_name.value;
    var string = "puvin's Fastest Climbing in "+str;
    document.getElementById("movers_text").innerHTML = string;
  } 
} 


var scrollClear1;
var scrollSet1;
var scrollClear2;
var scrollSet2;
var speed=3;
var i=0;
var c=1; 
var scrollCount;
var scrollNum = 0;
var trScroll;
var scrollText;
var scrollDone = true;



function resetScroll(){
  c=1;
  document.getElementById("scroll").innerHTML = scrollText;
}


function loadScroll(){
  str = document.getElementById('parent').value;

  xmlHttp9=GetXmlHttpObject();
  if (xmlHttp9==null){
    alert ("Browser does not support HTTP Request")
    return;
  }
 
  url="scroll.php?parent="+str+"";
  xmlHttp9.onreadystatechange=scrollChanged; 
  xmlHttp9.open("GET",url,true);
  xmlHttp9.send(null);
}


function scrollChanged() { 
  if (xmlHttp9.readyState==4 || xmlHttp9.readyState=="complete"){
    scrollText =  xmlHttp9.responseText;
    resetScroll();
    //puvinInfo();
  } 
} 

function scroll()
{
	clearInterval(scrollClear1);
	scrollSet1=setInterval("moveScroll()",50);
	
}


var scrollTempId = "";
var thisElement;
var tempid;
var tempElement;
var scrollText = "";


function moveScroll(){
  i=i+speed;
  if(scrollDone){
    scrollDone = false;
    if(i>scrollNum*120+550){
      /*    scrollTempId = "num"+c;
      trScroll = document.getElementById('trScroll');
      thisElement = document.getElementById(scrollTempId);
      tempid = "num"+scrollNum;
      tempElement = document.createElement('td');
      tempElement.setAttribute("id",tempid);
      scrollText = thisElement.innerHTML;
      tempElement.innerHTML = scrollText;
      tempElement.onclick = thisElement.onclick;
      tempElement.onmouseover = thisElement.onmouseover;
      tempElement.onmouseout = thisElement.onmouseout;
      trScroll.appendChild(tempElement);
      trScroll.removeChild(thisElement);
      i=i-120
      c++;
      scrollNum++;
 
      */
      i=0;
      resetScroll();
      loadScroll();
    }
    tempi = 790-i;
    document.getElementById('scroll').style.left=tempi;
    scrollDone = true;
  }
}

function speedUp(){
  if(speed<20){
    speed++;
  }
}

function slowDown(){
  if(speed>0){
    speed--;
  }
}

function stopScroll(){
  speed=0;
}









function top10Changed() 
{ 
  if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")
  { 
    document.getElementById('top10').innerHTML=xmlHttp2.responseText; 
    var cat_name = document.getElementById('cat_name'); 
    var str = cat_name.value;
    string = "puvin's Top 10 in "+str;
    document.getElementById('top_10_text').innerHTML = string;
  } 
} 





 



function GetXmlHttpObject()
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
} 


function openLink(link,serial,parent){

  
    var sid = document.getElementById('sid').value;
 temp = 0;
 if(parent >=0 ){
  temp = clicksArr[serial];
  temp++;
  clicksArr[serial] = temp;
 }
  if(temp<=2){
	 xmlHttp4=GetXmlHttpObject();
	
	 if (xmlHttp4==null)
	 {
	 alert ("Browser does not support HTTP Request")
		return;
	 }

 	var url="link.php";
 	url=url+"?serial="+serial+"&parent="+parent+"&sid="+sid+"";
 	xmlHttp4.onreadystatechange=linkClicked;
 	xmlHttp4.open("GET",url,true);
 	xmlHttp4.send(null);
  }
 window.open(link, '_blank');
 
}

function linkClicked(){
  if (xmlHttp4.readyState==4 || xmlHttp4.readyState=="complete"){ 
    reloadCategory();
    loadCats();
  } 


}

var x3;

function visit(serial,parent){

  var sid = document.getElementById('sid').value;
  
  	 x3=GetXmlHttpObject();
	
	 if (x3==null)
	 {
	 alert ("Browser does not support HTTP Request")
		return;
	 }

 	var url="link.php";
 	url=url+"?serial="+serial+"&parent="+parent+"&sid="+sid;
 	x3.open("GET",url,true);
 	x3.send(null);
}



