var MP = {

	debug: false,
	autostart: false,
	reserved: new Array(),
	currentPlayListID: 0,
	currentPlayListType: 'playlist',
	currentTrackID: -1,
	currentArtistID: -1,
	currentAlbumID: -1,
	artistRequest: {},
	userID: 0,
	flashReady: false,

	/**
	*
	* Object instatiating
	*
	* autostart - weather to start player automaticly or not
	* debug - verbose output to console (usefull if browser has console object)
	* reserved - array of user playlist ids
	* firstTrack - first song id (if autostart set to 'true' this one will be played first)
	*
	* @param object {autostart: boolean, debug: boolean, reserved: array, firstTrack: integer}
	* @return object self
	*
	*/
	init: function() {
		if (this.instance == null) {
			var o = (typeof(arguments[0]) == 'object') ? arguments[0] : {};
			this.debug = (typeof(o.debug) == 'boolean') ? ((window.console && window.console.log) ? o.debug : false) : false;
			this.autostart = (typeof(o.autostart) == 'boolean') ? o.autostart : false;
			this.reserved = (typeof(o.reserved) == 'object') ? o.reserved : ((typeof(o.reserved) == 'number') ? new Array(o.reserved) : this.reserved);
			this.artistRequest = o.artist;
			this.userID = o.userID;
      this.initUI();
      return this;
		}
		return this.instance;
	},

	/**
	*
	* User interface initializing and UI event binding
	*
	* @return void
	*
	*/
	initUI: function() {
    $("#trackList").disableTextSelect();
		if (this.autostart) {
			this.play();
		}

    if (this.artistRequest.album > 0) {
			this.loadAlbum(this.artistRequest.album);
		} else if(this.artistRequest.id > 0){
			this.loadArtist(this.artistRequest.id);
		}
		if (this.artistRequest.song > 0){
			this.currentTrackID = this.artistRequest.song;
		}
		this.initListEvents();
	},

	/**
	*
	* Navigation update
	* This one is mostly requested upon navigation click event
	*
	* @return void
	*
	*/
	updateNavigation: function() {

	},

	/**
	*
	* Rerturns media player object
	*
	* @param string, media player id
	* @return object
	*
	*/
	mediaPlayer: function(movieName) {
		if(document.embeds[movieName]) return document.embeds[movieName]; // FireFox
		if(window.document[movieName]) return window.document[movieName]; // IE
		if(window[movieName]) return window[movieName];
		if(document[movieName]) return document[movieName];
		return null;
	},

	mediaPlayerReady: function(){
		this.flashReady = true;
	},

	/**
	*
	*	Find tr with attribute rel and return object with track, artist and album id.
	*
	* @param mixed DOM element
	* @return object { trackID: integer, artistID: integer, albumID: integer }
	*
	*/
	getListId: function() {
		if (arguments.length == 0) {
			return { trackID: -1, artistID: -1, albumID: -1 };
		}

		var ids = [0,0,0];
		if ($(arguments[0]).is('tr')) {
			ids = $(arguments[0]).attr('rel').split('|');
		} else if ($(arguments[0]).parents('tr:first').length > 0 && $(arguments[0]).parents('tr:first').attr('rel')) {
			ids = $(arguments[0]).parents('tr:first').attr('rel').split('|');
		} else if ($(arguments[0]).attr('rel')){
			ids = $(arguments[0]).attr('rel').split('|');
		}
		if (ids[0] == '' || ids[0] == null || isNaN(ids[0])){
			ids[0] = 0;
		}
		if (ids[1] == '' || ids[1] == null || isNaN(ids[1])){
			ids[1] = 0;
		}
		if (ids[2] == '' || ids[2] == null || isNaN(ids[2])){
			ids[2] = 0;
		}
		return { trackID: ids[0], artistID: ids[1], albumID: ids[2] };
	},

	/**
	*
	*	Bind trackList events
	*
	* @return void
	*
	*/
	initListEvents: function() {
  	$("#trackList tbody tr").click(function(e) {
			//MP.play(MP.getListId(this));
			window.open($(this).find('a.play').attr('href'));
			MP.mediaPlayer('music_player').stopSong();
			return false;
		});

		$("#trackList tbody tr.albumData").unbind('click');

		this.highLightTrack();

	},

	initLogin: function(){

	},

	/**
	*
	*	Unbind trackList events
	*
	* @return void
	*
	*/
	deinitListEvents: function() {
		$("trackList tbody tr").unbind('click');
	},

	/**
	*
	*	Parse JSON data and return tracklist
	*
	* @param object, data passed by ajax request response
	* @param boolean, weather to show cover and album data in list
	* @param integer, playlist ID
	* @return string
	*
	*/
	parseJSON: function() {

	},

	/**
	*
	*	.. comment goes here
	*
	* @return boolean
	*
	*/
	loadPlayList: function() {

	},

	/**
	*
	*	.. comment goes here
	*
	* @return boolean
	*
	*/
	loadPlayPage: function() {

	},

	/**
	*
	*	.. comment goes here
	*
	* @return boolean
	*
	*/
	loadArtist: function() {

	},

	/**
	*
	*	.. comment goes here
	*
	* @return boolean
	*
	*/
	loadAlbum: function() {

	},

	/**
	*
	*	.. comment goes here
	*
	* @return void
	*
	*/
	updatePlayList: function() {
		if (arguments.length == 0) {
			return false;
		}
	},

	/**
	*
	*	.. comment goes here
	*
	* @return boolean
	*
	*/
	play: function() {
		if (arguments.length > 0) {
			var item = arguments[0];
			this.currentTrackID = item.trackID;
			this.currentArtistID = item.artistID;
			this.currentAlbumID = item.albumID;
		}

		if (this.flashReady){
			this.highLightTrack();
			MP.mediaPlayer('music_player').loadSong(this.currentTrackID, this.currentArtistID, this.currentAlbumID);
		} else {
			setTimeout('MP.play()', 500);
		}
	},

	/**
	*
	*	.. comment goes here
	*
	* @return void
	*
	*/
	highLightTrack: function() {
		if (arguments.length > 0) {
			var item = arguments[0];
			this.currentTrackID = item.trackID;
			this.currentArtistID = item.artistID;
			this.currentAlbumID = item.albumID;
		}

    $("#trackList tbody tr").filter('.selected').removeClass('selected');
		$("#trackList tbody tr[rel=" + this.currentTrackID + '|' + this.currentArtistID + '|' + this.currentAlbumID + "]").addClass('selected');
	},

	postPlaylistLoad: function(){

	},

	/**
	*
	*	.. comment goes here
	*
	* @return void
	*
	*/
	postSongLoad: function() {
		var o = arguments[0];

		if(o.isVideo == true){
			MP.switchToVideo();
 		} else {
			MP.switchToMusic();
			var image =  o.imageUrl;
			var extraData = o.other.data.split('|');
			var price = extraData[0];
			var songName = o.title;
			var artistName = o.artist;
			var songID = extraData[1];

			this.currentTrackID = o.id;
			this.currentArtistID = o.artistId;
			this.currentAlbumID = o.albumId;
			//console.log('info from flash');
			//console.log(this.currentTrackID,this.currentArtistID,this.currentAlbumID);
			this.highLightTrack();
		}
	},

	/**
	*
	*	.. comment goes here
	*
	* @return void
	*
	*/
	showAdvert: function() {
		if (arguments.length == 0) {
			return false;
		}
	},

	/**
	*
	*	.. comment goes here
	*
	* @return void
	*
	*/
	hideAdvert: function() {

	},

	/**
	*
	*	.. comment goes here
	*
	* @return void
	*
	*/
	notice: function() {
		/*if (arguments.length > 0 && typeof(arguments[0]) == 'string' && arguments[0].length > 0) {
			$.jGrowl(arguments[0], { header: 'Important', position: 'bottom-right' });
		}*/
	},

	/**
	*
	*	.. comment goes here
	*
	* @return boolean
	*
	*/
	in_array: function(v, a) {
		for(var i = 0; i < a.length; i ++) {
			if (a[i] == v) {
				return true;
			}
		}
		return false;
	},

	/**
	*
	*	switch to video content
	*
	*
	*/
	switchToVideo: function() {

  },

	/**
	*
	*	switch to video content
	*
	*
	*/
	switchToMusic: function() {

	},

	/**
	*
	*	.. comment goes here
	*
	* @return boolean
	*
	*/
	switchSponsor: function() {

	},

	/**
	*
	*	.. comment goes here
	*
	* @return void
	*
	*/
	initRating: function() {

	},

	/**
	*
	*	.. comment goes here
	*
	* @return void
	*
	*/
	updateInfo: function() {
		//alert(1);
		//if (this.debug) console.log('Unused function updateInfo in action');
	},

	/**
	*
	*	.. comment goes here
	*
	* @return boolean
	*
	*/
	switchMedia: function() {

	},

	instance: null

};

