$(document).ready(function() {
	FLIR.init( { path: '/facelift/' } );

	// Home

	$('.home #wrapper h2').each( function() {
		FLIR.replace(this, new FLIRStyle({
			cFont: 'vonnesthin',
			mode: 'wrap',
			realFontHeight: true,
			resizeBox: true
		}));
	});

	$('.home #wrapper h3').each( function() {
		FLIR.replace(this, new FLIRStyle({
			cFont: 'vonnesbold',
			mode: 'wrap',
			realFontHeight: true,
			resizeBox: true
		}));
	});

	$('.home #wrapper h4,.ingredients #wrapper h4').each( function() {
		FLIR.replace(this, new FLIRStyle({
			cFont: 'vonnesthin',
			mode: 'wrap',
			realFontHeight: true,
			resizeBox: true
		}));
	});
	
	// Submenu
	$('#subnav ul li').each( function() {
		FLIR.replace(this, new FLIRStyle({
			cFont: 'vonnesmediumcond',
			mode: 'wrap',
			realFontHeight: true,
			resizeBox: true
		}));
	});

	// Timeline
	if($('#timeline').length) new Timeline();
 
	// Recipes
	if ($('.recipes #form_explore').length) {
	  	$('.recipes #recipes_results h3,.recipes #recipes_results h4').each( function() {
	 		FLIR.replace(this, new FLIRStyle({
	 			cFont: 'vonnesmedium',
				mode: 'wrap',
				realFontHeight: true,
				resizeBox: true
	 		}));
	  	});
	  	$('.recipes #recipes_results h2 strong').each( function() {
			FLIR.replace(this, new FLIRStyle({
				cFont: 'vonnesblackcond',
				mode: 'wrap',
				realFontHeight: true,
				resizeBox: true
			}));
		});$('.recipes #recipes_results h2').each( function() {
			FLIR.replace(this, new FLIRStyle({
				cFont: 'vonnesthin',
				mode: 'wrap',
				realFontHeight: true,
				resizeBox: true
			}));
		});
	}
	
	if($('#recipe_search').length) {
		// ==================================================================
		// Autosuggest search box
		// ==================================================================
		$('.recipes #form_search #button_search,.recipes #form_search label').toggle();
		$('.recipes #form_search #recipe_search').addClass('full').addClass('inactive');
		
		// Autoclear
		if ($("#recipe_search").val() == '') {
			if($('body').hasClass('en')) $("#recipe_search").val('Search for a cocktail or ingredient...');
			else if($('body').hasClass('fr')) $("#recipe_search").val('Recherchez un cocktail ou un ingrédient...');
			var search_field_value = $("#recipe_search").val();
			$("#recipe_search").focus(function(){if ($(this).val() == search_field_value) $(this).val('').removeClass('inactive');});
			$("#recipe_search").blur(function(){if ($(this).val() == '') $(this).addClass('inactive').val(search_field_value);});
		} else {
			$("#recipe_search").removeClass('inactive');
		}
		
		
		// Unbind form submit
		$('#form_search').bind('submit', function(){return false;});
		
		// Set autosuggest options
		var options = {
			script:"/" + ($('body').hasClass('en') ? 'en' : 'fr') + "/tools/recipes?ajax=1&action=autosuggest&limit=8&",
			varname:"input",
			json:true,						// Returned response type
			shownoresults:true,				// If disable, display nothing if no results
			noresults: $('body').hasClass('en') ? "No results" : "Aucun résultats", // String displayed when no results
			maxentries:8,					// Max num results displayed
			cache:false,   					// To enable cache
			minchars:3,						// Start AJAX request with at least 3 chars
			timeout:100000,					// AutoHide in XX ms
			callback: function (obj) { 		// Callback after click or selection
				var cok_results = obj.id;
				$("#recipes_results").load('/' + ($('body').hasClass('en') ? 'en' : 'fr') + '/tools/recipes-results', {ajax: 1, 'cok_results': cok_results}, function(){
					setTimeout(function(){$.scrollTo($('#recipes_results'), 800);},1000);
					
					$('.recipes #recipes_results h3,.recipes #recipes_results h4').each( function() {
				 		FLIR.replace(this, new FLIRStyle({
					 		cFont: 'vonnesmedium',
							mode: 'wrap',
							realFontHeight: true,
							resizeBox: true
					 	}));
				  	});
				  	$('.recipes #recipes_results h2 strong').each( function() {
						FLIR.replace(this, new FLIRStyle({
							cFont: 'vonnesblackcond',
							mode: 'wrap',
							realFontHeight: true,
							resizeBox: true
						}));
					});
					$('.recipes #recipes_results h2').each( function() {
						FLIR.replace(this, new FLIRStyle({
							cFont: 'vonnesthin',
							mode: 'wrap',
							realFontHeight: true,
							resizeBox: true
						}));
					});
				});
			}
		};
		// Init autosuggest
		var as_json = new bsn.AutoSuggest('recipe_search', options);
	}
	if($('#explore_name').length) {
		// ==================================================================
		// Select boxes
		// ==================================================================
		$('.recipes #form_explore .block_button,.recipes #form_explore .block_buttons').toggle();
		$('.recipes #form_explore select').focus(function(){
			if ($('.recipes div.instructions').css('display') == 'none') setTimeout(function(){$('.recipes div.instructions').fadeIn(800);},2000);
		});
		
		var explore_name = $('#explore_name');
		var explore_alc_ing = $('#explore_alc_ing');
		var explore_nonalc_ing = $('#explore_nonalc_ing');
		
		var explore_name_values = explore_name.children()
		var explore_alc_ing_values = explore_alc_ing.children();
		var explore_nonalc_ing_values = explore_nonalc_ing.children();
		
		var resetSelect = function(select_box, select_box_values){
			select_box.empty();
			for (var i = 0; i < select_box_values.length; i++) {
		   		select_box.append(select_box_values[i]);
			}
			select_box.val(false);
		}
		
		var filterText = function(select_box){
			var option_selected = select_box.find('option:selected');
			if (option_selected.length == 1) 
				var filter_text = ($('body').hasClass('en') ? 'containing ' : 'contenant ') + option_selected.text();
			else if (option_selected.length > 1) {
				var filter_texts_title = [];
				option_selected.each(function(i){
					filter_texts_title[i] = $(this).text();
				});
				var filter_text_title = filter_texts_title.join(' or ');
				var filter_text = ($('body').hasClass('en') ? 'with one of the ' : 'avec l’un des ') + '<span title="'+filter_text_title+'">' + ($('body').hasClass('en') ? 'selected ingredients ' : 'ingédients sélectionnés ') + '</span>';
			}
			return filter_text;
		}
		
		// Cocktails names
		explore_name.selectChain({
			target: explore_alc_ing,
			type: 'post',
			data: {
				ajax: true,
				inc_type: 'alcohol'
			}
		}).selectChain({
			target: explore_nonalc_ing,
			type: 'post',
			data: {
				ajax: true,
				inc_type: 'non-alcohol'
			}
		}).change(function(){
			explore_alc_ing.val(false).css({'color':'#000'});
			explore_nonalc_ing.val(false).css({'color':'#000'});
			$('.block_buttons a').addClass('active');
		});
		
		// Ingredients with alcohol
		explore_alc_ing.selectChain({
			target: explore_name,
			type: 'post',
			data: {ajax: true}
		}).change(function(){
			$(this).css({'color':'#000'});
			var filter_text = filterText($(this));
			if (typeof(filter_text) != 'undefined') $('#explore_name_filter').html(filter_text);
			explore_name.val(false);
			resetSelect(explore_nonalc_ing, explore_nonalc_ing_values);
			explore_nonalc_ing.css({'color':'#999'});
			$('.block_buttons a').addClass('active');
		});
		
		// Ingredients without alcohol
		explore_nonalc_ing.selectChain({
			target: explore_name,
			type: 'post',
			data: {ajax: true}
		}).change(function(){
			$(this).css({'color':'#000'});
			var filter_text = filterText($(this));
			if (typeof(filter_text) != 'undefined') $('#explore_name_filter').html(filter_text);
			explore_name.val(false);
			resetSelect(explore_alc_ing, explore_alc_ing_values);
			explore_alc_ing.css({'color':'#999'});
			$('.block_buttons a').addClass('active');
		});
		
		// Buttons
		$('#btn_results').click(function(){
			var cok_results = [];
			if ($('#explore_name :selected').length == 1) {
				cok_results[0] = $('#explore_name :selected').val();
			}
			else if ($('#explore_name :selected').length > 1) {
				$('#explore_name :selected').each(function(i, selected){
					cok_results[i] = $(selected).val();
				});
			}
			else {
				$('#explore_name option').each(function(i, opt){
					cok_results[i] = $(opt).val();
				});
			}
			$("#recipes_results").load('/' + ($('body').hasClass('en') ? 'en' : 'fr') + '/tools/recipes-results', {ajax: 1, 'cok_results': cok_results.join(';')}, function(){
				setTimeout(function(){$.scrollTo($('#recipes_results'), 800);},1000);
				$('.recipes #recipes_results h2 strong').each( function() {
					FLIR.replace(this, new FLIRStyle({
						cFont: 'vonnesblackcond',
						mode: 'wrap',
						realFontHeight: true,
						resizeBox: true
					}));
				});$('.recipes #recipes_results h2').each( function() {
					FLIR.replace(this, new FLIRStyle({
						cFont: 'vonnesthin',
						mode: 'wrap',
						realFontHeight: true,
						resizeBox: true
					}));
				});
				$('.recipes #recipes_results h3,.recipes #recipes_results h4').each( function() {
					FLIR.replace(this, new FLIRStyle({
						cFont: 'vonnesmedium',
						mode: 'wrap',
						realFontHeight: true,
						resizeBox: true
					}));
				});
			});
			return false;
		});
		$('#btn_clear').click(function(){
			explore_name.css({'color':'#000'});
			resetSelect(explore_name, explore_name_values);
			explore_alc_ing.css({'color':'#000'});
			resetSelect(explore_alc_ing, explore_alc_ing_values);
			explore_nonalc_ing.css({'color':'#000'});
			resetSelect(explore_nonalc_ing, explore_nonalc_ing_values);
			$('.filter').empty();
			$('.block_buttons a').removeClass('active');
			return false;
		});
	}

	// Instruments
	if($('body.instruments').length) {
	  $('.instruments h3,.instruments h2').each(function() {
		 FLIR.replace(this, new FLIRStyle({
			 cFont: 'vonnesblackcond',
			 mode: 'wrap',
			 resizeBox: true
		 }));
	  });
	  $('.instruments .intro span.one-would-think,.instruments .intro span.couple-of-bottles').each( function() {
		 FLIR.replace(this, new FLIRStyle({
			 cFont: 'vonnesmedium',
			 mode: 'wrap',
			 resizeBox: true
		 }));
	  });
	  var tools_index_pos = $('.tools-index ul').position().top;
	  $(window).scroll(function() {
	  	$('.tools-index ul').css('top' , ($(document).scrollTop() > tools_index_pos) ? $(document).scrollTop() + 20 : tools_index_pos);
	  });
   }

   // Techniques	
   if($('body.techniques').length) {
	  var techniques_index_pos = $('.techniques-index ul').position().top;
	  $(window).scroll(function() {
	  	$('.techniques-index ul').css('top' , ($(document).scrollTop() > techniques_index_pos) ? $(document).scrollTop() + 20 : techniques_index_pos);
	  });
	  $('#making-a-twist .video').flash(
		  { src: '/flash/mini-player.swf',
			width: 299,
			height: 280,
			wmode : 'transparent',
			flashvars: {
				path: '/video/techniques/knowledge_twist.mov',
				image: '/video/techniques/knowledge_twist.png'
			}
		  },
		  { version: 10 }
	  );
	  $('#shaking .video').flash(
		  { src: '/flash/mini-player.swf',
			width: 299,
			height: 280,
			wmode : 'transparent',
			flashvars: {
				path: '/video/techniques/knowledge_shaking.mov',
				image: '/video/techniques/knowledge_shaking.png'
			}
		  },
		  { version: 10 }
	  );
	  $('#throwing .video').flash(
		  { src: '/flash/mini-player.swf',
			width: 299,
			height: 280,
			wmode : 'transparent',
			flashvars: {
	 			path: '/video/techniques/knowledge_throwing.mov',
				image: '/video/techniques/knowledge_throwing.png'
			}
		  },
		  { version: 10 }
	  );
	  $('#muddling .video').flash(
		  { src: '/flash/mini-player.swf',
			width: 299,
			height: 280,
			wmode : 'transparent',
			flashvars: {
				path: '/video/techniques/knowledge_muddling.mov',
				image: '/video/techniques/knowledge_muddling.png'
			}
		  },
		  { version: 10 }
	  );
	  $('#rolling .video').flash(
		  { src: '/flash/mini-player.swf',
			width: 299,
			height: 280,
			wmode : 'transparent',
			flashvars: {
				path: '/video/techniques/knowledge_rolling.mov',
				image: '/video/techniques/knowledge_rolling.png'
			}
		  },
		  { version: 10 }
	  );
	  $('#stirring .video').flash(
		  { src: '/flash/mini-player.swf',
			width: 299,
			height: 280,
			wmode : 'transparent',
			flashvars: {
				path: '/video/techniques/knowledge_stirring.mov',
				image: '/video/techniques/knowledge_stirring.png'
			}
		  },
		  { version: 10 }
	  );

   }

   // Ingredients	
   if($('body.ingredients').length) {
	  $('.ingredients h3 span').each( function() {
		 FLIR.replace(this, new FLIRStyle({
			 cFont: 'vonneslight',
			 mode: 'wrap',
			 resizeBox: true
		 }));
	  });
	  $('.ingredients h3').each( function() {
		 FLIR.replace(this, new FLIRStyle({
			 cFont: 'vonnesmedium',
			 mode: 'wrap',
			 resizeBox: true
		 }));
	  });
	}

	// Menu Creator
	var referer = $('.menucreator #content a.back').attr('href');
	var lang = $('body').hasClass('en') ? 'en' : 'fr';

	$('.menucreator #content').flash(
		{ src: '/flash/menucreator/menucreator.swf',
		  flashvars : {
			referer : referer,
			lang : lang,
			config_file   : '/flash/menucreator/config.xml',
			cocktails_xml : '/flash/menucreator/cocktails_'+lang+'.xml'
		},
		width: $(window).width(),
		height: $(window).height() > 680 ? ($(window).height() - $('#footer').height() - 22) : 	680,
		bgcolor : '#000000' },
		{ version: 10 }
	);
	$(window).resize(function(){
		var height = $(window).height() > 680 ? ($(window).height() - $('#footer').height() - 22) :	680;
		var width = $(window).width();
		$('.menucreator #content embed').height(height);
		$('.menucreator #content embed').width(width);
	});

});
