var myScroll;
var myScroll2;
function loaded() {
	var mad=document.getElementById('main');
	mad.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
	jQuery('#scroller').width(jQuery('#thelist li').length*185-10);
	myScroll = new iScroll('wrapper');
	//myScroll2 = new iScroll('contenidote');
}
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))|| (navigator.userAgent.match(/iPad/i))) 
{
	document.addEventListener('DOMContentLoaded', loaded, false);
}

jQuery(window).load(function(){
	

	MouseRelXpos = 0;  
    sumW = 10;
	var xSlider = jQuery("#scroller");     // cache
	var imagenes = jQuery('#thelist li').length;
    var posX = 0;
    var wd=0;   
    // auto-SET mmGallery_container WIDTH ()
    jQuery('#thelist li').each(function(){
        sumW += jQuery(this).width(); // collect all images widths
        jQuery('#scroller').width(sumW);//SET gallery WIDTH!
		wd=jQuery(this).width();
    });        
    // Calculate 'compensation speed': width difference between the gallery container and the gallery
    wDiff1 = jQuery('#scroller').width()+5;
    wDiff2 = jQuery('#wrapper').width();
    wDiff3 = wDiff1-wDiff2;
    wDiff = ((wDiff2)/(wDiff1))-1;  //(-1 is for the already existant container width)        
    //#
	//jQuery('#mmGallery_container').css({overflow:"scroll"})
	//imagenes = imagenes - ((imagenes - 1) / 2);
	var imagen_actual = 0;

	//if (5 < imagenes) {
		jQuery('#btn-left').click(function () {
			fdesplaza(-1);
		});
		jQuery('#btn-right').click(function () {
			fdesplaza(1);
		});
		
	//}
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))|| (navigator.userAgent.match(/iPad/i))) 
	{


/*		if(navigator.userAgent.match(/iPad/i)) {
			//jQuery('#mmGallery_container').css({overflow:"scroll"})
			jQuery('#wrappertrack').show();		

	AC.ProductBrowser.init({
			categories: [
					{ id: 'pb-cat1', offset: 0 },
					{ id: 'pb-cat2', offset: 0.327 },
					{ id: 'pb-cat3', offset: 0.605 },
					{ id: 'pb-cat4', offset: 1.000 }
			],
			imageOverlap: 0,
			sliderCentering: 138,
			initialCategory: 0,
			arrowScrollAmount: 0.32,
			iPhoneCategories: [
					{ id: 'pb-cat1', offset: 0.0 },
					{ id: 'pb-cat2', offset: 0.28 },
					{ id: 'pb-cat3', offset: 0.56 },
					{ id: 'pb-cat4', offset: 1 }
			],
			iPhoneScrollAmount: 0.278,
			iPhoneContainerWidth: 895
	});
*/
			jQuery("#proyectos-back").css("padding-left", 15);
//		}

/*		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
			jQuery('.botones-slider').show();
			jQuery("#proyectos-back").css("padding-left", 0);
		}
		*/
	}
	else
	{

		jQuery("#proyectos-back").css("padding-left", 15);
    jQuery("#wrapper").mousemove(function(e) {
		
        MouseRelXpos = (e.pageX - this.offsetLeft)/wDiff2; // = mouse pos. 'minus' offsetLeft of this element
		
        xSlider.css({marginLeft:  Math.round(-wDiff3*MouseRelXpos) +'px' }); // instead 'marginLeft' use 'left' for absolute pos. #mmGallery
    });
        
    setInterval(function(){
        posX += (-MouseRelXpos - posX) / 14; // 14 = speed (higher val = slower animation)
      //  xSlider.css({marginLeft:  Math.round(-posX * wDiff) +'px' }); // instead 'marginLeft' use 'left' for absolute pos. #mmGallery
    }, 10); // 10 = loop timeout

	}

	function fdesplaza(x) {
		imagen_actual  = imagen_actual + x;
		if (0 < x) {
			if (imagenes - 5 < imagen_actual) {
				imagen_actual = imagenes - 5;
			}
		} else {
			if (0 > imagen_actual) {
				imagen_actual = 0;
			}
		}
		xSlider.animate({marginLeft:  (imagen_actual * -185) +'px' },500,'swing');
		
	}
});
