/*
random image displayer
(C)2008 B-Lex

Tested in
- Firefox 3
- Google Chrome 0.2.149.29 (build 1798)
- Internet Explorer 6
- Opera 9.24 / 9.50
*/

function rndImage()
{
  this.previmages = [];
}

rndImage.prototype.randomizelist = function()
{
  this.rndorder = [];

  var temp
    , swapwith
    , imagesavail = this.imgfiles.length;

  for(tel=0; tel<imagesavail; tel++)
  {
    this.rndorder[tel] = tel;
  }

  for(tel=0; tel<imagesavail; tel++)
  {
    swapwith = Math.round(Math.random() * (imagesavail-1));
    if (swapwith == tel)
      continue;

    temp = this.rndorder[swapwith];
    this.rndorder[swapwith] = this.rndorder[tel];
    this.rndorder[tel] = temp;
  }
}

rndImage.prototype.start = function()
{
  var _this = this;

  this.randomizelist();
  this.currentitem = 0;
  this.timer = setTimeout( function() { _this.nextImage() }, this.delay );
}

rndImage.prototype.stop = function()
{
  clearTimeout(this.timer);
}

rndImage.prototype.nextImage = function()
{
  var _this = this;

//  var imagesavail = this.imgfiles.length-1;
//  var chosenimageid = Math.round(Math.random() * imagesavail);


//  this.cacheimage = new Image();
//  this.cacheimage.src = this.imgfiles[chosenimageid];

  this.cacheimage = document.createElement('img');
  this.selectimageid = this.rndorder[this.currentitem];

  // for browsers that only load images that are in the DOM
//  this.cacheimage.style.cssText = 'display: block; visibility: hidden; position: absolute; top: 0; left: 0;';
//  document.body.appendChild(this.cacheimage);

  this.cacheimage.src = this.imgfiles[this.selectimageid];
  this.cacheimage.onload = function() { _this.showImage(); };
  this.cacheimage.onerror = function() { _this.showImage(); };

  this.cachesentence = this.sentences[this.selectimageid];

//  this.cacheimage.onReadyStateChange = function() { alert(this.readyState); };

  // fallback in case onload or onerror isn't called (sometimes for local image files)
  this.checktimer = setTimeout( function() { _this.check(); }, 7000 );

  this.currentitem++;
  if (this.currentitem > this.rndorder.length-1)
    this.currentitem = 0;
}

rndImage.prototype.check = function()
{
  if (this.cacheimage.width > 0)
    this.showImage();
  else
    this.checktimer = setTimeout( function() { _this.check  }, 7000 );
}

rndImage.prototype.showImage = function()
{
  clearTimeout(this.checktimer);

  // determine max zoom before hitting max width/height
  //var zoomw = this.availwidth / this.cacheimage.width;
  //var zoomh = this.availheight / this.cacheimage.height;
  //var zoom = zoomw < zoomh ? zoomw : zoomh;

  var newimg = document.createElement('img');
  newimg.src = this.cacheimage.src;
  newimg.style.width = this.availwidth +'px';
  newimg.style.height = this.availheight +'px';
  //newimg.style.width = (this.cacheimage.width * zoom)+'px';
  //newimg.style.height = (this.cacheimage.height * zoom)+'px';

  // for browsers that only load images that are in the DOM
  //  this.cacheimage = this.cacheimage.parentNode.removeChild(this.cacheimage);

  this.element.parentNode.replaceChild(newimg, this.element);
  this.element = newimg;

  var sentenceDiv = document.getElementById("randomsentence");
  sentenceDiv.innerHTML = this.cachesentence;

//  this.element.src = this.cacheimage.src;
  var _this = this;
  this.timer = setTimeout( function() { _this.nextImage() }, this.delay );
}

function showuploadpopup()
{
  document.getElementById('greyout').style.display = 'block';
  document.getElementById('uploadpopup').style.display = 'block';
}

function closeuploadpopup()
{
  document.getElementById('uploadpopup').style.display = 'none';
  document.getElementById('greyout').style.display = 'none';
}

function check_email(e)
{
  ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

  for(i=0; i < e.length ;i++)
  {
    if(ok.indexOf(e.charAt(i))<0)
      return false;
  }

  if (document.images)
  {
    re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (!e.match(re) && e.match(re_two))
      return true;
  }
}



function checkfields()
{
  var errormsg = "";
  if (document.getElementById("photo").value == "")
  {
    errormsg += "\n- 'Upload foto' is een verplicht veld";
  }
  else
  {
    // Is the photo from a correct file type? (Note: very basic check)
    var fileTypes = ['gif', 'jpg', 'png', 'jpeg'];

    var dots = document.getElementById("photo").value.split(".");

    //get the part AFTER the LAST period.
    var fileType = dots[dots.length-1];

    for (var i = 0; i < fileTypes.length; i++)
    {
      if (fileTypes[i] == fileType)
        break;
    }

    if (i >= fileTypes.length)
      errormsg += "\n- 'Upload foto' is van een verkeerd bestandstype";
  }

  var email = document.getElementById("email").value;
  if (email == "")
  {
    errormsg += "\n- 'E-mailadres' is een verplicht veld";
  }
  else
  {
    var correctemail = check_email(email);
    if (!correctemail)
      errormsg += "\n- 'E-mailadres' bevat een ongeldig e-mailadres";
  }

  if (document.getElementById("sentence").value == "")
    errormsg += "\n- 'Maak de slagzin af' is een verplicht veld";
  else
  {
    if (document.getElementById("sentence").value.length > 100)
      errormsg += "\n- Teveel ingevuld (" + document.getElementById("sentence").value.length + " karakters) bij 'Maak de slagzin af'";
  }

  if (errormsg != "")
  {
    errormsg = "Een of meer velden zijn niet correct ingevuld:\n" + errormsg;
    alert(errormsg);
    return false;
  }
  else
  {
    return true;
  }
}

