// Image preloading

var nav = new Image();
nav.src = "images/nav-link.jpg";
var navhover = new Image();
navhover.src = "images/nav-hover.jpg";


function addOption(selectbox,text,value )
{
    var optn = document.createElement("option");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}

function reFormat(selectbox, selectbox2, showprop) {
    var i;
    selectbox = document.getElementById(selectbox);
    selectbox2 = document.getElementById(selectbox2);
    for(i=selectbox.options.length-1;i>=0;i--) {
        selectbox.remove(i);
    }
    var j;
    for(j=selectbox2.options.length-1;j>=0;j--) {
        selectbox2.remove(j);
    }
    var y;
    if(showprop == "showsales") {
        addOption(selectbox,'Minimum Price','')
        addOption(selectbox,'£25,000','25000')
        addOption(selectbox,'£50,000','50000')
        addOption(selectbox,'£75,000','75000')
        addOption(selectbox,'£100,000','100000')
        addOption(selectbox,'£125,000','125000')
        addOption(selectbox,'£150,000','150000')
        addOption(selectbox,'£175,000','175000')
        addOption(selectbox,'£200,000','200000')
        addOption(selectbox,'£300,000','300000')
        addOption(selectbox,'£400,000','400000')
        addOption(selectbox,'£500,000','500000')
        addOption(selectbox,'£600,000','600000')
        addOption(selectbox,'£700,000','700000')
        addOption(selectbox,'£800,000','800000')
        addOption(selectbox,'£900,000','900000')
        addOption(selectbox,'£1,000,000','1000000')
        addOption(selectbox,'£3,000,000','3000000')
        addOption(selectbox,'£5,000,000','5000000')
        addOption(selectbox,'£6,000,000+','6000000')
        selectbox.options[0].selected = true;   
        addOption(selectbox2,'Maximum Price','')
        addOption(selectbox2,'£25,000','25000')
        addOption(selectbox2,'£50,000','50000')
        addOption(selectbox2,'£75,000','75000')
        addOption(selectbox2,'£100,000','100000')
        addOption(selectbox2,'£125,000','125000')
        addOption(selectbox2,'£150,000','150000')
        addOption(selectbox2,'£175,000','175000')
        addOption(selectbox2,'£200,000','200000')
        addOption(selectbox2,'£300,000','300000')
        addOption(selectbox2,'£400,000','400000')
        addOption(selectbox2,'£500,000','500000')
        addOption(selectbox2,'£600,000','600000')
        addOption(selectbox2,'£700,000','700000')
        addOption(selectbox2,'£800,000','800000')
        addOption(selectbox2,'£900,000','900000')
        addOption(selectbox2,'£1,000,000','1000000')
        addOption(selectbox2,'£3,000,000','3000000')
        addOption(selectbox2,'£5,000,000','5000000')
        addOption(selectbox2,'£6,000,000+','6000000')
        selectbox2.options[0].selected = true;
    }
    if(showprop=="showlets") {
        addOption(selectbox,'Minimum Rent','')
        addOption(selectbox,'£100 pcm','100')
        addOption(selectbox,'£200 pcm','200')
        addOption(selectbox,'£300 pcm','300')
        addOption(selectbox,'£400 pcm','400')
        addOption(selectbox,'£500 pcm','500')
        addOption(selectbox,'£600 pcm','600')
        addOption(selectbox,'£700 pcm','700')
        addOption(selectbox,'£800 pcm','800')
        addOption(selectbox,'£900 pcm','900')
        addOption(selectbox,'£1000 pcm','1000')
        addOption(selectbox,'£2500 pcm','2500')
        selectbox.options[0].selected = true;
        addOption(selectbox2,'Maximum Rent','')
        addOption(selectbox2,'£100 pcm','100')
        addOption(selectbox2,'£200 pcm','200')
        addOption(selectbox2,'£300 pcm','300')
        addOption(selectbox2,'£400 pcm','400')
        addOption(selectbox2,'£500 pcm','500')
        addOption(selectbox2,'£600 pcm','600')
        addOption(selectbox2,'£700 pcm','700')
        addOption(selectbox2,'£800 pcm','800')
        addOption(selectbox2,'£900 pcm','900')
        addOption(selectbox2,'£1000 pcm','1000')
        addOption(selectbox2,'£2500 pcm','2500')
        selectbox2.options[0].selected = true;
    }
}

function swapimg(id) {
   var targetImg = document.getElementById('mainimg');
   targetImg.src="images/propertyimages/img"+id+"-lrg.jpg";
}


//SET ARRAYS FOR DATE

  var d = new Array(
  "Sunday",
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday"
  );
  var m = new Array(
  "January",
  "February",
  "March",
  "April",
  "May",
  "June",
  "July",
  "August",
  "September",
  "October",
  "November",
  "December"
  );
  var today = new Date();
  var day = today.getDate();
  var year = today.getYear();
  if (year < 2000)    // Y2K Fix, Isaac Powell
  year = year + 1900; // http://onyx.idbsu.edu/~ipowell
  end = "th";
  if (day==1 || day==21 || day==31) end="st";
  if (day==2 || day==22) end="nd";
  if (day==3 || day==23) end="rd";
  day+=end;

// ADVERT ARRAY FUNCTIONS
function FlipAds(ads,links,alts) {
  var switchImgTo = ads.splice(0,1);
  var switchAltTo = alts.splice(0,1);
  var switchLinkTo = links.splice(0,1);

  ChangeAdTo(switchImgTo, switchLinkTo, switchAltTo);

  ads.push(switchImgTo);
  alts.push(switchAltTo);
  links.push(switchLinkTo);

  window.setTimeout('FlipAds(ads, links, alts)', 10000);
}

function ChangeAdTo(newImg, newLink, newAlt) {
  var targetImg = document.getElementById("adimg");
  var targetLink = document.getElementById("adlink");

  targetImg.src = "/images/adverts/"+newImg;
  targetImg.alt = newAlt;
  targetLink.href="adverts.php?from="+newLink;
}


//slideshow script v3
function startSlideshow(path, numimgs, numslots, timer, rnd, start, imagetype)
{
  if (typeof imagetype == "undefined") {
      imagetype = "jpg";
  }

  if (start==0) var firstimg = 1 + Math.floor(Math.random()*numimgs);   // random first image
      else var firstimg = start;
       // we use base 0 for our slot calculations
  var delay = timer + ( Math.random()*rnd)- (rnd/2)  // random length of time if rnd is non-zero
  window.setTimeout('cueNextSlide("'+path+'",'+firstimg+','+numimgs+',0,'+numslots+','+timer+','+rnd+',"'+imagetype+'")', delay*500);
}
function cueNextSlide(path, imgno, numimgs, slotno, numslots, timer, rnd, imagetype)
{
  if (slotno >= numslots) { toback=false; } else { toback=true;  }
  var nextimg = (imgno % numimgs) + 1;
  var nextimgforthisslot = ((imgno+numslots)%numimgs)+1
  var nextslot = (slotno+1)%(numslots*2);
  var thisslot = (slotno%numslots)+1;
  var frontId = path+'-sl'+thisslot+'front';
  var backId = path+'-sl'+thisslot+'back';
  var delay = timer + ( Math.random()*rnd)- (rnd/2);  // random length of time if rnd is non-zero
  if (toback) {
     window.setTimeout('changeimg("front","'+path+'","'+thisslot+'","'+nextimgforthisslot+'","'+imagetype+'")', delay*750);
     fade(backId,0.5, 'in');
     fade(frontId, 0.5, 'out');
  }
  else
  {
     window.setTimeout('changeimg("back","'+path+'","'+thisslot+'","'+nextimgforthisslot+'","'+imagetype+'")', delay*750);
     fade(frontId,0.5,'in');
     fade(backId,0.5,'out');
  }
  window.setTimeout('cueNextSlide("'+path+'",'+nextimg+','+numimgs+','+nextslot+','+numslots+','+timer+','+rnd+',"'+imagetype+'")', delay*1000);
}
function changeimg(layer, path, thisslot, imgno,imagetype)
{
  var chgimgId = path+'-sl'+thisslot+layer;
  chgimg = document.getElementById(chgimgId);
  chgimg.src="images/"+path+"/sshow"+imgno+"."+imagetype;
}
function fade(img, time, dir)
{
  img = document.getElementById(img);
  var steps=time*10;
  if (typeof img.style.opacity != 'undefined')
  {
    var otype='w3c';
  }
  else if (typeof img.style.MozOpacity != 'undefined')
  {
    var otype='moz';
  }
  else if (typeof img.style.MKhtmlOpacity != 'undefined')
  {
    var otype='khtml';
  }
  else if (typeof img.filters =='object')
  {
    otype = (img.filters.length >0
      && typeof img.filters.alpha == 'object'
      && typeof img.filters.alpha.opacity == 'number')
      ? 'ie' : 'none';
  }
  else { otype = 'none';}
  if (otype != 'none')
  {
    if (dir == 'out') { dofade(steps,img,1,false,otype);}
    else { dofade(steps, img, 0, true, otype); }
  }
}
function dofade(steps, img, value, targetvisibility, otype)
{
  value += (targetvisibility ? 1 : -1) / steps;
  if (targetvisibility ? value >1 : value <0)
    value = targetvisibility ? 1:0;
  setfade(img,value,otype);
  if (targetvisibility ? value <1 : value >0)
  {
    setTimeout(function()
    {
      dofade(steps, img, value, targetvisibility, otype);
    }, 100);
  }
}
function setfade(img,value, otype)
{
  switch(otype)
  {
    case 'ie':
      img.filters.alpha.opacity = value * 100;
      break;
    case 'khtml':
      img.style.KhtmlOpacity = value;
      break;
    case 'moz':
      img.style.MozOpacity = (value==1?0.9999999 : value);
      break;
    default:
      img.style.opacity = (value == 1?0.9999999 : value);
  }
}