﻿var song_i = 0;
var repeat = false;
var currect_act = 'unknown';
var FlashHelper =
{
	movieIsLoaded : function (theMovie)
	{
		if (typeof(theMovie) != "undefined") return theMovie.PercentLoaded() == 100;
		else return
		false;
  },

	getMovie : function (movieName)
	{
  	if (navigator.appName.indexOf ("Microsoft") !=-1) return window[movieName];
	  else return document[movieName];
	}
};

function player(name)
{
	this.obj = FlashHelper.getMovie(name);

	if (!FlashHelper.movieIsLoaded(this.obj)) return;

	this.play = function () {
		this.obj.TCallLabel('/','play');
	};

	this.stop = function () {
		this.obj.TCallLabel('/','stop');
	};

	this.pause = function () {
		this.obj.TCallLabel('/','pause');
	};

	this.playToggle = function () {
		this.obj.TCallLabel('/','playToggle');
	};

	this.reset = function () {
		this.obj.TCallLabel('/','reset');
	};

	this.load = function (url) {
		this.obj.SetVariable('currentSong', url);
		this.obj.TCallLabel('/','load');
	};

	this.loadAndPlay = function (url) {
		this.load(url);
		this.play();
	};

	this.getState = function () {
		var ps = this.obj.GetVariable('playingState');
		var ls = this.obj.GetVariable('loadingState');

		// returns
		//   'empty' if no file is loaded
		//   'loading' if file is loading
		//   'playing' if user has pressed play AND file has loaded
		//   'stopped' if not empty and file is stopped
		//   'paused' if file is paused
		//   'finished' if file has finished playing
		//   'error' if an error occurred
		if (ps == 'playing')
			if (ls == 'loaded') return ps;
			else return ls;

		if (ps == 'stopped')
			if (ls == 'empty') return ls;
			if (ls == 'error') return ls;
			else return ps;

		return ps;

	};

	this.getPlayingState = function () {
		// returns 'playing', 'paused', 'stopped' or 'finished'
		return this.obj.GetVariable('playingState');
	};

	this.getLoadingState = function () {
		// returns 'empty', 'loading', 'loaded' or 'error'
		return this.obj.GetVariable('loadingState');
	};

	this.registerEvent = function (eventName, action) {
		// eventName is a string with one of the following values: onPlay, onStop, onPause, onError, onSongOver, onBufferingComplete, onBufferingStarted
		// action is a string with the javascript code to run.
		//
		// example: player('player1').registerEvent('onPlay', 'alert("playing!")');

		this.obj.SetVariable(eventName, action);
	};

	return this;
}
SoditajiPub.player = {
	player : {
		thisSong : '0',
		nextSong : '0',
		thisFile : 'none',
		replay : 'no',
		stauss : 'load',
		restartPlay : function(){
			this.thisSong = '0';
			this.nextSong = '0';
			this.thisFile = 'none';
			this.startPlay();
		},
		startPlaySong:function(sid){
			this.thisSong = sid;
			this.startPlaying();
		},
		startPlay : function(){
			this.addEvents();
			SoditajiPub.player.player.loadSong('first','0');
		},
		loadSong : function(act, song){
		plist = SoditajiPub.playlist.type;
			$.get("song.php", {a: act, s: song, p: plist},
  				function(data){
    				if(data.r == 'first'){
    					SoditajiPub.player.player.thisSong = data.v1;
    					SoditajiPub.player.player.nextSong = data.v2;
    					SoditajiPub.player.player.startPlaying();
    				}
    				if(data.r == 'file'){
	    				SoditajiPub.player.player.thisFile = data.v1;
	    				SoditajiPub.player.player.playFile();
    				}
    				if(data.r=='next'){
    					if(data.v1 == 'replay'){
    						if(SoditajiPub.player.player.replay == 'yes'){
    							SoditajiPub.player.player.nextSong = data.v2;
    						} else {
    							player('Player1').registerEvent('onSongOver', 'SoditajiPub.player.player.songsOver();');
    						}
    					} else {
    						SoditajiPub.player.player.nextSong = data.v1;
    					}
    				}
  				}, "json");

		},
		songsOver : function(){
			SoditajiPub.inform.openConf("AtskaÅ†oÅ¡anas saraksts ir izskanÄ“jis un atkÄrtoÅ¡anas funkcijas ir izslÄ“gta.<br>Vai sÄkt atskaÅ†ot no sÄkuma?","SoditajiPub.player.player.restartPlay();","");
		},
		startPlaying : function(){
			a = this.thisSong;
			this.loadSong('file',a);
		},
		playFile : function(){
			song_uri = "player/"+this.thisFile;
			player('Player1').loadAndPlay(song_uri);
		},
		playNext : function(){
			if(this.nextSong=='n'){
				if(SoditajiPub.player.player.replay == 'yes'){
					SoditajiPub.player.player.restartPlay();
				} else {
					SoditajiPub.player.player.songsOver();
				}	
			} else {
			this.thisSong = this.nextSong;
			b = this.nextSong;
			this.startPlaying();
			this.loadSong('next',b);
			}
		},
		userStoped : function(){
			this.statuss = 'stop';
		},
		userPlayed : function(){
			this.statuss = 'play';
		},
		userPaused : function(){
			this.statuss = 'pause';
		},
		addEvents : function(){
			player('Player1').registerEvent('onSongOver', 'SoditajiPub.player.player.playNext();');
			player('Player1').registerEvent('onStop', 'SoditajiPub.player.player.userStoped();');
			player('Player1').registerEvent('onPlay', 'SoditajiPub.player.player.userPlayed();');
			player('Player1').registerEvent('onPause', 'SoditajiPub.player.player.userPaused();');

		}
	}
}
function req4start(i){
	SoditajiPub.inform.openConf("Vai vēlaties sākt dziesmas atskaņošanu?","SoditajiPub.player.player.startPlaySong("+i+");","");
}