var DEBUG = 0;
var page = 0;
var px = [270,255,285,240,270,300,225,255,285,315,210,240,270,300,330,195,225,255,285,315,345,180,210,240,270,300,330,360,165,195,225,255,285,315,345,375,150,180,210,240,270,300,330,360,390,135,165,195,225,255,285,315,345,375,405,120,150,180,210,240,270,300,330,360,390,420];
var py = [20,53,53,86,86,86,119,119,119,119,152,152,152,152,152,185,185,185,185,185,185,218,218,218,218,218,218,218,251,251,251,251,251,251,251,251,284,284,284,284,284,284,284,284,284,317,317,317,317,317,317,317,317,317,317,350,350,350,350,350,350,350,350,350,350,350];
var playerhead = "<embed src='http://christmasmusictree.com/player/player.swf' width='200' height='20' wmode='transparent' allowscriptaccess='always' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' flashvars='autostart=true&wmode=transparent&backcolor=#3B5998&frontcolor=#ffffff&file=";
var playertail = "'/></embed></div>";
var SONGDIR = "http://christmasmusictree.com/musictree/songs/";
var PUBSONGDIR = "http://christmasmusictree.com/musictree/isongs/";

function show(id){ try{document.getElementById(id).style.display = 'block'; } catch(ex){} }
function hide(id){try{document.getElementById(id).style.display = 'none';}catch(ex){}}
function hover(id,i) { 
	if(DEBUG) { console.debug("hover id=" + id + "\n"); }
	if(i == 1) { document.getElementById(id).style.border = '1px solid black';}
	else { document.getElementById(id).style.border = '1px solid #DDDDDD';}
}
function paging(inc,pagesize,maxpage){
	if(DEBUG) { console.debug("paging inc=" + inc + " maxpage=" + maxpage + " pagesize=" + pagesize + "\n"); }
	var p = page * pagesize; var lastp = p + pagesize;
	try { for(i=p; i<lastp; i++) { document.getElementById('icon' + i).style.display = 'none'; }}catch(ex){}
	page += inc; 
	if(page < 0) {page = 0;} 
	if(page > maxpage) {page = maxpage;}
	if(page == 0) {document.getElementById('fprev').style.display = 'none';}
	else {document.getElementById('fprev').style.display = 'inline';}
	if(page == maxpage) {document.getElementById('fnext').style.display = 'none';}
	else {document.getElementById('fnext').style.display = 'inline';}
	if(DEBUG) { console.debug("paging new page=" + page + "\\n"); }
	p = page * pagesize; lastp = p + pagesize;
	try { for(i=p; i<lastp; i++){document.getElementById('icon' + i).style.display = 'inline';}}catch(ex){}
}

function showinfo(key){
	try {
	if(DEBUG) { console.debug("showinfo key=" + key + "\\n"); }
	var songix = document.getElementById('songix' + key).value;
	try{if(songix == ''){if(DEBUG){console.debug("showinfo no songix\\n");} songix = -1; }} catch(xx){}
	if(DEBUG) { console.debug("showinfo songix=" + songix + "\\n"); }
	var s = "<font color=gray>no song selected</font>";
	try{if(songix >= 0){ s=document.getElementById('songselector')[songix].innerHTML;}}catch(xx){}
	if(DEBUG) { console.debug("showinfo songix=" + songix + " s=" + s + "\\n"); }
	var t = document.getElementById('names');
	t.innerHTML = document.getElementById('name' + key).innerHTML;
	t.style.display = 'block';
	var w = document.getElementById('selsongs');
	w.innerHTML = s;
	w.style.display = 'block';
	} catch(ex){ if(DEBUG) {console.debug("showinfo ex=" + ex + "\\n");}}
}
function friend(key){
	try {
	var ss = document.getElementById('songselector');
	var sel = ss.selectedIndex;
	if(DEBUG) { console.debug("friend key=" + key + " sel=" + sel + "\\n"); }
	if(sel < 0){document.getElementById('names').innerHTML = "<font color=red>Please select a song</font>";}
	else { 
		var mysong = ss[sel].innerHTML;
		var myval = ss[sel].value;
		if(DEBUG) { console.debug("friend mysong=" + mysong + " myval=" + myval + "\\n"); }
		var songnum = document.getElementById('songnum' + key);
		var songix = document.getElementById('songix' + key);
		var musicup = document.getElementById('musicup' + key);
		if(DEBUG) { console.debug("friend songnum=" + songnum.value + " songix=" + songix.value + "\\n"); }
		songnum.value = myval;
		songix.value = sel;
		musicup.value = '1';
		if(DEBUG) { console.debug("friend songnum=" + songnum.value + " songix=" + songix.value + "\\n"); }
		showinfo(key);
	}
	} catch(ex){ if(DEBUG) {console.debug("friend ex=" + ex + "\\n");}}
}

function mousetreeshow(id,ev,ix){
	try{
		var x = ev.clientX;
		var y = ev.clientY;
		var div = document.getElementById(id); 
		if(friends[ix].length > 0) {
			div.innerHTML = friends[ix];
    		div.style.left = x + 10 + 'px';
    		div.style.top = y - 30 + 'px';
			div.style.display = 'block';
		} 
	} 
	catch(ex){ }
}
function mouseshow(id,ev,cont){
	try{
		var x = ev.clientX;
		var y = ev.clientY;
		var div = document.getElementById(id); 
		div.innerHTML = cont;
    	div.style.left = x + 10 + 'px';
    	div.style.top = y - 30 + 'px';
		div.style.display = 'block';
	} 
	catch(ex){ }
}
function showall() { 
	document.getElementById('slink').style.display = 'none'; 
	document.getElementById('allsongs').style.display = 'block'; 
}
function publicpreview(song,duration,songdir){ 
	try {
		if(duration == undefined) { duration = '&duration=30'; }
		if(songdir == undefined) { songdir = PUBSONGDIR; }
		if(DEBUG) { console.debug("publicpreview songdir=" + songdir); }
		if(DEBUG) { console.debug("publicpreview duration=" + duration); }
		var s = playerhead + songdir + song + duration + playertail;
		if(DEBUG) { console.debug("play song=" + s + "\\n"); }
		document.getElementById('player').innerHTML = s;
		document.getElementById('player').style.display = 'block';
	} catch(ex){if(DEBUG) { console.debug("publicpreview ex=" + ex + "\\n"); }}
}
function playpreview(song,duration,songdir){ 
	try {
		if(duration == undefined) { duration = '&duration=30'; }
		if(songdir == undefined) { songdir = SONGDIR; }
		if(DEBUG) { console.debug("playpreview songdir=" + songdir); }
		if(DEBUG) { console.debug("playpreview duration=" + duration); }
		var s = playerhead + songdir + song + duration + playertail;
		if(DEBUG) { console.debug("play song=" + s + "\\n"); }
		document.getElementById('player').innerHTML = s;
		document.getElementById('player').style.display = 'block';
	} catch(ex){if(DEBUG) { console.debug("playpreview ex=" + ex + "\\n"); }}
}
function play(ix,songsdir){ 
	try {
		var s = playerhead + songsdir + song[ix] + playertail;
		var t = "<div class=mycyy>" + friends[ix] + "<br>" + songs[ix] + "</div>";
		if(DEBUG) { console.debug("play song=" + s + "\\n"); }
		document.getElementById('finfo').innerHTML = t;
		document.getElementById('player').innerHTML = s;
		document.getElementById('player').style.display = 'block';
	} catch(ex){if(DEBUG) { console.debug("play ex=" + ex + "\\n"); }}
}
function showicons(topy,diff,topx){
    try {
		if(DEBUG) { console.debug("showicons: topy=" + topy + " diff=" + diff); }
        var numpos = px.length;
        var numfr = pics.length;
        if(diff==0) { diff = Math.floor(numpos / numfr); if(diff > 3) { diff-=2; }}
        var loop = (numfr < numpos) ? numfr : numpos;
		if(topx == undefined) { topx = 0; }
		if(DEBUG) { console.debug("showicons: loop=" + loop + " diff=" + diff + " topx=" + topx); }
        for(var i=0; i < loop; i++) {
            var icon = document.getElementById("icon" + i);
            icon.style.left = topx + px[i*diff] + 'px';
            icon.style.top = topy + py[i*diff] + 'px';
        }
        var info = document.getElementById('info');
    } catch(ex){ if(DEBUG) { console.debug("showicons ex=" + ex + "\\n"); } }
}
function copy(src,dest,dest2) {
	try {
		document.getElementById(dest).innerHTML = document.getElementById(src).innerHTML;
		var s = "<input type=hidden name=newbgi value=\"" + src + "\"/>";
		document.getElementById(dest2).innerHTML = s;
	}
	catch(ex) { }
}

function copydiv(src,dest,dest2,dest2name) {
	try {
		if(DEBUG) { console.debug("copydiv src=" + src + " dest=" + dest + "\n"); }
		if(DEBUG) { console.debug("copydiv dest2=" + dest2 + " dest2name=" + dest2name + "\n"); }
		document.getElementById(dest).innerHTML = document.getElementById(src).innerHTML;
		var s = "<input type=hidden name=\"" + dest2name + "\" value=\"" + src + "\"/>";
		if(DEBUG) { console.debug("copydiv s=" + s + "\n"); }
		document.getElementById(dest2).innerHTML = s;
	}
	catch(ex) { if(DEBUG) { console.debug("copydiv ex=" + ex + "\n"); } }
}

function GetDiv(src) { 
	var inner;
	try { inner = document.getElementById(src).innerHTML; }
	catch(ex) { inner=0; } 
	if(DEBUG) { console.debug("GetDiv: src=" + src + "  inner = " + inner); }
	return inner;
}

function GetFriends(params,dest,server,pagerdiv) {
	if(DEBUG) { console.debug("GetFriends: params = " + params + " pagerdiv=" + pagerdiv); }
	try {
		var pager = GetDiv(pagerdiv);
		if(DEBUG) { console.debug("GetFriends: pager = " + pager); }
		params = params + ":" + pager;
		if(DEBUG) { console.debug("GetFriends: params = " + params); }
		postData(params,dest,server);
	}
	catch(ex) { if(DEBUG) { console.debug("GetFriends ex=" + ex + "\n"); } }
}

function SavePositions(params,server,launch) {
	if(DEBUG) { console.debug("SavePositions: params=" + params + " server = " + server); }
	try {
		params = params + ":" + saved;		// defined in script drag
		if(DEBUG) { console.debug("SavePositions: params = " + params); }
		postData(params,'none',server);
		if(DEBUG) { console.debug("SavePositions: launch = " + launch); }
		var fun="OpenLocation('"+launch+"')"; 
		setTimeout(fun,2000);
	}
	catch(ex) { if(DEBUG) { console.debug("SavePositions ex=" + ex + "\n"); } }
}

function OpenLocation(launch) {
	try{
		if(DEBUG) { console.debug("OpenLocation: launch = " + launch); }
		document.location=launch;
	}
	catch(ex) { if(DEBUG) { console.debug("OpenLocation ex=" + ex + "\n"); } }
}




