/*

    olhSound by Oliver Lutz Haase
    http:// www.olh.de

    Version: 0

    Requires jquery, jqplugin, swfobject

    URL: your/path/sound
    create flat (all rolloversounds in one file) soundfiles mp3 or ogg

*/
var olhSound = new Object({

  datas:{},
  soundready:false,
  pausing:false,
  mobile:false,
  soundon:true,
  flash:false,
  soundstopat:5,

  init:function(d){
    this.datas = d;
    $(this.datas.audio).hide();
    /* if flash installed use flash */
    if( jQuery.browser.flash ){
      this.flash = true;
      for(var i=0; i < this.datas.files.length; i++){
        if( 'MP3' == this.datas.files[i].toUpperCase().match( /(?:MP3|OGG)/ ) ){ this.datas.playfile = this.datas.files[i] }
      };
      var params = {};
      params.allowFullscreen = false;
      params.wmode = "transparent"; 
      var attributes = {};
      attributes.id = "olhsound";
      var flashvars = {};
      /*
      bird_1:  1-3
      bird_2:  4-7
      logo: 8-10
      mushroom: 11-15
      wind: 16-20
      */
      flashvars.playfile = this.datas.playfile;
      flashvars.channels = 5;
      swfobject.embedSWF("../olhsound.swf", "olhsound", "1px", "1px", "10", null, flashvars, params, attributes);
    } else {
      /* else try html5 audio */
      if( navigator.userAgent.match(/Android/i) ||
        navigator.userAgent.match(/webOS/i) ||
        navigator.userAgent.match(/iPhone/i) ||
        navigator.userAgent.match(/iPod/i)
      ){ this.mobile = true; }
      var audio_type = "";
      if( !!(document.createElement('audio')).canPlayType('audio/ogg; codecs="vorbis"') ){ audio_type= "ogg"; }
      /* mp3 first */
      if( !!(document.createElement('audio')).canPlayType('audio/mpeg') ){ audio_type= "mp3"; }
      for(var i=0; i < this.datas.files.length; i++){
        if( audio_type.toUpperCase() == this.datas.files[i].toUpperCase().match( /(?:MP3|OGG)/ ) ){ this.datas.playfile = this.datas.files[i] }
      };
      this.datas.audio.addEventListener('timeupdate',function(){
        /* loooop */
        olhSound.soundready = true;
        if( this.currentTime > (this.duration-1) ){ this.currentTime = 0 }
        if( this.currentTime > olhSound.soundstopat ){ this.pause(); olhSound.pausing=true; }
      },false);
      if(this.mobile){
        $('#olhimage').append('<div id="mobile_sound_confirm"><img src="images/sound_on.jpg" id="soundon" /></div>');
        $('#soundon').bind('click', function(){ olhSound.confirmation(true); });
        $('#soundoff').bind('click', function(){ olhSound.confirmation(false); });
      }
    }
   },
  confirmation:function(b){
    $('#soundon').unbind('click');
    $('#soundoff').unbind('click');
    $('#mobile_sound_confirm').remove();
    if(b){
      olhSound.soundon=true;
      olhSound.play(0,0);
    } else {
      olhSound.soundon=false;
    }
  },
  play:function(s,e){
    if(this.flash){
      $("object#olhsound").get(0).flashplay( s, e );
    } else {
      if(this.soundon){
        if(this.soundready == false){
          this.datas.audio.play();
          this.datas.audio.pause();
        } else {
          this.datas.audio.play();
          this.datas.audio.currentTime = s;
          this.soundstopat = e;
        }
      }
    }
  }
});
/*
     document.getElementById('imagesound').addEventListener('abort',function(){ alert('onabort'); },false);
     document.getElementById('imagesound').addEventListener('canplay',function(){ alert('oncanplay'); },false);
     document.getElementById('imagesound').addEventListener('canplaythrough',function(){ alert('oncanplaythrough'); },false);
     document.getElementById('imagesound').addEventListener('durationchange',function(){ alert('ondurationchange'); },false);
     document.getElementById('imagesound').addEventListener('emptied',function(){ alert('onemptied'); },false);
     document.getElementById('imagesound').addEventListener('ended',function(){ alert('onended'); },false);
     document.getElementById('imagesound').addEventListener('error',function(){ alert('onerror'); },false);
     document.getElementById('imagesound').addEventListener('loadeddata',function(){ alert('onloadeddata'); },false);
     document.getElementById('imagesound').addEventListener('loadedmetadata',function(){ alert('onloadedmetadata'); },false);
     document.getElementById('imagesound').addEventListener('loadstart',function(){ alert('onloadstart'); },false);
     document.getElementById('imagesound').addEventListener('pause',function(){ alert('onpause'); },false);
     document.getElementById('imagesound').addEventListener('play',function(){ alert('onplay'); },false);
     document.getElementById('imagesound').addEventListener('playing',function(){ alert('onplaying'); },false);
     document.getElementById('imagesound').addEventListener('progress',function(){ alert('onprogress'); },false);
     document.getElementById('imagesound').addEventListener('ratechange',function(){ alert('onratechange'); },false);
     document.getElementById('imagesound').addEventListener('readystatechange',function(){ alert('onreadystatechange'); },false);
     document.getElementById('imagesound').addEventListener('seeked',function(){ alert('onseeked'); },false);
     document.getElementById('imagesound').addEventListener('seeking',function(){ alert('onseeking'); },false);
     document.getElementById('imagesound').addEventListener('stalled',function(){ alert('onstalled'); },false);
     document.getElementById('imagesound').addEventListener('suspend',function(){ alert('onsuspend'); },false);
     document.getElementById('imagesound').addEventListener('volumechange',function(){ alert('onvolumechange'); },false);
     document.getElementById('imagesound').addEventListener('waiting',function(){ alert('onwaiting'); },false);
*/

