/**
 * @author Rahul Malik
 */

/*
 * Channel Class
 */

/*
 * Track Class
 * 	Offers the functionality needed at the Track level granularity.
 */
var Track = Class.create({
	initialize: function(track_name, artist_of_track){
		this.title = track_name;
		this.artist_name = artist_of_track;
		
		this.image_url = '';
		this.video_id = '';
		this.tags = [];
		this.related_tracks = [];
		this.threshold = 0;
		this.hasBeenPlayed = false;		
		
		this.get_video();
		this.get_tags();
	},
	
	get_video: function(){
		new Ajax.Request('server/get_artist_videos.php', {
		parameters: {artist: this.artist_name, track: this.title},
		onComplete: (function(video_id){
				var vid_info = video_id.responseText.split(';');
				this.video_id = vid_info[0];
				this.image_url = vid_info[1];
		}).bind(this)
	});
	},
	
	get_tags: function(){
		new Ajax.Request('server/get_track_tags.php', {
			parameters: {artist: this.artist_name, track: this.title},
			onComplete: (function(tags_obj){
					this.tags = tags_obj.responseText.split(',');
			}).bind(this)
		});
	},
	update_threshold: function(artist_name, rating){
		if (!!(artist_name == this.artist_name)){
			if (!!(rating=="positive")){
				this.threshold = this.threshold + 1;
			}
			else{
				this.threshold = this.threshold - 1;	
			}
		}
	},
	display_video: function(){
		if(!!this.video_id)	{
			
			if(!large_view){
				var	video_markup = '<embed src="http://www.youtube.com/v/' + this.video_id + '&rel=0&color1=0x006699&color2=0x54abd6&autoplay=1&loop=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed>';
				$('video').innerHTML = video_markup;
				if(isExtended==0){
					slideSideBar();			
				}						
			}
			else{ // large view
				var	video_markup = '<embed src="http://www.youtube.com/v/' + this.video_id + '&rel=0&color1=0x006699&color2=0x54abd6&autoplay=1&loop=1" type="application/x-shockwave-flash" wmode="transparent" width="' + (document.viewport.getWidth()-340) + '" height="' + (document.viewport.getHeight()-100) + '"></embed>';
				$('videobig').innerHTML = video_markup;
			}
			this.get_related_tracks();
		}
		update_now_playing_info(this);
		this.hasBeenPlayed = true;
	},
	
	get_related_tracks: function(){
		new Ajax.Request('server/get_related_tracks.php', {
			parameters: {artist: this.artist_name, track: this.title},
			onComplete: (function(tracks_obj){
					var track_arr = tracks_obj.responseText.split('}');
					var len = (track_arr.length >= 10)? 10 : track_arr.length;
					for (var i = 0; i < len; ++i){
						var artist_track_names = track_arr[i].split('{');
						if (!!artist_track_names[0] && !!artist_track_names[1]){
							track_db[track_db.length] = new Track(artist_track_names[1],artist_track_names[0]);							
						}
						track_db.uniq();
					}
			}).bind(this)
		});
	}
});

/*
 * Artist Class
 * 	Offers a client level interface to the Artist functionality
 *  of Last.FM
 */
var Artist = Class.create({
	initialize: function(artist_name){
		this.artist_name = artist_name;
		this.tracks = [];
		this.get_popular_tracks();
	},

	get_popular_tracks: function(){
		new Ajax.Request('server/get_artist_tracks.php',{
			parameters: {artist: this.artist_name},
			onComplete: (function(artist_tracks){
					var track_arr = artist_tracks.responseText.split(';');
					var len = (track_arr.length >= 10)? 10 : track_arr.length;
					for (var i = 0; i < len; ++i){
						this.tracks.push(new Track(track_arr[i], this.artist_name));
					}
			}).bind(this)
		})
	}
});
/*
 * Tag Class
 *  For Last.FM abstract functionality of the Tag portion of the API
 */
var Tag = Class.create({
	initialize: function(tag_name){
		this.name = tag_name;
		this.tracks = [];
		this.get_popular_tracks();
	},

	get_popular_tracks: function(){
		new Ajax.Request('server/get_tag_tracks.php',{
			parameters: {tag: this.name},
			onComplete: (function(tracks_obj){
					var track_arr = tracks_obj.responseText.split('}');
					var len = (track_arr.length >= 10)? 10 : track_arr.length;
					for (var i = 0; i < len; ++i){
						var artist_track_names = track_arr[i].split('{');
						if (!!artist_track_names[0] && !!artist_track_names[1]){
							track_db[track_db.length] = new Track(artist_track_names[1],artist_track_names[0]);							
						}
						track_db.uniq();
					}
			}).bind(this)
		})
	}
});
