/* DOCUMENT READY */
$(document).ready(function() {
						   
	// Clearing form fields
	$('input[type=text]').clearMe();
	
	$('.trigger, .triggerto').toggler();
	
	$('.timeline li').timeline();
	$('.timeline').timelineDistribute();
	$(this).selectedProductviews({
		productList		: 'productList',
		productItemList	: 'productItemList',
		productItemList2: 'ProductItemList',
		productItem		: 'productItem'
	});
	
	$('.productMenu>ul>li').removeSelectedClass();
	
	$('#content').adjustBodyHeight();
	
	$('.slider').slider();
	
	$('nav.wwd').wwdmenuclick();
	
	$('.lightboxinline').simplelightbox({
		slbWrapper			: '#slbWrapper',
		slbOverlay			: '#slbOverlay',
		slbOverlayOpacity	: .4,
		slbContent			: '#slbContent',
		slbClose			: '#slbClose',
		slbContentWidth		: 800,
		slbContentHeight	: 'auto'
	});
});

/* WINDOW RESIZE */
$(window).resize(function() {
	$('#content').adjustBodyHeight();
});
/* FUNCTIONS */

// Clearing form fields (YWN)
(function( $ ){
	$.fn.clearMe = function( options ) {

		return this.each(function() {
			// Clear input fields focus function:
			$(this).focus(function() {
				var $input = $(this);
				if ($input.attr('value') === $input.attr('defaultValue')) {
					$input.attr('value', '');	
				}
			});
			// Clear input fields blur function:
			$(this).blur(function () {
				var $input = $(this);
				if ($input.attr('value') === '') {
					$input.attr('value', $input.attr('defaultValue'));	
				}
			});
		});

	};
})( jQuery );

// Open / Close box
(function( $ ){
	$.fn.toggler = function( options ) {
		
		// Default settings here:
		var settings = {
			activeClass	: ".selected",
			iconOpen	: "http://prosjekt.inbusiness.no/www_ik_no/wwwroot/gfx/iconOpen.jpg",
			iconClose	: "http://prosjekt.inbusiness.no/www_ik_no/wwwroot/gfx/iconClose.jpg",
			triggerHeight: 12
		};
	
		return this.each(function() {        
			// merge options with settings
			if ( options ) { $.extend( settings, options ); }
			
			var parentElement = $(this).parent();
			var parentElementHeight = parentElement.height();
			// remove the dot from active class setting:
			var activeClass2 = settings.activeClass.substring(1);
			// the click thing
			$(this).click(function() {
				if ( parentElement.hasClass(activeClass2) ) {
					$(parentElement).animate({height: settings.triggerHeight}, 500, 'swing').removeClass(activeClass2);
					$(this).parent().find('.ocimg').attr('src', settings.iconOpen);
				} else {
					// first close other elements
					$(parentElement).siblings('.selected').each(function(index) {
						$(this).removeAttr('style').removeClass(activeClass2, 1000);
						$(this).parent().find('.ocimg').attr('src', settings.iconOpen);
					});
				var elHeight = $(parentElement).height();
					$(parentElement).css('height', 'auto');
					var trueHeight = $(parentElement).height();
					$(parentElement).css('height', elHeight);
					$(parentElement).animate({height: trueHeight}, 500, 'swing').addClass(activeClass2);
					$(this).parent().find('.ocimg').attr('src', settings.iconClose);
				}
				return false;
			});
		});

	};
})( jQuery );


// Timeline
(function( $ ){
	$.fn.timeline = function( options ) {
		
		// Default settings here:
		var settings = {
			speed : 300
		};
	
		return this.each(function() {        
			// If options exist, lets merge them with our default settings
			if ( options ) { $.extend( settings, options ); }
			// CODE:
			$(this).mouseover(function() {
				$(this).css('color', '#ce0800');
				$(this).find('.bubble, .bubbleimage img').stop(true,true).fadeIn(settings.speed);
			}).mouseleave(function() {
				$(this).css('color', '#090644');
				$(this).find('.bubble, .bubbleimage img').fadeOut(settings.speed / 2);
			});
			
		});
	};
})( jQuery );



(function( $ ){
	$.fn.timelineDistribute = function( options ) {
		
		// Default settings here:
		var settings = {
			speed : 300
		};
	
		return this.each(function() {        
			// If options exist, lets merge them with our default settings
			if ( options ) { $.extend( settings, options ); }
			// CODE:
			var avWidth = $(this).parent().width() - 10; //alert(avWidth)
			var noSections = $(this).children().size(); //alert(noSections)
			var seWidth = avWidth / noSections;
			
			$(this).children().css('width', seWidth);
			
			//center bubble txt box
			var leftmove1 = $(this).find('.bubble').outerWidth() / 2;
			var leftmove2 = seWidth /2;
			var movethis = (leftmove1 - leftmove2) * -1;
			$(this).find('.bubble').css('left', movethis);
			
			//center bubble img box
			$(this).find('.bubbleimage img').each(function() {  
				var imagewidth = $(this).width() / 2;
				var leftmoveb = seWidth /2;
				var movethisto = (imagewidth - leftmoveb) * -1;
				
				
				$(this).parent().css('left', movethisto);
			});
		});
	};
})( jQuery );



(function( $ ){
	$.fn.removeSelectedClass = function( options ) {
		return this.each(function() {
			if ( $(this).find('ul li.selected').length >= 1 ) { $(this).removeClass('selected'); }
		});
	};
})( jQuery );



(function( $ ){
	$.fn.selectedProductviews = function( options ) {
		
		// Default settings here:
		var settings = {
			productList : 'productList',
			productItemList : 'productItemList',
			productItemList2 : 'ProductItemList',
			productItem : 'productItem'
		};
	
		return this.each(function() {        
			// If options exist, lets merge them with our default settings
			if ( options ) { $.extend( settings, options ); }
			// CODE:
			var vCurURL = $(location).attr('href');
			// Check for "productList"
			if ( vCurURL.indexOf(settings.productList) >= 0 ) {
				$(this).find('a[href*="'+settings.productList+'"]').parent().parent().addClass('selected'); }
			// Check for "productItemList"
			if ( vCurURL.indexOf(settings.productItemList) >= 0 ) {
				$(this).find('a[href*="'+settings.productList+'"]').parent().parent().addClass('selected'); }
			if ( vCurURL.indexOf(settings.productItemList2) >= 0 ) {
				$(this).find('a[href*="'+settings.productList+'"]').parent().parent().addClass('selected'); }
			// Check for "productItem"
			if ( vCurURL.indexOf(settings.productItem) >= 0 ) {
				$(this).find('a[href*="'+settings.productList+'"]').parent().parent().addClass('selected'); }
		});
	};
})( jQuery );



(function( $ ){
	$.fn.docs = function( options ) {
		
		// Default settings here:
		var settings = {
			webroot : "",
			imgpath : "/gfx/documentarchive/",
			imgtype : ".gif"
		};
	
		return this.each(function() {        
			// merge options with settings
			if ( options ) { $.extend( settings, options ); }
			// code:
			var filetype = $(this).attr('title').split('.',2)[1];
			var useimage = settings.webroot + '/' + settings.imgpath + filetype + settings.imgtype;
			$(this).css('background', 'url('+useimage+') no-repeat left center')
			
		});
	};
})( jQuery );


(function( $ ){
	$.fn.adjustBodyHeight = function( options ) {
	
		return this.each(function() { 
			// code:
			$(this).removeAttr('style');
			var thisElement = $(this);
			var screenheight = $(window).height();
			
			var headerheight = $('body>header').outerHeight() + 25;
			var contentHeight = $(this).outerHeight();
			var footerHeight = $('body>footer').outerHeight();
			
			var contTotHeight = headerheight + contentHeight + footerHeight;
			
			if ( screenheight > contTotHeight ) {
				$('body footer').css({
					'position'	: 'fixed',
					'bottom'	: 0
				})
				$('.products').css({'padding-bottom': $('footer').height() });
			};
			if ( (contTotHeight + 40) > screenheight ) {
				var screenheight = $(window).height();
				$('body footer').removeAttr('style').css('position', 'inherit');
				$(thisElement).css('padding-bottom', 40);
			};
			
		});
	};
})( jQuery );

(function( $ ){
	$.fn.slider = function( options ) {
	
		return this.each(function() {        
			// merge options with settings
			if ( options ) { $.extend( settings, options ); }
			// code:
			if ( $(this).find('li').hasClass('selected') ) {
				var txtstring = $(this).find('.selected span').text();
				$(this).before('<p id="sliderstring">'+txtstring+'</p>');
			}
			$(this).find('li').click(function() {
				$(this).siblings().removeClass('selected');
				$(this).addClass('selected');
				$(this).parent().parent().find('#sliderstring').remove();
				
					var txtstring = $(this).find('span').text();
					$(this).parent().before('<p id="sliderstring">'+txtstring+'</p>');
			});
			
		});
	};
})( jQuery );


(function( $ ){
	$.fn.wwdmenuclick = function( options ) {
	
		return this.each(function() {        
			// merge options with settings
			if ( options ) { $.extend( settings, options ); }
			// code:
			$(this).find('li').click(function() {
				window.location = $(this).find('a').attr('href');
			});
			
		});
	};
})( jQuery );

// SIMPLE LIGHTBOX (call it on the wrapper wich becomes this)
(function($) {
	$.fn.simplelightbox = function(options) {
		/* settings: */
		var settings = {
			slbWrapper		: '#slbWrapper',
			slbOverlay		: '#slbOverlay',
			slbOverlayOpacity: .7,
			slbContent		: '#slbContent',
			slbClose		: '#slbClose',
			slbContentWidth	: 800,
			slbContentHeight: 'auto'
		};

		if (options) { $.extend(settings, options); } // merges options and settings

		/* code: */
		return this.each(function() {
			$(this).click(function(){
				// wrapper dimensions and make visible:			   
				$(settings.slbWrapper).css({
					width: $(window).width(),
					height: $(window).height()
				}).show();
				// overlay dimensions and fadein visible:
				$(settings.slbOverlay).css({
					width: $(window).width(),
					height: $(window).height(),
					opacity: settings.slbOverlayOpacity
				}).fadeIn(1000);
				// content dimensions:
				$(settings.slbContent).css({
					width: settings.slbContentWidth,
					height: settings.slbContentHeight
				});
				// position content horizontal:
				var movefromleft = $(window).width() * 0.5 - ( $(settings.slbContent).outerWidth() / 2 );
				$(settings.slbContent).css('left', movefromleft);
				// position content vertical:
				if ( $(settings.slbContent).height() > $(window).height() ) {
					//content is higher:
					var movefromtop = $(window).height() * .1;
					scrollTo(0, 0);
					// overlay dimensions (height of overlay according to body:
					$(settings.slbOverlay).css('height', $('body').height());
				} else {
					//content is NOT higher:
					var movefromtop = ( $(window).height() * 0.5 ) - ( $(settings.slbContent).outerHeight() / 2 );
				}
				$(settings.slbContent).css('top', movefromtop);
				// content made visible:
				$(settings.slbContent).fadeIn(1000);
				// click stop
				return false;
			});
			$(settings.slbClose).click(function(){
				// hide stuff
				$(settings.slbOverlay).fadeOut(1000);
				$(settings.slbContent).fadeOut(1);
				// remove styles
				$(settings.slbWrapper).attr('style', '');
				$(settings.slbOverlay).attr('style', '');
				$(settings.slbContent).attr('style', '');
				// click stop
				return false;
			});
		});
	};
})(jQuery);
