// Younilife Music 2.0 Homepage JS - AW, 06/08/2009 //

music_obj = {
	
	artists: [],
	genre: '',
	genre_id: 0,
	page: 0,
	pages: 0,
	
	get: function(genre, page) {
		music_obj.genre_id = genre || 0, music_obj.page = page || 1, music_obj.artists = [];
		$('#boxm_content').hide();
		$('#boxm_waiting').show();
		$.get('/rankings/ajax/ylm-client.php?artists&genre=' + music_obj.genre_id + '&page=' + music_obj.page + '&r=' + Math.random(), '', music_obj.response, 'xml');
	},
	
	init: function() {
		
		switch (button_mode) {
			case 'new':
				$('#music_acct').html('<a href="?add"><div class="btn_music_add"></div></a>')
								.show()
								.fadeTo(1000, 1);
				break;
			case 'edit':
				$('#music_acct').html('<a href="' + base_url + edit_url + '/"><div class="btn_music_edit"></div></a>')
								.show()
								.fadeTo(1000, 1);
				break;
		}
		music_obj.get();
		if (slideshow)
			slideshow.init();
	},
	
	response: function(doc, status) {
		//alert(doc); return;
		if (status != 'success') {
			alert(status);
			return;
		}
		
		var current_artist, 
			node = doc.firstChild.firstChild,
			status = node.firstChild.nodeValue;
		
		if (status == 'ok') {
			
			while (node = node.nextSibling) {
			
				switch (node.nodeName) {
					
					case 'artists':
						
						music_obj.artists = [];
						current_artist = node.firstChild;
						
						while (current_artist) {
							
							var data = new Object();
							var artist_node = current_artist.firstChild;
							while (artist_node) {
								data[artist_node.nodeName] = artist_node.firstChild.nodeValue;
								artist_node = artist_node.nextSibling;
							}
							music_obj.artists.push(data);
							current_artist = current_artist.nextSibling;
							
						}
						break;
						
					default:
					
						music_obj[node.nodeName] = node.firstChild.nodeValue;
						break;
				
				}
				
			}
	
			// Output //
		
			var html = '';
			for (var i=0;i<music_obj.artists.length;i++) {
				var the_artist = music_obj.artists[i];
				html += '<div class="l_artist"><div class="l_artist_pic"><a href="' + base_url + 
						the_artist.url + '/"><img src="' + (the_artist.picture == 'none' ? 
						'/rankings/images/no-image55x55.gif' : '/imgcache.php?mode=crop&size=55x55&image=' + 
						path_to_photo(the_artist.picture)) + '" /></a></div><div class="l_artist_name"><a href="' + base_url + 
						the_artist.url + '/">' + the_artist.name + '</a></div><div class="l_artist_desc">' + 
						unescapeData(the_artist.desc) + '</div>' +'<div class="l_artist_tracks"><a href="' + base_url + 
						the_artist.url + '/">' + the_artist.tracks + ' track' + (the_artist.tracks > 1 ? 's' : '') + 
						'</a></div><div style="clear:left"></div></div>';
			}
			
			if (music_obj.pages > 1) {
				html += '<div class="l_pages">';
				for (var i=1;i<=music_obj.pages;i++) {
					if (i > 1)
						html += ' | ';
					if (music_obj.page == i) {
						html += '<strong>' + i + '</strong>';
					} else {
						html += '<a href="javascript:void(0);" onclick="music_obj.get(music_obj.genre_id, ' + i + ');">' + i + '</a>';
					}
				}
				html += '</div>';
			}
			
			$('#boxm_genre').html(music_obj.genre == 'All' ? 'All Genres' : music_obj.genre);
			$('#boxm_content').html(html);
		
		} else if (status == 'empty') {
			music_obj.genre = node.nextSibling.firstChild.nodeValue;
			$('#boxm_content').html('<div class="l_track_empty">There are currently no artists in this category</div>');
		}
		$('#boxm_content').show();
		$('#boxm_waiting').hide();
		
	}
	
};

// Create artist object with a couple of dummy functions, to prevent hacks to the 360-player erroring //
artist = {
	sound: {
		addSound: function(id) { },
		attachSound: function(id, url) { },
		setCurrentByURL: function(url) { }
	}
};


// Generate path from filename
function path_to(file) {
	return music_dir + file.charAt(0) + '/' + file;	
}

function path_to_photo(file) {
	return photos_dir + file.charAt(0) + '/' + file;	
}

window.onload = music_obj.init;