///////////////////////////////////////////////////////////////
//
//  Script: Random Projects
//  Version: 1.0
//
//  Notes: oo version with dynamic html output
//

  var Ascribe = Ascribe || {};
  
  Ascribe.RandomProjects = function (config) {
    // data and general configs
    this.projects = config.projects; // raw project data
    this.numbers = []; // to hold our unique numbers
    this.maxValue = (this.projects.length - 1);
    this.uniqueNumbers = config.projectsToDisplay || 4;
    this.count = 0;
    this.found;
    this.randNum;
    this.projectsOutput = '';
  
    // ids
    this.outputElementID = config.outputElementID || 'recent-projects-container';

    // methods
    this.getUniqueNumbers = function () {
      for (var i=0; this.count<this.uniqueNumbers; this.count++) {
        this.found = false;
        this.randNum = Math.floor(Math.random() * this.maxValue);
        for (var j=0; j<this.numbers.length; j++) {
          if (this.numbers[j] == this.randNum) {
            this.found = true;
            break;
          }
        }
        if (this.found) {
          this.count--; // keep repeating loop if found == true
        }
        else {
          this.numbers.push(this.randNum);
        }
      }
    };

    this.projectsHTMLOutput = config.projectsHTMLOutput || function (aNumbers, uniqueNumbers, aProjects) {
      output = '<ul class="portfolio">';
      for (var k=0; k<uniqueNumbers; k++) {
        var x = aNumbers[k];
        output += '<li><a href="' + aProjects[x].projectPath + '" title="View"><img src="' + aProjects[x].projectThumbnailPath + '" alt="' + aProjects[x].projectTitle + '" /></a><h3>' + aProjects[x].projectTitle + '</h3></li>';
      }
      output += '</ul><!-- #recent-projects -->';
      return output;
    };
  
    this.getUniqueNumbers();
    this.projectsOutput = this.projectsHTMLOutput(this.numbers, this.uniqueNumbers, this.projects);
    $(this.outputElementID).update(this.projectsOutput);
  }; // constructor
