$(document).ready(function() {
  s8.masterInit();
});

s8 = {
  masterInit: function() {
    $(".main_top a>img").mouseover(function() {
      $(this).attr("src", "images/site/" + $(this).attr("id") + "_mouseover.gif");
    }).mouseout(function() {
      $(this).attr("src", "images/site/" + $(this).attr("id") + ".gif");
    }).mousedown(function() {
      $(this).attr("src", "images/site/" + $(this).attr("id") + "_onclick.gif");
    }).each(function() {
      s8.newImage("images/site/" + $(this).attr("id") + "_mouseover.gif");
      s8.newImage("images/site/" + $(this).attr("id") + "_onclick.gif");
    });

    s8.setMenuHighlight();
  },
  newImage: function(arg) {
    if (document.images) {
      rslt = new Image();
      rslt.src = arg;
      return rslt;
    }
  },
  setMenuHighlight: function() {
    // highlight menu
    var href = location.href;
    $(".main_top a").each(function() {
      if (href.indexOf($(this).attr("href")) > -1) {
        $("img", this).attr("src", "images/site/" + $("img", this).attr("id") + "_mouseover.gif").unbind("mouseout");
      }
    });
  },
  homeInit: function() {
    $(".home_thumbs img").mouseover(function() {
      $(".home_left img").attr("src", "images/clients/" + this.id + "/" + this.id + "_RO.jpg");
    });
  },
  showClient: function(client) {
    $(".preview>div").hide();
    $("#" + client + "Div").show();

    // Highlight selected thumb
    $(".thumb_list img").removeClass("selected");
    $("#" + client).addClass("selected");
  },
  workInit: function() {
    var id = "";
    if (location.href.split("?").length > 1 && location.href.split("?")[1].length > 0) {
      id = location.href.split("?")[1];
      if (id.indexOf("client=") > -1) {
        id = id.substring(id.indexOf("client=") + 7);
      }
      else {
        id = "";
      }
    }
    if (id == "") {
      id = $(".thumb_list img:first").attr("id");
    }
    s8.showClient(id);

    $(".thumb_list img").click(function(e) {
      e.preventDefault();
      s8.showClient(this.id);
    });
  }
};

// Search (PopShops API)
function search() {
    if (document.getElementById('q')) {
        var query = document.getElementById("q").value;
        gosearch(query);
    }
}

function gosearch(query) {
    var url = location.href;

    if (query != null && query != "") {
        url = "popshops.aspx?q=" + escape(query);
    }

    location.href = url;
}

function queryKeyPress(e) {
    var keynum;
    if (window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }
    if (keynum == 13) {
        search();
    }
}

function focusSearchBox() {
    var searchBox = document.getElementById("q");

    if (searchBox != null) {
        searchBox.focus();
    }
}
