(function($){
  $.fn.simpleSlideShow = function(settings){
    var config = {
      'timeOut': 7000,
      'speed': '3000'
    };
    if (settings) $.extend(config, settings);
    this.each(function(){
      var $elem = $(this);
      $elem.children(':gt(0)').hide();
      setInterval(function(){
        $elem.children().eq(0).fadeOut(config['speed'])
        .next().fadeIn(config['speed'])
        .end().appendTo($elem);
      }, config['timeOut']);
    });
    return this;
  };
  $.fn.makeVideoThumb = function(settings){
    var config = {
      'overlay-dim-image': '/sitefiles/10/design/video_thumb_big_overlay.png',
      'overlay-reel-image': '/sitefiles/10/design/video_thumb_big_reel.png'
    };
    if (settings) $.extend(config, settings);
    this.each(function(){
      var $elem = $(this);
	  	$(this)
			.append('<img class="opacity_overlay" src="'+config['overlay-dim-image']+'"/><img class="video_overlay" src="'+config['overlay-reel-image']+'"/>')
			.hover(function(){
				$('.opacity_overlay',this).fadeTo(100,.2);
				},function(){
				$('.opacity_overlay',this).fadeTo(100,1);
				});
    });
    return this;
  };
  $.fn.makeImageChangerLink = function(settings){
    var config = {
      'speed': '3000',
	  'target-element':'.image-description',
	  'active-class':''
    };
    if (settings) $.extend(config, settings);
      var $elem = $(this);
	  $elem.click(function(e){
		e.preventDefault();
		 id=$(this).attr('href');
		 $(".active-image").html($(".active-image").html());
		 $(".active-image").fadeOut(config['speed']).removeClass('active-image');
		 if($(this).hasClass('video')){
		 	var video_html = $(id).html();
		 	$(id).fadeIn(config['speed'])
		 		.addClass('active-image')
		 		.html(video_html);
		 	
		 }
		 else{
		 	$(id).fadeIn(config['speed']).addClass('active-image');
		 }
		 jQuery('html,body').animate({scrollTop: [jQuery(id).offset().top,'easeOutExpo']},700);
		 if(config['active-class']!=''){
			$('.'+config['active-class']).removeClass(config['active-class']);
			$(this).addClass(config['active-class']);
			}
    });
    return this;
  };
  $.fn.addBar = function(settings){
    var config = {
      'width': 'auto',
      'height': 27,
	  'bar-color':'transparent',
	  'bar-background-repeat':'repeat-x',
	  'bar-background':'/sitefiles/10/design/bar_shadow.png'
    };
    if (settings) $.extend(config, settings);
    this.each(function(){
	  var $elem = $(this);
	  $elem.css({'position':'relative'});
	  $elem.children(':first').css({'position':'relative','z-index':99});
		var bar=document.createElement('span');
		var $bar=$(bar);
		$bar
		.attr({'class':'active_indicator'})
		.css({
			'position':'absolute',
			'top':0,
			'left':0,
			'z-index':1,
			'display':'block',
			'height':config['height'],
			'width':config['width'],
			'background-color':config['bar-color'],
			'background-image':'url('+config['bar-background']+')',
			'background-repeat':config['bar-background-repeat']
			});
		$(this).append(bar);

    });
    return this;
  };
  /*
  activeHoverize should be added to the element to which load indicator should be appended, this element will be positioned relative by plugin
  */
  $.fn.activeHoverize = function(settings){
    var config = {
      'width': 'auto',
      'height': 5,
	  'bar-color':'#be3232',
	  'loadInTime':500,
	  'loadOutTime':200
    };
    if (settings) $.extend(config, settings);
    this.each(function(){
      var $elem = $(this);
	  $elem.css({'position':'relative'});
	  $elem.hover(function(){
		$(this).addBar('width',0);
		if($(this).hasClass('last'))
			$('.active_indicator',this).animate({'width':config['width']-2});
		else
			$('.active_indicator',this).animate({'width':config['width']});
		},function(){
		$('.active_indicator',this).animate({'width':0},config['loadOutTime'],function(){
			$(this).remove();
			});
		});
    });
    return this;
  };
  $.fn.newsExpand = function(settings){
    var config = {
	'speed':500,
	'text_less':'Lukk',
	'text_more':'Les Mer'
    };
    if (settings) $.extend(config, settings);
    this.each(function(){
      var $elem = $(this);
	  $elem
	  .find('.news')
		  .css({'position':'relative'})
		  .each(function(){
			$(this).find('.newstriggers').click(function(){
				if($(this).hasClass('expanded'))
				$(this).text(config['text_more']).removeClass('expanded').parents('.news').find('.expandablen').slideUp(config['speed']);
				else
				$(this).addClass('expanded').text(config['text_less']).parents('.news').find('.expandablen').slideDown(config['speed']);
				return false;
				});
			})
		  .find('h5')
			.css({'cursor':'pointer'})
			.click(function(){
				$(this)
					.parents('.news')
						.find('h5')
							.fadeTo(100,1)
							.end()
						.find(':not("h5,.expandablen")')
							.slideDown(config['speed'])
							.end()
						.siblings('.news')
							.find(':not("h5,.expandablen")')
								.slideUp(config['speed'])
								.end()
							.find('h5').fadeTo(100,.5);
			
			});
		
	  
    });
    return this;
  };
  //
  var j=0;
  $.fn.flashInSlogans = function(settings){
    var config = {
	  'speed':1000,
	  'timeout':5000
    };
    if (settings) $.extend(config, settings);
    var $elem = $(this);
	for(var i=0;i<$(".slogan").length;i++){
		$(".slogan").eq(i).hide();
		}
	setTimeout(function showSlogans(){
		var a = $(".slogan").length;
		if(j < a){   
			$(".slogan").eq(j).fadeIn(config['speed'], function(){ j++; showSlogans(); });
			}
		else if(j < a*2){
			/*$(".slogan").eq(j-a).fadeTo(config['speed'],.3,function(){
				$(this).fadeTo(config['speed'],1,function(){
					j++;
					showSlogans();
					});
				});*/
			}
		else{
			j=a;
			showSlogans();
			}
		},config['timeout']);
    return this;
  };
  
  $.fn.createTabs = function(settings){
    var config = {
      'tab-width': 'auto',
      'top': 96
    };
    if (settings) $.extend(config, settings);
    this.each(function(){
      var $elem = $(this);
	  $elem.css({'position':'relative'});
	  $('.detail h4',$elem)
		.css({
			'cursor':'pointer',
			'opacity':.3,
			'float':'left',
			'width':config['tab-width']
			});
	  $('.detail:not(".active") h4',$elem)
		.live('mouseenter',function(){
		        if(!$.browser.msie)$(this).fadeTo(200,1);
		        if($.browser.msie)$(this).css('opacity',1);
			})
		.live('mouseleave',function(){
		        if(!$.browser.msie)$(this).fadeTo(100,.3);
			if($.browser.msie)$(this).css('opacity',.3);
			});
	  $('.detail:not(".active,.scroll_link") h4',$elem)
		.live('click',function(){
		        if(!$.browser.msie)$('.active .detail_content',$elem).fadeOut(200);
		        if($.browser.msie)$('.active .detail_content',$elem).hide();
		        
			if(!$.browser.msie)$('.active h4',$elem).fadeTo(200,.3);
			if($.browser.msie)$('.active h4',$elem).css('opacity',.3);
			
			$('.active',$elem).removeClass('active');
			$(this).parents('.detail').addClass('active');
			
			if(!$.browser.msie)$('.active .detail_content',$elem).fadeIn(300);
			if($.browser.msie)$('.active .detail_content',$elem).show();
			});
	  $('.detail.scroll_link h4',$elem)
		.live('click',function(){
			$('#vis_oversikt,#malsatt_tegning').slideDown(1000);
			jQuery('html,body').animate({scrollTop: [jQuery('#thumbnail_navs').offset().top-20,'easeOutExpo']},700);
			});
	  $('.detail_content',$elem).css({
		'position':'absolute',
		'top':config['top'],
		});
	  $('.detail_content:not(".active .detail_content")',$elem).hide();
	  $('.detail:first h4',$elem).fadeTo(0,1);
    });
    return this;
  };

})(jQuery);
function KRABATinit(){

	jQuery(document).ready(function($){
		var deviceAgent = navigator.userAgent.toLowerCase();
		var iOS = deviceAgent.match(/(iphone|ipod|ipad)/);
		if (!iOS) $('ul#main_menu li:not(".active")').activeHoverize({'width':197})
	});

	
	if($('ul#main_menu li.active').hasClass('last'))
		$('ul#main_menu li.active').addBar({'width':195});	
	else
		$('ul#main_menu li.active').addBar({'width':197});	
	// enter/exit: form fields	
	$('input[type=text],input[type=password],textarea').focus(function(){
		if($(this).val()==$(this).attr('title'))$(this).val('');
	}).blur(function(){
		if($(this).val()=='')$(this).val($(this).attr('title'));
	});
	
	$('#social .icon').hover(function(){
		$(this).animate({'padding-bottom':3},200);
		},function(){
		$(this).animate({'pading-bottom':0},100);
		});
	$('#logo').hover(function(){
		$(this).fadeTo(200,.7);
		},function(){
		$(this).fadeTo(100,1);
		});
		
	$('.product').click(function(){
		document.location.href=$('.product_title h3 a',this).attr('href');
		});
	if(!$.browser.msie){
	$('.product').hover(function(){
		$(this).animate({'opacity':0.8},200);
		},function(){
		$(this).animate({'opacity':1},200);
		});

	}
	}
	
function KRABATinnerpage_nyheter(){
	$('#news').newsExpand({
		  'speed':500,
		  'text_more':'Les mer',
		  'text_less':'Lukk'
		});
	}
	
function KRABATinnerpage_news(){
	$('#news').newsExpand({
		  'speed':500,
		  'text_more':'Read more',
		  'text_less':'Close'
		});
	}
	
function KRABATproduct_expandable(){
	$('#product_content_expandable').newsExpand({
		  'speed':500,
		  'text_more':'Les mer',
		  'text_less':'Lukk'
		});
	}
	
function KRABATproduct_expandable_en(){
	$('#product_content_expandable').newsExpand({
		  'speed':500,
		  'text_more':'Read more',
		  'text_less':'Close'
		});
	}
	
function KRABATinnerpage_product(){
	
	$('.video').makeVideoThumb();
	$('#details').createTabs({'tab-width':126});
	$('#vis_oversikt,#malsatt_tegning').slideUp(0);
	$('#product-in-use-1').addClass('active-image');
	$('#product-in-use-1').show();
	$('.image_description:not(".active-image")').hide(0);
	$('.ruler,.no_icon').click(function(){
		//$('.measurement_table').slideUp(0);
		$('#malsatt_tegning').slideDown(1000);
		jQuery('html,body').animate({scrollTop: [jQuery('#malsatt_tegning').offset().top-20,'easeOutExpo']},700);
		return false;
		});
	$('.calculator').click(function(){
		//$('.measurement_table').slideUp(0);
		$('#vis_oversikt').slideDown(1000);
		jQuery('html,body').animate({scrollTop: [jQuery('#vis_oversikt').offset().top-20,'easeOutExpo']},700);
		return false;
		});
	// make image links work
	$('.thumbnail_nav a').makeImageChangerLink({'active-class':'active_thumb'});
	$('.arrow_45 a,.arrow_blue a,.image_crop a,.plus a').makeImageChangerLink();
	
	//submenu
	initialHiding=setTimeout(function(){
	$('#subnav-wrap').slideUp(500,'easeOutQuad');
	},3000);
	if(!$.browser.msie){
	$('#sub_menu li:not(".active")').css({'opacity':.25});
	$('#sub_menu li:not(".active")').hover(function(){
		$(this).animate({'opacity':1});
		},function(){
		$(this).animate({'opacity':.25});
		});
		}
	//show submenu on nav hover
	$('#subnav-wrap').hover(function(){
		initialHiding=clearTimeout(initialHiding);
		});
	$('#produkter').hover(function(){
		initialHiding=clearTimeout(initialHiding);
		$('#subnav-wrap').slideDown(300,'easeOutQuad');
		});
	$('#subnav-wrap').bind('mouseleave',function(){
		$('#subnav-wrap').slideUp(200,'easeOutQuad');
		});
	if(!$.browser.msie){	
	   $('.thumbnail_nav img').hover(function(){
		$(this).animate({'opacity':0.8},200);
		},function(){
		$(this).animate({'opacity':1},200);
		});
        }
	
	}
function KRABATfrontpage(){
	if(window.location.hash=='#produkter'){
		$('#produkter').parent().addClass('active').unbind('mouseleave mouseenter').click(function(){return false});
		$('#highlight-wrap').hide();
		jQuery('html,body').animate({scrollTop: [0,'easeOutExpo']},0);
		}
	else{
		// $('#slide_show').simpleSlideShow();
		$('#produkter').click(function(){
			$(this).parent().addClass('active').addBar().unbind('mouseleave mouseenter');
			$('#highlight-wrap').slideUp(500,'easeOutQuad');
			window.location.hash='#produkter';
			jQuery('html,body').animate({scrollTop: [0,'easeOutExpo']},0);
			return false;
			});
		$('#highlight').flashInSlogans({
		  'speed':1000,
		  'timeout':1000
		});
		
		// setting hash link on Produkter from frontpage
		$('#produkter').attr('href','#produkter');
		
		}
	}

