var startTime = new Date();

function setformfromwotd(str) {
	document.f.q.value = str;
}

function setup(){
    try{
        document.f.q.focus();
    }
    catch(e){
    }
    /*if(contains(window.location.search, "ref=browse")){
        geid('SetupLink').style.display = "block";
    }*/
    //FB.init("a455133f344af4e485ff8e95e0c1d1e7", "connect/xd_receiver.htm");
}
function switchEngine(url){
    document.location = url + document.f.q.value;
    return false;
}
function bgurl(){
    var wpid = getQuerystring('id', '1');
    return "http://d2bq9nzg9io1qe.cloudfront.net/bg." + wpid + "." + screen.width + "x" + screen.height + ".jpg";
}
function tempurl(){
    var wpid = getQuerystring('id', '1');
    return "http://wallpaperexplorer.com/background.ashx?id=" + wpid + "&w=" + screen.width + "&h=" + screen.height;
}

function setBackground()
{
    var wpid = getQuerystring('id', '1');
    var PED = document.getElementById('PreloadImageDiv');
    PED.innerHTML = "<img src='" + bgurl() + "' style='width:1px;height:1px;' onload='handlePreloadImage()' onerror='handlePreloadImageError()'></img>";
}
function handlePreloadImage(){
    loadBackground(bgurl());
}
function handlePreloadImageError(){
    loadBackground(tempurl());
}
function loadBackground(targetbgurl) {
    document.body.style.backgroundImage = "url(" + targetbgurl + ")";
    document.body.style.backgroundRepeat = "no-repeat";
    document.body.style.backgroundPosition = "center top";
    geid('WarningDiv').style.display = 'none';
    geid('PreloadImageDiv').style.display = 'none';
    if(contains(window.location.search, "ref=browse")){
        geid('SetupLink').style.display = "block";
    }
    var elapsed = (new Date).getTime() - startTime.getTime();
    ajax('../latencyreport.ashx?l=' + elapsed);
}

function setHomepage(targetButton)
{
    var wpid = getQuerystring('id', '1');
    var url = "http://" + location.hostname + "/?ref=home";
    var userAgent = navigator.userAgent;
    if(contains(userAgent, 'MSIE')){
        targetButton.style.behavior='url(#default#homepage)';
        targetButton.setHomePage(url);
        showFB();
    }
    else if (contains(userAgent, 'Chrome')){
        document.getElementById('ChromeSetupLink').innerHTML = url;
        showDialog('ChromeSetupBox');
    }
    else {
        document.getElementById('FirefoxSetupLink').href = url;
        showDialog('FirefoxSetupBox');
    }
    return false;
}

function showFB(){
    var body_general = '';
    var user_message_prompt = 'What do you think about your new wallpaper?';
    var user_message = '';
    var comment_data = {"images":[{'src':'http://s3.amazonaws.com/gpaper/wallpaper.62.p.jpg', 'href':'http://www.searchslate.com'},{'src':'http://s3.amazonaws.com/gpaper/wallpaper.3.p.jpg', 'href':'http://www.searchslate.com'},{'src':'http://s3.amazonaws.com/gpaper/wallpaper.4.p.jpg', 'href':'http://www.searchslate.com'}]};
    FB.Connect.showFeedDialog('105140556737', comment_data, null, body_general, null, FB.RequireConnect.doNotRequire, fbFinish, user_message_prompt, user_message);
}
function fbFinish() {
    return false;
}

function download(){
    var wpid = getQuerystring('id', '1');
    window.location.href = bgurl();
}
function share(){
    var u=location.href;
    var t=document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&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";
    geid('DarkDiv').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 fixImage(img, url)
{
    img.src = url;
    img.onerror = "";
    return true;
}
function contains(haystack, needle){
    if(haystack.indexOf(needle) != -1){
        return true;
    }
    else{
        return false;
    }
}
function getQuerystring(key, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}
function ajax(strURL) {
    var http = false;
    if (navigator.appName == "Microsoft Internet Explorer") {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        http = new XMLHttpRequest();
    }
    http.open("GET", strURL);
    http.onreadystatechange = function() {
        if (http.readyState == 4) {
            //alert(http.responseText);
        }
    }
    http.send(null);
}

