// On Document Ready
$(function(){ 
  
	//Submenu Toogle 
	$('li.toggle ul').css('display', 'none');
	$('li.toggle.active ul').slideDown(500); 
	$('li.toggle a:eq(0)').toggle(function() {
		$(this).parent().addClass('active');
		$('li.toggle ul').slideToggle(500);		
	}, function() {
		$(this).parent().removeClass('active');  
		$('li.toggle ul').slideToggle(500);				
	});
	
	//Covers hover (with hoverIntent) 	
	var dropconfig = {    
         sensitivity: 10,     
         interval: 70,      
         over: doOpen,    
         timeout: 50,      
         out: doClose    
    };
    function doOpen() {
        $(this).addClass("hover");
        $(this).children('div').slideToggle(350, function () {
		      $('ul.columns a').fadeIn(150);
	    });
    }
    function doClose() {
        $(this).removeClass("hover");
        $('ul.columns a').fadeOut(0).css('display', 'none');  
        $(this).children('div').slideToggle(350);
    }	
	//Init Hover	
	$('ul.columns li div').css('display', 'none');
	$('ul.columns li div a').css('display', 'none');			
    $('ul.columns li').hoverIntent(dropconfig);

	// Mail obfuscation
	$('.obfuscate').attr('href', "mailto:tonicoelectronico@gmail.com");
	
	// Disable gallery links
	if ( $('.gallery').length ) {
		$('.gallery a').removeAttr('href');		 
	   	$('.gallery a').click(function() {
	   		return false;
	   	});
    }
	
	// Modal window
	if ( $('.event').length ) {
		$('.modal').hide();
		$('.event h3 a').click(function() {
			$(this).parent('h3').parent('div.event').children('div', this).modal();
			return false;
		});
	}

	// Streampad API 
	// Load all songs on current page 
   	function loadSongs() {
		// Find music on page
	   	$('ul.columns div').each(function(index) {
			// Get attributes
			var song = $(this).children('p').children('a.spmp3').attr('href');
		   	var artist = $(this).children('span').text();
			var title = $(this).children('h5').text();
			var page = $(this).children('a.play').attr('rel');
			// Add song		
			SPAPI.add(title, artist, null, song, page, null, null); 	
		});
	}
	
	// Streampad Init
	$('#streampadBottomBar').css('cursor', 'default');	
	$('#streampadBottomBar').css('height', '0');
	$('#streampadBottomBar').animate({height: 30}, 500); 
	loadSongs();
	 
	// Open and close buttons
	$('div#player a.open-close').toggle(function() {		
		//$('#streampadBottomBar').slideUp(250);
		SPAPI.hide(); 		
		$(this).children('span').text("Show");
		$(this).css('background-position', '-44px -26px');
		//SPAPI.hide();
		// Sinchronizes animation and launch show param
		$('#player').animate({height: 75}, 250);
		$('.play-stop').fadeOut(100);
		$('.playlist').fadeOut(100);		   		   
	}, function() {		
		//$('#streampadBottomBar').slideDown(250);
		$(this).children('span').text("Hide");
		$(this).css('background-position', '-44px 0');
	    //Sinchronizes animation and launch show param   	
	    $('#player').animate({height: 113}, 250, function () {
   		    $('.play-stop').fadeIn(100);
			$('.playlist').fadeIn(100);			
	    });
		SPAPI.show();   			   	
	});
	
	// // Play and Stop buttons
	$('div#player a.play-stop').toggle(function() {   										
		$(this).children('span').text("Stop");
		$(this).css('background-position', '-42px -76px');
		SPAPI.start();
		SPAPI.play();   		   
	}, function() {
		$(this).children('span').text("Play");
		$(this).css('background-position', '-42px -52px');		
		SPAPI.pause();   	
	});
	
	// Clear playlist and add 
	$('div#player a.playlist').click(function() {
		    SPAPI.clear();
			// Load songs on current page			
			loadSongs(); 		 
		return false;
	});
		
	// Play single song button 
	$('a.play').click(function() {
		SPAPI.show();
		SPAPI.start();
		// Get attributes
		var song = $(this).parent('div').children('p').children('a.spmp3').attr('href');
	   	var title = $(this).parent('div').children('h5').text();
		var artist = $(this).parent('div').children('span').text();
		var page = $(this).attr('rel');		   	   						   		
		// Clear queue
		//SPAPI.clear();
		// Add song	 to queue	
		SPAPI.add(title, artist, null, song, page, null, null); 		
		//Get total songs
		var totalSongs = SPAPI.total();
		// Set player to the end
	    SPAPI.skip(totalSongs); 		
		SPAPI.start();
		SPAPI.play();
	   	return false;   		   
	});     		
 	                                                           
}); 

//Streampad init parameters 
streampadPlayer.params.trackcolor = '#fff';
streampadPlayer.params.clicktext = 'Tonico Electronico';  
streampadPlayer.params.clickimg = 'false';
streampadPlayer.params.showpop  = 'false';

