﻿$(document).ready(function() { 
	var current = true;
	
	$('.active .topmenu-secondlevel').each(function(){
		if($(this).hasClass("hidden"))
		{
			current = false;	
		}
	});
	
	$('.item').live('mouseenter mouseleave',function(){
													 
		
	
			$('.item').each(function(){
				if($(this).hasClass("current"))
				{
					/*if( $(this).attr('id') == 'node_id_88' || $(this).attr('id') == 'node_id_102' || $(this).attr('id') == 'node_id_800')
					{
						current = false;
					}
					else
					{
						current = true;
					}*/
					
					current=false;
					
					
					
				}
			});
	
		if (current)
		{
			
			if($('.topmenu-secondlevel', this).hasClass('hidden')){	
				
				$('.topmenu-secondlevel', this).removeClass('hidden');
				$('.firstlevel',this).css('color','#00d5ff');	
			}
			else
			{	
				$('.topmenu-secondlevel', this).addClass('hidden');		
				$('.firstlevel',this).css('color','#ffffff');
			}
			
			
			$(this).toggleClass('active');	
			
			
		}
		
		
		Cufon.refresh();
	});
	
	$('.item').live('mouseenter',function(){
		$('.topitem a', this).css('color','#00d5ff');			
	});
	
	$('.item').live('mouseleave',function(){
		$('.topitem a', this).css('color','#ffffff');							 
	});
	
	$('.subitem').live('mouseenter mouseleave',function(){
	
		var current = true;
		
		$('.subitem').each(function(){
			if($(this).hasClass("current"))
			{
				current = false;
			}
		});
	
		if (current)
		{
			if($(".topmenu-thirdlevel", this).hasClass('hidden'))
			{
				$(".topmenu-thirdlevel", this).removeClass('hidden');
				$('.secondlevel',this).css('color','#00d5ff');
			}
			else
			{			
				wait(500);
				$('.secondlevel',this).css('color','#ffffff');
				//$(this).parent().parent().find('.firstlevel').css('color','#ffffff');
				$(".topmenu-thirdlevel", this).addClass('hidden');		
			}
		}
		
		//$(this).toggleClass('active');
		Cufon.refresh();
		
	});
	
	$('.subitem').live('mouseenter',function(){
		$('.sublink a', this).css('color','#00d5ff');
	});
	
	$('.subitem').live('mouseleave',function(){
		$('.sublink a', this).css('color','#ffffff');							 
	});
	
	
	function wait(msecs)
	{
		var start = new Date().getTime();
		var cur = start
		while(cur - start < msecs)
		{
			cur = new Date().getTime();
		}
	}

	
	
	$('#nav-prev').click(function(){
								  
		var found = 0;
								  
		var count = $('.galleryitems .img_container').children().length;
		
		if ( count > 1 )
		{
								 
			$('.galleryitems .content-view-galleryslide').each(function(){
				if ( $(this).is(":visible") && found == 0 ) 
				{
					
					found = 1;
					
					var id = $(this).attr("id");
					var arr = id.split("_");
					
					var prev = arr[1]-1;
					
					if ( prev <= 0 )
					{
						var prev_img = "img_" + count;
					}
					else
					{
						var prev_img = "img_" + prev;	
					}
					
					
					
					$('#'+prev_img).fadeIn("slow");
					
					$(this).fadeOut("slow");
					
					$('#'+prev_img).css('position','relative');
					$('#'+prev_img).css('margin-left','0');
					
					$(this).css('position','absolute');
					$(this).css('margin-left','20px');
					
					
				}
			});	
		
		}
	});
	
	$('#nav-next').click(function(){
								  
		var found = 0;
								  
		var count = $('.galleryitems .img_container').children().length;
		
		if ( count > 1 )
		{
								 
			$('.galleryitems .content-view-galleryslide').each(function(){
																		
																		
				if ( $(this).is(":visible") && found == 0 ) 
				{
					
					found = 1;
					
					var id = $(this).attr("id");
					var arr = id.split("_");
					
					var num = new Number(arr[1]);
					
					var next = num+1;
					
					if ( next > count )
					{
						var next_img = "img_1";
					}
					else
					{
						var next_img = "img_" + next;	
					}
					
					$('#'+next_img).fadeIn("slow");
					
					$(this).fadeOut("slow");
					
					$('#'+next_img).css('position','relative');
					$('#'+next_img).css('margin-left','0');
					
					$(this).css('position','absolute');
					$(this).css('margin-left','20px');
					
				}
			});	
		
		}
	});
	
	$('.year a').click(function(){
								   
		
								
		$('.articles .class-article').each(function(){
														
				$(this).switchClass('active','',1000,'easeOutQuad');
 				
		});	
		
		var num = $(this).attr("name");
		
		$('.year'+num+' .class-article').switchClass('','active',1000,'easeOutQuad');
		
		$('.year a').each(function(){
			
			$(this).removeClass('active_year');

		});
		
		Cufon.refresh();
		$(this).toggleClass('active_year');
								  
	});
	
	$('.history .attribute-short a').click(function(){
								   
		
								
		$('.articles .class-article').each(function(){
														
				$(this).removeClass('active');
 				
		});	
		
		var num = $(this).attr("name");
		
		$(this).parent().parent().switchClass('','active',1000,'easeOutQuad');
		
		$('.year a').each(function(){
			
			$(this).removeClass('active_year');

		});
		
		Cufon.refresh();
		$('.year'+num+' .year a').toggleClass('active_year');
								  
	});
	
	
	$('.select-header').click(function(){
	
		$(this).parent().find('.query-options').slideToggle("fast");
		$('h3', this).toggleClass('arrow');
	
	});
	
	$('.opt a').click(function(){
		var value = $(this).html();

		var id = $(this).parent().parent().parent().parent().find('.select-header').attr("id");
		
		var node = $(this).parent().parent().attr("id");
		
		$("#"+id+"_input").val(value);
		
	
	
		$(this).parent().parent().parent().parent().find('.select-header h3').html( value );
		$(this).parent().parent().parent().parent().find('.select-header h3').addClass('parent_'+node);
		$(this).parent().parent().parent().parent().find('.select-header h3').addClass('selected');
		$(this).parent().parent().parent().slideToggle("fast");
		$(this).parent().parent().parent().parent().find('.select-header h3').toggleClass('arrow');
		
		$('.model-select .query-options .opt').each(function(){
			if ($(this).hasClass(node))
			{
				$(this).show();
			}
			else
			{
				$(this).hide();
			}
		});
		
	});
	
	$('.mod a').click(function(){
			$('.model-select .select-header h3').html("Velg modell");
			$('.model-select .select-header h3').removeClass('selected');
			$('.model-select').show();			
			$('.select-info').hide();
			$('#showhide').hide();
			
	});
	
	$('.sec a').click(function(){
							   
			$('.select-info').show();
			$('#showhide').show();
			
			$('.info').hide();
			
			switch ($("#type_input").val())
			{
				case 'Seilb\u00e5t':
					$('.options-seilbaat').show();
					$('#innbytte').show();
					$('#showhide').hide();
				break;
				
				case 'Motorb\u00e5t':
					$('.options-motorbaat').show();
					$('#innbytte').show();
					$('#showhide').hide();
				break;
				
				case 'Generelt':
					$('.options-generelt').show();
					$('.model-select').hide();
					$('#innbytte').hide();
				break;
				
				case 'Service':
					$('.options-service').show();
					$('.model-select').hide();
					$('#innbytte').hide();
					$('#showhide').show();
				break;
			}
	});
	
	$('input[type=checkbox]').each(function() {
			$(this).wrap(function() {
				return ($(this).is(':checked')) ? '<div class="custom_checkbox checked" />' : '<div class="custom_checkbox" />';
			});
	});
		
	$('.custom_checkbox input[type=checkbox]').click(function () {
		$(this).parent().toggleClass('checked');
	});


	if ( $("#type_input").val() != '' )
	{
		if($('.select-info').hasClass("autoselect"))
		{
			$('.model-select').show();
			
			switch ($("#type_input").val())
			{
				case 'Seilb\u00e5t':
					var node = "n88";
				break;
				
				case 'Motorb\u00e5t':
					var node = "n102";
				break;
			}
			
			
			$('.model-select .query-options .opt').each(function(){
				if ($(this).hasClass(node))
				{
					$(this).show();
				}
				else
				{
					$(this).hide();
				}
			});
		}
		else
		{
		
			switch ($("#type_input").val())
			{
				case 'Seilb\u00e5t':
					$('.select-info').show();
					$('.options-seilbaat').show();
				break;
				
				case 'Motorb\u00e5t':
					$('.select-info').show();
					$('.options-motorbaat').show();
				break;
			}
		
		}
	}
	
	$('.btn').bind('mouseenter mouseleave',function(){
		$(this).toggleClass('activebutton');	
	});
	
});
