$(function() {

	$(".player_btn,.play_btn").click(function () {
				var playlistUrl = null;
				if($(this).attr('rel')){
					playlistUrl = $(this).attr('rel');
				}
				if($(this).hasClass('player_open')){
					$.ajax({
					    url: '/player/getPlayerWindow',
					    cache: false,
						 async: false,
					    success: function(msg){
											if(msg.indexOf('true') != -1){
												openExternalPlayer(playlistUrl);
											} else {
												openPlayer(playlistUrl);
												$(".player_btn,.play_btn").removeClass('player_open');
											}
									  }
						});
					
				} else {
					openPlayer(playlistUrl);
					$(".player_btn,.play_btn").addClass('player_open');
				}
				
				return false;
	});

});

function sendURLToFlash(id, type){
		var flashMovie = getFlashMovieObject(type);
		if(flashMovie && flashMovie.sendTextToFlash){
			if(id){
				flashMovie.sendTextToFlash("/releases/songList/" + id, id);
			} else {
				flashMovie.sendTextToFlash("/releases/songList");
			}
		} else {
			if(id){
				setTimeout(function(){sendURLToFlash(id, type)}, 1000);
			} else {
				setTimeout(function(){sendURLToFlash(null, type)}, 1000);
			}
		}
		
	}
	
function openPlayer(url){
		$("html, body").animate({scrollTop:0}, 'slow');
		$("#header").animate({marginTop: "0px"});
		$(".player_btn").fadeOut("fast");
		$("#player").animate({"top": "0px"});
		if(url){
			sendURLToFlash(url, "inlinePlayer");
		} else {
			sendURLToFlash(null, "inlinePlayer");
		}
	}

function getFlashMovieObject(movieName){
		if (window.document[movieName]){
			return window.document[movieName];
		}
		if (navigator.appName.indexOf("Microsoft Internet")==-1){
			if (document.embeds && document.embeds[movieName]){
					return document.embeds[movieName];
				}
			}
		else
		{
			return document.getElementById(movieName);
		}
		return false;
	}