// JavaScript Document

$(document).ready(function() {

	// Clear input fields focus function:
	$('input[type="text"], textarea').clearForm();
	
	$('#btnTwitter').tweetMe({
		windowWidth: 400,
		windowHeight: 300
	});
	
	$('.step').mySteps({
		speed: 300
	});

	// FOOTER MENU - REMOVE SELECTED ELEMENT:
	$('.footermenu li.selected').remove();
	
	var winHeight = $(window).height();
	var usedHeight = $('#top').height() + $('#middle').height() + $('#bottom').height();
	
	if ( winHeight > usedHeight ) {
		var topMiddleHeight = $('#top').height() + $('#middle').height();
		var availableHeight = winHeight - topMiddleHeight;
		$('#bottom footer').css('min-height', availableHeight - 116);
	}
	
	if ( $('#middle .page:has(#front)') ) {
		$('#middle .page').css('paddingBottom', 0);
	}
	
	
	$('.tiplink').openthis({
		showthis: '.tipafriend',
		duration: 300 // Optional, default(300)
	});
	
	$('.subscribelink').openthis({
		showthis: 'footer form',
		duration: 300 // Optional, default(300)
	});
	
});



/* FUNCTION FOR CLEARING FORM FIELDS FOR DEFAULT VALUES */
(function( $ ){
	$.fn.clearForm = 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 );


/*	Slide open on link click

	Call this function using this:
	$('LINKELEMENT').openthis({
		showthis: 'ELEMENTTOSHOW',
		duration: 300 // Optional, default(300)
	});
*/
(function( $ ){
	$.fn.openthis = function( options ) {
		
		// Default settings here:
		var settings = {
			showthis : '',
			duration : 300
		};
	
		return this.each(function() {        
			// If options exist, lets merge them with our default settings
			if ( options ) { $.extend( settings, options ); }
			// CODE:
			$(this).click(function() {
				$(settings.showthis).toggle(settings.duration);
			});
		});
	};
})( jQuery );


/* FUNCTION FOR DISPLAYING SLIDEUP IMAGE OVER BOTTOM MENU */
(function( $ ){
	$.fn.nokkel = function( options ) {
		
		// Default settings here:
		var settings = {
			webroot		: 'www.itools.no',
			element		: '.link',
			image		: '/gfx/noimage.jpg'
		};
	
		return this.each(function() {        
			// If options exist, lets merge them with our default settings
			if ( options ) { $.extend( settings, options ); }
			// CODE:
			
			// add popper
			$(settings.element).parent().before('<img class="popper" alt="" src="' + settings.image + '" />');
			
			// add background
			/*if ($(settings.element).parent().parent().is(':last-child')) {
				$(settings.element).parent().before('<img class="background" alt="" src="' + settings.webroot + '/gfx/bgMenuFooter2.png" />');
			} else {
				$(settings.element).parent().before('<img class="background" alt="" src="' + settings.webroot + '/gfx/bgMenuFooter1.png" />');
			}*/
			
			$(settings.element).mouseover(function() {
				$(this).parent().parent().find('img.popper').stop(true,true).fadeIn(200); // show image
			}).mouseout(function() {
				$(this).parent().parent().find('img.popper').fadeOut(200); // hide image
			});

		});
	};
})( jQuery );

/* FUNCTION FOR ADDING IMAGE TO READMORE LINKS */
(function( $ ){
	$.fn.readMore = function( options ) {
		
		// Default settings here:
		var settings = {
			pathToImage : 'gfx/iconArrow.png',
			altText		: 'Les mer'
		};
		return this.each(function() {        
			// If options exist, lets merge them with our default settings
			if ( options ) { $.extend( settings, options ); }
			// CODE:
			$(this).parent().append('<img src="' + settings.pathToImage + '" alt="' + settings.altText + '" />');
			$(this).parent().addClass('lesMerWrapper');
		});
	};
})( jQuery );


(function( $ ){
	$.fn.mySteps = 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).hover(function() {
				$(this).find('.bubble').stop(true,true).toggle();
			});
			// USE THIS INSTEAD OF ABOVE IF EFFECTS NEEDED
			/*$(this).mouseover(function() {
				$(this).find('.bubble').stop(true,true).fadeIn(settings.speed);
			}).mouseleave(function() {
				$(this).find('.bubble').fadeOut(settings.speed);
			});*/
		});
	};
})( jQuery );


(function( $ ){
	$.fn.tweetMe = function( options ) {
		
		// Default settings here:
		var settings = {
			'windowWidth'	: 575,
			'windowHeight'	: 400
		};
	
		return this.each(function() {        
			// If options exist, lets merge them with our default settings
			if ( options ) { $.extend( settings, options ); }
			// CODE:
		  $(this).click(function(event) {
			var width  = settings.windowWidth,
				height = settings.windowHeight,
				left   = ($(window).width()  - width)  / 2,
				top    = ($(window).height() - height) / 2,
				url    = this.href,
				opts   = 'status=1' +
						 ',width='  + width  +
						 ',height=' + height +
						 ',top='    + top    +
						 ',left='   + left;
			
			window.open(url, 'twitter', opts);
		 
			return false;
		  });
		});
	};
})( jQuery );
(function( $ ){
	$.fn.faceMe = function( options ) {
		
		// Default settings here:
		var settings = {
			'windowWidth'	: 575,
			'windowHeight'	: 400
		};
	
		return this.each(function() {        
			// If options exist, lets merge them with our default settings
			if ( options ) { $.extend( settings, options ); }
			// CODE:
		  $(this).click(function(event) {
			var width  = settings.windowWidth,
				height = settings.windowHeight,
				left   = ($(window).width()  - width)  / 2,
				top    = ($(window).height() - height) / 2,
				url    = this.href,
				opts   = 'status=1' +
						 ',width='  + width  +
						 ',height=' + height +
						 ',top='    + top    +
						 ',left='   + left;
			
			window.open(url, 'facebook', opts);
		 
			return false;
		  });
		});
	};
})( jQuery );


