//
// copyright 17set2005 www.AlejandroLapeyre.com.ar
//
// UTF-8 á

var site_root = "www.alberto-aguero.com.ar";

var _g_ = new Array();

function getUniqueId(e) 
{
	var index = _g_.length;
	_g_[index] = e;
	return "_g_[" + index + "]";
}
var dbgNum = 0;

function dbg(msg)
{
	var e = document.getElementById("pDebug");
	if (!e) return;
	var p = document.createDocumentFragment();
	var s = "00000" + ++dbgNum;
	s = s.substr(s.length-5);
	p.appendChild(document.createTextNode(s + " " + msg));
	p.appendChild(document.createElement("BR"));
	e.insertBefore(p, e.firstChild);
}

var lbl = new Array();
var gLabels = new Array();

function getRoot() {
	var url = document.URL;
	var i = url.indexOf(site_root);
	url  = url.substr(i);
	var sep = (url.indexOf("\\") >= 0) ? "\\" : "/";
	var p = url.split(sep);
	var r = "";
	for (var i = 0; i < p.length-2; i++) r += "../";
	return r;
}
var gRoot = getRoot();
document.write('<LINK rel="stylesheet" type="text/css" href="' + gRoot + 'css/style-invalid.css' + '">');

function getSearch() 
{
	var ret = new Array();
	var s = (document.location) ? document.location.search : document.URL;
	if (s) {
		var i = s.lastIndexOf("?");
		if (i >= 0) {
			s = s.substr(i + 1);
			var parts = s.split(/,/g);
			for (var i = 0; i < parts.length; i++) {
				var s = parts[i].split(/=/);
				if (s.length == 2) ret[s[0]] = s[1];
			}
		}
	}
	return ret;
}

function ce(tagName, parent) {
	if (parent) return parent.appendChild(document.createElement(tagName));
	return document.createElement(tagName);
}
function ct(text, parent) {
	if (parent) return parent.appendChild(document.createTextNode(text));
	return document.createTextNode(text);
}

function createTable(rows, cols) {
	var table = ce("TABLE");
	table.cellPadding = table.cellSpacing = table.border = 0;
	var tbody = ce("TBODY", table);
	var tr = ce("TR", tbody);
	for (var col = 0; col < cols; col++) ce("TD", tr);
	for (var row = 1; row < rows; row++) tbody.appendChild(tr.cloneNode(true));
	table.summary = "Layout only";
	return table;
}

// cookies

/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0) {
    date.setTime(date.getTime() - skew);
	}
}

function randomizeList(a)
{
	var b = new Array().concat(a);
	var temp, j;
	var len = b.length;
	for (var i = 0; i < len; i++) {
		j = Math.floor(Math.random() * len);
		temp = b[i];
		b[i] = b[j];
		b[j] = temp;
	}
	return b;	
}

lbl["MusicCtl.music_off"] = "music off";
lbl["MusicCtl.playing"] = "Now playing";


function MusicCtl(e, songs) 
{
	if ((!e) || (!songs) || (!songs.length)) return;

	var disabled = !window.navigator.cookieEnabled;
	var s = window.navigator.userAgent;
	
	var ie = this.ie = s.indexOf("MSIE") != -1;
	// this.disabled = disabled = disabled || (s.indexOf("MSIE") == -1);	
	if (disabled) return;
	
	this.e = this.img = e;
	this.items = randomizeList(songs);
	this.index = -1;
	this.id = getUniqueId(this);
	this.timerId = 0
	this.bgs = null;
	e.musicCtl = this;
	e.onclick = e.ondblclick = toggleMusicState;
	
	var b = parseInt(getCookie("music"));
	if (isNaN(b)) b = 1;
	if (disabled) b = 0;
	this.setState(b);
}

function toggleMusicState() {
	this.musicCtl.toggleState();
	return false;
}

MusicCtl.prototype.toggleState = function() {
	this.setState(!this.playing, true);
}

MusicCtl.prototype.stopTimer = function() {
	if (this.timerId) window.clearInterval(this.timerId);
	this.timerId = 0;
}
MusicCtl.prototype.timer = function() {
	var now = new Date().getTime();
	if (now > this.endTime) this.setState(this.playing);
}

MusicCtl.prototype.setState = function(playing, bsetCookie) {
	this.stopTimer();
	playing = this.playing = playing ? 1 : 0;
	this.img.src = getRoot() + "css/music_" + (playing ? "on.gif" : "off.gif");
	if (playing) {
		this.index++;
		if (this.index >= this.items.length) {
			this.items = randomizeList(this.items);
			this.index = 0;
		}
		var song = this.items[this.index];
		var src = getRoot() + song.src;
		if (!this.bgs) {
			if (this.ie) {
				var head = document.getElementsByTagName("HEAD")[0];
				this.bgs = head.appendChild(document.createElement("BGSOUND"));
				this.bgs.loop = 1;
				this.bgs.src = src;
			} else {
				var div = document.createElement("DIV");
				div.innerHTML = "<EMBED style='display:block;position:absolute;top:-10000px' src='" + src + "'>";
				this.bgs = div.firstChild;
				document.body.insertBefore(this.bgs, document.body.firstChild)
			}		
		} else {
			this.bgs.src = src;
		}
		var title = lbl["MusicCtl.playing"] + " \"" + song.title + "\"";
		if (song.player) title += " pianist: " + song.player;
		this.img.title = title;
		
		this.endTime = (new Date()).getTime() + ((song.duration + 6) * 1000);
		if (song.duration) this.timerId = window.setInterval(this.id + ".timer()", 1000);
	} else {
		if (this.bgs) {
			this.bgs.src = getRoot() + "null.mid";
		}
		this.img.title = lbl["MusicCtl.music_off"];
	}
	if (bsetCookie) {
		var now = new Date();
		fixDate(now);
		now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
		setCookie("music", playing, now, "/");
	}
}


function Song(duration, title, src, composer, year, player, copyright)
{
	this.duration = duration;
	this.src = src;
	this.title = title;
	this.year = year;
	this.composer = composer;
	this.player = player;
	this.copyright = copyright;
}
Song.prototype.toString = function() {	return this.src;}

// songs
var gSongs;

function initSongs() {
	if (gSongs) return gSongs;
	gSongs = new Array();
	var players = new Array("", "W.S. Trachtman");
	var composers = new Array("", "Scott Joplin", "Scott Joplin & Scott Hayden", "Scott Joplin & Arthur Marshall");
	function s(duration, title, src, composer, year, player, copyright) {
		player = players[player];
		composer = composers[composer];
		copyright = copyright || "© by Warren Trachtman";
		gSongs[gSongs.length] = new Song(duration, title, "music/" + src, composer, year, player, copyright);
	}
s(215, "A Breeze From Alabama", "Breezala.mid", 1, 1902, 1, "© 1998 by Warren Trachtman");
s(155, "The Cascades", "Cascades.mid", 1, 1904, 1, " © 1998 by Warren Trachtman"); 
s(168, "Cleopha - March & Two-Step", "cleopha.mid", 1, 1902, 1, "© 1999 by Warren Trachtman"); 
s(199, "The Easy Winners", "EasyWinners.mid", 1, 1901, 1, "© 1998 by Warren Trachtman"); 
s(183, "Elite Syncopations", "Elitesyn.mid", 1, null, 1, "© 1997 by Warren Trachtman"); 
s(215, "The Entertainer", "entrtanr.mid", 1, null, 1, "© 1996 by Warren Trachtman"); 
s(182, "The Favorite", "Favorite.mid", 1, 1904, 1);
s(153, "Felicity Rag", "felicity.mid", 2, null, 1);
s(216, "Fig Leaf Rag", "figleaf.mid", 1, 1908, 1, "© 1997 by Warren Trachtman");
s(200, "Gladiolus Rag", "Gladiols.mid", 1, 1907, 1);
s(180, "Kismet Rag", "Kismet.mid", 2, 1913, 1)
s(174, "Lilly Queen", "Lilyquen.mid", 3, 1907, 1)
s(286, "Magnetic Rag", "Magnetic.mid", 1, 1914, 1);
s(176, "Maple Leaf Rag", "mapleaf.mid", 1, 1899, 1);
s(189, "Scott Joplin's New Rag", "newrag.mid", 1, 1912, 1, "© 2002 by Warren Trachtman");
s(167, "Nonpareil (none to equal)", "Nonparel.mid", 1, 1907, 1);
s(226, "Original Rags", "Original.mid", 1, 1899, 1);
s(172, "Palm Leaf Rag", "Palmleaf.mid", 1, 1903, 1);
s(196, "Paragon Rag", "paragon.mid", 1, 1909, 1);
s(183, "Peacherine Rag", "Peachrne.mid", 1, 1901, 1);
s(185, "Pine Apple Rag", "Pineapp.mid", 1, 1908, 1);
s(170, "Pine Apple Rag", "ragdance.mid", 1, 1906, 1);
s(219, "Reflection Rag", "reflectn.mid", 1, 1917, 1);
s(188, "Searchlight Rag", "SearchlightRag.mid", 1, 1907, 1,  "© 1999 by Warren Trachtman");
s(174, "Silver Swan Rag", "Silvrswn.mid", 1, null, 1);
s(180, "Something Doing", "Smthngdn.mid", 2, 1903, 1);
s(182, "Sugar Cane", "Sugarcn.mid", 1, 1908, 1, "© 1997 by Warren Trachtman");
s(194, "Sunflower Slow Drag", "sunflowr.mid", 2, null, 1);
s(185, "Wall Street Rag", "Wallstrt.mid", 1, 1909, 1);
s(202, "Weeping Willow", "Weepwilo.mid", 1, 1903, 1);
return gSongs;
}

function toggle_display(id) {
	var e = document.getElementById(id);
	if (e) {	
		if ((e.style.display) && (e.style.display == "none")) {
			e.style.display = "block";
		} else {
			e.style.display = "none";
		}
	}
}


