$(function() {

	LoadRecentMixes();
	LoadUpcomingSchedule();
	
	function LoadRecentMixes(){
		// make ajax call
		var actionUrl = 'data/mixes_calls.php?method=getRecentMixes';
		
		$.getJSON(actionUrl, function(data){
			drawMixesFromJSON(data);
		}).error(function(request, error){
			alert("Error: " + error);
		});
		// *** end ajax call ***
	}

	function LoadUpcomingSchedule(){
		// make ajax call
		var actionUrl = 'data/schedule_calls.php?method=getUpcomingSchedule';
		
		$.getJSON(actionUrl, function(data){
			drawScheduleFromJSON(data);
		}).error(function(request, error){
			alert("Error: " + error);
		});
		// *** end ajax call ***
	}

	function drawScheduleFromJSON(schedule){
		var scheduleList = $('#upcomingScheduleContent');
		
		// clear list
		$(scheduleList)
			.html('');
			
		// build new list
		$.each(schedule, function(i,item){
			var schedDate = new Date(item.long_date);
			var scheduleRow = $('<div/>')
				.addClass('scheduleRow')
				.append(
					$('<div/>')
						.addClass('scheduleDate')
						.html(dateFormat(schedDate, "mm/dd/yy"))
				)
				.append(
					$('<div/>')
						.addClass('scheduleTitle')
						.html(item.title)
				)
				.append(
					$('<div/>')
						.addClass('scheduleLocation')
						.html(item.location)
				)
				.appendTo(scheduleList);
				
			if (item.info_link != "") {
				$('<div/>')
					.addClass('scheduleLink')
					.append(
						$('<a/>')
							.attr('href', item.info_link)
							.attr('target', '_blank')
							.html('INFO')
					)
					.appendTo(scheduleRow);
			} else {
				$('<div/>')
					.addClass('scheduleLink')
					.addClass('inactive')
					.html('INFO')
					.appendTo(scheduleRow);
			}
		});
		
		var moreRow = $('<div/>')
			.addClass('scheduleRow')
			.append(
				$('<div/>')
					.addClass('scheduleDate')
					.html('')
			)
			.append(
				$('<div/>')
					.addClass('moreSchedule')
					.append(
						$('<a/>')
							.attr('href', 'schedule.php')
							.html('more')
					)
			)
			.append(
				$('<div/>')
					.addClass('scheduleRight')
					.html(' ')
			)
			.appendTo(scheduleList);
	}
	
	function drawMixesFromJSON(mixes){
		var mixList = $('#recentMixesContainer');
		
		$.each(mixes, function(i,item){
			var mixArt = 'content/mixes/' + item.album_cover_art_name;
			//var downloadLink = 'download.php?type=mix&filename=' + item.music_file_name;
			//var downloadLink = 'content/mixes/' + item.music_file_name;
			var mixItem = $('<div/>')
				.addClass('mixItem')
				.append(
					$('<div/>')
						.addClass('mixImage')
						.append(
							$('<img/>')
								.attr('src', mixArt)
						)
				)
				.append(
					$('<div/>')
						.addClass('mixText')
						.append(
							$('<div/>')
								.addClass('mixTitle')
								.html(item.album_title)
						)
						.append(
							$('<div/>')
								.addClass('mixGenre')
								.html(item.genre)
						)
						.append(
							$('<div/>')
								.addClass('mixDownload')
								.append(
									$('<a/>')
										.html('download')
										.attr('href', item.music_file_name)
										//.attr('target', '_blank')
								)
						)
				)
				.appendTo(mixList);
		});
	}

});
