
// INITIATE SLIDER OBJ
function Slider(tbl, lPos, rPos)
{
    this.tbl = tbl;
    
    /* Register for the touchstart event */
    this.tbl.addEventListener("touchstart", this, false);
   
    /* Callback for changing tblPos */
    this.movTouchedTblCall = function(value){ 
         movTouchedTbl(value);
    };
}
trackedObjectCount = 0
// HANDLES FOR OBJ
Slider.prototype.handleEvent = function (evt)
{
    switch(evt.type)
    {
        case "touchstart" :
          this.onTouchStart(evt);
          break;
        case "touchmove" :
          this.onTouchMove(evt);
          break;
        case "touchend" :
          this.onTouchEnd(evt);
          break;
    }
}

// EVENT MANAGEMENT ON
Slider.prototype.onTouchStart = function(evt)
{
    /* Prevent the browser from scrolling */
    evt.preventDefault();
    this.registerTouches();
    
    if (evt.targetTouches.length < 1)
    {
        return false;
    }
    
    pos_array = new Array();
    pos_array[0] = evt.targetTouches[0].clientX;
    //addMSG('<h1>[start: '+pos_array[0]+']</h1>','secretc');
}
// CAPTURE EVENT MANAGER ON
Slider.prototype.onTouchMove = function(evt)
{
    /* Prevent the browser from scrolling */
    evt.preventDefault();
    
    if (evt.targetTouches.length < 1)
    {
        return false;
    }
    /* Move the box to its new location */
    //this.positionToTouch(evt.targetTouches[0]);
    
    if(pos_array.length > 1) pos_array[0] = pos_array[1];
    pos_array[1] = evt.targetTouches[0].clientX;
    
    diff = pos_array[1]-pos_array[0];
    
    //this.positionToTouch(diff)
    //movePics(diff);
}
// RESET EVENT ON
Slider.prototype.onTouchEnd = function(evt)
{
	var picNum = false
    /* Prevent the browser from scrolling */
    evt.preventDefault();
    
    evt = (evt) //? evt : ((window.event) ? window.event : "");
    var elem = (evt.target) // ? evt.target : evt.srcElement;
    
    this.resetToTouch(evt);
    this.unregisterTouches();
    
    //this.resetToTouch(evt);
    this.unregisterTouches();
    
    //addMSG('<h1>[start: '+pos_array[1]+'//'+pos_array[0]+']</h1>','secretc');
    if(pos_array.length == 1)
    {
    	//alert(scWidth.length);
    	width = tblPos;
    	msg = width+'-';
    	for(i=0;i<scWidth.length;i++)
    	{
    		if(width<pos_array[0]) 
    		{
    			width+=scWidth[i];
    			msg += '+'+scWidth[i]
    			pic = 'pic_'+i;
    		}
    	}
    	msg += ' = '+width;
    	//addMSG('<h1>[klick:'+msg+']</h1>',pic);
    	doMedia(pic)
    }
}

/*  */
Slider.prototype.positionToTouch = function(diff)
{
	if(diff != 0 && checkPos == false)
	{
		  addMSG('<h1>[p2t: '+diff+']</h1>','secretc');
		
	}
	movePics(diff,true);
}
Slider.prototype.resetToTouch = function(evt)
{
	/*
	sform = '<form action="mail.php" method="POST">';
	sform += '<input type="textarea" name="msgbox" id="msgbox" value="" />';
	sform += '<input type="submit" value="senden" /></form>';
	msg = '<ul>';
	for ( var i in evt )
	{
    	//alert( names[i] );
    	 if ( typeof( evt[i]) != "object") 
    	 {
    	 	//addMSG('<b>['+i+']</b>'+evt[i]+'','secretc');
    	 	msg += '<li><b>['+i+']</b>'+evt[i]+'</li>';
    	 }
    	 else 
    	 {
    	 	arr = evt[i];
			msg += '<li>['+i+']<ul>';
    	 	//addMSG('<b>['+i+']</b>','secretc');
    	 	for (var j in arr) 
    	 	{
    	 		msg += '<li><b>['+j+']</b>'+arr[j]+'</li>';
    	 		//addMSG('&nbsp;&nbsp;<b>'+j+')</b>'+arr[j]+'','secretc');
    	 	}
			msg += '</ul></li>';
    	 }
	}
	msg += '</ul>';
	document.getElementById('secretc').innerHTML = sform;
	document.getElementById('msgbox').value=msg;
	*/
	//addMSG('<h1>'+evt.view.id+'</h1>','secretc');
	
	
    //doMedia(evt.view.id)
}


/* Listen for touchmove and touchend events */
Slider.prototype.registerTouches = function()
{           
    window.addEventListener("touchmove", this, false);
    window.addEventListener("touchend", this, false);  
    trackedObjectCount++;
}
/* Listen for touchmove and touchend events */
Slider.prototype.unregisterTouches = function()
{       
   window.removeEventListener("touchmove", this.touchMoveHandler, false);
   window.removeEventListener("touchend", this.touchEndHandler, false);
   trackedObjectCount--;
}
