// Functions needed for an XPI install
// Simplified version of Chris Cooks' install script


// Finds the name of the theme from the filename
function getName(raw) {
  var grabFileStart = raw.lastIndexOf('/');
  var grabFileEnd = raw.lastIndexOf('.');
  if (grabFileStart >= grabFileEnd) {
    return 'Invalid file name';
  } else {
    return raw.substring(grabFileStart + 1,grabFileEnd);
  }
}

function installThemeNow(file) {
// used in the hrefs 
  var extStart = file.lastIndexOf('.');
  var fileType = file.substring(extStart, file.length).toLowerCase();
  if (fileType == 'xpi') {
    doXPIInstall(file, getName(file));
  } else {
    InstallTrigger.installChrome(InstallTrigger.SKIN, file, getName(file));
  }
  return true;
}

function theme(themeName) {
  this.name = themeName;
  this.author = new Array();
  this.authorEmail = new Array();
  this.description = '';
  this.preview = ''; 
  this.homepage = '';
  this.version = '';
  this.updated = '';
  this.size = '';
  this.installFile = '';
}
var themeArray = new Array();

function getThemeCount() {
	document.write(themeArray.length);
  return true;
}

function makeThemeGrid() {
  for (var i = 0; i < themeArray.length; i++) {
    document.write('<div');
    if (checkDateWindow(themeArray[i].updated)) { 
      document.write(' class="updated"');
    }
    document.write('>\n<a href="#' + escape(themeArray[i].name) + '">' + themeArray[i].name);
    document.write('<br /><img src="' + themeArray[i].preview + '" alt="" title="Preview of ' + themeArray[i].name + ' theme ');
    if (checkDateWindow(themeArray[i].updated)) { 
      document.write('(Theme recently updated!)');
    }
    document.write('" /></a></div>');
  }
}

function listThemes() {
  for (var i = 0; i < themeArray.length; i++) { 
    document.write('<a name="' + themeArray[i].name + '"></a><div class="theme">\n<img class="screenshot" src="' + themeArray[i].preview
		    + '" alt="" title="Screenshot di ' + themeArray[i].name + ' theme" /><p class="info"><strong>' + themeArray[i].name + '</strong> di ');
    for (var j=0; j < themeArray[i].author.length; j++) {
      if (j > 0 && themeArray[i].author.length > 1 ) {
        document.write(', ');
      }
      if (themeArray[i].authorEmail[j] != '') {
        document.write('<a href="mailto:' + themeArray[i].authorEmail[j] + '">');
      }
      document.write(themeArray[i].author[j]);
      if (themeArray[i].authorEmail[j] != '') {
        document.write('</a>');
      }
    }
    
    document.write('<br />Version ' + themeArray[i].version + ', ');
    if (checkDateWindow(themeArray[i].updated)) {
    	document.write('<span class="updated">');
    }
    document.write('Updated ' + themeArray[i].updated);
    if (checkDateWindow(themeArray[i].updated)) {
    	document.write('</span>');
    }
		    document.write('<br /><a class="install" href="' + themeArray[i].installFile + '" onclick="installThemeNow(\'' + themeArray[i].installFile
		     + '\'); return false;" title="Cliccare per installare oppure utilizzare il tasto desto per scaricare">Installazione</a> (' + themeArray[i].size + ' KB)<br />');
    if (themeArray[i].homepage != '') {
		      document.write('<a class="homepage" href="' + themeArray[i].homepage + '" title="Cliccare per visitare la pagina web">Pagina web</a>');
    }
    else {
		      document.write('Pagina web non disponibile.');
    }
    document.write('</p><p class="desc">' + themeArray[i].description + '</p></div><p class="toplink"><a href="#">Back to Top</a></p>\n\n');
  }
}

function checkDateWindow(dateString) { // Input string of yyyy-mm-dd
  var newDaysWindow = 7 * (1000 * 60 * 60 * 24); // Only new for 7 days
  var dateArray = dateString.split('-');
  var newDate = (new Date( dateArray[0], dateArray[1] - 1, dateArray[2])).valueOf() + newDaysWindow;
  return ( newDate > (new Date()).valueOf() );
}






