$(function()
{ 
    var regionInstrumentCount = [6,3,4,1,2,2,5,3,3,4,4];
    var instrumentTypes = [
                [10,12,9,6,7,5], [3,10,12], [5,13,14,15], [10], [10,3], [10,3], [8,11,10,5,2], [4,10,12], [4,10,12], 
                [10,1,5,2], [10,1,5,2]
    ];
    var regionNames = ["Mazowsze", "Wielkopolska", "Śląsk", "Podhale", "Beskid Śląski", 
            "Beskid Żywiecki", "Kaszuby", "Rzeszowskie", "Tarnowskie", "Lubelskie", "Zamojskie"
    ];
    for(var i=1;i<=11;++i)
    {   
        $('.elements').append('<div class="regions region'+i+'" data-value="'+i+'"></div>'); 
    } 	
    for(var i=1;i<=11;++i)
    {   
        $('.elements').append('<div style="display: none" class="popups popup'+i+'" data-value="'+i+'"></div>'); 
    }
    audio_player('./media/lektor.mp3', 'regions');
	
    $('.regions').click(function() {
       var value = $(this).data("value");
       $('.popups').fadeOut();
       $('.popup'+value).fadeIn();
       $("#jquery_jplayer_regions").jPlayer("setMedia", { mp3: './media/111_reg_'+value+'.mp3' } );
	   $("#jquery_jplayer_regions").jPlayer("play");
    });
    $('.popups').click(function() {
	   $('.description').hide();
       //audio_player('./media/lektor.mp3', 333);
       //$("#jquery_jplayer_").jPlayer("setMedia", { mp3: './media/lektor.mp3' } );
       $("#jplayer").jPlayer("setMedia", { mp3: './media/lektor.mp3' } );
      /* setTimeout(function() {
            $("#jquery_jplayer_333").jPlayer("play");
        }, 500);  */ 
       var value = $(this).data("value");
       var instruments = regionInstrumentCount[value-1];
       
       //alert(sizeof(instrumentTypes[10]));
       $('.elements').empty();
       $('.elements').append('<a class="back-to-map" href="./index.html"></a>');
       $('main').append('<div class="region-name">'+regionNames[value-1]+'</div>');
       for(var i=1;i<=instruments;++i)
       {   
            $('.elements').append('<div style="position: relative; top:80px; margin-left: 60px; margin-right: 60px;" class="instrument'+value+'_'+i+' ins" data-type="'+instrumentTypes[value-1][i-1]+'"></div>'); 	
            $('.ins').click(function() {
               var type = $(this).data("type");
                audio_player('./media/111_'+type+'.mp3', type);
                //$("#jquery_jplayer_").jPlayer("setMedia", { mp3: './media/111_'+type+'.mp3' } );
                        setTimeout(function() {
                $("#jquery_jplayer_"+type).jPlayer("play");
                        }, 500);   
            });
       }
    }); 
    
    
    function audio_player(audio, id) {
      $('main').append('<div id="jquery_jplayer_'+id+'" class="jp-jplayer" data-file="'+audio+'"></div>');

      $('#jquery_jplayer_'+id).jPlayer({
          ready: function(event) {
              $(this).jPlayer("setMedia", {
                  mp3: $(this).attr('data-file')
              });
          },
          play: function() { // To avoid both jPlayers playing together.
              $(this).jPlayer("pauseOthers");
          },
          swfPath: '../../styles/flash',
          cssSelectorAncestor:'#jp_container_'+id,
          supplied: 'mp3',
          errorAlerts: true,
          warningAlerts: false,
          wmode:"window"
      });
    }
});
