var $j = jQuery.noConflict();
$j(document).ready(function() {

	// Ved klikk på artikkelliste på forside, hent den samme artikkel og display i hovedfeltet
	$j(".FrontpageArtikkel .artikkelliste a").live("click", function(e) {
		e.preventDefault();
		var id = $j(this).attr('id');
		var link = '/frontpageArticles.php';
		var params = "articleId="+id;
		$j.ajax({
		   type: "POST",
		   url: link,     // this is the path from above
		   data: params,
			async: false,	
		   success: function(result) {
				$j('div#ajaxres').html(result);
			}
		 });
	})


	// Scroll event handlers
	$j(".flerenyheter .tile-content-start").live("click", function() {
		scrollField = $j(this).next();
		$j(scrollField).scrollTo( '-=565px', 1000, {axis:'x'} );
	});

	$j(".flerenyheter .tile-content-end").live("click", function() {
		scrollFieldR = $j(this).prev();
		$j(scrollFieldR).scrollTo( '+=565px', 600, {axis:'x'} );
	});


	// del en ul liste i to lister
	if ($j(".medlemskapGirDeg li").length) {
		var antallLi = $j(".medlemskapGirDeg li").length;
		var skiftPos = Math.ceil(antallLi/2);
		var splitPoint = $j(".medlemskapGirDeg li:nth-child("+skiftPos+")");
		$j(splitPoint).addClass('last');
		$j(splitPoint).parent().after($j(splitPoint).nextAll().wrapAll('<ul/>').parent());
	}
	
	// del en ul liste i tre lister
	if ($j(".temaKategorier li").length) {
		var antallLi = $j(".temaKategorier li").length;
		var skiftPos = Math.ceil(antallLi/3);
		var splitPoint1 = $j(".temaKategorier li:nth-child("+skiftPos+")");
		var splitPoint2 = $j(".temaKategorier li:nth-child("+(skiftPos*2)+")");
		$j(splitPoint1).addClass('last');
		$j(splitPoint2).addClass('last');
		
		$j(splitPoint1).parent().after($j(splitPoint1).nextAll().wrapAll('<ul/>').parent());
		$j(splitPoint2).parent().after($j(splitPoint2).nextAll().wrapAll('<ul/>').parent().addClass('last'));
	}
	
	$j("#notloggedin a").click(function() {
		var iemode = IEMode();
		if (iemode && iemode == 7) {
			$j("#placeholder-content-no-right").css("position","static");
		}
		$j("#mask").css("display","block");
		$j("#loginform").css("display","block");
	});
	
	$j("a.googlemap").click(function(e) {
		e.preventDefault();
		$j("#mask").css({"display":"block","height":$j(document).height()});
		$j('#mapcontainer').css("visibility","visible");		
	});
	$j("#mask").bind('click', function (event) {
		$j('#mapcontainer').css('visibility','hidden');
		$j('#mask').css('display','none');
	});
	

	$j("#lukk").click(function() {
		$j("#mask").css("display","none");
		$j("#loginform").css("display","none");
	});

	$j("#loggedin.lastned .button a").click(function(e) {
		//e.preventDefault();
		var filename = $j(this).attr('href');
		// php-fila har kode for å tvinge browseren til å vise dialogboks for nedlasting
		window.location = "/download.php?filename"+filename;
	});

	// articlelist_tags.php og articlelist_tema.php: skifte mellom Nyheter, Kalender, Bibliotek
	// - må gjøres dynamisk via jQuery siden det ikke alltid er innhold og 
	// overskriftene skrives ikke alltid ut (inline js var statisk)
	$j("a.switchdisplay").click(function(e) {
        e.preventDefault();
		var clickID = $j(this).attr("id");
		clickID = clickID.substr(4);
		$j(".tabs li.active").removeClass("active");
		$j(this).parent().addClass("active");
		$j(".switchcontent").each(function() {
			$j(this).css("display","none");			
		});

		$j("#focusid"+clickID+".switchcontent").css("display","block");
	});
	
	// Skift mellom "mest leste" og "fra nho" på nyheter forside
	$j(".runtime-cid-110 .small-rsslist h3 a").click(function(e) {
		e.preventDefault();
		$j(this).parents('.small-rsslist').addClass("aktiv");		
		$j(".full-articlelist.relatert.aktiv").removeClass("aktiv");
        var iemode = IEMode();
		if (iemode && iemode == 7) {
			$j(".full-articlelist.relatert").css("height",0);
		}
	});
	
	$j(".runtime-cid-110 .full-articlelist.relatert h2").click(function(e) {
		e.preventDefault();
		$j(this).parents('.relatert').addClass("aktiv");		
		$j(".runtime-cid-110 .small-rsslist.aktiv").removeClass("aktiv");
        var iemode = IEMode();        
		if (iemode && iemode == 7) {
			$j(".full-articlelist.relatert").css("height","16em");
		}
	});
    
    $j(".full-articlelist.dokumenter li.expanded .header").click(function(e) {        
        subcats = $j(this).next();
        pil = $j(this).find('span.pil');
        subcats.toggle('slow', function() {
            $j(pil).toggleClass("open");
        });
    });
    
    /*
        FACEBOOK LIKE event trigger ligger i tema_nyhet.php
    */
    
    // DEL, lagrer klikk i lokal db 
    $j("#a2apage_dropdown").live("click", function(e){
 		var id = $j("div.full-articledisplay").attr('id');
		var type = 'share';
		var link = '/sharestats.php';
		var params = "action=ny&articleId="+id+"&type="+type;
		$j.ajax({
			type: "GET",
			url: link,
			data: params,
			async: false,	
			success: function(result) {
		
        	}
		});
	});
    
    // Send "Ofte søkt på" fraser til søkemotoren ved klikk på dem 
    $j(".sokeord li").click(function(e){
        var sokeord = $j(this).html();
        var sistebokstav = sokeord.indexOf(',');
        if( sistebokstav != -1 ){
            sokeord = sokeord.substr(0,sistebokstav); 
        }    
        window.location.href = "/searchresult/?q="+sokeord;
    })
	        
});

function validate(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
	  //$j("[name=tl_epost]").val('Ugyldig E-post!').css({'background-color':'red','color':'white'});
	  return false;
   }
   return true;
}

function IEMode() {
	engine = null;
	if (window.navigator.appName == "Microsoft Internet Explorer")
	{
	   // This is an IE browser. What mode is the engine in?
	   if (document.documentMode) // IE8 or later
		  engine = document.documentMode;
	   else // IE 5-7
	   {
		  engine = 5; // Assume quirks mode unless proven otherwise
		  if (document.compatMode)
		  {
			 if (document.compatMode == "CSS1Compat")
				engine = 7; // standards mode
		  }
		  // There is no test for IE6 standards mode because that mode  
		  // was replaced by IE7 standards mode; there is no emulation.
	   }
	   // the engine variable now contains the document compatibility mode.
	}
	return engine;
}
