function _(name) {return document.getElementById(name);}
var openFolder="file:///android_asset/images/Gnome-Folder-Open-32.png";
var closedFolder="file:///android_asset/images/Gnome-Folder-32.png";
function toggle(id, suffix, imgSuffix) {
	try {
		var openState = _(id+suffix).style.display=='block';
		var newState =  openState ? "none":"block";
		_(id+suffix).style.display=newState;
		if (imgSuffix) {
			_(id+imgSuffix).src = !openState? openFolder : closedFolder;
		}
	} catch(e) {trace.out(e.description);}
}
function selectFile(url) {
	document.location=url;
}
function downloadFile(id) {
	_(id+"_title").className="header headerQueued";
	_(id+"_status").innerHTML="Queued";
	document.location=dlPfx+id;
	_(id+"_butt2").onclick=function (e) { stopPropagation(e);return false;}
	_(id+"_butt2i").src=quImg;
}
function streamFile(id) {
	_(id+"_title").className="header headerListened";
	document.location=stPfx+id;
	if (_(id+"_status").innerHTML.indexOf("Listened")==-1) {
		_(id+"_status").innerHTML+=" Listened";
	}
}
function playFile(id) {
	_(id+"_title").className="header headerListened";
	document.location=plPfx+id;
	if (_(id+"_status").innerHTML.indexOf("Listened")==-1) {
		_(id+"_status").innerHTML+=" Listened";
	}
}
function streamFileBG(id) {
	_(id+"_title").className="header headerListened";
	document.location=stbPfx+id;
	if (_(id+"_status").innerHTML.indexOf("Listened")==-1) {
		_(id+"_status").innerHTML+=" Listened";
	}
}
function playFileBG(id) {
	_(id+"_title").className="header headerListened";
	document.location=plbPfx+id;
	if (_(id+"_status").innerHTML.indexOf("Listened")==-1) {
		_(id+"_status").innerHTML+=" Listened";
	}
}
var imgPfx = "file:///android_asset/images/";
var dlPfx = "/org.my_pod.mypod.download/";
var dlImg = imgPfx+"butt_dl_off_32.png"
var stPfx = "/org.my_pod.mypod.stream/";
var stImg = imgPfx+"butt_stream_off_32.png"
var stbPfx = "/org.my_pod.mypod.streambg/";
var plPfx = "/org.my_pod.mypod.play/";
var plImg = imgPfx+"butt_play_off_32.png"
var plbPfx = "/org.my_pod.mypod.playbg/";
var quImg = imgPfx+"butt_queue_off_32.png"

function showDetails(id) {
	id=parseInt(id);
	var lastId=pageObject.getCurrentIndex();
	var closing = ((lastId == id) && _(lastId+"_body") &&  _(lastId+"_body").style.display!="none");
	if (lastId>-1 && _(lastId+"_body")) {
		_(lastId+"_body").style.display="none";
		_(lastId+"_buttons").style.display="none";
	}
	if ( !closing) {
		if (_(id+"_desc") && !_(id+"_desc").innerHTML) {
			_(id+"_desc").innerHTML=pageObject.getDescription(id);
		}
		updateButtons(id);
		pageObject.setCurrentIndex(parseInt(id));
		if (_(id+"_body")) {
			$("#"+id+"_body").fadeOut(1);
			_(id+"_body").style.display="block";
			_(id+"_title").scrollIntoView(true);
			$("#"+id+"_body").fadeIn(1000);
		}
	} 
}

function updateButtons(id) {
	if (!_(id+"_buttons")) {return;}
	_(id+"_buttons").style.display='block';
	_(id+"_bg_buttons").style.display='block';
	if (pageObject.hasAttachment(id)) {
		if (pageObject.getFileFlag(id,1)) {
			_(id+"_butt1").onclick=function (e) {  playFile(id);stopPropagation(e);return false;}
			_(id+"_butt1i").src=plImg;
			_(id+"_bg_butt1").onclick=function (e) {  playFileBG(id);stopPropagation(e);return false;}
			_(id+"_bg_butt1i").src=plImg;
		} else {
			_(id+"_butt1").onclick=function (e) {  streamFile(id);stopPropagation(e);return false;}
			_(id+"_butt1i").src=stImg;
			_(id+"_bg_butt1").onclick=function (e) {  streamFileBG(id);stopPropagation(e);return false;}
			_(id+"_bg_butt1i").src=stImg;
		}
		
		_(id+"_butt2").style.display='block';
		if (pageObject.getFileFlag(id,16)) {// queued
			_(id+"_butt2").onclick=function (e) {stopPropagation(e); return false;}
			_(id+"_butt2i").src=quImg;
		} else if (!pageObject.getFileFlag(id,1)) {// not downloaded
			_(id+"_butt2").onclick=getDlFunction(id);//function (e) { downloadFile(id);stopPropagation(e);return false;}
			_(id+"_butt2i").src=dlImg;
			//setTimeout("_('"+id+"_butt2').onclick=getDlFunction("+id+")",10);
		} else{
			_(id+"_butt2").style.display='none';
		}
	} else {
		_(id+"_buttons").style.display='none';
		_(id+"_bg_buttons").style.display='none';
	}
}

function initTweak() {
	showDetails(0);
	setTimeout("showDetails(0)",50);
}

function getDlFunction(id) {
	return function (e) { downloadFile(id);stopPropagation(e);return false;}
}
function stopPropagation(e) { 
    e = e||event;/* get IE event ( not passed ) */ 
    e.stopPropagation? e.stopPropagation() : e.cancelBubble = true; 
} 