<%inherit file="base.html" />
<%! 
	from headphones import db, helpers
	myDB = db.DBConnection()
%>

<%def name="headerIncludes()">
	<div id="subhead_container">
		
	</div>	
</%def>

<%def name="body()">
	<div id="nav-sub-container">
		<ul id="nav-sub">
			<li><a href="deleteAlbum?AlbumID=${album['AlbumID']}&ArtistID=${album['ArtistID']}">Delete Album</a></li>
			%if album['Status'] == 'Skipped':
			<li><a href="queueAlbum?AlbumID=${album['AlbumID']}&ArtistID=${album['ArtistID']}&new=False">Mark Album as Wanted</a></li>
			%elif album['Status'] == 'Wanted':
			<li><a href="queueAlbum?AlbumID=${album['AlbumID']}&ArtistID=${album['ArtistID']}&new=True">Force Check</a></li>
			<li><a href="unqueueAlbum?AlbumID=${album['AlbumID']}&ArtistID=${album['ArtistID']}">Mark Album as Skipped</a></li>
			%else:
			<li><a href="queueAlbum?AlbumID=${album['AlbumID']}&ArtistID=${album['ArtistID']}&new=False">Retry Download</a></li>
			<li><a href="queueAlbum?AlbumID=${album['AlbumID']}&ArtistID=${album['ArtistID']}&new=True">Try New Version</a></li>
			%endif			
		</ul>
	</div>
	<div class="table_wrapper">
		<a id="btnBack" class="btnStatus" href="artistPage?ArtistID=${album['ArtistID']}">
			Back to ${album['ArtistName']}
		</a>
		<div class="clear"></div>
		<div id="albumheader">
			<div class="album-art-big">
				<img src="http://ec1.images-amazon.com/images/P/${album['AlbumASIN']}.01.LZZZZZZZ.jpg" alt="albumart" class="albumArt" title="${album['ArtistName']} - ${album['AlbumTitle']}">
				<div class="album-art-cd"></div>
			</div>
			<div id="album-describtion">
			<h1>${album['AlbumTitle']}</h1>
			by <a href="/artistPage?ArtistID=${album['ArtistID']}">${album['ArtistName']}</a>
			<br>
			<%
				totalduration = myDB.action("SELECT SUM(TrackDuration) FROM tracks WHERE AlbumID=?", [album['AlbumID']]).fetchone()[0]
				totaltracks = len(myDB.select("SELECT TrackTitle from tracks WHERE AlbumID=?", [album['AlbumID']]))
				try:
					albumduration = helpers.convert_milliseconds(totalduration)
				except:
					albumduration = 'n/a'
			
			%>
			<br />
			%if description:
			<p>${description['Summary']}</p>
			%endif
			<ul>
				<li>Tracks: <span>${totaltracks}</span></li> 
				<li>Duration: <span>${albumduration}</span></li>
			</ul>
			</div>
		</div>
		<div id="track_wrapper">
			<table class="display" id="track_table">
				<thead>
					<tr>
						<th id="number">#</th>
						<th id="name">Track Title</th>
						<th id="duration">Duration</th>
						<th id="location">Local File</th>
						<th id="bitrate">Bit Rate</th>
						<th id="format">Format</th>
					</tr>
				</thead>
				<tbody>
				%for track in tracks:
					<%
						if track['Location']:
							grade = 'A'
							location = track['Location']
						else:
							grade = 'X'
							location = ''
							
						if track['BitRate']:
							bitrate = str(track['BitRate']/1000) + ' kbps'
						else:
							bitrate = ''

						try:
							trackduration = helpers.convert_milliseconds(track['TrackDuration'])
						except:
							trackduration = 'n/a'
					%>
					<tr class="grade${grade}">
						<td id="number">${track['TrackNumber']}</td>
						<td id="name">${track['TrackTitle']}</td>
						<td id="duration">${trackduration}</td>
						<td id="location">${location}</td>
						<td id="bitrate">${bitrate}</td>
						<td id="format">${format}</td>
					</tr>
				%endfor
				<%
				unmatched = myDB.select('SELECT * from have WHERE ArtistName LIKE ? AND AlbumTitle LIKE ?', [album['ArtistName'], album['AlbumTitle']])			
				%>
				%if unmatched:
					 %for track in unmatched:
					 <%
					 	duration = helpers.convert_seconds(float(track['TrackLength']))
					 %>
					<tr class="gradeC">
						<td id="number">${track['TrackNumber']}</td>
						<td id="name">${track['TrackTitle']}</td>
						<td id="duration">${duration}</td>
						<td id="location">${track['Location']}</td>
						<td id="bitrate">${int(track['BitRate'])/1000} kbps</td>
						<td id="format">${track['Format']}</td>
					</tr>
					%endfor
				%endif
				</tbody>
			</table>
		</div>
	</div>
</%def>

<%def name="headIncludes()">
	
</%def>

<%def name="javascriptIncludes()">
	<script src="interfaces/brink/JS/libs/jquery.dataTables.min.js"></script>
	<script>
	$(document).ready(function() {
		getAlbumInfo("${album['ArtistName']}","${album['AlbumTitle']}","#albumheader .albumArt",3);
		initActions();
		$('#track_table').dataTable(
			{
				"aaSorting": [
				],
				"bFilter": false,
				"bInfo": false,
				"bPaginate": false
			});
	});
	//CD Rotate
	$(function() {
		var $rotateElement = $(".album-art-cd");
		rotate(0);
		function rotate(degree) {

		  // For webkit browsers: e.g. Chrome
		$rotateElement.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});
		  // For Mozilla browser: e.g. Firefox
		$rotateElement.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});

		  // Animate rotation with a recursive call
		setTimeout(function() { rotate(++degree); },30);
		}
	});
	</script>
</%def>