//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){
  var yScroll;
  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) {
    yScroll = document.documentElement.scrollTop;
  } else if (document.body) {
    yScroll = document.body.scrollTop;
  }
  arrayPageScroll = new Array('',yScroll) 
  return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize() {
  var xScroll, yScroll;
  if (window.innerHeight && window.scrollMaxY) {	
    xScroll = document.body.scrollWidth;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight) {
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else {
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }
	
  var windowWidth, windowHeight;
  if (self.innerHeight) {	// all except Explorer
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  } else if (document.documentElement &&
             document.documentElement.clientHeight) {
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }	
	
  // for small pages with total height less then height of the viewport
  if (yScroll < windowHeight) {
    pageHeight = windowHeight;
  } else { 
    pageHeight = yScroll;
  }

  // for small pages with total width less then width of the viewport
  if (xScroll < windowWidth) {	
    pageWidth = windowWidth;
  } else {
    pageWidth = xScroll;
  }

  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
  return arrayPageSize;
}

var page_size = getPageSize();
var page_scroll = getPageScroll();

var overlay = $('overlay');
var login_prompt = $('login_prompt');
var login_btn = $('login-fc-button');

overlay.observe('click', function(event) {
  this.style.display = 'none';
  login_prompt.style.display = 'none';
});

function showLoginForm() {
  var page_size = getPageSize();
  var page_scroll = getPageScroll();
  overlay.style.width = page_size[0] + 'px';
  overlay.style.height = page_size[1] + 'px';
  login_prompt.style.top = (page_scroll[1] + ((page_size[3] - 35 - 300) / 2) + 'px');
  login_prompt.style.left = (((page_size[0] - 20 - 350) / 2) + 'px');
  overlay.style.display = 'block';
  login_prompt.style.display = 'block';
  login_btn.style.display = 'block';
}

function hideLoginForm() {
  login_prompt.style.display = 'none';
  overlay.style.display = 'none';
}

login_btn.observe('click', function(event) {
  hideLoginForm();
});

function story_digg(id, slug, digg) {
  var digg_count = $('digg_count_'+id);
  var count = +digg_count.innerHTML;
  digg_count.innerHTML = "<img src='/media/img/count_loading.gif' />";
  new Ajax.Request('/story/digg/'+id+'/'+slug, {
    method: 'post',
    parameters: {digg: digg},
    onSuccess: function(transport) {
      var resp = eval('(' + transport.responseText + ')');
      if (resp.status == 'OK') {
        digg_count.innerHTML = resp.diggs; 
      } else {
        digg_count.innerHTML = count;
      }
    },
    onFailure: function() {
      digg_count.innerHTML = count; 
    }
  });
}

function storyline_digg(id, slug, digg) {
  var digg_count = $('digg_count_'+id);
  var count = +digg_count.innerHTML;
  digg_count.innerHTML = "<img src='/media/img/count_loading.gif' />";
  new Ajax.Request('/storyline/digg/'+id+'/'+slug, {
       method: 'post',
       parameters: {digg: digg},
       onSuccess: function(transport) {
         var resp = eval('(' + transport.responseText + ')');
         if (resp.status == 'OK') {
           digg_count.innerHTML = resp.diggs;
         } else {
           digg_count.innerHTML = count;
         }
       },
       onFailure: function() {
         digg_count.innerHTML = count;
       }
     });
}

function searchInit() {
  var searchbox = $('search-q');
  if (searchbox.value == 'search...') {
    searchbox.value = '';
  }
}
