
var myScroll;
var animate = 'startAni';
var position = 'left';
$(window).load(function(){

	//loadMyScroll();
	if(navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPhone/i) ){
		loadMyScroll();
	}
	else{
		$("#imageslider").myscroller();
	}
});
function loadMyScroll(){
	$('.ipadWrapper').attr('id','IpadWrapper');
	$('.ipadScroller').attr('id','IpadScroller');
	
	var totalWidth1 = 0;
	for(var i=0;i<$('#IpadScroller li').length; i++){
		totalWidth1 += $('#IpadScroller ul').children('li').eq(i).width();
	}
	$('.ipadScroller').css('width',totalWidth1+'px');
	
	myScroll = new iScroll('IpadWrapper', {
		useTransform: false,
		momentum: true,
		hScrollbar: true,
		vScrollbar: false,
		onBeforeScrollStart: function (e) {
			var target = e.target;
			while (target.nodeType != 1) target = target.parentNode;

			if (target.tagName != 'SELECT' && target.tagName != 'INPUT' && target.tagName != 'TEXTAREA')
				e.preventDefault();
		},
		onBeforeScrollStart: function (e) {
			animate = 'stopAni';
			animateScroller();
		},
		onBeforeScrollMove: function (e) {
			animate = 'stopAni';
			animateScroller();
		},
		onScrollEnd: function (e) {
			animate = 'startAni';
			animateScroller();
		}
	});
	
	animateScroller()
	$('<div class="btn btn-left"></div><div class="btn btn-right"></div>').appendTo('.ipadWrapper');
	
	$('#IpadWrapper .btn-left').click(function(){
		position = 'left';
		animateScroller();
	});
	$('#IpadWrapper .btn-right').click(function(){
		position = 'right';
		animateScroller();
	});
	$('#playpause').hide();
}
function animateScroller(){
	animateMyScroller();
};
function animateMyScroller(){
	if(animate == 'startAni'){
		setTimeout(function(){
			$('#IpadScroller').stop();
			var IpadScrollPosition = (($('#IpadScroller').css('left')).split('px')[0]);
			if(position == 'left'){
				wid = -(($('#IpadScroller').width() - $('#IpadWrapper').width() )-(-20));
				if(IpadScrollPosition <= wid){
					$('#IpadScroller').stop();
					leftAni = (IpadScrollPosition)-(-100);
				}
				else{
					leftAni = (IpadScrollPosition)-(100);
				}
			}
			else if(position == 'right'){
				if(IpadScrollPosition >= 0){
					$('#IpadScroller').stop();
					leftAni = (IpadScrollPosition)-(100);
				}
				else{
					leftAni = (IpadScrollPosition)-(-100);
				}
			}
			$('#IpadScroller').animate({'left':leftAni+'px'},1500, 'linear');
			animateScroller();
		},1500);
	}
	else if(animate == 'stopAni'){
		$('#IpadScroller').stop();
	}
};
