$(document).ready(function(){
	addMenuList();
	addSideMenuAudio();
	addSideMenuVideo();
	addFooterTable();
});

/**
 * Adds the navigation menu.
 */
function addMenuList() {
	var ul = document.createElement("ul");
	ul.appendChild(getMenuLink("Events", "./events.php"));
	ul.appendChild(getMenuLink("Photos", "./photos.php"));
	ul.appendChild(getMenuLink("Discography", "./discography.php"));
	ul.appendChild(getMenuLink("Bios", "./bios.php"));
	ul.appendChild(getMenuLink("Shop", "./shop.php"));
	ul.appendChild(getMenuLink("Contact", "./contact.php"));
	$("#menu").append(ul);
}

/***
 * Adds the footer table.
 */
function addFooterTable() {
	
	// set up the footer table
	var tbl = document.createElement("table");
	tbl.setAttribute("cellspacing", "0px");
	var tr = document.createElement("tr");
	
	// create and add the booking info cell
	var cell = document.createElement("td");
	cell.innerHTML = "Booking:&nbsp;&nbsp;<strong>booking@northside-garage.com</strong>";
	tr.appendChild(cell);
	
	// add the link icons
	tr.appendChild(getFooterLink("Facebook", "facebook_icon.jpg", "www.facebook.com/pages/Northside-Garage/188528971185055"));
	tr.appendChild(getFooterLink("SonicBids", "sonicbids_icon.jpg", "www.sonicbids.com/epk/epk.asp?epk_id=237759"));
	tr.appendChild(getFooterLink("Reverbnation", "reverbnation_icon.jpg", "www.reverbnation.com/northsidegarage"));
	tr.appendChild(getFooterLink("Twitter", "twitter_icon.jpg", "www.twitter.com/northsidegarage"));
	tr.appendChild(getFooterLink("YouTube", "youtube_icon.jpg", "www.youtube.com/user/northsidegarage"));
	tr.appendChild(getFooterLink("LastFM", "lastfm_icon.jpg", "www.last.fm/music/Northside Garage"));
	tr.appendChild(getFooterLink("MySpace", "myspace_icon.jpg", "www.myspace.com/nsgarage"));
	
	tbl.appendChild(tr);
	$("#footer").append(tbl);
}

/**
 * Adds the Audio section of the side menu.
 * @param menu the side menu
 */
function addSideMenuAudio() {
	
	// add the Audio title
	var audioTitle = document.createElement("h2");
	audioTitle.appendChild(document.createTextNode("Audio"));
	$("#audio").append(audioTitle);
	
	// spacer
	var spcr = document.createElement("p");
	spcr.innerHTML = "&nbsp;";
	$("#audio").append(spcr);
	
	// add Waiting for the Breakdown
	addWaitingForTheBreakdownPlaylist();

	// spacer
	spcr = document.createElement("p");
	spcr.innerHTML = "&nbsp;";
	$("#audio").append(spcr);
	
	// add Grease, Guns, and Guitars
	addGreaseGunsAndGuitarsPlaylist();

	// spacer
	spcr = document.createElement("p");
	spcr.innerHTML = "&nbsp;";
	$("#audio").append(spcr);
}

/**
 * Adds the Video section of the side menu.
 * @return
 */
function addSideMenuVideo() {
	var videoTitle = document.createElement("h2");
	videoTitle.appendChild(document.createTextNode("Video"));
	$("#video").append(videoTitle);

	// spacer
	var spcr = document.createElement("p");
	spcr.innerHTML = "&nbsp;";
	$("#video").append(spcr);
	
	var youtubeFrame = document.createElement("div");
	youtubeFrame.innerHTML = "<iframe width=\"197\" height=\"148\" src=\""+getProtocol()+"www.youtube.com/embed/3OmKODGFtPU\" frameborder=\"0\" style=\"margin-left:3px\" allowfullscreen></iframe>";
	$("#video").append(youtubeFrame);
	var p = document.createElement("p");
	p.appendChild(document.createTextNode("Voodoo Love Song"));
	p.setAttribute("class", "videoTitle");
	youtubeFrame.appendChild(p);
}

/**
 * Adds the playlist for Waiting for the Breakdown.
 * @param menu the side menu
 */
function addWaitingForTheBreakdownPlaylist() {
	var wftbTitle = document.createElement("h3");
	wftbTitle.innerHTML = "Waiting for the Breakdown";
	$("#audio").append(wftbTitle);
	var title = "Waiting for the Breakdown";
	var playerImage = "wftbPlayerImage.jpg";
	var wftbImg = document.createElement("img");
	wftbImg.setAttribute("src", "./images/wftb.jpg");
	wftbImg.setAttribute("alt", title);
	wftbImg.setAttribute("class", "playlistAlbumImg");
	$("#audio").append(wftbImg);
	var playlistTable = document.createElement("table");
	playlistTable.setAttribute("class", "playlistTable");
	playlistTable.appendChild(getPlaylistRow(title, "Can't Find Me", "cant_find_me.mp3", playerImage));
	playlistTable.appendChild(getPlaylistRow(title, "Dirty Old Man", "dirty_old_man.mp3", playerImage));
	playlistTable.appendChild(getPlaylistRow(title, "Voodoo Love Song", "voodoo_love_song.mp3", playerImage));
	playlistTable.appendChild(getPlaylistRow(title, "Train, Train", "train_train.mp3", playerImage));
	$("#audio").append(playlistTable);
}

/**
 * Adds the playlist for Grease, Guns, and Guitars.
 */
function addGreaseGunsAndGuitarsPlaylist() {
	var ggagTitle = document.createElement("h3");
	ggagTitle.innerHTML = "Grease, Guns, and Guitars";
	$("#audio").append(ggagTitle);
	var title = "Grease, Guns, and Guitars";
	var playerImage = "ggagPlayerImage.jpg";
	var wftbImg = document.createElement("img");
	wftbImg.setAttribute("src", "./images/ggag.jpg");
	wftbImg.setAttribute("alt", title);
	wftbImg.setAttribute("class", "playlistAlbumImg");
	$("#audio").append(wftbImg);
	var playlistTable = document.createElement("table");
	playlistTable.setAttribute("class", "playlistTable");
	playlistTable.appendChild(getPlaylistRow(title, "Amanda Chuva", "amanda_chuva.mp3", playerImage));
	playlistTable.appendChild(getPlaylistRow(title, "Fake Smiles", "fake_smiles.mp3", playerImage));
	playlistTable.appendChild(getPlaylistRow(title, "You Keep Calling Out To Me", "you_keep_calling_out_to_me.mp3", playerImage));
	playlistTable.appendChild(getPlaylistRow(title, "Anti-Cool", "anti-cool.mp3", playerImage));
	playlistTable.appendChild(getPlaylistRow(title, "Nothing's Gonna Change", "nothings_gonna_change.mp3", playerImage));
	playlistTable.appendChild(getPlaylistRow(title, "Lights Go Out", "lights_go_out.mp3", playerImage));
	$("#audio").append(playlistTable);
}

/**
 * Creates a list item for the main menu.
 * @param name: the name to display
 * @param href: the link value
 * @return a list item (li)
 */
function getMenuLink(name, href) {
	var li = document.createElement("li");
	var a = document.createElement("a");
	a.setAttribute("href", href);
	a.appendChild(document.createTextNode(name));
	li.appendChild(a);
	return li;
}

/**
 * Adds a song to the playlist.
 * @param albumTitle: the album the song is from
 * @param songName: the name of the song
 * @param songFilename: the file's name
 * @param imageFilename: the image to display in the player
 * @return the row to add
 */
function getPlaylistRow(albumTitle, songName, songFilename, imageFilename) {
	var songsDir = "./songs/";
	var imagesDir = "./images/";
	var tr = document.createElement("tr");
	var td = document.createElement("td");
	var a = document.createElement("a");
	a.setAttribute("href", songsDir + songFilename);
	var img = document.createElement("img");
	img.setAttribute("src", imagesDir + imageFilename);
	img.setAttribute("alt", albumTitle);
	img.setAttribute("class", "playerImage");
	a.appendChild(img);
	a.appendChild(document.createTextNode(songName));
	td.appendChild(a);
	tr.appendChild(td);
	return tr;
}

/***
 * Creates a table cell for the footer
 * @param title: the title/alt text
 * @param icon: icon filename
 * @param href: the link
 * @return a td element footer link
 */
function getFooterLink(title, icon, href) {
	href = getProtocol()+href;
	var td = document.createElement("td");
	var a = document.createElement("a");
	a.setAttribute("href", href);
	a.setAttribute("target", "_blank");
	a.setAttribute("title", title);
	var img = document.createElement("img");
	img.setAttribute("src", "images/icons/" + icon);
	img.setAttribute("alt", title);
	a.appendChild(img);
	td.appendChild(a);
	return td;
}

function getProtocol() {
	var prefix = parent.location.protocol;
	if (prefix == "file:") prefix = "http:";
	return prefix+"//";
}
