/*
 Dining At Glyndebourne
 HotHorse 2011
 James Allen / Will Swain
 Main Site Javascript
*/

$(document).ready(function() {	
	$(".nojs").hide();
	
	setupHomePage();
});

function makeExtLinks() {
	$('.ext').click(function() {
		window.open(this.href);
		return false;
	});	
}

/* Homepage - START */

function setupHomePage(){
	// Animate Sliding Overlays on Panels	
	$('.rollover_img a').hover(function(){
		$('.overlay', this).stop().animate({top:'0px'},{queue:false,duration:300});
	}, function() {
		$('.overlay', this).stop().animate({top:'160px'},{queue:false,duration:300});
	});
	// Change Cursor on Panels
/*	$('.rollover_img a').hover(function() {
		$(this).css('cursor','pointer');
	}, function() {
		$(this).css('cursor','auto');
	});*/
	
	// Make full Overlay clickable
	//$('.feature-box-linked').click(function(){
	//	if(!$(this).find('a.discover_feature_link3').length && !$(this).find('a.discover_feature_link2').length && !$(this).find('a.homepage_feature_link2').length) {
	//		window.location = $(this).find('a').attr('href');
	//		return false;
	//	}
	//});
}

/* Homepage - END */

function setupFoodAssignment(menuChoice,courseData) {
	var menuChoice = menuChoice;
	var courseData = courseData;
	
	refreshDishes(courseData,menuChoice);
			
	$(".selectInput").each(function(){
		$(this).change(function(){
			foodSelectHandler(this,menuChoice);
						
			// Reload all dropdowns, then trigger the onChange events on each dropdown to ensure everything ripples through.
			refreshDishes(courseData,menuChoice);
			
			if(this.selectedIndex == 0)
				$(this.options[0]).html("Choose Dish");
			else
				$(this.options[0]).html("Clear Selection");
		});
	});
}

function foodSelectHandler(el,menuChoice){
	var currentDiner = el.name;
	var foodID = el.value;
	var count = 1;
	var currentFoodItem = 0;
		
	// Remove the dish that has just been selected
	$(".selectInput").each(function() {
		if (this.name != currentDiner) {
			currentFoodItem = this.value;
			
			$(this).find("option").each(function(){									
				if(this.value == foodID) {																	
					if(currentFoodItem == foodID)
						count++;
				}
			});
		}
	});
		
	$(".selectInput").each(function() {		
		if(this.name != currentDiner) {
			currentFoodItem = this.value;
			
			$(this).find("option").each(function(){						
				if(this.value == foodID && currentFoodItem != foodID) {												
					if(count >= menuChoice[foodID]) {
						// Remove the option
						$(this).remove();
					}
				}
			});
		}
	});
}

function refreshDishes(courseData,menuChoice) {
	// Refresh all dropdowns
	var courseCount = 0;
	var currentDinerNo = 1;
	var dinerNo = 0;
	var currentSelection = 0;
	
	$(".selectInput").each(function() {
		dinerNo = this.name.replace("diner_","");
		currentSelection = this.value;
										
		if(dinerNo != currentDinerNo) {
			currentDinerNo++;
			courseCount = 1;
		} else {
			courseCount++;
		}
					
		$(this).find("option").each(function(){ $(this).remove(); });
		
		$(this).css("width","1px");
										
		$(this).append('<option value="">Choose Dish</option');
		
		for(i=0;i<courseData[courseCount-1].length;i++) {			
			if(currentSelection == courseData[courseCount-1][i].ID){
				selected = 'selected="selected"';
			} else {
				selected = '';
			}
						
			$(this).append('<option ' + selected + ' value="' + courseData[courseCount-1][i].ID + '">' + courseData[courseCount-1][i].TITLE + '</option>');
		}
		
		$(this).css("width","397px");
	});
	
	$(".selectInput").each(function() { foodSelectHandler(this,menuChoice); });
}

