
var IPadNav = {

	cellWidth: 0,
	cellCount: 0,

	numberOfWantedCell: 0,

	ready: false,

	init: function () {
			var nr;

			this.cellWidth = parseInt(jQuery('.is_slidebar').children('.is_cell').css('width'), 10);
			this.cellCount = jQuery('.is_slidebar').children('.is_cell').length;

			this.drawKnobs();

			this.prepareLeftArrow();
			this.prepareRightArrow();

			if (this.cellCount < 2 || this.getNumberOfActiveCell() === 0) {
				jQuery('.is_arrow_left').addClass('hide');
			}

			if (this.cellCount < 2) {
				jQuery('.is_arrow_left').addClass('hide');
				jQuery('.is_arrow_right').addClass('hide');
			}

			this.ready = true;
	},

	drawKnobs: function () {
		if (this.cellCount < 2) {
			return;
		}

		nr = 0;
		jQuery('.is_slidebar').children('.is_cell').each(function () {
			var $knob = jQuery('<li></li>').clone(),
				knobId = 'knob_' + nr,
				knobLinkId = 'knobLink_' + nr,
				$knobLink = jQuery('<a href="javascript:;"></a>').clone();
				firstKnob = false;

			$knob.attr('id', knobId);
			$knob.attr('data-nr', nr);
			$knob.addClass('knob is_knob');

			if (0 == nr) {
				firstKnob = true;
				$knobLink.addClass('active');
			}
			$knobLink.attr('id', knobLinkId);
			$knob.append($knobLink);

			if (firstKnob) {
				jQuery('.is_ipadnav').html($knob);
			}
			else {
				jQuery('.is_ipadnav').append($knob);
			}

			nr++;
		});

		IPadNav.addKnobClicks();
	},

	knobClick: function (numberOfWantedCell, noAnimation) {
		var	numberOfActiveCell,
			cellDistance,
			pixelsToMove,
			offset;

		IPadNav.numberOfWantedCell = numberOfWantedCell;

		// Determine which cell is actually shown.
		numberOfActiveCell = IPadNav.getNumberOfActiveCell();

		// Calculate how far the wanted cell is.
		if (numberOfWantedCell > numberOfActiveCell) {
			// Move cells to the left, so x is negative in relative mode.
			cellDistance = numberOfWantedCell - numberOfActiveCell;
			x = (cellDistance * IPadNav.cellWidth) * (-1);
		} else {
			// Move cells to the right, x is positive.
			cellDistance = numberOfActiveCell - numberOfWantedCell;
			x = cellDistance * IPadNav.cellWidth;
		}

		IPadNav.moveCells(x, noAnimation);

		jQuery('#knobLink_'+numberOfActiveCell).removeClass('active');
		jQuery('#knobLink_'+numberOfWantedCell).addClass('active');
	},

	moveCells: function (x, noAnimation) {
		x = parseInt(x, 10);

		if (x > 0 && 0 === this.getNumberOfActiveCell()) {
			return false;
		}

		if (x < 0 && this.getNumberOfActiveCell() === this.cellCount-1) {
			return false;
		}

		IPadNav.unlinkArrows();
		IPadNav.unlinkKnobs();
		jQuery('.is_slidebar').children('.is_cell').each(function() {
			var curPos = jQuery(this).css('left'),
			newPos;

			if (noAnimation) {
				newPos = parseInt(curPos, 10) + x;
				jQuery(this).css('left', newPos);
				IPadNav.prepareRightArrow();
				IPadNav.prepareLeftArrow();
				IPadNav.addKnobClicks();
			}
			else {
				new Effect.Move(this, {
					x: x,
					y: 0,
					mode: 'relative',
					afterFinish: function () {
						setTimeout(function() {
							IPadNav.prepareRightArrow();
							IPadNav.prepareLeftArrow();
							IPadNav.addKnobClicks();
						}, 100);
					}
				});
			}
		});

		IPadNav.showArrows();

		if (IPadNav.isFirstCellWanted()) {
			jQuery('.is_arrow_left').addClass('hide');
		}

		if (IPadNav.isLastCellWanted()) {
			jQuery('.is_arrow_right').addClass('hide');
		}

		return true;
	},

	isFirstCellWanted: function () {
		return (IPadNav.numberOfWantedCell == 0);
	},

	isLastCellWanted: function () {
		return (IPadNav.numberOfWantedCell == this.cellCount-1);
	},

	showArrows: function () {
		jQuery('.is_arrow_right').removeClass('hide');
		jQuery('.is_arrow_left').removeClass('hide');
	},

	getNumberOfActiveCell: function () {
		var $firstCell = jQuery('.is_slidebar').children('.is_cell').first(),
			offset = parseInt($firstCell.css('left'),  10) * (-1);

		return Math.ceil(offset / IPadNav.cellWidth);
	},

	setPrevKnobActive: function () {
		jQuery('#knobLink_'+this.getNumberOfActiveCell()).removeClass('active');
		jQuery('#knobLink_'+(this.getNumberOfActiveCell()-1)).addClass('active');
	},

	setNextKnobActive: function () {
		jQuery('#knobLink_'+this.getNumberOfActiveCell()).removeClass('active');
		jQuery('#knobLink_'+(this.getNumberOfActiveCell()+1)).addClass('active');
	},

	prepareRightArrow: function () {
		jQuery('.is_arrow_right').unbind('click').bind('click.slide_left', function() {
			if (IPadNav.getNumberOfActiveCell() < IPadNav.cellCount-1) {
				IPadNav.numberOfWantedCell = IPadNav.getNumberOfActiveCell()+1;
			}

			if (IPadNav.moveCells(-930)) {
				IPadNav.setNextKnobActive();
			}
		});
	},

	prepareLeftArrow: function () {
		jQuery('.is_arrow_left').unbind('click').bind('click.slide_right', function() {
			if (IPadNav.getNumberOfActiveCell() > 0) {
				IPadNav.numberOfWantedCell = IPadNav.getNumberOfActiveCell()-1;
			}

			if (IPadNav.moveCells(930)) {
				IPadNav.setPrevKnobActive();
			}
		});
	},

	addKnobClicks: function () {
		jQuery('.is_knob').each(function () {
			jQuery(this).unbind('click').bind('click.knob', function () {
				var numberOfWantedCell = jQuery(this).data('nr');
				IPadNav.knobClick(numberOfWantedCell);
			});
		});
	},

	unlinkArrows: function () {
		jQuery('.is_arrow_right').unbind('click');
		jQuery('.is_arrow_left').unbind('click');
	},

	unlinkKnobs: function () {
		jQuery('.is_knob').each(function () {
			jQuery(this).unbind('click');
		});
	}
};


(function ($) {
	$(document).ready(function () {
		window.setTimeout(function() {
			if( !isiOS() ) {
				IPadNav.init();
			}
			else {
				RealIPadNav.init();
			}
		}, 20);
	});

	var checkNavState = window.setInterval(function () {

		if (IPadNav.ready) {
			window.clearInterval(checkNavState);
			var pc = 0;
			if (GET('pc') !== 'undefined') {
				pc = GET('pc');
				if( !isiOS() ){
					IPadNav.knobClick(pc, true);
				}

			}
		}
	}, 1);
})(jQuery);



