jQuery(document).ready(function() { //Or $(function() {
	
	
	var userId;
	var basketId;
	var domain = "http://"+document.domain+"/";
	var radioPlaying = false;
	var pathname = window.location.pathname;
	
	var playlistIdMobile;
	var firstLoad = true;
	var next = 0;
	var currentSong;
	var justLoggedIn = false;
	var infoShow = true;
	var t;
	var mainContentHovered;
	var libraryShowing;
	
	$('#mainContent').hide();
	$('#mainContentGrey').hide();
	//$('#messageContent').hide();
	$('#messagePopUpContent').hide();
	$('#searchInfoBubble').hide();
	$('#infoBubble').hide();
	$('#audioVideo').hide();
	$('#audioVideoText').hide();
	//$('#audioListJavascript').hide();
	$('#bigAd').load(domain + 'includes/bigAd.php');
	$('#twoCols').load(domain + 'includes/2Cols.php',activateAlbum);
	$('#thirdCol').load(domain + 'includes/3rdCol.php');
	activateRemovePlaylist();
	activateRemoveFeaturedTracks();
	activateTextLinks();
	activateViewBasket(0);
	
	$("#login").html( 'Login' );
	/*embedAudioPlayer();
	function embedAudioPlayer()
	{
		var flashvars = {},
		params = {wmode:"transparent"},
		attributes = {};
		swfobject.embedSWF("AudioPlayerStreaming.swf", "mediaPlayer", "338", "283", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
				
	}*/	
	function test()
	{
		//alert('test');
	}
	
	if (admin)
	{
		
	}
	else
	{
		checkLogin();
	}
		
	function showMessageContent()
	{
		$('#messageContent').show();
	}
	
	function showLoginMessage(message, item)
	{
		$('#messagePopUpContent').html(message);
		$('#messagePopUpContent').fadeIn("fast");		
		var offset = item.offset();
   		$('#messagePopUpContent').css("left",offset.left - 180);
   		$('#messagePopUpContent').css("top",offset.top - 30);
   		$("#messagePopUpContent").delay(1700).fadeOut(500, "linear");
	}
	
	$("#searchHeader").submit(function(){

		  var str = $(this).serialize();

		     $.ajax({
		    	 
		     type: "POST",
		     url: domain + "includes/search.php",
		     data: str,
		     success: function(msg)
		     {		
		    	 libraryShowing = false;
		    	 showMainContent();		 $('#mainContent').css('background-color','#fefefe');   	 
		    	 $('#mainContent').html(msg);
		    	 activateBackToGenres();
		 		 activateBackToHome();
		    	 runSearchPagination($('#searchTerm').text(), '');
		    	 $('#searchTerm').hide();
		    	 $('#thirdCol').hide();
		     }
		   });

		  return false;

	});
	
	$("#searchAdmin").submit(function(){
		var str = $(this).serialize();
		
	     $.ajax({
	    	 
	     type: "POST",
	     url: domain + "admin/searchAdmin.php",
	     data: str,
	     success: function(msg)
	     {
	    	 $('#searchResults').html(msg);
	    	 activateFeaturedTracks();
	    	 //runSearchPagination();
	     }
	   });

	  return false;

	});
	$("#searchCompanies").submit(function(){
		var str = $(this).serialize();
		
	     $.ajax({
	    	 
	     type: "POST",
	     url: domain + "admin/searchCompanies.php",
	     data: str,
	     success: function(msg)
	     {
	    	 $('#searchResults').html(msg);
	     }
	   });

	  return false;

	});
	$("#searchRegistrants").submit(function(){
		var str = $(this).serialize();
		
	     $.ajax({
	    	 
	     type: "POST",
	     url: domain + "admin/searchRegistrants.php",
	     data: str,
	     success: function(msg)
	     {
	    	 $('#searchResults').html(msg);
	     }
	   });

	  return false;

	});
	
	function activateGenreSearch()
	{
		activateTextLinks();
		showMainContentGrey();
		$(".searchGenre").click(function(event){

			event.preventDefault;
			var find = this.id;

			     $.ajax({
			    	 
			     type: "POST",
			     url: domain + "includes/search.php",
			     data: {
			    	 find : find,
			    	 list : 'genre'
			     },
			     success: function(msg)
			     {		
			    	 libraryShowing = false;
			    	 showMainContent(); $('#mainContent').css('background-color','#fefefe');
			    	 $('#mainContent').html(msg);
			    	 runSearchPagination($('#searchTerm').text(), $('#searchList').text());
			    	 $('#searchTerm').hide();
			    	 $('#searchList').hide();
			    	 activateBackToGenres();
			     }
			   });

			  return false;

		});
		
	}
	
	$('.moreInfo').hide();	
	
	$('#tabMenu > li').click(function(){
		var menuId = this.id;
		//refreshCaptcha();
		
	    //remove the selected class from all LI    
	    $('#tabMenu > li').removeClass('selected');
	    $('#loginMenu > li').removeClass('selected');
	    
	    $('#searchInfoBubble').hide();
		$('#infoBubble').hide();
	    
	    //Reassign the LI
	    $(this).addClass('selected');
	    //$(this)
	    
	    if (menuId=='home')
    	{
	    	$('#thirdCol').show();$('#audioVideo').hide(); $('#audioVideoText').hide();	
	    	$('#bigAd').load(domain + 'includes/bigAd.php',showBigAd);
	    	libraryShowing = false;
    	}
	    if (menuId=='playlist')
    	{
	    	$('#thirdCol').hide();
	    	libraryShowing = true;
	    	//showMainContent();
	    	activateMyLibrary(loggedIn);
	    	$('#mainContent').css('min-height','283px');
    	}
	    if (menuId=='radio')
	    {
	    	$('#thirdCol').hide();	    	
	    	$('#mainContentGrey').css('min-height','355px');
	    	$('#mainContentGrey').load(domain + 'includes/radio.php', activateRadio);
	    	
	    	libraryShowing = false;
    	}
	    if (menuId=='audioAndVideo')
	    {
	    	$('#thirdCol').show();	    	
	    	if (pathname=="/mobile/" || pathname=="/mobile/index.php")
	    	{
	    		$('#audioVideo').load(domain + 'includes/audioVideoMobile.php', activateTextLinks);
	    	}
	    	else
	    	{
	    		$('#audioVideo').load(domain + 'includes/audioVideo.php', activateTextLinks); $('#audioVideoText').show();
	    	}
	    	showAudioVideo();
	    	libraryShowing = false;
    	}

	    if (menuId=='genres')
	    {
	    	$('#thirdCol').show();	    	
	    	/*setTimeout(function() {
	    		$('#mainContent').css('background-color','#434343');
	        }, 3000);*/
	    	$('#mainContentGrey').load(domain + 'includes/genres.php', activateGenreSearch);
	    	
	    	libraryShowing = false;
    	}
	    if (menuId=='licensing')
	    {
	    	$('#thirdCol').show();	    	
	    	/*setTimeout(function() {
	    		$('#mainContent').css('background-color','#fefefe');
		    	$('#mainContent').css('min-height','685px');
	        }, 3000);*/
	    	
	    	$('#mainContent').load(domain + 'includes/licensing.php',activateLicensing);
	    	
	    	libraryShowing = false;
    	}
	    if (menuId=='contact')
	    {
	    	$('#thirdCol').show();	    	
	    	$('#mainContent').load(domain + 'includes/contact.php',activateContactForm);
	    	//showMainContent();
	    	libraryShowing = false;
    	}
	    
	});
	
	$('#loginMenu > li').click(function(){
		var menuId = this.id;
		//refreshCaptcha();
		//alert('menuId ='+menuId)
	    //remove the selected class from all LI    
	    $('#tabMenu > li').removeClass('selected');
	    $('#loginMenu > li').removeClass('selected');
	    
	    $('#searchInfoBubble').hide();
		$('#infoBubble').hide();
	    
	    //Reassign the LI
	    $(this).addClass('selected');
	    //$(this)
	    
	    if (menuId=='login')
    	{	    	
	    	//alert('logged in ='+loggedIn)
	    	
	    	if (loggedIn)
	    	{
	    		//alert('logged in');
	    		$('#mainContent').load(domain + 'includes/logout.php');
	    		loggedIn = false;
	    		$('#mainContent').load(domain + 'includes/loginForm.php', activateLogin);
	    		activateViewBasket(0);
	    		$("#login").html( 'Login' );
	    		$('#thirdCol').hide();
	    		if (pathname=="/mobile/" || pathname=="/mobile/index.php")
		    	{
	    			createMobilePlaylist(playlistIdMobile, "playList", "");
		    	}
		    	else
		    	{
		    		setAudioPlayerLoggedIn(false);
		    	}	    		
	    	}
	    	else
	    	{	    		
	    		//alert('not logged in');
	    		$('#mainContent').load(domain + 'includes/loginForm.php', activateLogin);
	    		$("#login").html( 'Login' );
	    		$('#thirdCol').hide();
	    		if (pathname=="/mobile/" || pathname=="/mobile/index.php")
		    	{
	    			//createMobilePlaylist(playlistIdMobile, "playList", "");
		    	}
		    	else
		    	{
		    		setAudioPlayerLoggedIn(false);
		    	}	
	    	}
	    	
	    	libraryShowing = false;
    	}
	    if (menuId=='register')
    	{
	      	$('#mainContent').load(domain + 'includes/register.php', activateRegistration);
	    	
	    	libraryShowing = false;
	    }
	   
	});
	
	$("#bigAd").hover(
			
			  function () {

				  activateAlbum();
			  },
			  function () {
				 
			  }
		);
	
	function activateTextLinks()
	{
		$('.tabLinkContact').bind('click', function(event) {
			event.preventDefault();  
		    $('#tabMenu > li').removeClass('selected');
		    $('#tabMenu > li.contact').addClass('selected');
		    $('#loginMenu > li').removeClass('selected');		    	    
		    $('#thirdCol').show();
	    	showMainContent(); $('#mainContent').css('background-color','#fefefe');
	    	$('#mainContent').load(domain + 'includes/contact.php'); 		  
		});
		$('.tabLinkLegal').bind('click', function(event) {
			event.preventDefault(); 
		    $('#tabMenu > li').removeClass('selected');
		    $('#loginMenu > li').removeClass('selected');		    	    
		    $('#thirdCol').show();
	    	showMainContent(); $('#mainContent').css('background-color','#fefefe');
	    	$('#mainContent').load(domain + 'includes/legal.php',activateTextLinks); 	  
		});
		$('.tabLinkHome').bind('click', function(event) {
			event.preventDefault(); 
		    $('#tabMenu > li').removeClass('selected');
		    $('#tabMenu > li.home').addClass('selected');
		    $('#loginMenu > li').removeClass('selected');		    	    
		    $('#thirdCol').show();$('#audioVideo').hide(); $('#audioVideoText').hide();
	    	showBigAd();$('#mainContent').css('background-color','#fefefe');
	    	$('#bigAd').load(domain + 'includes/bigAd.php'); 	  
		});		
		$('.tabLinkLibrary').bind('click', function(event) {
			event.preventDefault(); 
		    $('#tabMenu > li').removeClass('selected');
		    $('#tabMenu > li.playlist').addClass('selected');
		    $('#loginMenu > li').removeClass('selected');		    	    
		    $('#thirdCol').hide();
	    	showMainContent(); $('#mainContent').css('background-color','#fefefe');
	    	activateMyLibrary(loggedIn);	  
		});
		$('.tabLinkRadio').bind('click', function(event) {
			event.preventDefault(); 
		    $('#tabMenu > li').removeClass('selected');
		    $('#tabMenu > li.radio').addClass('selected');
		    $('#loginMenu > li').removeClass('selected');		    	    
		    $('#thirdCol').show();
	    	showMainContent(); $('#mainContent').css('background-color','#fefefe');
	    	$('#mainContent').load(domain + 'includes/legal.php',activateTextLinks); 	  
		});
		$('.tabLinkAV').bind('click', function(event) {
			event.preventDefault(); 
		    $('#tabMenu > li').removeClass('selected');
		    $('#tabMenu > li.audioAndVideo').addClass('selected');
		    $('#loginMenu > li').removeClass('selected');		    	    
		    $('#thirdCol').show();
	    	showAudioVideo(); $('#mainContent').css('background-color','#fefefe');
	    	$('#audioVideo').load(domain + 'includes/audioVideo.php', activateTextLinks);
	    	$('#audioVideoText').show();
		});
		$('.tabLinkGenres').bind('click', function(event) {
			event.preventDefault(); 
		    $('#tabMenu > li').removeClass('selected');
		    $('#loginMenu > li').removeClass('selected');
		    $('#tabMenu > li.genres').addClass('selected');
		    $('#thirdCol').show();
	    	showMainContent(); $('#mainContent').css('background-color','#434343');
	    	$('#mainContent').load(domain + 'includes/genres.php', activateGenreSearch);	  
		});
		$('.tabLinkLicensing').bind('click', function(event) {
			event.preventDefault(); 
		    $('#tabMenu > li').removeClass('selected');
		    $('#tabMenu > li.licensing').addClass('selected');
		    $('#loginMenu > li').removeClass('selected');		    	    
		    $('#thirdCol').hide();
	    	showMainContent();
	    	$('#mainContent').css('background-color','#fefefe');
	    	$('#mainContent').css('min-height','624px');
	    	$('#mainContent').load(domain + 'includes/licensing.php');	  
		});
		$('.tabLinkBasket').bind('click', function(event) {
			event.preventDefault(); 
			//$('#tabMenu > li').removeClass('selected');
		   // $('#tabMenu > li.playlist').addClass('selected');
		   // $('#loginMenu > li').removeClass('selected');	    	
	    	//showMainContent();
	    	//$('#mainContent').html(msg);
			if (loggedIn)
	    	{
	 		     $.ajax({
	 		    	 
		 		     type: "POST",
		 		     data: {
		 		    	 basketId : basketId,
		 		    	 userId : userId
		 		     },
		 		     url: domain + "includes/basket.php",
		 		     success: function(msg)
		 		     {    	 
		 		    	showMainContent();	
		 		    	$('#mainContent').html(msg);
		 		    	activateBasketDetails();
		 		    	$('#mainContentGrey').css('min-height','355px');
		 		    	$('#mainContent').css('background-color','#fefefe');
		 		    	$('#thirdCol').hide();
		 			 }
	 		     });
	    	}
	    	else
	    	{	    		
	    		showLoginMessage('Sorry, you need to register to view your basket.', p);
	        }	
	    	//$('#mainContentGrey').css('min-height','355px');
	    	//$('#mainContent').css('background-color','#fefefe');
	    	//$('#thirdCol').hide();
		});
		$('.tabLinkLogin').bind('click', function(event) {
			event.preventDefault(); 
		    $('#tabMenu > li').removeClass('selected');
		    $('#loginMenu > li').removeClass('selected');	
		    $('#loginMenu > li.login').addClass('selected');
		    showMainContent();
	    	if (loggedIn)
	    	{
	    		$('#mainContent').load(domain + 'includes/logout.php');
	    		$('#mainContent').load(domain + 'includes/loginForm.php', activateLogin);
	    		$("#login").html( 'Login' );
	    		$('#thirdCol').hide();
	    	}
	    	else
	    	{	    		
	    		$('#mainContent').load(domain + 'includes/loginForm.php', activateLogin);
	    		$("#login").html( 'Login' );
	    		$('#thirdCol').hide();
	    	}
		});
		$('.tabLinkRegister').bind('click', function(event) {
			event.preventDefault(); 
		    $('#tabMenu > li').removeClass('selected');
		    $('#loginMenu > li').removeClass('selected');	
		    $('#loginMenu > li.register').addClass('selected');
		    $('#mainContent').load(domain + 'includes/register.php', activateRegistration);
	    	showMainContent(); 	  
		});
	}
	
	$('#bidAd1').click(function(event){

		event.preventDefault;
		$('#thirdCol').show();
    	showMainContent(); $('#mainContent').css('background-color','#434343');
    	$('#mainContent').load(domain + 'includes/genres.php', activateGenreSearch); 

	  return false;
	   
	});
	function activateBackToGenres()
	{
		$('#backToGenres').click(function(event){
			event.preventDefault;
			$('#thirdCol').show();
	    	showMainContentGrey();// $('#mainContent').css('background-color','#434343');
	    	$('#mainContentGrey').load(domain + 'includes/genres.php', activateGenreSearch); 
	    	hideBubbles();
		    return false;		   
		});
	}
	function activateBackToBasket()
	{
		
		$('#backToBasket').click(function(event){
			event.preventDefault;
			if (loggedIn)
	    	{
	 		     $.ajax({
	 		    	 
		 		     type: "POST",
		 		     data: {
		 		    	 basketId : basketId,
		 		    	 userId : userId
		 		     },
		 		     url: domain + "includes/basket.php",
		 		     success: function(msg)
		 		     {    	 
		 		    	showMainContent();	
		 		    	$('#mainContent').html(msg);
		 		    	activateBasketDetails();
		 		    	$('#mainContentGrey').css('min-height','355px');
		 		    	$('#mainContent').css('background-color','#fefefe');
		 		    	$('#thirdCol').hide();
		 			 }
	 		     });
	    	}
	    	else
	    	{	    		
	    		showLoginMessage('Sorry, you need to register to view your basket.', p);
	        }	
		    return false;		   
		});
	}
	function hideBubbles()
	{
		$('#searchInfoBubble').hide();
    	$('#infoBubble').hide();
	}
	function activateBackToHome()
	{

		$('#backToHome').click(function(event){

			event.preventDefault;
			$('#thirdCol').show();$('#audioVideo').hide(); $('#audioVideoText').hide();
	    	showBigAd();$('#mainContent').css('background-color','#fefefe');
	    	$('#bigAd').load(domain + 'includes/bigAd.php');
	
		  return false;
		   
		});
	}
	function activateLogin(){

		activateTextLinks();
		showMainContent();
		$("#loginForm").submit(function(){

		  var str = $(this).serialize();
		  
		  var request = $.ajax({
			  url: domain + "includes/processLogin.php",
			  type: "POST",
			  data: str
			});
	
			request.done(function(msg) {
				//alert(msg);
				$('#messageContent').show();
				$("#messageContent").html( msg );
				$('#thirdCol').hide();
				justLoggedIn = true;
				checkLogin();
				libraryShowing = true;
				if (pathname=="/mobile/" || pathname=="/mobile/index.php")
		    	{
					//createMobilePlaylist(playlistIdMobile, "playList", "");
		    	}
		    	else
		    	{
		    		setAudioPlayerLoggedIn(true);
		    	}
		    	//showMainContent();
		    	//$('#messageContent').show();
				//$("#messageContent").html( 'That user does not exist, please register first.');
		    	$('#mainContent').css('min-height','283px');
				$('#playlist').addClass('selected');
				$('#login').removeClass('selected');
			});
	
			request.fail(function(jqXHR, textStatus) {
			  //alert( "Request failed: " + textStatus );
			});
			
			return false;

		});
		
		$("#forgotPassword").click(function(event){

			event.preventDefault;
			 
			$('#mainContent').load(domain + 'includes/forgotPassword.php', activatePassword);
			  return false;
		});	
		
		$("#changePassword").click(function(event){

			event.preventDefault;
			 
			$('#mainContent').load(domain + 'includes/changePassword.php', activatePassword);
			  return false;

		});	
	};
	function activatePassword()
	{
	   	$("#sendPassword").submit(function(){

	  		 var str = $(this).serialize();
		 
		     $.ajax({
		    	 
		     type: "POST",
		     url: domain + "includes/sendPassword.php",
		     data: str,
		     success: function(msg)
		     {		
		    	$('#messageContent').show();
				$("#messageContent").html( msg );
				$("#messageContent").css( 'padding','10px 20px' );
				$('html, body').animate({ scrollTop: 0 }, 'slow', 'linear');		    	
		     }
		   });

		  return false;

	    });
	   	
	   	$("#editPassword").submit(function(){

	  		 var str = $(this).serialize();
		 
		     $.ajax({
		    	 
		     type: "POST",
		     url: domain + "includes/editPassword.php",
		     data: str,
		     success: function(msg)
		     {		
		    	$('#messageContent').show();
				$("#messageContent").html( msg );
				$("#messageContent").css( 'padding','10px 20px' );
				$('html, body').animate({ scrollTop: 0 }, 'slow', 'linear');		    	
		     }
		   });

		  return false;

	    });
	}
	
	function checkLogin(){
		//alert(domain + "includes/checkLogin.php");
		  var request = $.ajax({
			  url: domain + "includes/checkLogin.php",
			  type: "POST"
			});
	
			request.done(function(msg) {
				var userName = msg.split("&");
				//alert(msg);
				//alert(userName[0]);
				//alert(userName[1]);
				//alert(userName[2]);
				if (msg=='Login')
				{
					loggedIn = false;
					$("#login").html( msg );
					//setAudioPlayerLoggedIn(false);
				}
				else
				{
					loggedIn = true;
					$("#login").html( userName[0]);
					basketId = Number(userName[2]);
					userId = Number(userName[1]);
					updateBasket(basketId);
					if (justLoggedIn)
					{
						justLoggedIn = false;
						activateMyLibrary(true);
					}
					if (pathname=="/mobile/" || pathname=="/mobile/index.php")
			    	{
						createMobilePlaylist(playlistIdMobile, "playList", "");
			    	}
			    	//setAudioPlayerLoggedIn(true);
				}
			});
	
			request.fail(function(jqXHR, textStatus) {
			  //alert( "Request failed: " + textStatus );
			});
	};
		
	function activateRegistration(){
		activateTextLinks();
		showMainContent();
		$("#registrationForm").submit(function(){
		
			$('#messageContent').show();
			$("#messageContent").html( 'Please wait while your we process your registration...' );
		  var str = $(this).serialize();
		  
		  var request = $.ajax({
			  url: domain + "includes/processRegister.php",
			  type: "POST",
			  data: str
			});
	
			request.done(function(msg) {
				$('#messageContent').show();
				$("#messageContent").html( msg );
				$("#messageContent").css( 'padding','10px 20px' );
				$('html, body').animate({ scrollTop: 0 }, 'slow', 'linear');
			});
	
			request.fail(function(jqXHR, textStatus) {
			  //alert( "Request failed: " + textStatus );
			});
			
			return false;

		});
	};
	function showBigAd()
	{
		activateTextLinks();
		$('#mainContent').hide();
		$('#mainContentGrey').hide();
		$('#bigAd').show();
		$('#twoCols').show();
	}
	
	function showMainContent()
	{
		$('#bigAd').hide();
		$('#twoCols').hide();
		$('#audioVideo').hide(); $('#audioVideoText').hide();
		$('#messageContent').hide();
		$('#mainContentGrey').hide();
		//$('#mainContent').hide();
		//$('#mainContent').fadeIn("fast");
		$('#mainContent').show();
		
		mainContentHovered = false;
		//alert('libraryShowing = '+libraryShowing);
		if (libraryShowing==true)
		{
			//alert('library showing');
			$('#mainContent').hover(

				  function () {
					  //alert(mainContentHovered);
					  $("#searchInfoBubble").hide();
					  if (mainContentHovered==false)
					  {
						  mainContentHovered = true;
						  activatePlaylist();
						  activateTextLinks();
					  }
				  },
				  function () {
					  //mainContentHovered = false;
				  }
			);
		}
		else
		{
			//alert('library not showing');			
			$('#mainContent').unbind('mouseenter mouseleave');
			
			$('#mainContent').hover(

				  function () {
					  $("#searchInfoBubble").hide();					 
				  },
				  function () {
					  
				  }
			);
		}
	}
	function showMainContentGrey()
	{
		$('#bigAd').hide();
		$('#twoCols').hide();
		$('#audioVideo').hide(); $('#audioVideoText').hide();
		$('#messageContent').hide();
		$('#mainContent').hide();
		//$('#mainContent').hide();
		//$('#mainContent').fadeIn("fast");
		$('#mainContentGrey').show();
	}
	function showAudioVideo()
	{
		$('#bigAd').hide();
		$('#twoCols').hide();
		$('#messageContent').hide();
		$('#mainContent').hide();
		$('#mainContentGrey').hide();
		$('#audioVideo').show();	 $('#audioVideoText').show();	
		//$('#audioVideo').hide();
		//$('#audioVideo').fadeIn("fast");
	}
	function showBasketContent()
	{
		$('#messagePopUpContent').fadeIn("fast");
		$('#mainContentGrey').hide();
		$('#mainContent').hide();
	}
	$(function () {
		$("#txtSearchHeader").watermark("Search our libraries");		
	});
	
	$(function () {
		$("#txtSearchAdmin").watermark("Search");		
	});
		
	
	function activatePlaylist(){
		//alert('activatePlaylist');
		var basketIdActivated = basketId;
		$('.playlist').click(function(event){
			
			event.preventDefault;
			if (pathname=="/mobile/" || pathname=="/mobile/index.php")
			{
				var playlistId = playlistIdMobile;
			}
			else
			{
				var playlistId = newPlaylistId;
			}
			
			var audioId = this.id;
			var userId = this.id;
			var p = $(this);
			//alert(playlistId);alert(audioId);alert(userId);
		     $.ajax({
		    	 
		     type: "POST",
		     data: {
		    	 playlistId : playlistId,
		    	 audioId : audioId,
		    	 userId : userId,
		    	 playNow : 0
		     },
		     url: domain + "admin/addToPlaylist.php",
		     success: function(msg)
		     {
		    	 if (msg=='already')
		    	 {
					 showLoginMessage('This track is already in your playlist.', p);
		    	 }
		    	 else
		    	 {
		    		 if (pathname=="/mobile/" || pathname=="/mobile/index.php")
	    			 {
		    			 //alert('playlistIdMobile = '+playlistIdMobile);
			    	 	 createMobilePlaylist(playlistIdMobile, "playList", "");			    	 	 
	    			 }
		    		 else
		    		 {
		    			 updateAudioXML();
		    			 if (libraryShowing)
		    			 {
		    				 changePlayerTitle("MY LIBRARY/FEATURED TRACKS");
		    			 }
		    			 else
		    			 {
		    				 changePlayerTitle("SEARCH/FEATURED TRACKS");
		    			 }	
		    		 }
		    		 activateRemovePlaylist();			    	 
			     }
			 }
		   });
	
		  return false;
		   
		});
		$('.playnow').click(function(event){
			event.preventDefault;
			if (pathname=="/mobile/" || pathname=="/mobile/index.php")
			{
				var playlistId = playlistIdMobile;
			}
			else
			{
				var playlistId = newPlaylistId;
			}
			var audioId = this.id;

			//alert(playlistId);alert(audioId);
			if (loggedIn)
	    	{
				
	    	}
		     $.ajax({
		    	 
		     type: "POST",
		     data: {
		    	 playlistId : playlistId,
		    	 audioId : audioId,
		    	 playNow : 1,
		    	 radioCurrentlyPlaying: radioPlaying
		     },
		     url: domain + "admin/addToPlaylist.php",
		     success: function(msg)
		     {
		    	 //$('#featuredPlaylist').html(msg);
		    	 //alert(msg);
		    	 if (msg=='already')
		    	 {		    		 
					 showLoginMessage('This track is already in your playlist.', p);
		    	 }
		    	 else
		    	 {
		    		 if (pathname=="/mobile/" || pathname=="/mobile/index.php")
	    			 {
		    			// alert('playlistIdMobile = '+playlistIdMobile);
			    	 	 createMobilePlaylist(playlistIdMobile, "playNow", "");
	    			 }
		    		 else
		    		 {
		    			 updateAudioXML();
		    			 if (libraryShowing)
		    			 {
		    				 changePlayerTitle("MY LIBRARY/FEATURED TRACKS");
		    			 }
		    			 else
		    			 {
		    				 if (msg=="1")
		    		    	 {		    		 
		    					 changePlayerTitle("SEARCH");
		    		    	 }
		    				 else
		    				 {
		    					 changePlayerTitle("SEARCH/FEATURED TRACKS");
		    				 }
		    			 }	
		    		 }
		    		 activateRemovePlaylist();
			    	 radioPlaying = false;
			     }		    	 
			 }
		   });
	
		  return false;
		   
		});
		$('.basket').click(function(event){

			event.preventDefault;
			var p = $(this);
			if (loggedIn)
	    	{
				 var audioId = this.id;
	 		     $.ajax({
	 		    	 
		 		     type: "POST",
		 		     data: {
		 		    	 basketId : basketIdActivated,
		 		    	 audioId : audioId
		 		     },
		 		     url: domain + "includes/addToBasket.php",
		 		     success: function(msg)
		 		     {    	 
	 		    		 showLoginMessage(msg, p);
	 		    		 updateBasket(basketIdActivated);
		 			 }
	 		     });
	    	}
	    	else
	    	{	    		
	    		showLoginMessage('Sorry, you need to be a registered user to download music.', p);
	        }			
	
		  return false;
		});
		$(".searchInfo").click(function(event){
			event.preventDefault;
			return false;
		});
		if (pathname=="/mobile/" || pathname=="/mobile/index.php")
    	{
			
    	}
		else
		{
			$(".searchInfo").hover(
					  function () {
						  var audioId = this.id;
						  var p = $(this);
						  var offset = p.offset();
						     $.ajax({
						    	 
						     type: "POST",
						     data: {
						    	 audioId : audioId
						     },
						     url: domain + "includes/getSearchInfo.php",
						     success: function(msg)
						     {
						    	// alert(infoShow);
						    	 if (infoShow)
						    	 {
						    		 infoShow = false;
						    		 clearTimeout(t);
						    		 $('#searchInfoBubble').show();
							    	 $('#searchInfoBubble').css("left",offset.left-90);
							    	 $('#searchInfoBubble').css("top",offset.top+15);
							    	 $('#searchInfoBubble').html(msg);
							    	 t = setTimeout(function() {
							    		 //alert('hello');
							    		    $('#searchInfoBubble').fadeOut('fast');
							    		    infoShow = true;
							    		}, 6000);
						    	 }		    	 
							 }
						   });
					  },
					  function () {
						  $('#searchInfoBubble').hide();infoShow = true;
					  }
				);
				$(".playnow").hover(
		
						  function () {
							  var p = $(this);var offset = p.offset();
							  $('#searchInfoBubble').hide();
							  $('#infoBubble').show();
						      $('#infoBubble').css("left",offset.left-35);
						      $('#infoBubble').css("top",offset.top+15);
						      $('#infoBubble').html('Play track now');
						  },
						  function () {
							  $('#infoBubble').hide();
						  }
					);
				$(".playlist").hover(
		
					  function () {
						  var p = $(this);var offset = p.offset();
						  $('#searchInfoBubble').hide();
						  $('#infoBubble').show();
					      $('#infoBubble').css("left",offset.left-35);
					      $('#infoBubble').css("top",offset.top+15);
					      $('#infoBubble').html('Add to playlist');
					  },
					  function () {
						  $('#infoBubble').hide();
					  }
				);
				$(".basket").hover(
						
					  function () {
		
						  var p = $(this);var offset = p.offset();
						  $('#searchInfoBubble').hide();
						  $('#infoBubble').show();
					      $('#infoBubble').css("left",offset.left-35);
					      $('#infoBubble').css("top",offset.top+15);
					      $('#infoBubble').html('Add to basket');
					  },
					  function () {
						  $('#infoBubble').hide();
					  }
				);
				$(".removeLibraryItem").hover(
						
					  function () {
						  var p = $(this);var offset = p.offset();
						  $('#searchInfoBubble').hide();
						  $('#infoBubble').show();
					      $('#infoBubble').css("left",offset.left-35);
					      $('#infoBubble').css("top",offset.top+15);
					      $('#infoBubble').html('Remove track');
					  },
					  function () {
						  $('#infoBubble').hide();
					  }
				);
		}
	}
	function updateBasket(basketId)
	{
		$.ajax({
	    	 
		     type: "POST",
		     data: {
		    	 basketId : basketId
		     },
		     url: domain + "includes/updateBasket.php",
		     success: function(msg)
		     {
		    	 $('#basket').html(msg);
		    	 activateViewBasket(basketId);
			 }
	     });    	
	}
	function activateViewBasket(basketId)
	{
		//alert('activate basket');
		$('#viewBasket').click(function(event){
			//alert('viewBasket, loggedIn = '+loggedIn);
			event.preventDefault;
			var p = $(this);
			if (loggedIn)
	    	{
	 		     $.ajax({
	 		    	 
		 		     type: "POST",
		 		     data: {
		 		    	 basketId : basketId,
		 		    	 userId : userId
		 		     },
		 		     url: domain + "includes/basket.php",
		 		     success: function(msg)
		 		     {    	 
		 		    	showMainContent();	
		 		    	$('#mainContent').html(msg);
		 		    	activateBasketDetails();
		 		    	$('#mainContentGrey').css('min-height','355px');
		 		    	$('#mainContent').css('background-color','#fefefe');
		 		    	$('#thirdCol').hide();
		 			 }
	 		     });
	    	}
	    	else
	    	{	    		
	    		showLoginMessage('Sorry, you need to register to view your basket.', p);
	        }			
	
		  return false;
		});
		
	}
	
	function activateBasketDetails()
	{
		//alert('activateBasketDetails');
		$('.basketHistory').click(function(event){
			
			event.preventDefault;
			var userId = this.id;
 		     $.ajax({
 		    	 
	 		     type: "POST",
	 		     data: {
	 		    	 userId : userId
	 		     },
	 		     url: domain + "includes/basketHistory.php",
	 		     success: function(msg)
	 		     {    	 
	 		    	showMainContent();	
	 		    	$('#mainContent').html(msg);
	 		    	activateBackToBasket();
	 		    	//updateBasket(basketId);
	 			 }
 		     });
	    	
		  return false;
		});
		$('.clearBasket').click(function(event){
			
			event.preventDefault;
			var basketId = this.id;
 		     $.ajax({
 		    	 
	 		     type: "POST",
	 		     data: {
	 		    	 basketId : basketId
	 		     },
	 		     url: domain + "includes/clearBasket.php",
	 		     success: function(msg)
	 		     {    	 
	 		    	showMainContent();	
	 		    	$('#mainContent').html(msg);
	 		    	updateBasket(basketId);
	 			 }
 		     });
	    	
		  return false;
		});
		$('.download').click(function(event){
			
			event.preventDefault;
			var dataSplit = this.id.split("&");
			//alert('userId = '+userId);
			
			var audioId = dataSplit[0];
			var fileName = dataSplit[1];
			//alert('audioId = '+audioId);
			//alert('fileName = '+fileName);
			$.download(domain + 'includes/downloadTrack.php','fileName='+fileName+'&format=mp3&audioId=' + audioId + '&userId=' + userId);
			$('#infoBubble').hide();
	    	
		  return false;
		});
		$('.downloadAll').click(function(event){
			
			event.preventDefault;
			var basketId = this.id;
			//alert('basket Id = '+basketId);
			//alert('userId = '+userId);
			$.ajax({
		    	 
	 		     type: "POST",
	 		     data: {
	 		    	userId : userId
	 		     },
	 		     url: domain + "includes/checkDownloadLimit.php",
	 		     success: function(msg)
	 		     {   	 		    	
	 		    	//alert(msg);
	 		    	if (msg==1)
	 		    	{
	 		    		$.download(domain + 'includes/downloadAllTracks.php','basketId='+basketId+'&format=mp3&userId=' + userId);
	 		    	}
	 		    	else
	 		    	{
	 		    		//showMainContent();
	 		    		$('#messageContent').show();
		 		    	$('#messageContent').html(msg);
		 		    	//updateBasket(basketId);
	 		    	}
	 			 }
		     });
			
			$('#infoBubble').hide();	    	
		    return false;
		});
		$('.removeBasketItem').click(function(event){
			
			event.preventDefault;
			var dataSplit = this.id.split("&");
			//alert('userId = '+userId);
			
			var audioId = dataSplit[0];
			var basketId = dataSplit[1];
			//alert('audioId = '+audioId);
			//alert('basketId = '+basketId);
			//alert('userId = '+userId);
			$.ajax({
		    	 
	 		     type: "POST",
	 		     data: {
	 		    	audioId : audioId,
	 		    	basketId : basketId,
	 		    	userId : userId
	 		     },
	 		     url: domain + "includes/removeBasketItem.php",
	 		     success: function(msg)
	 		     {
	 		    	$('#mainContent').html(msg);
	 		    	updateBasket(basketId);
	 		    	activateBasketDetails();
	 			 }
		     });;
	    	
		  return false;
		});
		$(".download").hover(
				
			  function () {
				  var p = $(this);var offset = p.offset();
				  $('#searchInfoBubble').hide();
				  $('#infoBubble').show();
			      $('#infoBubble').css("left",offset.left-35);
			      $('#infoBubble').css("top",offset.top+15);
			      $('#infoBubble').html('Download track');
			  },
			  function () {
				  $('#infoBubble').hide();
			  }
		);
		$(".removeBasketItem").hover(
				
				  function () {
					  var p = $(this);var offset = p.offset();
					  $('#searchInfoBubble').hide();
					  $('#infoBubble').show();
				      $('#infoBubble').css("left",offset.left-35);
				      $('#infoBubble').css("top",offset.top+15);
				      $('#infoBubble').html('Remove track');
				  },
				  function () {
					  $('#infoBubble').hide();
				  }
			);
		
		$(".searchInfo").click(function(event){
			event.preventDefault;
			return false;
		});
		$(".searchInfo").hover(
			  function () {
				  var audioId = this.id;
				  var p = $(this);
				  var offset = p.offset();
				     $.ajax({
				    	 
				     type: "POST",
				     data: {
				    	 audioId : audioId
				     },
				     url: domain + "includes/getSearchInfo.php",
				     success: function(msg)
				     {
				    	 if (infoShow)
				    	 {
				    		 infoShow = false;
				    		 clearTimeout(t);
				    		 $('#searchInfoBubble').show();
					    	 $('#searchInfoBubble').css("left",offset.left-90);
					    	 $('#searchInfoBubble').css("top",offset.top+15);
					    	 $('#searchInfoBubble').html(msg);
					    	 t = setTimeout(function() {
					    		 //alert('hello');
					    		    $('#searchInfoBubble').fadeOut('fast');
					    		    infoShow = true;
					    		}, 6000);
				    	 }
				     }
				   });
			  },
			  function () {
				  $('#searchInfoBubble').hide();infoShow = true;
			  }
		);
		$('.playnow').click(function(event){
			event.preventDefault;
			if (pathname=="/mobile/" || pathname=="/mobile/index.php")
			{
				var playlistId = playlistIdMobile;
			}
			else
			{
				var playlistId = newPlaylistId;
			}
			var audioId = this.id;

			//alert(playlistId);alert(audioId);
			if (loggedIn)
	    	{
				
	    	}
		     $.ajax({
		    	 
		     type: "POST",
		     data: {
		    	 playlistId : playlistId,
		    	 audioId : audioId,
		    	 playNow : 1,
		    	 radioCurrentlyPlaying: radioPlaying
		     },
		     url: domain + "admin/addToPlaylist.php",
		     success: function(msg)
		     {
		    	 //$('#featuredPlaylist').html(msg);
		    	 if (msg=='already')
		    	 {		    		 
					 showLoginMessage('This track is already in your playlist.', p);
		    	 }
		    	 else
		    	 {
		    		 if (pathname=="/mobile/" || pathname=="/mobile/index.php")
	    			 {
		    			// alert('playlistIdMobile = '+playlistIdMobile);
			    	 	 createMobilePlaylist(playlistIdMobile, "playNow", "");
	    			 }
		    		 else
		    		 {
		    			 updateAudioXML();
		    			 changePlayerTitle("BASKET/FEATURED TRACKS");
		    		 }
		    		 activateRemovePlaylist();
			    	 radioPlaying = false;
			     }		    	 
			 }
		   });
	
		  return false;
		   
		});		
		$(".playnow").hover(

			  function () {
				  var p = $(this);var offset = p.offset();
				  $('#searchInfoBubble').hide();
				  $('#infoBubble').show();
			      $('#infoBubble').css("left",offset.left-35);
			      $('#infoBubble').css("top",offset.top+15);
			      $('#infoBubble').html('Play track now');
			  },
			  function () {
				  $('#infoBubble').hide();
			  }
		);
	}
	function activateRemovePlaylist(){
		$('.removePlaylistItem').click(function(event){
			event.preventDefault;
			
			var playlistId = newPlaylistId;
			var audioId = this.id;
		     $.ajax({
		    	 
		     type: "POST",
		     data: {
		    	 playlistId : playlistId,
		    	 audioId : audioId
		     },
		     url: domain + "admin/removeFromPlaylist.php",
		     success: function(msg)
		     {
		    	 //$('#featuredPlaylist').html(msg);
		    	 activateRemovePlaylist();
		     }
		   });
	
		  return false;
		   
		});
	}
	function activateRemoveLibrary(){

		activateTextLinks();
		showMainContent();		
		
		$('.removeLibraryItem').click(function(event){
			event.preventDefault;
			var libraryId = this.id;
		     $.ajax({
		    	 
		     type: "POST",
		     data: {
		    	 libraryId : libraryId,
		    	 userId : userId
		     },
		     url: domain + "includes/removeFromLibrary.php",
		     success: function(msg)
		     {
		    	 $('#mainContent').html(msg);
		    	 activateRemoveLibrary();
		    	 $('#infoBubble').hide();
		    	 activatePlaylist();
		     }
		   });
	
		  return false;
		   
		});
		
		$('.removeLibraryTracks').click(function(event){

			event.preventDefault;
			     
 		    if(!e) var e = window.event;

			var answer = confirm("Are you sure you want to remove all your library tracks?")
			if (answer){
				var userId = this.id;
	 		     $.ajax({
	 		    	 
		 		     type: "POST",
		 		     data: {
		 		    	userId : userId
		 		     },
		 		     url: domain + "includes/removeLibraryTracks.php",
		 		     success: function(msg)
		 		     {    	 
		 		    	showMainContent();	
		 		    	$('#mainContent').html('<h1>My library</h1><p>Your library is empty.</p>');
		 			 }
	 		     });
			}
			else{
			}			
				//e.cancelBubble is supported by IE - this will kill the bubbling process.
				e.cancelBubble = true;
				e.returnValue = false;
	
				//e.stopPropagation works only in Firefox.
				if (e.stopPropagation) {
					e.stopPropagation();
					e.preventDefault();
				}
		  return false;
		});
		
		//activatePlaylist();
	}
	function activateFeaturedTracks(){
		$('.listfeatured_tracks').click(function(event){

			event.preventDefault;
			var audioId = this.id;
		     $.ajax({
		    	 
		     type: "POST",
		     data: {
		    	 audioId : audioId,
		    	 list : 'featured_tracks'
		     },
		     url: domain + "admin/addToFeaturedTracks.php",
		     success: function(msg)
		     {
		    	 $('#featuredPlaylist').html(msg);
		    	 activateRemoveFeaturedTracks();
		    	 //updateAudioXML();
			 }
		   });
	
		  return false;
		   
		});
		$('.listlatest_releases').click(function(event){

			event.preventDefault;
			var audioId = this.id;
		     $.ajax({
		    	 
		     type: "POST",
		     data: {
		    	 audioId : audioId,
		    	 list : 'latest_releases'
		     },
		     url: domain + "admin/addToFeaturedTracks.php",
		     success: function(msg)
		     {
		    	 $('#featuredPlaylist').html(msg);
		    	 activateRemoveFeaturedTracks();
		    	 //updateAudioXML();
			 }
		   });
	
		  return false;
		   
		});
		$('.listradio_channel_items').click(function(event){

			event.preventDefault;
			var idSplit = this.id.split("&");
			var audioId = idSplit[0];
			var radioId = idSplit[1];
			var radioName = idSplit[2];
			//alert('radioId = '+radioId);
			//alert('radioName = '+radioName);
		     $.ajax({
		    	 
		     type: "POST",
		     data: {
		    	 audioId : audioId,
		    	 radioId : radioId,
		    	 radioName : radioName,
		    	 list : 'radio_channel_items'
		     },
		     url: domain + "admin/addToFeaturedTracks.php",
		     success: function(msg)
		     {
		    	 $('#featuredPlaylist').html(msg);
		    	 activateRemoveFeaturedTracks();
		    	 //updateAudioXML();
			 }
		   });
	
		  return false;
		   
		});
	}
	
	function activateRemoveFeaturedTracks(){

		$('.removelistfeatured_tracks').click(function(event){
			event.preventDefault;

			var audioId = this.id;		     
	
		     if(!e) var e = window.event;

				var answer = confirm("Are you sure you want to remove this item?")
				if (answer){
					$.ajax({
				    	 
					     type: "POST",
					     data: {
					    	 audioId : audioId,
					    	 list : 'featured_tracks'
					     },
					     url: domain + "admin/removeFromFeaturedTracks.php",
					     success: function(msg)
					     {
					    	 $('#featuredPlaylist').html(msg);
					    	 activateRemoveFeaturedTracks();
					     }
					   });
				}
				else{
				}			
					//e.cancelBubble is supported by IE - this will kill the bubbling process.
					e.cancelBubble = true;
					e.returnValue = false;
		
					//e.stopPropagation works only in Firefox.
					if (e.stopPropagation) {
						e.stopPropagation();
						e.preventDefault();
					}
				return false;	
		   
		});
		$('.removelistlatest_releases').click(function(event){
				event.preventDefault;

				var audioId = this.id;			     
		
			     if(!e) var e = window.event;

					var answer = confirm("Are you sure you want to remove this item?")
					if (answer){
						$.ajax({
					    	 
						     type: "POST",
						     data: {
						    	 audioId : audioId,
						    	 list : 'latest_releases'
						     },
						     url: domain + "admin/removeFromFeaturedTracks.php",
						     success: function(msg)
						     {
						    	 $('#featuredPlaylist').html(msg);
						    	 activateRemoveFeaturedTracks();
						     }
						   });
					}
					else{
					}			
						//e.cancelBubble is supported by IE - this will kill the bubbling process.
						e.cancelBubble = true;
						e.returnValue = false;
			
						//e.stopPropagation works only in Firefox.
						if (e.stopPropagation) {
							e.stopPropagation();
							e.preventDefault();
						}
					return false;	
			   
			});
		
		$('.removelistradio_channel_items').click(function(event){
			event.preventDefault;

			var idSplit = this.id.split("&");
			var audioId = idSplit[0];
			var radioId = idSplit[1];
			var radioName = idSplit[2];
			
			if(!e) var e = window.event;

			var answer = confirm("Are you sure you want to remove this item?")
			if (answer){
				 $.ajax({
			    	 
				     type: "POST",
				     data: {
				    	 audioId : audioId,
				    	 radioId : radioId,
				    	 radioName : radioName,
				    	 list : 'radio_channel_items'
				     },
				     url: domain + "admin/removeFromFeaturedTracks.php",
				     success: function(msg)
				     {
				    	 $('#featuredPlaylist').html(msg);
				    	 activateRemoveFeaturedTracks();
				     }
				   });
			}
			else{
			}			
				//e.cancelBubble is supported by IE - this will kill the bubbling process.
				e.cancelBubble = true;
				e.returnValue = false;
	
				//e.stopPropagation works only in Firefox.
				if (e.stopPropagation) {
					e.stopPropagation();
					e.preventDefault();
				}
			return false;	
		   
		});
	}
	
	function activateMyLibrary(loggedIn)
	{
		//alert('logged in = '+loggedIn);
		
		if (loggedIn)
		{
			$('#mainContent').load(domain + 'includes/myLibrary.php?userId='+userId+'&playlistId='+newPlaylistId, activateRemoveLibrary);			
		}
		else
		{
			setLibraryLoggedOut();
			
		}
		
		/*$('#tabMenu > li').removeClass('selected');
		$('#tabMenu > li.playlist').addClass('selected');
	    $('#loginMenu > li').removeClass('selected');		    	    
	    $('#thirdCol').hide();*/
	}
	function setLibraryLoggedOut()
	{
		activateTextLinks();
		$('#mainContent').html('<h1>My library</h1><p>You do not need to register to listen to our music.</p><p>As a bona fide production music library user, you will need to register with us to download and create</p><ul><li>My Library (to save and come back to tracks at your leisure)</li><li>My Basket (to download music for your productions)</li></ul><p>and license our music via the MCPS in the UK. Go to <a href="" class="tabLinkLicensing">Licensing</a> for terms and links.</p><br />');
		showMainContent();		
	}
	function checkLoginStatue(loggedIn)
	{
		if (loggedIn)
		{
			$('#mainContent').html('you are logged in, here is your library');
		}
		else
		{
			$('#mainContent').html('You need to be a registered user to create a library.');
		}
	}
	function activateAlbum()
	{
		//alert('activate album');
		$('.album').click(function(event){

			event.preventDefault;
			var cat_no = this.id;
			//alert('cat_no = '+cat_no);
		     $.ajax({
		    	 
		     type: "POST",
		     data: {
		    	 cat_no : cat_no,
		    	 list : 'album'
		     },
		     url: domain + "includes/search.php",
		     success: function(msg)
		     {		
		    	 showMainContent();		    	 
		    	 $('#mainContent').html(msg);
		    	 runSearchPagination($('#searchTerm').text(), $('#searchList').text());
		    	 $('#searchTerm').hide();
		    	 $('#searchList').hide();	    	 
		     }
		   });
	
		  return false;
		   
		});
	}
	//Pagination ---------------------
function runSearchPagination(str, list)
{
	
	str = str.replace(/\s/g,"%20");
	//$array = array('lastname', 'email', 'phone');
	//$comma_separated = implode(",", $array);
	//alert(str);
	//References
	var pages = $("#paginate li.number");
	var loading = $("#loading");
	var loading2 = $("#loading2");
	var content = $("#content");
	var pageNum = 1;
	var totalPages;
	var firstLoad = true;
	
	//show loading bar
	function showLoading(){

		if (firstLoad==false)
		{
			//unBindAnchorLinks();alert('11');
		}
		firstLoad = false;
		loading
			.css({visibility:"visible"})
			.css({opacity:"1"})
			.css({display:"block"})
		;
		loading2
			.css({visibility:"visible"})
			.css({opacity:"1"})
			.css({display:"block"})
		;
	}
	//hide loading bar
	function hideLoading(){
		loading.fadeTo(1000, 0);
		loading2.fadeTo(1000, 0);
	//	if (anchorLinksLoaded==false)
		//{
		//loadAnchorLinks();
		//}
		activatePlaylist();
		activateBackToGenres();
		activateBackToHome();
	};
	

	//Manage click events
	pages.click(function(){
		//show the loading bar
		showLoading();
		hideBubbles();
		pages.removeClass('selected');
		//Highlight current page number
		//pages.css({'background-color' : ''});
		//$(this).css({'background-color' : 'yellow'});
		totalPages = this.value;
		//Load content
		pageNum = this.id;
		var targetUrl = domain + "includes/content.php?str="+str+"&list="+list+"&page=" + pageNum + "&" + $("#myForm").serialize() + " #content";				
		content.load(targetUrl, hideLoading);
		//loadAnchorLinks();
		$(this).addClass('selected');
		
		if (pum==1)
		{
			$(".previous").addClass('disabled');
			$(".next").removeClass('disabled');
		}

		if (pageNum==totalPages)
		{
			$(".previous").removeClass('disabled');
			$(".next").addClass('disabled');
		}
	});
	
	$(".previous").click(function(){
		totalPages = this.value;
		hideBubbles();
		//alert(pageNum + '/' + totalPages);
		if (pageNum>1)
		{
			showLoading();
			pages.removeClass('selected');
			$(".next").removeClass('disabled');

			pageNum--;
			var targetUrl = domain + "includes/content.php?str="+str+"&list="+list+"&page=" + pageNum + "&" + $("#myForm").serialize() + " #content";
			content.load(targetUrl, hideLoading);
			//loadAnchorLinks();
			$("#paginate li#"+pageNum).addClass('selected');
			//if ()
			//alert(pageNum);
			if (pageNum==1)
			{
				$(".previous").addClass('disabled');
			}
			if (pageNum>9)
			{
				//alert(pageNum);
				$("#paginate li#"+(pageNum-8)).html(pageNum-9);
				$("#paginate li#"+(pageNum-7)).html(pageNum-8);
				$("#paginate li#"+(pageNum-6)).html(pageNum-7);
				$("#paginate li#"+(pageNum-5)).html(pageNum-6);
				$("#paginate li#"+(pageNum-4)).html(pageNum-5);
				$("#paginate li#"+(pageNum-3)).html(pageNum-4);
				$("#paginate li#"+(pageNum-2)).html(pageNum-3);
				$("#paginate li#"+(pageNum-1)).html(pageNum-2);
				$("#paginate li#"+(pageNum)).html(pageNum-1);
				$("#paginate li#"+(pageNum+1)).html(pageNum);	
				
				$("#paginate li#"+(pageNum-8)).attr("id",pageNum-9);
				$("#paginate li#"+(pageNum-7)).attr("id",pageNum-8);
				$("#paginate li#"+(pageNum-6)).attr("id",pageNum-7);
				$("#paginate li#"+(pageNum-5)).attr("id",pageNum-6);
				$("#paginate li#"+(pageNum-4)).attr("id",pageNum-5);
				$("#paginate li#"+(pageNum-3)).attr("id",pageNum-4);
				$("#paginate li#"+(pageNum-2)).attr("id",pageNum-3);
				$("#paginate li#"+(pageNum-1)).attr("id",pageNum-2);
				$("#paginate li#"+(pageNum)).attr("id",pageNum-1);
				$("#paginate li#"+(pageNum+1)).attr("id",pageNum);
			}
		}
	});
	
	$(".next").click(function(){
		totalPages = this.value;
		hideBubbles();
		//alert(pageNum + '/' + totalPages);
		if (pageNum<totalPages)
		{
			
			showLoading();
			pages.removeClass('selected');
			$(".previous").removeClass('disabled');

			pageNum++;
			var targetUrl = domain + "includes/content.php?str="+str+"&list="+list+"&page=" + pageNum + "&" + $("#myForm").serialize() + " #content";
			content.load(targetUrl, hideLoading);
			//loadAnchorLinks();
			
			//alert(pageNum);
			if (pageNum==totalPages)
			{
				$(".next").addClass('disabled');
			}
		}
		
		if (pageNum>1)
		{
			$("#paginate li#"+pageNum).addClass('visible');
			$("#paginate li#1").addClass('invisible');
		}
		if (pageNum>10)
		{
			//alert(pageNum);
						
			$("#paginate li#"+(pageNum-1)).html(pageNum);			
			$("#paginate li#"+(pageNum-2)).html(pageNum-1);
			$("#paginate li#"+(pageNum-3)).html(pageNum-2);
			$("#paginate li#"+(pageNum-4)).html(pageNum-3);
			$("#paginate li#"+(pageNum-5)).html(pageNum-4);
			$("#paginate li#"+(pageNum-6)).html(pageNum-5);
			$("#paginate li#"+(pageNum-7)).html(pageNum-6);
			$("#paginate li#"+(pageNum-8)).html(pageNum-7);
			$("#paginate li#"+(pageNum-9)).html(pageNum-8);
			$("#paginate li#"+(pageNum-10)).html(pageNum-9);
			
			$("#paginate li#"+(pageNum-1)).attr("id",pageNum);
			$("#paginate li#"+(pageNum-2)).attr("id",pageNum-1);
			$("#paginate li#"+(pageNum-3)).attr("id",pageNum-2);	
			$("#paginate li#"+(pageNum-4)).attr("id",pageNum-3);
			$("#paginate li#"+(pageNum-5)).attr("id",pageNum-4);
			$("#paginate li#"+(pageNum-6)).attr("id",pageNum-5);
			$("#paginate li#"+(pageNum-7)).attr("id",pageNum-6);
			$("#paginate li#"+(pageNum-8)).attr("id",pageNum-7);
			$("#paginate li#"+(pageNum-9)).attr("id",pageNum-8);
			$("#paginate li#"+(pageNum-10)).attr("id",pageNum-9);
		}
		$("#paginate li#"+pageNum).addClass('selected');
	});
	
	//default - 1st page
	//alert("includes/content.php?str="+str+"&list="+list);
	var targetUrl = domain + "includes/content.php?str="+str+"&list="+list+"&page=1&" + $("#myForm").serialize() + " #content";
	showLoading();
	content.load(targetUrl, hideLoading);
	//loadAnchorLinks();
	$("#1").addClass('selected');
	if (pages==1)
	{
		$(".previous").addClass('disabled');
		$(".next").addClass('disabled');
	}
	
	
}
function activateLicensing()
{
	activateTextLinks();
	showMainContent();
}
function activateContactForm()
{
	activateTextLinks();
	showMainContent();
	$("#contactUsForm").submit(function(){

		$('#messageContent').show();
		$("#messageContent").html( 'Please wait while your we process your comment...' );
		  var str = $(this).serialize();

		     $.ajax({
		    	 
		     type: "POST",
		     url: domain + "includes/form-to-email.php",
		     data: str,
		     success: function(msg)
		     {		
		    	 if(msg == 1) // Message Sent? Show the 'Thank You' message and hide the form
				  {
		    		 showBigAd();
		    		 $('#messageContent').show();
					 $("#messageContent").html( 'Thank you, your message has been sent.' );
					 $("#messageContent").css( 'padding','10px 20px' );
					 $('html, body').animate({ scrollTop: 0 }, 'slow', 'linear');
				  	 $(':input','#contactUsForm')
				  	 .not(':button, :submit, :reset, :hidden')
				  	 .val('')
				  	 .removeAttr('checked')
				  	 .removeAttr('selected');
				  }
				  else
				  {
					  //alert(msg);  
				  }
		     }
		   });

		  return false;

	});	
}
function activateRadio()
{
	activateTextLinks();
	showMainContentGrey();
	$(".playRadio").click(function(event){
		event.preventDefault;
		radioPlaying = true;
		 var radioSplit = this.id.split("&");
		 var radioId = radioSplit[0];
		 var radioName = radioSplit[1];
		 var activeChannel = $(this).parent().parent();
		 
		 $('.radioChannel').css('background-color','#282828');
		 
		 $('.radioChannel').animate({
			 opacity: 0.5
			  }, 500, function() {
				  activeChannel.css('background-color','#BD1F2D');
			  });
		 
		 $(this).parent().parent().animate({
			 opacity: 1
			  }, 500, function() {
				  
			  });
		 
		 if (pathname=="/mobile/" || pathname=="/mobile/index.php" || pathname=="/mobile/#" )
		 {
			 createMobilePlaylist("", "playNow", radioId);
		 }
		 else
		 {
			 //alert('radioId = '+radioId);
			 //alert('radioName = '+radioName);
		 
		     $.ajax({
		    	 
		     type: "POST",
		     url: domain + "includes/generateRadioPlaylist.php",
		     data: {
		    	 radioId : radioId,
		    	 radioName : radioName
		     },
		     success: function(msg)
		     {		
		    	 loadRadio(radioId); //Tell the flash player to load in the radio xml file.
		    	 changePlayerTitle("RADIO: "+radioName);
		     }
		   });
		 }
		 

		  return false;

	});	
}
jQuery.download = function(url, data, method){
	//url and data options required
	if( url && data ){ 
		//data can be string of parameters or array/object
		data = typeof data == 'string' ? data : jQuery.param(data);
		//split params into form inputs
		var inputs = '';
		jQuery.each(data.split('&'), function(){ 
			var pair = this.split('=');
			inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />'; 
		});
		//send request
		jQuery('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
		.appendTo('body').submit().remove();
	};
};


/* Mobile code */

//Create initial mobile list
if (pathname=="/mobile/" || pathname=="/mobile/index.php" || pathname=="/mobile/#" )
{				
	$.ajax({
	   	 
	     type: "POST",
	     url: domain + "includes/createPlaylist.php",
	     success: function(msg)
	     {
	    	 playlistIdMobile = msg;
	    	 createMobilePlaylist(playlistIdMobile, "playList", "");	    	 
	     }
	});	
}
else
{
	$('#audioPlayer').css("height","159"); //height for desktop browsers only to allow flexible height on mobile players
}
function createMobilePlaylist(playlistIdMobile, playWhen, radioId)
{
	//alert('CMP loggedIn = '+loggedIn);
	//alert('create mobile playlist');
	 $.ajax({
	   	 
	     type: "POST",
	     url: domain + "includes/createMobilePlaylist.php",
	     data: {
				playlistId:playlistIdMobile,
				playWhen:playWhen,
				radioId:radioId,
				loggedIn:loggedIn
		 },
	     success: function(msg)
	     {
	    	 //alert(msg);
	    	 $('#audioPlayer').html(msg);
		     currentSong = group[next][0] + ' : ' + group[next][1] + ' : ' + group[next][2];
		     document.getElementById('songMarquee').innerHTML = currentSong;	
		    // alert('firstLoad = '+firstLoad);
		     if (firstLoad)
		     {
		    	 firstLoad = false;
		    	 loadPlayer();
		     }
		     if (playWhen=="playNow")
		     {
		    	 next = 0;
		    	 nextSong()
		     }
		     activatePickSongMobile();
	     }
	});
				
}

function activatePickSongMobile()
{
	 $(".pickSong").click(function(event){
			event.preventDefault;
			var track = this.id;
			//alert('pick song, id = '+track);
		    next = track;
		    nextSong();
			return false;
		});
}

function loadPlayer() {
	//alert('load player');
	/*alert(group[next][0]);
    alert(group[next][1]);
    alert(group[next][2]);
    alert(group[next][3]);
    alert(group[next][4]);*/
	var audioPlayer = new Audio();
	audioPlayer.controls='controls';
	audioPlayer.addEventListener('ended',nextSong,false);
    audioPlayer.addEventListener('error',errorFallback,true);
	//audioPlayer.autoplay='true';
	audioPlayer.src='http://dh4o3s44ixfc3.cloudfront.net/' + group[next][3] + '/' + group[next][4] + '/' + group[next][5];
	audioPlayer.load();
    //audioPlayer.play();
	document.getElementById('aplayer').appendChild(audioPlayer);
}

function nextSong() {
	//alert('next song');
    var audioPlayer = document.getElementsByTagName('audio')[0];
    audioPlayer.src = 'http://dh4o3s44ixfc3.cloudfront.net/' + group[next][3] + '/' + group[next][4] + '/' + group[next][5];
    //alert(audioPlayer.src);
    audioPlayer.load();
    audioPlayer.play();
    currentSong = group[next][0] + ' : ' + group[next][1] + ' : ' + group[next][2];
   	//alert(currentSong);
    document.getElementById('songMarquee').innerHTML = currentSong;
    next++;
    //controlVideoPlayer('stop');
}              

function errorFallback() {
    next++;
    nextSong();
}

/*function playPause() {
    var audioPlayer = document.getElementsByTagName('audio')[0];
    if (audioPlayer.paused)
        audioPlayer.play();
    else
        audioPlayer.pause();
    }
}*/


});
