var win = window;    // window to search.
var n   = 0;

var debug = false;  // Set to display "alerts".

function findInPage(str) {

  if (debug) {alert ('FindInPage='+str);}

  var txt, i, found;

  if (str == "")
    return false;
 

  // Find next occurance of the given string on the page, wrap around to the
  // start of the page if necessary.
 
  if (window.execScript) {
    txt = win.document.body.createTextRange();

    // Find the nth match from the top of the page.

    for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
      txt.moveStart("character", 1);
      txt.moveEnd("textedit");
    }

    // If found, mark it and scroll it into view.
    if (found) {
      txt.moveStart("character", -1);
      txt.findText(str);
      txt.select();
      txt.scrollIntoView();
      n++;

      return true
    }

    // Otherwise, start over at the top of the page and find first match.

    else {
      if (n > 0) {
        n = 0;
        findInPage(str);
      }

      // Not found anywhere, give message.
      else
        return false;
    }
  } else {
    // Look for match starting at the current point. If not found, rewind
    // back to the first match.


    if (!win.find(str))
      while(win.find(str, false, true))
        n++;
    else
      n++;

    // If not found in either direction, give message.
    if (n == 0)
      return false;
  }
  return true;
}



// Pass the referrer string to this function. 
// The additional content is stripped out, certain keywords
// are stripped out to, and search is made on the page.
 
function SearchString(referrer)
{
var iPos = 0;
var found = false;


   if (debug) {alert ('referrer='+referrer);}

// alert(referrer);
  if (!referrer) {
    return false;
  }
  
  var queryPrefix
// See if referrer is yahoo or AOL
//  alert (referrer.indexOf("yahoo"),0);
  if (referrer.indexOf("yahoo")>0) {
     queryPrefix="p=";}
     else {
        if (referrer.indexOf("aol.")>0) {
               queryPrefix="query=";}
         else {
     queryPrefix="q=";}
          }
 
  var startPos = referrer.toLowerCase().indexOf(queryPrefix);

  if ((startPos < 0) || (startPos + queryPrefix.length == referrer.length)) {
    return false;
  }
  

  var endPos = referrer.indexOf("&", startPos);
  if (endPos < 0) {
    endPos = referrer.length;
  }

  var queryString = referrer.substring(startPos + queryPrefix.length, endPos);

  // fix the space characters
  queryString = queryString.replace(/%20/gi, " ");
  queryString = queryString.replace(/\+/gi, " ");
  queryString = queryString.replace(/%2f/gi, "/");

  // remove the quotes (if you're really creative, you could search for the
  // terms within the quotes as phrases, and everything else as single terms)
  queryString = queryString.replace(/%22/gi, "");
  queryString = queryString.replace(/\"/gi, "");
  queryString = queryString.toLowerCase();

  // Get an array of words in query string
  var queryStringWord = queryString.split(" ");
 
  //Remove certain words from the search criteria
  var keyWords = ['cottage', 'holiday', 'cottages', 'holidays', 'the', 'self', 'catering', 'villa', 'villas','in','like', 'of'];
  for (var i=0; i<keyWords.length; i++) {
       // Now look at each word in queryString, and remove word if it is in the list
       for (var i2=0; i2<queryStringWord.length; i2++) {
          // If keyword has been found, remove it
          if (queryStringWord[i2]==keyWords[i]) {
              if (debug) {alert ('Found keyWord='+keyWords[i]);}
              queryStringWord.splice(i2,1);
           } 
       }
  }


  // Look for entire search srtring
  if (debug) {alert ('first Page Search='+queryStringWord.join(" "));}
  found=findInPage(queryStringWord.join(" "));

  if(!found) {
    //We haven't found that string, so look for the first two words
    if (queryStringWord.length>1) {
       if (debug) {alert ('Two word page search='+queryStringWord[0]+' '+queryStringWord[1]+ '  Position='+iPos);}
       found=findInPage(queryStringWord[0]+' '+queryStringWord[1]);
    }
  }

  if(!found) {
    //We haven't found the first two words, so look loop round, looking for word we can find
    var i=0;
    while (i<queryStringWord.length && !found) {
       if (debug) {alert ('word array page search='+queryStringWord[i]);}
       found=findInPage(queryStringWord[i]);
       i++;
    }
  }
}
//var str='q=a';
//SearchString(str);



function fadeOut(id) {
   // default the speed of fade
   opacity(id, 100, 40, 50);
}

function fadeIn(id) {
   // default the speed of fade (fade in)
    opacity(id, 40, 100, 1000)
}


// Fade the images on mouse over / bring back on mouse out
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

