/**
 * @author Thomas Sømoen
 */
(function($){
	$.fn.extend({
		searchHover:function(){
			return $(this).each(function(){
				$(this).hover(
					function(){
						$('#inspiration div.form').addClass('hover');
					},
					function(){
						$('#inspiration div.form').removeClass('hover');
					}
				);
			});
		},
		
		inspirationSubcontent:function(){
			var open = false;
			var visibleSub = null;
			
			return $(this).each(function(){
				var elm = $(this);
				var anchor = $(this).find('div.header a');
				var sub = $(this).find('div.sub');
				var searchBlocker = $('#inspiration').find('.inactive-blocker').removeClass('hidden').hide();
				var visible = visibleSub;
				var visibleSubSub = null;
				
				elm.find('.subsub').hide();

				var openSubcontent = function(){
					elm.siblings().removeClass('open');
					elm.addClass('open');
					elm.siblings().find('div.sub').css('z-index','');
					elm.find('div.sub').css('z-index','1');
					visibleSub = sub;
					
					if(elm.hasClass('search')){
						searchBlocker.show();
					}
					
					$('#inspiration').animate(
						{height:$.settings.inspiration.height.open},
						'fast',
						function(){
							open = true;
							$(this).addClass('open');
						}
					);	
				}
				
				var closeSubcontent = function(){
					$('#inspiration').removeClass('open').animate(
						{height:$.settings.inspiration.height.closed},
						'fast',
						function(){
							open = false;
							elm.parent().find('li').removeClass('open');
							searchBlocker.hide();
						}
					);	
				}
				
				var showSubContent = function(){
					elm.siblings().removeClass('open');
					elm.addClass('open');
					visible = visibleSub;
					visible.css('z-index','2');
					
					elm.find('div.sub').css('z-index','1');
					visibleSub = sub;
					
					if(elm.hasClass('search')){
						searchBlocker.show();
					}else{
						searchBlocker.hide();
					}
					
					visible.fadeOut('fast',function(){
						$(this).css({
							zIndex:'',
							display:''
						});
					});
				}
				
				
				anchor.click(
					function(e){
						e.preventDefault();
						
						// Dersom inspirasjonsbjelken er åpen lukkes bjelken eller annet innhold vises.
						if(open){
							// Dersom menypunktet allerede er synlig har man trykket to ganger, og inspirasjonsbjelken lukkes
							if(elm.hasClass('open')){
								closeSubcontent();
								
							// Dersom menypunktet ikke er synlig vises dette i stedet.
							}else{
								showSubContent();
							}
							
						// Dersom inspirasjonsbjelken er lukket åpnes bjelken.
						}else{
							openSubcontent();
						}
					}
				);
			
				elm.find('div.multiple > ul > li > a').click(function(e){
					e.preventDefault();
					
					var nextSubSub = $(this).parent().find('.subsub');
					
					if(!nextSubSub.parent().hasClass('selected')){
						
						if(visibleSubSub != null){
							visibleSubSub.parent().removeClass('selected');
							
							nextSubSub.css('z-index','').show();
							visibleSubSub.css('z-index','1');
							nextSubSub.parent().addClass('selected');
							
							visibleSubSub.fadeOut(
								'fast',
								function(){
									$(this).css('z-index','');
									visibleSubSub = nextSubSub;
									visibleSubSub.css('z-index','1');
								}
							);
						}else{
							visibleSubSub = nextSubSub;
							nextSubSub.show();
							nextSubSub.css('z-index','1');
							nextSubSub.parent().addClass('selected');
						}
					}
				});
				
				elm.find('div.multiple > ul > li:first > a').trigger('click');
			});	
		},
		
		handleContinents:function(e){
			return $(this).each(function(){
				var container = $(this).parents('ul:first');
				var li = $(this).parent();
				var countries = li.find('ul:first');
				var defaultWidth = li.width();
				var defaultHeight = li.height();
				
				var showCountries = function(e){
					e.preventDefault();
					var newWidth = countries.width()+15;
					var newHeight = (countries.height()+li.height()+30);
					
					// Lukker alle åpne verdensdeler.
					container.find('li.open > a').trigger('click');
					
					li.css('z-index','1').addClass('open').animate(
						{
							width:newWidth,
							height:newHeight
						},
						'fast'
					);
				}
				
				var hideCountries = function(e){
					e.preventDefault();
					li.animate(
						{
							width:defaultWidth,
							height:defaultHeight
						},
						'fast',
						function(){
							$(this).css('z-index','').removeClass('open');
						}
					);
				}
				
				// Legger på lukke- og åpne-hendelser på verdensdelene.
				$(this).toggle(
					showCountries,
					hideCountries
				);
				
				countries.find('>li > a').click(function(e){
					e.preventDefault();
					var data = $(this).attr('rel');
					var title = $(this).text();
					
					// Dersom første element i listen er valgt skal navet på verdensdelen vises.
					if($(this).parent().get(0) === $(this).parents('ul:first').find('li:first').get(0)){
						title = $(this).parents('ul:first').parents('li:first').find('>a').text();
					}
					
					$('#worldrecipies').fadeOut(
						'fast',
						function(){
							
							$(this).load(
								$.settings.actions.getWorldRecipes,
								{id: data},
								function(){
									if($(this).find('li').length > 10){
										$(this).addClass('scroll');
									}else{
										$(this).removeClass('scroll');
									}
									
									$(this).prev().text(title);
									$(this).fadeIn('fast');
									
								}
							);
							
						}
					);
				});
				
				
			});
		}
	});
	
	
	$(document).ready(function(){
		$('#inspiration ul.top li.search').searchHover();
		$('#inspiration ul.top > li').inspirationSubcontent();
		$('#countries > li > a').handleContinents();
		
		$('#map area[id]').hover(
			function(){
				$('#worldmapimg').attr('src','elm/bg_inspiration_map_'+$(this).attr('id')+'.gif');
			},
			function(){}
		);
		
		$('#map').hover(
			function(){},
			function(){
				$('#worldmapimg').attr('src','elm/bg_inspiration_map_default.gif');
			}
		);
		
		$('#inspiration li.search select').sSelect();
		$('#btnSearchExtended').click(function(e){
			e.preventDefault();
			$(this).parents('form').submit();
		});
	});
})(jQuery);
