﻿




	/* 			 agrimsrud.com 			 */





$(function(){

	/* ========== Last inn ::selection ========== */

	$("head").append($("<link>").attr({type:'text/css',rel:'stylesheet',href: '/css/css3.css'})); 
	$(".jQsjekk").removeClass("jQsjekk");




	
	/* ========== Bakgrunnsanimasjon ========== */
	/* 		BgPos v 1.02 by Alexander Farkas 	*/

	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});





	/* ========== Bakgrunnseffekt på meny ========== */
	/* 		Inspired by Jonathan Snook, snook.ca 	 */

	$.fn.navigasjon = function($tid){

		if(!$tid){$tid = 300;}

		$(this).not(".aktiv,.kontakt").css({backgroundPosition: "50% 40px"}).bind({
			mouseover:
				function(){ $(this).stop().animate({backgroundPosition:"(50% 0)"},{duration:$tid}); },
			mouseout:
				function(){ $(this).stop().animate({backgroundPosition:"(50% 40px)"},{duration:($tid*1.8)}) },
			click:
				function(){
				//	var $href = $(this).attr("href");					
				//	$("#boks").golink($href);
				//	return false;
				}
		});
	}; $('nav ul li a[href]').navigasjon();





	/* ========== Bakgrunnseffekt på hjem- og kontaktlink ========== */

	$.fn.fikshover = function(img,format){

		$(this).append('<span />');
		$(this).find("span").css({
			opacity:	'0',
			display:	'block',
			width:		'100%',
			height:		'100%',
			position:	'absolute',
			top:		'0',
			'background-image':	'url(/img/'+img+'hover.'+format+')'
		}).bind({
			mouseover:	function(){ $(this).stop().animate({opacity:1},300); },
			mouseout:	function(){ $(this).stop().animate({opacity:0},300); }
		});	
	};
	$("h1 a").fikshover('hjem','jpg');
	$("nav .kontakt").fikshover('kontakt','jpg');





	/* ========== Labels -> Text inputs ========== */

	$("#kontaktform textarea,#kontaktform input[type=text][title]").prev("label").css({display:'none'});
	$('#kontaktform input[type=text][title]').each(function(){

		if($(this).val === ''){
			$(this).val($(this).attr('title')).addClass('inaktiv');
		}
		
		$(this).bind({
			focus:	function(){
				if($(this).val === ''||$(this).val === $(this).attr('title')) {
					$(this).val('').removeClass('inaktiv');
				}
			},
			blur:	function(){
				if($(this).val === ''||$(this).val === $(this).attr('title')) {
					$(this).val($(this).attr('title')).addClass('inaktiv'); 
				}
			}
		});

	});





	/* ========== SimpleModal ========== */

	$('body').append('<div id="modal"></div>');
	$('#modal').load('/kontakt.htm article .kontaktdetaljer');
	$('#modal').css({/*width:400*/});

	$('.kontakt').click(function(e){
		e.preventDefault();
		$("#modal").modal({
			opacity:80,
			onOpen: function (dialog) {
				dialog.overlay.fadeIn('slow', function () {
					//dialog.data.hide();
					dialog.container.fadeIn('slow', function () {
						dialog.data.fadeIn('slow');
					});
				});
			},
			onClose: function (dialog) {
				//dialog.data.slideUp('slow', function () {
					dialog.container.fadeOut('slow', function () {
						dialog.overlay.fadeOut('slow', function () {
							$.modal.close();
						});
					});
				//});
			}
		});
		return false;
	});

});
