var loaderindicator=0;


 function audiofunctinallity(id,source,dataobject,onLoadcallback,loadCallBackArgs){
_this= this;
 this.id=id;
 this.dataobject=dataobject;
 this.source=source;
 this._audioPlayer=null;
 this.loadprogress=null;
 this.startTime=0;
 this.endTime=0;
 this.callbackplay=null;
 this.counter=0;
 this.timer=[];
 this.timeiterator=0;
 this.audiosarray=[];
 windowcounter=0;

 
 this.init=function(){



     this._audioPlayer = document.createElement('audio');
    if(!! this._audioPlayer.canPlayType('audio/mpeg') === true){
         this._audioPlayer.setAttribute('src', this.source+".mp3");
    }else{
	   this._audioPlayer.setAttribute('src', this.source+".ogg");
	}
     this._audioPlayer.setAttribute('id', this.id);
    $('body').append( this._audioPlayer);
	  this._audioPlayer=document.getElementById(this.id);
	
	  //var x=this._audioPlayer.getAttribute("src");
	//alert(this._audioPlayer);
	   //this._audioPlayer.addEventListener('progress', this.getPercentProg, false);
	   //time
	   //this
	   //this
       this._audioPlayer.addEventListener("timeupdate",this.playProgress,false);
	     this._audioPlayer.load();
       
    
   if(navigator.userAgent.match(/ipad/i)!= null)
   
       {
	this.loadprogress=setInterval(function(){
	 _this.getPercentProg();
	 },50);
    
	  }
     else
  
        {
    
      if(typeof onLoadcallback != "undefined")
     {
     	
	  if(typeof loadCallBackArgs!="undefined")
     		     onLoadcallback(loadCallBackArgs);
     		     else
     		     onLoadcallback();
     		     
			
     		     	}
   
     		     	  }
 }

 this.getPercentProg=function(){

            var endBuf = this._audioPlayer.buffered.end(0);
       
            var soFar = parseInt(((endBuf / this._audioPlayer.duration) * 100));
       
			if(soFar == 100){
			clearInterval(this.loadprogress);
			loaderindicator=1;
			
			if(typeof onLoadcallback != "undefined"){
				
	            if(typeof loadCallBackArgs!="undefined")
	            {
	            	
     		     onLoadcallback(loadCallBackArgs);
     		     }
     		     else
     		     onLoadcallback();
     		     
				}
		}
 }
 
 
 this.playBackToBack=function(audioarray,callbackplayer){
 if(! _this._audioPlayer.paused){
  _this._audioPlayer.pause();
 }
 this.counter=0;
 this.audiosarray.length=0;
 this.audiosarray=audioarray;
 windowcounter=0;
 this.timeiterator=0;
 for (var i = 0; i < this.timer.length; i++) {
    clearTimeout(this.timer[i]);}

if(this.audiosarray.length>0)
{
	while(this.counter<audioarray.length)
	{
	
	var timeings=this.dataobject[audioarray[this.counter]];
	if(timeings==null)
    {
 	return;
    }
this.timer[this.counter]= setTimeout(function(){
    	extendedFun();
    	
    },this.timeiterator*1000);
    this.timeiterator=this.timeiterator+parseFloat(timeings.split("-")[1])-parseFloat(timeings.split("-")[0]);
    this.counter++
   }
}
else
{
	if(typeof callbackplayer !="undefined")
   this.callbackplay=callbackplayer;
   else
   this.callbackplay=null;
}
 	
 	
 }
 
 window.extendedFun=function()
 {
 	window.CollisionAudio.play(window.CollisionAudio.audiosarray[windowcounter++],1);
 }
 
this.pauseBackToBack=function()
{
	for (var i = 0; i < this.timer.length; i++) {
    clearTimeout(this.timer[i]);
}
  if(! _this._audioPlayer.paused){
  _this._audioPlayer.pause();
 }
}



 
this.play=function(key,notifieraud,callbackplayer){

if(typeof notifieraud =="undefined")
{
for (var i = 0; i < this.timer.length; i++) {
    clearTimeout(this.timer[i]);
}
}

 if(! _this._audioPlayer.paused){
  _this._audioPlayer.pause();
 }

 var timeings=this.dataobject[key];

 if(timeings==null)
 {
 	return;
 }
 timeings=timeings.split("-");
    
   this.startTime= parseFloat(timeings[0]);
   this.endTime=parseFloat(timeings[1]);
    
   this._audioPlayer.currentTime=this.startTime;
   
   this._audioPlayer.play();
   if(typeof callbackplayer !="undefined")
   this.callbackplay=callbackplayer;
   else
   this.callbackplay=null;
 }
 
 this.playProgress=function(){
    if(_this._audioPlayer.currentTime >= _this.endTime){
                _this._audioPlayer.pause() ;
				 if(_this.callbackplay != null)
				_this.callbackplay();
            }
 }
 
 this.pause=function(){
 	for (var i = 0; i < this.timer.length; i++) {
    clearTimeout(this.timer[i]);
}
    if(! _this._audioPlayer.paused){
	_this._audioPlayer.pause()
	}
 }
 
 this.init();
 return _this;
 }