soundManager.url = "swf/xdomain/";
soundManager.flashVersion = 9;
soundManager.useHighPerformance = true;
soundManager.useFastPolling = true;
soundManager.debugMode = false;
soundManager.wmode = 'transparent'; 
soundManager.useHTML5Audio = false; 


var jP = {
	playing: 0,
	video: 0,
	volume: 100,
	soundId: "jizzLive",
	streamSel: "0",
	peak: 1,
	maxMb: 35,
	cupertino:0,
	settings: {video: 0,live:0,default_mount:1},
	metaURL:"http://jizzradio.com/svc/tag?callback=?",
	settingsURL:"http://jizzradio.com/svc/settings?callback=?",
	mountsURL:"http://jizzradio.com/svc/mounts?callback=?",
	mountData:[],
	mountPlaying:1,
	streamURL:["http://ondalabs.com:8000/jizz.mp3"],
	play: function()
	{
		this.createSound();
		soundManager.play(this.soundId);
		this.playing = 1;
		$(document).trigger("playing");
		
	},
	stop: function()
	{
		soundManager.stop(this.soundId);
		soundManager.destroySound(this.soundId);
		this.playing = 0;
		$(document).trigger("stopping");
	},
	createSound: function()
	{
		if(soundManager.getSoundById(this.soundId) != null) soundManager.destroySound(this.soundId);
		_jP = this;
		var snd = soundManager.createSound(
			{	
				id: _jP.soundId,
				url: _jP.getPlayingMountURL(),
				volume: _jP.volume,
				usePeakData: true,
				whileplaying: function()
				{
					if(_jP.peak == 1)
					{
						$(document).trigger("jizzualizer",[this.peakData]);
					}
					var mbUsed = Math.round(this.bytesLoaded/1048576*100000)/100000
					
					if(mbUsed > _jP.maxMb && _jP.playing == 1)
					{
						_jP.rehash();
					}
				}
			}
		);
		
	},
	rehash: function()
	{
		soundManager.stop(this.soundId);
		soundManager.destroySound(this.soundId);
		this.createSound();	
		soundManager.play(this.soundId);
	},
	toggle: function()
	{
		this.playing == 1 ? this.stop() : this.play();
	},
	vol: function(v)
	{
		if(v > 100) v = 100;
		if(v < 0) v = 0;
		
		try{	
		soundManager.setVolume(this.soundId,v);
		}catch(e){};
		this.volume = v;


	},	
	startVideo: function()
	{
		$(document).trigger("videoMode",["on"]);
	},
	stopVideo: function()
	{
		$(document).trigger("videoMode",["off"]);
	},
	handle:function()
	{
		_jP = this;
		$(document).bind("changeVolume",function(event,vol){_jP.vol(vol*3)});
		$(document).bind("play",function(event){_jP.play();});
		$(document).bind("stop",function(event){_jP.stop();});
		$(document).bind("togglePlay",function(event){_jP.toggle();});
		
		
	},
	startMountHandler: function()
	{
		_jP = this;
		this.refreshMountData(function(){ if(!_jP.cupertino){_jP.toggle();}});	
		$(document).everyTime("15s",function(){ _jP.refreshMountData(); })
	},
	startSettings: function()
	{
		_jP = this;
		this.refreshSettings();	
		$(document).everyTime("15s",function(){ _jP.refreshSettings(); })
	},
	refreshSettings: function()
	{
		_jP = this;
		$.ajax({
		  dataType: 'jsonp',

		  url: this.settingsURL,
		  success: function (data) {
				$(data).each(function(){
					oldValue = _jP.settings[this.name]; 
		
					_jP.settings[this.name] = this.value;
					if(oldValue != this.value) _jP.updatedSetting(this.name);
					
				});
			
			 }
		});		
	},
	updatedSetting: function(name)
	{
		switch(name)
		{
			case "video":
				this.settings[name] == "1" ? this.startVideo() : this.stopVideo(); 
				break;
			case "live":
				this.setupMounts();
				break;
			
		}
	},
	sendTitleInfo: function(meta)
	{
		if($.md5(meta) != this.metaMD5)
	 	{
			mar = meta.split(" - ");
			artist = mar[mar.length - 1];
			mar.splice(mar.length-1,1);
			title = mar.join(" - ");
			this.metaMD5 = $.md5(meta);
			$(document).trigger("setTitleData",[artist,title])	
		}
	},
	getPlayingMountURL: function()
	{
		var url;
/*		if(this.settings.live == "1")
		{
			$(this.mountData).each(function(){
				if(this.livestream)
				{
					url = this.url; 
					return false;
				}
			});
		}
		else
		{*/
			(this.playingMount)?url=this.mountData[this.playingMount].url:url="";
			//(this.mountData[default_mount].url)?url=this.mountData[default_mount].url:url="";
	//	}
		return url;
	},
	setupMounts: function()
	{
		_jP=this;
		(this.settings.default_mount)?this.playingMount = parseInt(this.settings.default_mount):this.playingMount=1;
		if(this.settings.live == "1")
		{
			$(this.mountData).each(function(){
				if(this.livestream)
				{
					_jP.playingMount = this.id; 
					return false;
				}
			});
		}	
		(this.mountData[this.playingMount].meta)?this.sendTitleInfo(this.mountData[this.playingMount].meta):false;
		if(soundManager.getSoundById(this.soundId) != null)
		{
			if(soundManager.getSoundById(this.soundId).url != this.mountData[this.playingMount].url && this.playing == 1)
			{
				this.stop();this.play();
			}
		}
		if(this.cupertino)
		{
			
			if($("#jizzlogo source").attr("src") != this.mountData[this.playingMount].url)
			{
				$(document).trigger("isMapple");
			}
		}
	},
	refreshMountData: function(funct)
	{
		_jP = this;
	/*	$.ajax({
		  dataType: 'jsonp',

		  url: this.metaURL,
		  success: function (data) {

	 			if($.md5(data.meta) != _jP.metaMD5)
	 			{

	 				mar = data.meta.split(" - ");
	 				artist = mar[mar.length - 1];
	 				mar.splice(mar.length-1,1);
	 				title = mar.join(" - ");
	 				_jP.metaMD5 = $.md5(data.meta);
	 				
	 				$(document).trigger("setTitleData",[artist,title])	
	 			}
			
			 }
		});
	*/	
		var mounts = [];
		$.ajax({
		  dataType: 'jsonp',

		  url: this.mountsURL,
		  success: function (data) {
					if($.isArray(data))
					{
						$(data).each(function(){
							id=this.id;
							//this.id = null;
							mounts[id] = this;
						});
					}
					_jP.mountData = mounts;
					_jP.setupMounts();
					if($.isFunction(funct)) funct();
	 			}
			});	
			 

	},
	checkiPhone:function()
	{
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)))
		{
			this.cupertino = 1;
			this.startSettings();
			this.startMountHandler();
		}
	},
	init: function()
	{
		this.startSettings();
		this.startMountHandler();

		this.handle();
		
	}
}
//soundManager.onerror = function(){alert("err");}
//soundManager.onload = function(){jP.init();}


soundManager.onready(function(a){ if(soundManager.supported()){jP.init();}else{jP.checkiPhone();}  })
