// BUBBLE CLASS
function Bubble(){
	var _this = this;

	this.bubble = $("#bubble");
	//this.button = $("#bubble-link");
	this.select = new Classy.SelectInput("#bubble-real-select",{
		id:"bubble-select",
		isAnimated:true,
		onChange:function(value){
			_this.changeButtonLink(value);
		}
	});

	this.pageRequested = null;
}
Bubble.prototype = {
	showBubble:function(){
		this.bubble.fadeIn('fast');
		
		// this is because not all restaurans have a facebook page
		if(this.pageRequested == "facebook"){
			var options = $("#bubble-select").find("li"),
				first = false;
			
			for(var i=0, l=restaurantSelect.length; i<l; i++){
				if(restaurantSelect[i].facebook === ""){
					$(options[i]).css('display','none');
				} else if(first === false){
					first = i;
				}
			}
			//this.select.index(first); simulate selection of the first link
		} else {
			$("#bubble-select").find("li").css('display','block');
			// on the index we set the select on the current restaurant shown in the viewer
			if(typeof myViewer !== "undefined"){
				this.select.index(myViewer.index);
			}
			var val = this.select.value();
			//this.changeButtonLink(val); simulate selection of the first link
		}
	},
	hideBubble:function(){
		var _this = this;
		this.bubble.fadeOut('fast',function(){
			if(_this.select.optionsAreVisible){
				_this.select.hideOptions();
			}
		});
	},
	changeButtonLink:function(name){
	  
		var val,
			i=restaurantSelect.length;
		if(this.pageRequested != "facebook"){
			for(i; i--; ){
				if(restaurantSelect[i].codename == name){
					val = restaurantSelect[i].codename;
					break;
				}
			}
			//this.button.attr('href',ROOT+val); replace button link
			window.location.replace(ROOT+val);
		} else {
			for(i; i--; ){
				if(restaurantSelect[i].codename == name){
					val = restaurantSelect[i].facebook;
					break;
				}
			}
			//this.button.attr('href',val); replace button link
			window.location.replace(val);
		}
	}
};


// SCROLLBAR CLASS
function Scrollbar(opts){
	this.bar = opts.bar;
	this.slider = opts.slider;
	this.container = opts.container;
	this.mask = opts.mask;
	
	this.barHeight = this.bar.innerHeight();

	this.sliderOffset = this.slider.offset().top;
	this.barHeight = this.bar.innerHeight();

	this.position = 0;
	this.mousewheelSpeed = 8;

	this.resetContainerHeight();

	var _this = this;
	this.slider.bind('mousedown',function(){
		$(document).bind('mousemove',function(e){
			_this.scroll(e.pageX,e.pageY);
			return false;
		});
		return false;
	});
	$(document).bind('mouseup',function(){
		$(this).unbind('mousemove');
		return false;
	});

	// the -4 in this function and in the scroll method 
	//is to make sure the slide doesn't go further than the bar height minus its padding

	$(this.container).mousewheel(function(e,delta){
		e.preventDefault();
		if(delta > 0){
			_this.position = (_this.position - _this.mousewheelSpeed >= 0) ? _this.position - _this.mousewheelSpeed : 0;
		} else if(delta < 0){
			_this.position = (_this.position + _this.mousewheelSpeed + _this.sliderHeight < _this.barHeight - 4) ? _this.position + _this.mousewheelSpeed : _this.barHeight - 4 - _this.sliderHeight;
		}
		_this.scroll(0,_this.sliderOffset+_this.position+(_this.sliderHeight/2));
	});
}
Scrollbar.prototype = {
	needsScrollbar:function(){
		return (this.container.height() > this.mask.height());
	},
	resetContainerHeight:function(){
		this.containerHeight = this.container.height();

		// percentage of container visible
		var p = parseInt((100 * this.mask.innerHeight()) / this.containerHeight, 10) / 100;
		// make sliderHeight equal to percentage of container visible
		this.sliderHeight = ((this.barHeight * p) < 25) ? 25 : (this.barHeight * p);

		this.slider.height(this.sliderHeight);
	},
	scroll:function(mouseX,mouseY){
		var gotoPosition = parseInt(mouseY - this.sliderOffset - (this.sliderHeight / 2), 10);
		if(gotoPosition < 0){
			gotoPosition = 0;
		} else if(gotoPosition + this.sliderHeight > this.barHeight -4){
			gotoPosition = this.barHeight-4 - this.sliderHeight;
		}

		this.position = gotoPosition;

		this.slider.css('top', gotoPosition + "px");
		this.container.css('top',-(gotoPosition * ((this.containerHeight - this.mask.height()) / (this.barHeight-4 - this.sliderHeight))));
	}
};

$(function(){
	
	var fancyOpeningHoursScrollbar, fancyContactScrollbar, fancyJobsScrollbar;
	
	$("#header-link-opening-hours").fancybox({
		modal:true,
		scrolling:"no",
		padding:0,
		overlayColor:"#000",
		onComplete:function(){
			var container = $("#fancy-opening-hours .fancy-content-container");
			var mask = $("#fancy-opening-hours  .fancy-content-mask");
			var bar = $("#fancy-opening-hours .scrollbar");
			var slider = $("#fancy-opening-hours .slider");
			if(container.height() > mask.height()){
				bar.css('display','block');
				fancyContactScrollbar = new Scrollbar({
					bar:bar,
					slider:slider,
					container:container,
					mask:mask
				});
			}
			$("#fancy-opening-hours").find(".fancybox-close").fadeIn(200);
		},
		onCleanup:function(){
			$("#fancy-opening-hours").find(".fancybox-close").css('display','none');
		}
	});
	
	$("#header-link-contact").fancybox({
		modal:true,
		scrolling:"no",
		padding:0,
		overlayColor:"#000",
		onComplete:function(){
			var container = $("#fancy-contact .fancy-content-container");
			var mask = $("#fancy-contact .fancy-content-mask");
			var bar = $("#fancy-contact .scrollbar");
			var slider = $("#fancy-contact .slider");
			if(container.height() > mask.height()){
				bar.css('display','block');
				fancyContactScrollbar = new Scrollbar({
					bar:bar,
					slider:slider,
					container:container,
					mask:mask
				});
			}
			$("#fancy-contact").find(".fancybox-close").fadeIn(200);
		},
		onCleanup:function(){
			$("#fancy-contact").find(".fancybox-close").css('display','none');
		}
	});
	
	$("#header-link-concours").fancybox({
		modal:true,
		scrolling:"no",
		padding:0,
		overlayColor:"#000",
		onComplete:function(){
			var container = $("#fancy-concours .fancy-content-container");
			var mask = $("#fancy-concours .fancy-content-mask");
			$("#fancy-concours").find(".fancybox-close").fadeIn(200);
		},
		onCleanup:function(){
			$("#fancy-concours").find(".fancybox-close").css('display','none');
		}
	});
	
	$("#header-link-jobs").fancybox({
		modal:true,
		scrolling:"no",
		padding:0,
		overlayColor:"#000",
		onComplete:function(){
			var container = $("#fancy-jobs .fancy-content-container");
			var mask = $("#fancy-jobs .fancy-content-mask");
			var bar = $("#fancy-jobs .scrollbar");
			var slider = $("#fancy-jobs .slider");
			if(container.height() > mask.height()){
				bar.css('display','block');
				fancyJobsScrollbar = new Scrollbar({
					bar:bar,
					slider:slider,
					container:container,
					mask:mask
				});
			}
			$("#fancy-jobs").find(".fancybox-close").fadeIn(200);
		},
		onCleanup:function(){
			$("#fancy-jobs").find(".fancybox-close").css('display','none');
		}
		
	});
	
	$(".fancybox-close").bind("click",function(){
		$.fancybox.close();
		return false;
	});
	
	window.bubble = new Bubble();
	
	var bubbleTopOffset = $("#header-links-facebook").offset().top - 136;
	
	$("#header-links-facebook").bind('click',function(){
		$("#bubble").css({
			top:bubbleTopOffset+"px",
			left:"535px"
		});
		bubble.pageRequested = "facebook";
		bubble.showBubble();
		return false;
	});

	$("#bubble").bind('mouseenter',function(){
		$(this).bind('mouseleave',function(){				
			bubble.hideBubble();
			if(bubble.select.optionsAreVisible){
				bubble.select.hideOptions();
			}
			$(this).find("#photo-fix:visible").css('display','none');
			$("#btn-menu").removeClass('mouseenter');
		});
	});
});
