$(document).ready(function(){
	var correctScrollTop = function()
		{
			$(window).scrollTop(0);
		}
		$('.tablet-menu').click(function()
		{
			$('.tablet-menu').each(function()
				{
					$(this).removeClass('current');
				})
			$(this).addClass('current');
			var getId = $(this).attr('title');
			getId = '.' + getId;
			$('.section-wrap-tablet').each(function()
				{
					$(this).css('display','none');
				})
			$(getId).css('display','block');
	
		})
		var clearTabletMenu = function(){
	$('.tablet-menu').each(function(){
		$(this).removeClass('current');
		
	})
	}
		
	var hashScroll = function(){
	
	var hash = window.location.hash
	if(!hash){

			$(window).scrollLeft(0);
		
		}
		else if(hash == '#specials-menu'){
	
			$(window).scrollLeft(900);
			}
	else if(hash == '#breakfast-lunch-menu'){
	
			$(window).scrollLeft(1600);
			}
	else if(hash == '#dinner-menu'){
			$(window).scrollLeft(2300);
	}
	else if(hash == '#beverage-menu'){
		
		
			$(window).scrollLeft(3000);
			}
			else{
			
			$(window).scrollLeft(0);
				
				}					
	}
	var clearMenuClass = function()
		{
			$('.menulinks').each(function()
				{
					$(this).removeClass('current');
				})
		}
	

	$(window).scroll(function(){
	var xPos = 	$(window).scrollLeft();
		if(xPos >= 900 && xPos < 1500){
			 clearMenuClass();
	$('#mspecials').addClass('current');
		}else if(xPos >= 1500 && xPos < 2200){
			clearMenuClass();
	$('#mbreakfast').addClass('current');
		}else if(xPos >= 2200 && xPos < 2900){
			clearMenuClass();
	$('#mdinner').addClass('current');
		}else if(xPos >= 3000){
   clearMenuClass();
	$('#mbeverages').addClass('current');
		}else{
			clearMenuClass();
		}
	
	})
	
$('.menulinks').click(function(){
	correctScrollTop();
	var id = $(this).attr('id');
	if(id=='mhome'){
	$(window).scrollLeft(0);
	clearMenuClass();
	}else if(id=='mspecials'){
	$(window).scrollLeft(900);
	clearMenuClass();
	$('#mspecials').addClass('current');
	}else if(id=='mbreakfast'){
	$(window).scrollLeft(1600);
	clearMenuClass();
	$('#mbreakfast').addClass('current');
	}else if(id=='mdinner'){
	$(window).scrollLeft(2300);
	clearMenuClass();
	$('#mdinner').addClass('current');
	}else if(id=='mbeverages'){
	$(window).scrollLeft(3000);
	clearMenuClass();
	$('#mbeverages').addClass('current');
	}
	
	
	
})

//sidebarheights
 var sidebarHeight = function(){
 	var wHeight = parseInt($(window).height());
 	var blogHeight = parseInt($('.set-height').height());
 	if (wHeight > blogHeight){
 	$('#blog-triangle').css('height', wHeight + 25)
	}else{
		$('#blog-triangle').css('height', blogHeight)
	} 	
 }

	var setHeights=function(){
		var wHeight = parseInt($(window).height());
		var wWidth = parseInt($(window).width());
		var imgHeight = $('#bg-image').height();
		var imgWidth = $('#bg-image').width();
		
		var newHeight = wHeight - 46;
		var contentHeight = wHeight - 40;
		
		$('.viewer').css('height',newHeight);
	}
		//set conveyor height;
		var conHeight = function(theId, conveyorf, viewerf, itemf){
				  var a = [];
				  var itemHeights=0;
				  
				  var viewerWidths = $('#' + theId).width() - 105;
				  $(viewerf).css("width", viewerWidths);
				  var itemHeight = $(itemf).height() + 200;
				  $(conveyorf).css("height", itemHeight);
				
					 
					  
					var convHeight = $(conveyorf).height()
					var viewHeight = $(viewerf).height()
					
				if(convHeight>viewHeight){
					$('.viewer').css('overflow','hidden');
					
				}
	 }//conveyor height
	var createSlider = function(conveyorf, viewerf, sliderf) {     
	
	
	 var convHeight = $(conveyorf).height()
	 var viewHeight = $(viewerf).height()
     var difference = convHeight-viewHeight;
	 var sliderHeight = viewHeight - 165;
	 var proportion = difference / $(conveyorf).height();
	 
	 var handleHeight = Math.round((1-proportion)*sliderHeight);//set the proportional height
	     handleHeight -= handleHeight%2; 
	
     var sliderOpts = {
		 min: 0,
		 value: 100, 
         max: 100,  
	     orientation: 'vertical',
	     slide: function(e, ui) { 
	     var topValue = - ((100-ui.value)*difference/100);
         $(conveyorf).css("top", topValue + "px");
         },
	     change: function(e, ui){
		 var topValue = -((100-ui.value)*difference/100);
		 $(conveyorf).css("top", topValue + "px");		
	     }
     }//slideropts
	 $('.slide-wrapper').css('height', sliderHeight);
	   $(sliderf).slider(sliderOpts);	
	 	  
	}//create slider
		//size scrollbar and handle proportionally to scroll distance
		
		$('.viewer').each(function(){
			$(this).parent().mousewheel(function(event, delta){
  		var speed = 5;
	    var sliderVal = $(this).find(".slide-wrap").slider("value");//read current value of the slider
		
	    sliderVal += (delta*speed);//increment the current value
 
	    $(this).find(".slide-wrap").slider("value", sliderVal);//and set the new value of the slider
		
	    event.preventDefault();//stop any default behaviour
 	});
			
		})
   
   var handleHeighter = function(conveyorf, viewerf, sliderf, sliderwrapperf){
	   var convHeight = $(conveyorf).height()
	 var viewHeight = $(viewerf).height()
     var difference = convHeight-viewHeight;
	 var sliderHeight = viewHeight - 165;
	 var proportion = difference / $(conveyorf).height();
	 
	 var handleHeight = Math.round((1-proportion)*sliderHeight);//set the proportional height
	     handleHeight -= handleHeight%2;
	 $(sliderf+" .ui-slider-handle").css({height:handleHeight,'margin-bottom':-0.5*handleHeight});
	
   var origSliderHeight = $(sliderwrapperf).height();//read the original slider height
 
   var sliderHeight = origSliderHeight - handleHeight ;//the height through which the handle can move needs to be the original height minus the handle height
   var sliderMargin =  (origSliderHeight - sliderHeight)*0.5;//so the slider needs to have both top and bottom margins equal to half the difference
   $(sliderwrapperf+" .ui-slider").css({height:sliderHeight,'margin-top':sliderMargin});//set the slider height and margins   
   }
  
   	$('.section-wrap').each(function(){
		var thisId = $(this).attr('id');
		var makeId = '#' + thisId;
		var conveyorId = 'conveyor' + thisId
			conveyorId = '#' + conveyorId;
		var viewerId = 'viewer' + thisId;
	  		viewerId = '#' + viewerId;
		var itemId = 'item' + thisId;
			itemId = '.' + itemId;
		var sliderId = '#' + 'slider' + thisId;
		var slidewrapperId = '#' + 'slide-wrapper-' + thisId;
		var convHeight = $(conveyorId).height();
		var viewHeight = $(viewerId).height();
		conHeight(thisId, conveyorId, viewerId, itemId);
		
		
	})
	
   var setSrollbars = function(){
	   	$('.section-wrap').each(function(){
		var thisId = $(this).attr('id');
		var makeId = '#' + thisId;
		var conveyorId = 'conveyor' + thisId
			conveyorId = '#' + conveyorId;
		var viewerId = 'viewer' + thisId;
	  		viewerId = '#' + viewerId;
		var itemId = 'item' + thisId;
			itemId = '.' + itemId;
		var sliderId = '#' + 'slider' + thisId;
		var slidewrapperId = '#' + 'slide-wrapper-' + thisId;
		var convHeight = $(conveyorId).height();
		var viewHeight = $(viewerId).height();
		
		var makeDiff = convHeight - viewHeight;
		
		if(makeDiff > 0){
			if($(slidewrapperId).length == 0){
				$(makeId).append('<div class="slide-wrapper" id="slide-wrapper-'+thisId+'"><div class="slide-wrap" id="slider'+thisId+'"></div></div>');	
				createSlider(conveyorId, viewerId, sliderId);		
		 handleHeighter(conveyorId, viewerId, sliderId, slidewrapperId);
			}else{
				createSlider(conveyorId, viewerId, sliderId);		
		 handleHeighter(conveyorId, viewerId, sliderId, slidewrapperId);
			}				
		}else{
			$(slidewrapperId).remove();
		}	
	
	  })
	   //setScrollBars
   }
  
	$(window).load(function(){
		
	 setHeights();
		setSrollbars();
	
	});

$(window).resize(function() {

		 setHeights();
		setSrollbars();
			sidebarHeight();
	})
	//functions
	sidebarHeight();
	hashScroll();	
})


