var is_ipad = false;

//if ((navigator.userAgent.toLowerCase().indexOf('ipad') > -1) || (navigator.userAgent.toLowerCase().indexOf('iphone') > -1)) {
if (navigator.userAgent.toLowerCase().indexOf('ipad') > -1) {
	is_ipad = true;
}



if (is_ipad == true) {

	var slider_obj = new Array();
	
	function init_slider(wrap_index) {
		knob = $('.shrink_wrap').eq(wrap_index).find('#content_block')[0];
		subject_x = $('.shrink_wrap').eq(wrap_index).find('#cols_stream')[0];
		wrap = $('.shrink_wrap').eq(wrap_index)[0];
		slider_obj[wrap_index] = new slider(wrap_index, wrap, wrap, subject_x);
	}
	
	function slider(wrap_index, knob, wrap, subject) {
		this.wrap_index = wrap_index;
		this.knob = knob;
		this.wrap = wrap;
		this.subject_x = subject_x;
		this.touch_x = 0;
		this.touch_x_old = 0;
		this.wrap.style.webkitTransform = 'translate3d(0px, 0px, 0px)';
		this.pos_y = 0;
		this.start_y = 0;
		this.start_touch_y = 0;
		this.touch_y = 0;
		this.touch_y_old = 0;
		this.touch_speed_y = 0;
		this.direction = '';
		this.knob.addEventListener("touchstart", this, false);
		this.knob.addEventListener("touchmove", this, false);
		this.knob.addEventListener("touchend", this, false);
	}
	 
	slider.prototype.handleEvent = function (event) {
		switch(event.type)
		{
			case "touchstart" :
			  this.onTouchStart(event);
			  break;
			case "touchmove" :
			  this.onTouchMove(event);
			  break;
			case "touchend" :
			  this.onTouchEnd(event);
			  break;
		}
	}
	 
	slider.prototype.registerTouches = function() {           
		window.addEventListener("touchmove", this, false);
		window.addEventListener("touchend", this, false);    
	}
	 
	slider.prototype.onTouchStart = function(e) {
		this.direction = '';
		this.start_touch_x = e.targetTouches[0].clientX;
		this.touch_x = e.targetTouches[0].clientX;
		this.touch_x_old = this.touch_x;
		this.start_touch_y = e.targetTouches[0].clientY;
		this.touch_y = this.start_touch_y;
		this.touch_y_old = this.touch_y;
		this.touch_speed_y = 0;
		if (wraps_num > 1) {
			this.wrap.style.webkitTransition = '';
			this.wrap_3d = this.wrap.style.webkitTransform.replace(',', '').replace(' ', '').replace('translate3d(', '').split('px');
			this.pos_y = parseInt(this.wrap_3d[0]);
			this.start_y = parseInt(this.wrap_3d[0]);
		}
	}
	 
	slider.prototype.onTouchMove = function(e) {
		e.preventDefault();
		if (this.direction == '' || this.direction == 'h') {
			this.touch_x_old = this.touch_x;
			this.touch_x = e.targetTouches[0].clientX;
			if (this.direction == 'h') {
				if (cols_stream_obj[this.wrap_index] != null) {
					cols_stream_obj[this.wrap_index].shift_x(this.touch_x - this.touch_x_old);
				}
			}
		}
		if (this.direction == '' || this.direction == 'v') {
			this.touch_y_old = this.touch_y;
			this.touch_y = e.targetTouches[0].clientY;
			if (wraps_num > 1) {
				if (this.direction == 'v') {
					this.pos_y = this.start_y + (this.touch_y - this.start_touch_y);
					this.wrap.style.webkitTransform = 'translate3d(0px, '+this.pos_y+'px, 0px)';
					this.touch_speed_y = this.touch_y - this.touch_y_old;
				}
			}
		}
		if (this.direction == '') {
			distance = Math.sqrt((this.touch_y - this.start_touch_y) * (this.touch_y - this.start_touch_y) + (this.touch_x - this.start_touch_x) * (this.touch_x - this.start_touch_x));
			if (distance > 30) {
				//alert(distance);
				if (Math.abs(this.touch_y - this.start_touch_y) > Math.abs(this.touch_x - this.start_touch_x)) {
					this.direction = 'v';
				} else {
					this.direction = 'h';
				}
			}
		}
	}
	 
	slider.prototype.onTouchEnd = function(e) {
		if (this.direction == 'h') {
			if (cols_stream_obj[this.wrap_index] != null) {
				cols_stream_obj[this.wrap_index].release_x(this.touch_x - this.touch_x_old);
			}
		} else if (this.direction == 'v') {
			end_y = this.start_y + (this.touch_y - this.start_touch_y);
			pos_delta = end_y;
			if (pos_delta < 0) {
				if (wrap_index < wraps_num - 1) {
					next_wrap();
				} else {
					this.switch_y(0);
				}
			} else {
				if (wrap_index > 0) {
					prev_wrap();
				} else {
					this.switch_y(0);
				}
			}
		}
	}
	
	slider.prototype.switch_y = function(pos_y) {
		this.pos_y = pos_y;
		this.wrap.style.webkitTransitionProperty = '-webkit-transform';
		this.wrap.style.webkitTransitionDuration = '0.6s';
		this.wrap.style.webkitTransitionTimingFunction = 'ease-out';
		this.wrap.style.webkitTransform = 'translate3d(0px, '+this.pos_y+'px, 0px)';
	}
	
	slider.prototype.set_y = function(pos_y) {
		this.pos_y = pos_y;
		this.wrap.style.webkitTransitionProperty = 'none';
		this.wrap.style.webkitTransform = 'translate3d(0px, '+this.pos_y+'px, 0px)';
	}
 

} // iPad

