/*
 * The SearchRefinements object literal is used to relocate the
 * refinements in the DOM and make them accessible through a DHTML
 * dropdown menu.
 */

var SearchRefinements = {

  attributesTimeout: null,
  locationTimeout: null,
  sellerratingTimeout: null,
  delay: 200,  // Milliseconds
  xOffset: 0, // pixels without units
  yOffset: 23,
  refineX: 0,
  refineY: 0,
  refineWidth: null,


//newsearchwithin
//newSearchLocation
//dhtmlRefineSearch
  init: function() {
    if (!document.getElementById) {
      return;
    }

    switch(twoLetterISOCode) {
      case 'en':
        this.refineWidth = "205px";
        break;
      case 'de':
        this.refineWidth = "285px";
        break;
      case 'fr':
        this.refineWidth = "252px";
        break;
      case 'es':
        this.refineWidth = "360px";
        break;
      case 'it':
        this.refineWidth = "227px";
        break;
    }


    var refinement = new Array("attributes", "location", "sellerrating");
    var refinementHeader = new Array("attributesHeader", "locationHeader", "sellerratingHeader");
    var refinementLinks = new Array("attributesLinks", "locationLinks", "sellerratingLinks");


    var totalRefinements = new Array(3); // create and build refinements
    for(i=0; i < totalRefinements.length; i++){
      SearchRefinements.checkAndBuildRefinements(refinement[i], refinementHeader[i], refinementLinks[i]);
    }

    // Hide the sort by go button, register an event handler, and move the form to the results line
    SearchRefinements.initialFormSetUpForDHTML();

        // Create the TDs used for styles
        if($("searchResultsPage")){
           SearchRefinements.createTableCellsForStyleSearch();
        }else {
           SearchRefinements.createTableCellsForStyleBrowse();
        }

  },

  checkAndBuildRefinements: function(refinement, refinementHeader, refinementLinks){

          //alert(refinement + " " + refinementHeader + " " + refinementLinks);
      if ($(refinement) && $(refinementHeader) && $(refinementLinks)) {

        // Hide the refinement links
        $(refinementLinks).style.display = "none";

        // Add class to refinement link CSS

        $(refinementLinks).className = "refinementsLinks";
        $(refinementLinks).style.width = this.refineWidth;


        // Move the refinements to the new DOM location
        $("dhtml" + refinement + "Container").appendChild($(refinement));
        $("dhtml" + refinement + "Container").style.padding ="0px 0px 0px 10px";


        // Add refinements mouseover and mouseout observers
          Event.observe(refinementHeader, "mouseover",
                     function(e){
                       SearchRefinements.handleHeaderMouseover(refinement, refinementHeader, refinementLinks);
                     }, false);

          Event.observe(refinementHeader, "mouseout",
                     function(e){
                       SearchRefinements.handleHeaderMouseout(refinement, refinementLinks);
                     }, false);

          Event.observe(refinementLinks, "mouseover",
                     function(e){
                       SearchRefinements.handleLinksMouseover(refinement);
                     }, false);

          Event.observe(refinementLinks, "mouseout",
                     function(e){
                       SearchRefinements.handleLinksMouseout(refinement, refinementLinks);
                     }, false);



        }

    },

  handleHeaderMouseover: function(refinement, refinementHeader, refinementLinks) {
    // find x,y coords for the links


    switch(refinement){ // stop the delayed hide links
      case "attributes":

        clearTimeout(SearchRefinements.attributesTimeout);
      if (SearchRefinements.locationTimeout) {    // hide the location menu in case it is visisble
        clearTimeout(SearchRefinements.locationTimeout);
        SearchRefinements.hideLinks("locationLinks")
        }
      if (SearchRefinements.sellerratingTimeout) { // hide the sellerrating menu in case it is visisble
        clearTimeout(SearchRefinements.sellerratingTimeout);
        SearchRefinements.hideLinks("sellerratingLinks")
        }
        break;

      case "location":

        clearTimeout(SearchRefinements.locationTimeout);
      if (SearchRefinements.attributesTimeout) {    // etc
        clearTimeout(SearchRefinements.attributesTimeout);
        SearchRefinements.hideLinks("attributesLinks")
        }
      if (SearchRefinements.sellerratingTimeout) { // etc
        clearTimeout(SearchRefinements.sellerratingTimeout);
        SearchRefinements.hideLinks("sellerratingLinks")
        }
        break;

      case "sellerrating":

        clearTimeout(SearchRefinements.sellerratingTimeout);
      if (SearchRefinements.attributesTimeout) {    // etc
        clearTimeout(SearchRefinements.attributesTimeout);
        SearchRefinements.hideLinks("attributesLinks")
        }
      if (SearchRefinements.locationTimeout) {      // etc
        clearTimeout(SearchRefinements.locationTimeout);
        SearchRefinements.hideLinks("locationLinks")
        }
        break;
    }

    // show the links
    var pos = Position.cumulativeOffset($(refinementHeader))
    SearchRefinements.refineX = pos[0];
    SearchRefinements.refineY = pos[1];
    SearchRefinements.showLinks(refinementLinks, SearchRefinements.refineX, SearchRefinements.refineY);
  },

  handleHeaderMouseout: function(refinement) {

    switch(refinement){ // stop the delayed hide links
      case "attributes":
        SearchRefinements.attributesTimeout = setTimeout("SearchRefinements.hideLinks('attributesLinks')", SearchRefinements.delay);
        break;

      case "location":
        SearchRefinements.locationTimeout = setTimeout("SearchRefinements.hideLinks('locationLinks')", SearchRefinements.delay);
        break;

      case "sellerrating":
        SearchRefinements.sellerratingTimeout = setTimeout("SearchRefinements.hideLinks('sellerratingLinks')", SearchRefinements.delay);
        break;
    }

  },

  handleLinksMouseover: function(refinement) {

    switch(refinement){ // stop the delayed hide links
      case "attributes":
          clearTimeout(SearchRefinements.attributesTimeout);
        break;

      case "location":
        clearTimeout(SearchRefinements.locationTimeout);
        break;

      case "sellerrating":
            clearTimeout(SearchRefinements.sellerratingTimeout);
        break;
    }

  },

  handleLinksMouseout: function(refinement) {

    switch(refinement){ // stop the delayed hide links
      case "attributes":
        SearchRefinements.attributesTimeout = setTimeout("SearchRefinements.hideLinks('attributesLinks')", SearchRefinements.delay);
        break;

      case "location":
        SearchRefinements.locationTimeout = setTimeout("SearchRefinements.hideLinks('locationLinks')", SearchRefinements.delay);
        break;


      case "sellerrating":
        SearchRefinements.sellerratingTimeout = setTimeout("SearchRefinements.hideLinks('sellerratingLinks')", SearchRefinements.delay);
        break;

    }

  },


  showLinks: function(targetID, left, top) {
    var targetEl = $(targetID);
    targetEl.style.left = left + SearchRefinements.xOffset + "px";
    targetEl.style.top = top + SearchRefinements.yOffset + "px";
    targetEl.style.display = "block";
  },

  hideLinks: function(targetID) {
    $(targetID).style.display = "none";
  },

  handleSortBySelection: function() {
    document.forms["sortResults"].submit();
  },

    initialFormSetUpForDHTML: function() {


        Event.observe(document.forms["sortResults"].elements["sortby"], "change", SearchRefinements.handleSortBySelection, false);


                if($("searchResultsPage")){
          $("go").style.display = "none";
          //$("dhtmlinstructions").appendChild($("instructions"));
          $("matchesHeader").appendChild($("sortResultsDIV"));
          $("searchkeySelect").style.display = "none";
          $("searchkeySelect").value = "kn";
        }else {

          $("browsewithincat").style.display = "inline";
          $("subcatsdiv").style.display = "inline";
            $("dhtmlmatches").appendChild($("matches"));
            $("dhtmlmatches").style.padding = "10px 0px 10px 0px";
        }


        // hide the refinements
        $("refinements").style.display = "none";

        //move instructions into the DHTML DOM location


        // Move the SearchWithin header to the new location in DOM
                 $("searchwithinlabel").style.display = "inline";
                 //$("searchwithinlabel").style.font-weight= "bold";


        // Move the SearchWithin FORM to the new location in DOM
        $("newSearchLocation").appendChild($("refineSearchDIV"));
        $("newSearchLocation").style.background="#eeeeee";

        //TODO: enable when SR released to buyers
        //$("newSearchLocation").style.borderTop="1px dotted #999999";
        $("newSearchLocation").style.padding ="10px 0px 0px 10px";



    },

  createTableCellsForStyleSearch: function(){

        topLeft = document.createElement('TD');
        topLeft.className = "topLeft";
        topLeft.innerHTML = "&nbsp;&nbsp;";

        topRight = document.createElement('TD');
        topRight.className = "topRight";
        topRight.innerHTML = "&nbsp;&nbsp;";

        sideLeft = document.createElement('TD');
        sideLeft.className = "sideLeftGrey";
        sideLeft.innerHTML = "&nbsp;&nbsp;";


        sideRight = document.createElement('TD');
        sideRight.className = "sideRightGrey";
        sideRight.innerHTML = "&nbsp;&nbsp;";


        // This appends to the top row of the dhtml panel


        thisnode = $("panelfirstchild");
          $("dhtmlpanelheader").insertBefore(topLeft,thisnode);
        $("dhtmlpanelheader").appendChild(topRight);


        // This appends to the Middle row of the dhtml panel
        thisnodemiddle = $("refinementsfirstchild");
        $("dhtmlRefinements").insertBefore(sideLeft,thisnodemiddle);
        $("dhtmlRefinements").appendChild(sideRight);


        // Add style to Existing Table and TDs
        $("refinementsfirstchild").className ="dhtmlRefinements";
        $("dhtmlbottomleft").className ="bottomLeftGrey";
        $("dhtmlbottom").className ="bottomGrey";
        $("dhtmlbottomright").className ="bottomRightGrey";


        $("rightColDHTML").style.display = "none";
        $("dhtmlSpacer").style.width = "20%";


                // Make DHTML Refinements visible
            $("dhtmlRefinements").style.display = "";

  },

  createTableCellsForStyleBrowse: function() {

        // Apply styles to the container
        $("searchPanel").className = "formFrame";
        $("dhtmlRefinements").className="browsedhtml"

        $("dhtmltopleft").className = "topLeftGrey";
        $("dhtmltopleft").innerHTML = "&nbsp;";
        $("dhtmltop").className = "bannerGrey";
        $("dhtmltop").innerHTML = "&nbsp;";
        $("dhtmltopright").className = "topRightGrey";
        $("dhtmltopright").innerHTML = "&nbsp;";

        $("dhtmlsideleft").className = "sideLeftGrey";
        $("dhtmlsideright").className = "sideRightGrey";


        $("dhtmlbottomleft").className ="bottomLeftGrey";
        $("dhtmlbottomleft").innerHTML = "&nbsp;";
        $("dhtmlbottom").className ="bottomGrey";
        //  $("dhtmlbottom").innerHTML = "&nbsp;";
        $("dhtmlbottomright").className ="bottomRightGrey";
        $("dhtmlbottomright").innerHTML = "&nbsp;";


        //Applies styles for subcats table
        $("subcatstable").className = "formFrame subcats";

        $("subcatstableTopleft").className = "topLeftGrey";
        $("subcatstableTopleft").innerHTML = "&nbsp;";
        $("subcatstableHeader").className = "bannerGrey";
        $("subcatstableTopright").className = "topRightGrey";
        $("subcatstableTopright").innerHTML = "&nbsp;";

        $("subcatstableSideLeft").className = "sideLeftGrey";
        $("subcatstableSideRight").className = "sideRightGrey";

        $("subcatstableSideLeft2").className = "sideLeftGrey";
        $("subcatstableMiddle2").appendChild($("categories"));
        $("subcatstableMiddle2").className="";
        $("subcatstableSideRight2").className = "sideRightGrey";


        $("subcatstableBottomleft").className = "bottomLeftGrey";
        $("subcatstableBottom").className = "bottomGrey";
        $("subcatstableBottomright").className = "bottomRightGrey";



         //Moves the subcats table to the left panel and adds the subcats to it
        $("leftpaneldhtml").insertBefore($("subcatsdiv"),$("controlBox"));
        $("controlBox").style.display ="none";
        $("controlBox").type = "hidden";

  }

}


/*
 * The vCounter object literal is used to set a non-expiring cookie.
 * This cookie can be used later on to determine if a visitor as been
 * to the site before.
 */

var vCounter = {

  domain: "abebooks.com",
  expirationYear: 2012,
  name: "abe_vc",
  path: "/",

  init: function() {
    var cookieValue = Cookie.get(vCounter.name);
    var expires = new Date();
    expires.setFullYear(vCounter.expirationYear);

    var numberOfVisits = 0;
    if (cookieValue != null) {
      numberOfVisits = parseInt(cookieValue);
      if (numberOfVisits < 100) {
        numberOfVisits++;
      }
    }
    Cookie.set(vCounter.name, numberOfVisits, expires, vCounter.path, vCounter.domain, false);
  }

}
