// JavaScript Document for adda.no (JQUERY)

// google tracking
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-845839-2']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
	


/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

$(document).ready(function(){
	
	if($('#systemgaranti #container div.produktbilde').length)
	{

		$('#systemgaranti #container div.produktbilde').css('cursor','pointer').click(function(){
			
			window.location = $(this).find('a').attr('href');															   
		});
	}
	
	// TABS
	if($('#tabs').length)
	{
		$('#tabs').tabs();
	}
	
	// SLIDE SHOW FIRST PAGE
	if($('#slideshow').length)
	{
		$('#slideshow .slide h1').each(function(){
			
			var self = $(this);
			$(this).next().clone().addClass('rotated').insertAfter(self);
				
		});
		
		$('#slideshow').cycle({ 
			fx:        'fade',
			speed: 		500,
			timeout:	7500,
			pause:		0,
			cleartype: 	1, 
			cleartypeNoBg: true
		});	
	};
	
	
	// KONTAKTSKJEMA
	if($('#kontaktskjema').length)
	{
		$('#kontaktskjema').submit(function(){
											
			var result = true;
			if($('#navn').val().length == 0)
			{
				result = false;
				alert('Ooops! Du har glemt så skrive inn navnet ditt');
				$('#navn').focus();
			}
			else if($('#epost').val().length == 0)
			{
				result = false;
				alert('Ooops! Du har glemt så skrive inn epostadressen din');
				$('#epost').focus();
			}
			else if($('#emne').val().length == 0)
			{
				result = false;
				alert('Ooops! Du har glemt så skrive inn hva henvendelsen gjelder');
				$('#emne').focus();
			}
			else if($('#kommentar').val().length == 0)
			{
				result = false;
				alert('Ooops! Du har glemt så skrive inn en beskjed');
				$('#kommentar').focus();
			}
			
			return result;
		});
		
		if($('#kontaktskjema p.error').length)
		{
			// set class and focus of empty field
			var theError = $('p.error > span').text();
			$('#' + theError).addClass('empty').focus();
			// scroll to form
			var theTarget = $('#kontaktskjema');
			var targetOffset = theTarget.offset().top;
			$('html, body').animate({scrollTop: targetOffset}, 1000);
		}
		
	}
	
	// SAME ADDRESS (BESTILLINGSSKJEMA)
	if($('#same-address').length)
	{
		$('#same-address').click(function(){
										  
			if($(this).is(':checked')){
				$('#firmanavn_faktura').val($('#firmanavn_levering').val()).attr('readonly','readonly');
				$('#adresse_faktura').val($('#adresse_levering').val()).attr('readonly','readonly');
				$('#postnr_faktura').val($('#postnr_levering').val()).attr('readonly','readonly');
				$('#poststed_faktura').val($('#poststed_levering').val()).attr('readonly','readonly');
				$('#kontakt_faktura').val($('#kontakt_levering').val()).attr('readonly','readonly');
				$('#telefon_faktura').val($('#telefon_levering').val()).attr('readonly','readonly');
				$('#epost_faktura').val($('#epost_levering').val()).attr('readonly','readonly');
			}
			else
			{
				$('#firmanavn_faktura').val('').attr('readonly',false);
				$('#adresse_faktura').val('').attr('readonly',false);
				$('#postnr_faktura').val('').attr('readonly',false);
				$('#poststed_faktura').val('').attr('readonly',false);
				$('#kontakt_faktura').val('').attr('readonly',false);
				$('#telefon_faktura').val('').attr('readonly',false);
				$('#epost_faktura').val('').attr('readonly',false);
			}
		  
		  });	
	}
	
	// SEARCH-FORM
	$('#q').focus(function(){
						   
	   // if value is default text 
	   //	- remove default text
	   if($(this).val() == 'Søk etter produkter')
	   {
			$(this).val('');   
	   }
	   $(this).removeClass('subtle');
   
   });
	
	$('#q').blur(function(){
						   
	   // if value is default text or empty 
	   //	- set default text
	   // 	- add subtle class
	   if($(this).val() == 'Søk etter produkter' || $(this).val() == '')
	   {
			$(this).val('Søk etter produkter');   
		   $(this).addClass('subtle');
	   }
   
   });

	// NAVIGATION
	$('#produkter-link > a').click(function(e){e.preventDefault();})
	$('#nav > li').hoverIntent({    
		 sensitivity: 4, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 0, // number = milliseconds for onMouseOver polling interval    
		 over: makeVisible, // function = onMouseOver callback (REQUIRED)    
		 timeout: 750, // number = milliseconds delay before onMouseOut    
		 out: makeInvisible // function = onMouseOut callback (REQUIRED)    
	});
						   
	$('#nav > li ul li').hoverIntent({    
		 sensitivity: 4, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: flyOut, // function = onMouseOver callback (REQUIRED)    
		 timeout: 200, // number = milliseconds delay before onMouseOut    
		 out: flyIn // function = onMouseOut callback (REQUIRED)    
	});
	
	// TABLES
	if($('table').length)
	{
		// stripy table
		$('table tbody tr:even').addClass('like');
		// mouseover effect
		$('table tbody tr').hover(
			function(){
			  $(this).addClass('tr-hover');
			},
			function(){
			  $(this).removeClass('tr-hover');
			}
		);
	}
	
	// open links in new windows
	if($('a[rel="external"]').length) 
	{
		$('a[rel="external"]').each(function(){
			$(this).attr('title',$(this).attr('title') + ' Linken åpnes i et nytt vindu');
		})
		$('a[rel="external"]').click(function(){
			window.open($(this).attr('href'));
			return false;
		});
	}
	
	// set focus to formfield
	if($(".focus").length) 
	{
		$(".focus").focus();
	}
	
	// set focus to formfield
	if($(".empty").length) 
	{
		$(".empty").focus();
	}
	
	// set action for cancel-button
	if($('.cancel').length) 
	{
		$('.cancel').click(function(){
			window.history.back();							
		})
	}
	
	// set action for print-button
	if($('.printbutton').length) 
	{
		$('.printbutton').click(function(){
			window.print();
		})
	}
	
	
});


function makeVisible() {
	$(this).children('ul').show();
}
function makeInvisible() {
	$(this).children('ul').hide();
}

function flyOut() {
	$(this).children('ul').show();
}
function flyIn() {
	$(this).children('ul').hide();
}
