﻿/*********************************************************************/
/*                                                                   */
/*               SCRIPT DE PANDOs para SMF                           */
/*                                                                   */
/*********************************************************************/
/*                                                                   */
/*           Modificado por Especiales e Idudos                      */
/*                                                                   */
/*********************************************************************/

var package_out = new Array ();
var package_count = 0;

function printPackageCallBack(packageInfo) {
	
	//Tamaño
	var size = packageInfo['packageSize'] / 1024 / 1024;
	size = Math.round(size * 100) / 100;
	if (size < 1000) {
		size = size + ' MB';
	} else {
		size = Math.round([size / 1024] * 100) / 100 + ' GB';
	}
	//Get number of days till expiration (negative = expired)
	var days = Math.round( (new Date(packageInfo['expirationDate']) - new Date()) / 86400000);
	//store expiration status
	var expired = (days >= 0) ? 0 : 1;
	//use absolute values of days for a readable string
	days = Math.abs(days);
	//get title from package info
	var title = packageInfo['title'];
	//¿Titulo muy largo?
	if (title.length > 38) {
		title = title.substring(0,35) + '...';
	//titulo raton
	var linktitle = "title=\"" + packageInfo['title'] + "\"";
	}
	//generate img tag with thumbnail from package info
	var thumb = '<img width="50px" height="40px" src="'+ packageInfo['thumbnailURL'] +'" alt="" />';
	//generate anchor tag from package URL; assumes packageKey is set
	var packageA = "<a href='" + PandoAPI.getPackageURL(packageId,packageKey) + "&tt=S2W'" + linktitle + ">";
	//get appropriately pluralized X day(s) string
	var daystring = days +' d&iacute;a'+ ( (days > 1) ? ('s') : ('') );	
	//get # of downloads from package info
	var dl = packageInfo['downloads'];
	//if package is not expired, link the title and thumbnail to the package URL
	//  and set the expiration days string accordingly
	if(!expired) {
		title = packageA + title + '</a>';
		thumb = packageA + thumb + '</a>';
		dias = '<strong><span style="color: #47748E;">Expira en '+ daystring +'</span></strong><br />';
	} else if (!packageInfo['expirationDate']) {
			dias = '<span style="color: red;"><strong>Expirado</span></strong><br />'
			title = packageA + title + '</a>';
			thumb = packageA + thumb + '</a>';
		}
	//otherwise, do not link the title/thumbnail, and set expiration string accordingly
	else {
		dias = '<span style="color: red;">Expirado hace '+ daystring +' </span><br />';
	}

	//generate human-friendly Downloaded X times / Never Download string
	if(dl > 0)
		dlstring = '<strong>'+dl+'</strong> '+( (dl > 1) ? ('descargas') : ('descarga') );
	else
		dlstring = 'Nunca descargado';
	//El que lo sube
	var uploader = 'Subido por <strong>' + packageInfo['packager'] + '</strong>';
 	
	package_out[package_count] = '<table class="pandopackage"><tr><td colspan="2"><span class="ptitle"><abbr title="'+ packageInfo['title'] + '">' + title +'</abbr></span></td></tr><tr><td class="packagethumb">' + thumb + '</td><td valign="middle" class="packagemeta">'+ uploader +'<br />'+ dlstring + ' // ' + size + '<br />' + dias + '</td></tr></table><div class="clr"></div>';
	
		document.write(package_out[package_count]);
}
function parsePandoUrl(url) {
  var parts = url.split('?',2);
  var args = parts[1];
  parts = args.split('&amp;');
  for(var i=0; i<parts.length; i++) {
    if(parts[i].match(/^id=[A-Z0-9]*/))
      packageId = parts[i].substring(3);
    if(parts[i].match(/^key=[A-Z0-9]*/))
      packageKey = parts[i].substring(4);			
  }
}
function showPackage(url) {
  parsePandoUrl(url);
  PandoAPI.getPackageInfo(packageId,printPackageCallBack,packageKey);
  package_count++
}
