soundManager.flashVersion = 9;
soundManager.useConsole = false;
soundManager.consoleOnly = false;
soundManager.debugMode = false;
soundManager.defaultOptions.multiShot = false;
soundManager.url = "/swf/soundmanager2.swf";
// enable HTML5 audio support, if you're feeling adventurous.
// soundManager.useHTML5Audio = true;

jq.music={
	init: function(){
		jq.jQuery(document).ready(function(){
			jq.jQuery(".sounds_container ul li.sctr").hover(function(){
				jq.jQuery(this).css("border-color","#A4A4A4");
				jq.jQuery(this).children(".sc_uname").css("text-shadow","0 0 4px white");
			},function(){
				jq.jQuery(this).css("border-color","#343434");
				jq.jQuery(this).children(".sc_uname").css("text-shadow","none");
			});

			jq.jQuery(".playlist li").hover(function(){
				jq.jQuery(this).css("background-color","#868686");
			},function(){
				if (jq.jQuery(this).hasClass(".playing") || jq.jQuery(this).hasClass(".paused")){
					jq.jQuery(this).css("background-color","#000000");
				}else{
					jq.jQuery(this).css("background-color","transparent");
				}
			});

			jq.jQuery(".sc_uname").bind("click",jq.music.toggle_playlist_container);
			jq.jQuery(".play_activator").click(jq.music.play_the_music);
		});
	},

	toggle_playlist_container: function(){
		if (jq.jQuery(this).parent().children(".sc_tracks_container").css("display") == 'none'){
			//hidden - open
			jq.music.close_opened();
			var player = jq.jQuery(this).parent().children().find("#fst_player");
			//console.log(player);
			jq.music.play_the_music(player);
			jq.jQuery(this).children("img").animate({height:"70px",width:"70px"},300);
			jq.jQuery(this).parent().children(".sc_tracks_container").slideDown(300);
			jq.jQuery(this).parent().addClass("active");
		}else{
			//opened - hide
			jq.jQuery(this).children("img").animate({height:"40px",width:"40px"},300);
			jq.jQuery(this).parent().children(".sc_tracks_container").slideUp(300);
			jq.jQuery(this).parent().removeClass("active");
		}
	},

	close_opened : function(){
		if (jq.jQuery(".sounds_container ul li.active").length){
			jq.jQuery(".sounds_container ul li.active").each(function(){
				jq.jQuery(this).children(".sc_uname").children("img").animate({height:"40px",width:"40px"},300);
				jq.jQuery(this).children(".sc_tracks_container").slideUp(300);
				jq.jQuery(this).removeClass("active");
			});
		}
	},

	sound : "",

	play_the_music : function(player){
		//alert('clicked');
		//console.log(player);
		if (!jq.jQuery(this).parent("li").hasClass("playing")){

			jq.jQuery(".playlist li").removeClass("paused").removeClass("playing");

			if (!player.length){
				//console.log(jq.jQuery(this));
				jq.jQuery(this).parent("li").addClass("playing");
				var id = jq.jQuery(this).parent().children("#track_guid").val();
			}else{
				jq.jQuery(player).addClass("playing");
				var id = jq.jQuery(player).children("#track_guid").val();
			}

			var url = jq.music.buid_sound_url(id);
			//console.log(url);

			jq.music.stop();

			jq.jQuery.jsonp({
				url: url,
				error: function(xOptions, textStatus) {
					if (textStatus === "error"){
						//console.log('player error');
					}
					//console.log(xOptions);
				},
				success: function(track){
					// Sound
					jq.music.sound = soundManager.createSound({
						id: track.id,
						url: track.stream_url+'?oauth_consumer_key=s7iCgBuXsTZ738qOUTpGw',
						whileloading : function(){
							//console.log(track);
						},
	                    whileplaying : function(){},
	                    onfinish : function(){},
	                    onload : function(){}
					});
					//console.log(jq.music.sound);
					jq.music.play();
				}
			});
			return false;
		}else{
			jq.jQuery(".playlist li").removeClass("paused").removeClass("playing");
			jq.jQuery(this).parent("li").addClass("paused");
			jq.music.stop();
		}
	},

	buid_sound_url : function(id){
		return "http://api.soundcloud.com/tracks/" + id + ".js?callback=?&consumer_key=s7iCgBuXsTZ738qOUTpGw";
	},

	play : function(){
		if(jq.music.sound){
			jq.music.sound.paused ? jq.music.sound.resume() : jq.music.sound.play();
		}
	},

	stop : function(){
		if(jq.music.sound){
			jq.music.sound.pause();
		}
	}
}
jq.music.init();
