//config
var volDeactOpacity = .4;


//doing
var curPlayingAudio;
var playedSnippets = new Array();
var playing = 0;

$(document).ready(function(){
	
	//check browser requirements
	if(!Modernizr.audio || !Modernizr.localstorage){
			$('#content').before('<p class="telluser_error" style="width: 800px; margin: 0 auto; font-weight: bold; text-align: center; margin-bottom: 10px;">Your Browser doesn\'t support the HTML5-Audio Element. Please upgrade to a modern Browser.</p>');
	}

	
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) ) {
		//no preloading unter mobile safari
		$('.play').click(playSnippet);
	} else {
		
		$('.play').css('opacity', .2);
		$('audio').bind('progress', function(){
			
			if(this.buffered.length && this.buffered.end(0)){
				$('.' + this.id).css('opacity', 1).css('cursor', 'pointer').click(playSnippet);
			}
			
		});
	
		//catch esc key
		$(document).keyup(function(e) {
		  	if (e.keyCode == 27) { 
		  		cancelPlayAll();
		  	} 
		});
	} 
	
	
	$('audio').bind('ended', function(){
		this.currentTime = 0;
		this.ended = false;
		this.pause();
		$(this).parent().find('.playing').remove();
		hideMuteMsg();
		playing--;
	});
	   

	initVolumeSlider();
	
	if(!localStorage.getItem('playedSnippets')){
		localStorage.setItem('playedSnippets', JSON.stringify(playedSnippets));
	}

});


function playSnippet(){

    var patt = /a_[\d]+$/;
    var id = patt.exec(this.className)[0];
	var audio = document.getElementById(id);
	curPlayingAudio = audio;

	if(audio.paused){

		//todo: fix mobile safari cause it can play only one file at once
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) ) {
			$('audio').each(function(){
				this.pause();
			});
		}

		if(curVolume == 0){
			showMuteMsg();
		}

		audio.play();
		playing++;
		$(this).append('<img class="playing" src="' + WEB_ROOT + '_img/button_play.png" />');
		
		var snippet_id = id.replace('a_', '');
		writeSnippetStat(snippet_id);
		
	}


}

function cancelPlayAll(){

	$('audio').each(function(){
		if(!this.paused){
    		this.currentTime = 0;
    		this.ended = false;
    		this.pause();
			$('.playing').remove();
		}
	});


}

// Volume Slider

var volIsMute = false;
var curVolume = 10;
var volBars;

function initVolumeSlider(){
		
	$('#vol_speaker').css('cursor', 'pointer').click(volumeMute);
	$('.vol_bar').css('cursor', 'pointer').click(selectVolume);
	
	volBars = $('.vol_bar').get();
	
	if(localStorage.getItem('curVolume') != null){
		curVolume = localStorage.getItem('curVolume');
		setVolume();
	} else {
		localStorage.setItem('curVolume', curVolume)
	}

}


function volumeMute(){

	if(curVolume > 0){
		localStorage.setItem('mutedVolume', curVolume)
		curVolume = 0;
	} else {
		curVolume = localStorage.getItem('mutedVolume');
	}

	setVolume();

}


function selectVolume(){

	for(i = 0; i < volBars.length; i++){
		if(this == volBars[i]){
			break;
		}
	}

	curVolume = i + 1;
	setVolume();
	
}

function setVolume(){

	localStorage.setItem('curVolume', curVolume);

	if(curVolume == 0){
		$('#vol_speaker').attr('src', WEB_ROOT + '_img/icn_speaker_off.png');
		if(playing > 0){
			showMuteMsg();
		}
	} else {
		$('#vol_speaker').attr('src', WEB_ROOT + '_img/icn_speaker.png');
		hideMuteMsg();
	}
	
	for(i = 0; i < volBars.length; i++){
		if(i < curVolume){
			$(volBars[i]).css('opacity', 1);
		} else {
			$(volBars[i]).css('opacity', volDeactOpacity);
		}
	}
	
	if(curPlayingAudio){
		curPlayingAudio.volume = curVolume / 10;
	}
	
	$('audio').each(function(){
		this.volume = curVolume / 10;
	});

}


function showMuteMsg(){
	
	if(!$('#mute_message').length){
		$('body').append('<p id="mute_message">volume is muted!</p>');
		
		var top = $(document).scrollTop() + 90;
		var left = Math.floor(( $(document).width() - $('#mute_message').outerWidth() ) / 2);
	
		$('#mute_message').css({'top': top + 'px', 'left':  left + 'px'});
	}
	
}

function hideMuteMsg(){

	if($('#mute_message').length){
		$('#mute_message').remove();
	}

}


function writeSnippetStat(id){
	
	playedSnippets = JSON.parse(localStorage.getItem('playedSnippets'));

	if( $.inArray(id, playedSnippets) < 0 ){
		console.log('save');
		url = WEB_ROOT + '_ajax/ajax.increase_play_count.php?snippet_id=' + id;
	
		$.get(url, function(data){
			if(data == 'true'){
				playedSnippets[playedSnippets.length] = id;
				localStorage.setItem('playedSnippets', JSON.stringify(playedSnippets));
			}
		});	
		
	}	
	
}






