//debugger;

var siteRank = 1,
   gettingList = false,
   panelID = 1;

function applicationLoadHandler() 
{
   var dimensions = new Dimensions();
   GetToplist();
   $addHandler(window, "scroll", DoScroll);
}

function Dimensions()
{
   if (document.documentElement && document.documentElement.scrollTop)
   {
	   this.scrollTop = document.documentElement.scrollTop;
	   this.clientHeight = document.documentElement.clientHeight;
	}
   else
   {
	   this.scrollTop = document.body.scrollTop;
	   this.clientHeight = document.body.clientHeight;
	}
   this.offsetHeight = document.body.offsetHeight;
   this.difference = this.offsetHeight - this.scrollTop;
   this.scrollMore = false;

   if(this.scrollTop > 0)
   {
      this.scrollMore = this.difference < (this.clientHeight + 500);
   }
}

function DoScroll(eventElement)
{
   var dimensions = new Dimensions();

   if(!gettingList && dimensions.scrollMore)
   {
      GetToplist();
   }
}

function GetToplist()
{
   gettingList = true;
   ToplistWeb.ToplistWS.GetTopList(siteRank, panelID, GetTopListSucceeded);
}

function SiteClick(element)
{
   var rootUrl = "http://www.zirve100.com";
   window.open(rootUrl + "/Link.aspx?s=" + element.getAttribute("siteNo") + "&u=" + element.getAttribute("url"), "toplist");
}

function ShowSiteInfo(element, showPreview)
{
   element.title = element.getAttribute('url') + ' - ' + element.getAttribute('siteNo');
   element.style.textDecoration = 'underline';
   if(showPreview)
   {
      ab(element);
   }
}

function HideSiteInfo(element, showPreview)
{
   element.style.textDecoration='none';
   if(showPreview)
   {
      db(element);
   }
}

function GetTopListSucceeded(result, eventArgs)
{
   if(result.ToplistRankings.length == 0)
   {
      $removeHandler(window, "scroll", DoScroll);
      return;
   }
   
   var newTable = $get("ToplistTable").cloneNode(true);
   newTable.id = "ToplistTable" + panelID;
   for(var i = 0; i < result.ToplistRankings.length; i++)
   {
      var newRow = newTable.tBodies[0].rows[0].cloneNode(true);
      newTable.tBodies[0].appendChild(newRow);
      
      newRow.className = i%2==1?"oddrow":"";
      
      var imageSrc = "images/" + result.ToplistRankings[i].Direction + ".gif";
      var imageAlt = result.ToplistRankings[i].DirectionNative;
      
      newRow.title = result.ToplistRankings[i].SiteDescription;
      newRow.url = result.ToplistRankings[i].Url;
      newRow.cells[0].childNodes[0].href = "/WebSiteDetails.aspx?s=" + result.ToplistRankings[i].SiteNo;
      newRow.cells[0].childNodes[0].innerHTML = result.ToplistRankings[i].Rank;
      newRow.cells[1].childNodes[0].src = imageSrc;
      newRow.cells[1].childNodes[0].alt = imageAlt;
      newRow.cells[2].childNodes[0].setAttribute("siteNo", result.ToplistRankings[i].SiteNo);
      newRow.cells[2].childNodes[0].setAttribute("url", result.ToplistRankings[i].Url);
      newRow.cells[2].childNodes[0].setAttribute("hasTn", result.ToplistRankings[i].HasThumbnail);
      newRow.cells[2].childNodes[0].innerHTML = result.ToplistRankings[i].ShortUrl;
      newRow.cells[2].childNodes[2].innerHTML = result.ToplistRankings[i].SiteName;
      newRow.cells[3].innerHTML = result.ToplistRankings[i].PageRank;
      newRow.cells[4].childNodes[0].href = "/Search.aspx?CategoryID=" + result.ToplistRankings[i].CategoryID;
      newRow.cells[4].childNodes[0].innerHTML = result.ToplistRankings[i].ShortCategoryName;
      newRow.cells[4].childNodes[0].title = result.ToplistRankings[i].CategoryName;
      newRow.cells[5].innerHTML = result.ToplistRankings[i].UniqueHits;
      if(result.ToplistRankings[i].YesterdayRank > 0)
      {
         newRow.cells[6].childNodes[1].innerHTML = "(" + result.ToplistRankings[i].YesterdayRank + ")";
         newRow.cells[6].childNodes[0].innerHTML = result.ToplistRankings[i].YesterdayUniqueHits;
      }
      else
      {
         newRow.cells[6].innerHTML = "Yeni";
      }
   }
   newTable.tBodies[0].deleteRow(0);
   newTable.tFoot.rows[0].cells[0].innerHTML = result.AdString;
   $get("DivTopList").appendChild(newTable);
   
   var dimensions = new Dimensions();
   
   siteRank += 20;
   panelID++;
   
   gettingList = false;

   if(siteRank < (20 * result.TotalAds))
      GetToplist();
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
