var baseUrl = 'http://www.gallomusicpublishers.co.za/index.php/';
var baseFileUrl = 'http://www.gallomusicpublishers.co.za/';

var timerGlobal = '';
var panelDisplayDuration = 20000;
var nextPanel = 1;

$(document).ready(function(){
	//swap between banner panels
	swap_banners();

    //start the automated timer:
	setTimerGlobal();
	
	//track selection
	select_tracks()
	
	//toggle play/pause button
	$('.play_button').toggle(function(){
		
		$(this).attr('src', baseFileUrl+'application/images/pause_song_button.png');
		song = $(this).attr("id");
		//start player
	    document.getElementById('mp3Player').SetVariable('player:jsUrl', baseFileUrl + 'application/music/' + song);
	    document.getElementById('mp3Player').SetVariable('player:jsPlay', '');
		
	},function(){
		
		$(this).attr('src', baseFileUrl+'application/images/play_song_button.png');
		song = $(this).attr("id");
		
		//stop player    
		document.getElementById("mp3Player").SetVariable("player:jsStop", "");
		
	});
	
	//add song to requested song list
	$('.add_button').click(function(){
		alert("Song Added");
	});
	
	
});

function setTimerGlobal()
//cycle through the panels:
{
  //clear the interval in case the user clicked on a panel:
  clearInterval(timerGlobal);
  
  timerGlobal = setInterval(function()
  {
    //window.console.log('\ntimerGlobal: nextPanel = ' + nextPanel);
    
    $("#banner_tabs #"+nextPanel+"").click(); 
  }
  , panelDisplayDuration);
}

function swap_banners()
{
		count  = $("#banner_tabs").children().length;
		k = 0;
		while(k < count)
		{
	    	$("#banner_tabs img:eq("+k+")").click(function() { 
		    	
		    	//window.console.log('\nswap_banners: Clicked ID: ' + $(this).attr('id'));
		    	
		    	//set the clicked panel to this panel in case the user made a non-automated click:
		    	if (nextPanel == $(this).attr('id'))
		    	{
		    	  if (nextPanel == 2)
                  {
                    nextPanel = 0;
                    //window.console.log('swap_banners: nextPanel *reset to* ' + nextPanel);
                  }
                  else
                  {
                    nextPanel++;
                    //window.console.log('swap_banners: nextPanel *incremented to* ' + nextPanel);
                  }
		    	}
		    	else
		    	{
		    	  //this transition was triggered by a user click
		    	  //set the nextPanel to this panel:
		    	  nextPanel = $(this).attr('id');
		    	  $("#banner_tabs #"+nextPanel+"").click();
		    	  //window.console.log('swap_banners: nextPanel *set to clicked panel* ' + nextPanel);
		    	  
		    	  //reset the timerGlobal:
		    	  setTimerGlobal();
		    	}
		    	
		    	//this transition was triggered automatically:
		    	reference = $(this).attr("id");
				
				//swap tab background				
				$("#banner").css("background-image","url(http://www.gallomusicpublishers.co.za/application/images/banner_"+reference+".jpg)");		
			    
			    //swap content
				$("#banner div.panel:eq("+reference+")").fadeIn('fast');
				$("#banner div.panel:gt("+reference+")").hide();
				$("#banner div.panel:lt("+reference+")").hide();
				
			});
			
			k++;
			
		}
		
}

function select_tracks()
{
		count  = $("#player").children().length;
		k = 0;
		while(k < count)
		{
			$("#player li:eq("+k+")").click(function() { 
			
				reference = $(this).attr("id");
				document.getElementById("mp3Player").SetVariable("player:jsStop", "");	
						
				//swap content
				$("#player li:eq("+reference+")").addClass('selected_track');
				$("#player li:gt("+reference+")").removeClass('selected_track');
				$("#player li:lt("+reference+")").removeClass('selected_track');
			
			});
			
			k++;
			
		}
		
}
