
var RealIPadNav = {
	wantedCell: 0,
	currentCell: 0,
	cellWidth: 930,
	cellCount: 1,
	leftArrowLocked: false,
	rightArrowLocked: false,
	oldLeft: 0,
		
	init: function() {
		
		this.cellWidth = parseInt(jQuery('.is_slidebar').children('.is_cell').css('width'), 10);
		this.cellCount = jQuery('.is_slidebar').children('.is_cell').length;
		this.currentCell = (parseInt(jQuery('#slidebar').css('left'), 10) / this.cellWidth) * -1;		
		RealIPadNav.prepareLeftArrow();
		RealIPadNav.prepareRightArrow();
				
		RealIPadNav.handleArrows();
		
		RealIPadNav.drawKnobs();
		
		if(GET('pc') !== 'undefined') {
			RealIPadNav.wantedCell = GET('pc');
		}
	},
	
	showArrows: function () {
		jQuery('.is_arrow_left').removeClass('hide');
		jQuery('.is_arrow_right').removeClass('hide');
	},
	
	handleArrows: function() {
		
		RealIPadNav.showArrows();
		
		if( this.currentCell == 0 ) {
			jQuery('.is_arrow_left').addClass('hide');
		}
		
		if( this.currentCell == this.cellCount - 1) {
			jQuery('.is_arrow_right').addClass('hide');
		}
	},
	
	prepareLeftArrow: function() {
		jQuery('#pct_arrow_lft').bind('click', function(){ 
			
			if( RealIPadNav.leftArrowLocked == false ){
				RealIPadNav.leftArrowLocked = true;
				RealIPadNav.wantedCell--;
				RealIPadNav.oldLeft += 930;
				RealIPadNav.moveCells( ((RealIPadNav.currentCell - 1) * RealIPadNav.cellWidth) * -1 );
			}
		});
	},
	
	prepareRightArrow: function() {
		
		jQuery('#pct_arrow_right').bind('click', function(){ 
			if( RealIPadNav.rightArrowLocked == false) {
				RealIPadNav.rightArrowLocked = true;
				RealIPadNav.wantedCell++;
				RealIPadNav.oldLeft -= 930;
				RealIPadNav.moveCells(((RealIPadNav.currentCell + 1) * RealIPadNav.cellWidth) * -1);
			}
		});
	},
	
	slideLeft: function() {
		////console.log("left");
		if( RealIPadNav.cellCount > RealIPadNav.currentCell  ) {
			RealIPadNav.currentCell++;
			RealIPadNav.wantedCell++;
			
		}
		////console.log("currentCell: " + RealIPadNav.currentCell);
		RealIPadNav.handleArrows();
		RealIPadNav.handleKnobs();
		
	},
	
	slideRight: function() {
		////console.log("right");
		if( RealIPadNav.currentCell > 0) {
			RealIPadNav.currentCell--;
			RealIPadNav.wantedCell--;
		}
		////console.log("currentCell: " + RealIPadNav.currentCell);
		RealIPadNav.handleArrows();
		RealIPadNav.handleKnobs();
	},
	
	moveCells: function(x) {
		jQuery('#slidebar').css({
         '-webkit-transition-property': 'left',
         '-webkit-transition-duration': '0.7s'
         });
        
		jQuery('#slidebar').css('left',  x);	
			
		if( x > 0 ) {
		    RealIPadNav.currentCell = x / RealIPadNav.cellWidth; 
		}
		else {
		    RealIPadNav.currentCell = (x / RealIPadNav.cellWidth) * -1;
		}
		////console.log("currentCell: " + RealIPadNav.currentCell);
		RealIPadNav.handleArrows();
		RealIPadNav.handleKnobs();
		RealIPadNav.leftArrowLocked = false;
		RealIPadNav.rightArrowLocked = false;
		zlog(touchslider.oldLeft);
	},
	
	drawKnobs: function () {
		nr = 0;
		
		jQuery('.is_slidebar').children('.is_cell').each(function () {
			var $knob = jQuery('<li>').clone(),
				knobId = 'knob_' + nr,
				$knobLink = jQuery('<a href="javascript:;">')
				firstKnob = false;
			
			$knob.attr('id', knobId);
			$knob.attr('data-nr', nr);
			$knob.addClass('knob is_knob');
						
			if (nr == RealIPadNav.currentCell) {
				$knobLink.addClass('active');
			}
			
			if (0 == nr) {
				firstKnob = true;
			}
			
			nr++;
			$knob.append($knobLink);

			if (firstKnob) {
				jQuery('#iPadNav').html($knob);
			}
			else {
				jQuery('#iPadNav').append($knob);
			}
		});
		
		RealIPadNav.addKnobClicks();
	},
	
	addKnobClicks: function () {
		jQuery('.is_knob').each(function () {
			jQuery(this).unbind('click').bind('click.knob', function () {
				var numberOfWantedCell = jQuery(this).data('nr');
				RealIPadNav.knobClick(numberOfWantedCell);
			});
		});
	},
	
	knobClick: function (numberOfWantedCell) {
		
		if( numberOfWantedCell != RealIPadNav.currentCell ) {
			RealIPadNav.wantedCell = numberOfWantedCell;
			if( numberOfWantedCell < RealIPadNav.currentCell ) {
				/* animated => distance */
				/*
				distance = ((RealIPadNav.currentCell - numberOfWantedCell) * RealIPadNav.cellWidth) * -1;
				RealIPadNav.moveCells(distance, true);
				*/
			
				position = RealIPadNav.cellWidth * numberOfWantedCell * -1;
				RealIPadNav.oldLeft = position;
				RealIPadNav.moveCells(position, false);
				//////console.log(position);
			
			}
			else {
				/*	
				distance = ((numberOfWantedCell - RealIPadNav.currentCell) * RealIPadNav.cellWidth);
				RealIPadNav.moveCells(distance, true);
				*/
				
				position = RealIPadNav.cellWidth * numberOfWantedCell * -1;
				RealIPadNav.oldLeft = position;
				RealIPadNav.moveCells(position, false);
				
				//////console.log(position);
				
			}
			
			RealIPadNav.handleKnobs();
			
		}
		
				
	},
	
	handleKnobs: function() {
		////console.log("wantedCell: " + RealIPadNav.wantedCell);
		jQuery('.is_knob').each(function () {
			var knob = jQuery(this).children().first();
			if(jQuery(this).data('nr') != RealIPadNav.wantedCell) {
				jQuery(knob).removeClass(' active');
			}
			else {
				jQuery(knob).addClass(' active');
			}
				
		});
	
	}
}



