if (typeof(window.Whomix) == 'undefined') { window.Whomix = {}; }

Whomix.ApiBaseUrl = "/api/";
Whomix.getUrl = function(resource)
{
  if ("string" == typeof(resource)) {
    return Whomix.ApiBaseUrl + resource;
	}
}
Whomix.log = function(msg) {
	if (typeof(console) != "undefined" && typeof(console.log) == "function") {
		console.log(msg);
	} else {
		// nothing
	}
}
Whomix.sortRemixesByDate = function(remixA, remixB, isDesc)
{
	try {
		var adate = remixA.id(); //remixA.date().getTime();
		var bdate = remixB.id(); //remixB.date().getTime();
		return (adate - bdate) * (isDesc ? -1 : 1);
	}
	catch(e) {
		this.write_err("Exception thrown during sort: " + e.message);
	}
}

Whomix.Remix = function(init_hash)
{

  this.classId = function()
	{
		return "Whomix.Remix";
	}

	this.yyyymmddToDate = function(theDate)
	{
		try {
			var tmp = theDate.split('.');
			return new Date(tmp[0], tmp[1], tmp[2]);
		}
		catch(e) {
			this.write_err("Exception thrown during sort: " + e.message);
		}
	}
	this.setAttrAtKey = function(key, val)
	{
		if (key == 'date') {
		  val = new Date(val);
			//val = this.yyyymmddToDate(val);
		}
		else if (key == 'comments' || key == 'downloads')
		{
			val = parseInt(val);
		}
		this._attr[key] = val;
	}
	this.attr = function(arg1, arg2)
	{
		if (typeof(arg1) == 'object') {
			update_hash = arg1;
			for (k in update_hash) {
				this.setAttrAtKey(k, update_hash[k]);
			}
		}
		else if (typeof(arg1) == 'string') {
			if (typeof(arg2) == 'undefined') {
				return this._attr[arg1];
			} else {
				this.setAttrAtKey(arg1, arg2);
			}
		}
	}
	this.id = function()
	{
		return this.attr('id');
	}
	this.date = function()
	{
		return this.attr('date');
	}
	this.formattedDate = function()
	{
		var date = this.date();
		return date.getFullYear() + "." + this.padDigit(String(date.getMonth()),2,"0") + "." + this.padDigit(String(date.getDate()),2,"0");
	}
	this.padDigit = function(str, wantLength,padchar)
	{
		while (str.length < wantLength) {
			str = padchar + str;
		}
		return str;
	}
	this.formattedLength = function()
	{
		var lenS = parseFloat(this.attr('length'));
		var minutes = Math.floor(lenS/60.0);
		return String(minutes) + ":" + this.padDigit(String(Math.floor(lenS - (minutes*60))), 2, "0");
	}
	this.isFullyPopulated = function()
	{
		try {
			if (this.date() && this.attr('blurb') && this.attr('length')) {
				return true;
			}
		}
		catch(e)
		{
		}
		return false;
	}
	
	this._attr = {};

	var k;
	for (k in init_hash) {
		this.setAttrAtKey(k, init_hash[k]);
	}
}

