var wpid = 0;

function closefirefox() {
  hideDialog('FirefoxSetupBox');
}

function closechrome() {
  hideDialog('ChromeSetupBox');
}

function bothfirefox() {
  showFB()
  closefirefox()
}

function bothchrome() {
  showFB()
  closechrome()
}

function setup() {
  try {
    document.f.q.focus();
  } catch(e) {
  }
  FB.init("491c6c08d6253a5c795a1af1d17081b7", "connect/xd_receiver.htm");
}

function switchEngine(url) {
  document.location = url + document.f.q.value;
  return false;
}

function bgurl() {
  return '/cache/bg.' + wpid + '.' + screen.width + 'x' + screen.height + '.jpg';
}

function setBackground() {
  var PED = document.getElementById('PreloadImageDiv');
  PED.innerHTML = "<img src='" + bgurl() + "' style='width: 1px; height: 1px;' onload='handlePreloadImage()' onerror='handlePreloadImageError()' />";
}

function handlePreloadImage() {
  loadBackground(bgurl());
}

function handlePreloadImageError() {
  loadBackground(bgurl());
}

function loadBackground(targetbgurl) {
  document.body.style.backgroundImage    = "url(" + targetbgurl + ")";
  geid('WarningDiv').style.display = 'none';
  geid('PreloadImageDiv').style.display = 'none';
}

function setHomepage(targetButton) {
  var url = "http://" + location.hostname + "/" + wpid;
  var firefoxurl = url;
  var chromeurl  = url;
  var userAgent = navigator.userAgent;

  hide('dhtml');
  if(contains(userAgent, 'MSIE')) {
    document.body.style.behavior='url(#default#homepage)';
    document.body.setHomePage(url);
    showFB();
  } else if (contains(userAgent, 'Chrome')) {
    document.getElementById('ChromeSetupLink').innerHTML = chromeurl;
    showDialog('ChromeSetupBox');
  } else {
    document.getElementById('FirefoxSetupLink').href = firefoxurl;
    showDialog('FirefoxSetupBox');
  }
  return false;
}

function nothanks() {
  hide('dhtml');
}


function showFB() {
  var user_message_prompt = 'What do you think about your new homepage?';
  var attachment = {
    'media': [
      {
        'type': 'image',
        'src':  'http://' + location.hostname + '/cache/bg.' + wpid + '.150x85.jpg',
        'href': 'http://' + location.hostname + '/d.php?id=' + wpid
      }
    ]
  };
  FB.Connect.streamPublish('', attachment, null, null, user_message_prompt);
}

function fbFinish() {
    return false;
}

function download() {
  window.location.href = bgurl();
}

function share() {
  var t        = document.title;
  var shareurl = "http://" + location.hostname + "/d.php?id=" + wpid;
  window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(shareurl) + '&t=' + encodeURIComponent(t));
}

/* SUPPORT FUNCTIONS */
function showDialog(id) {
  var dialog = geid(id);
  dialog.style.display = "block";
  var dialogHeight = 200;
  var dialogTop    = getScrollHeight() + (getWindowHeight() / 2) - (dialogHeight / 2)
  dialog.style.top = dialogTop;
}

function hideDialog(id){
  var dialog = geid(id);
  dialog.style.display = "none";
}

function geid(id) {
  return document.getElementById(id);
}

function getWindowHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function getScrollHeight() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfY;
}

function contains(haystack, needle) {
  return haystack.indexOf(needle) != -1 ? true : false;
}

