
//jQuery onReady

$(document).ready(function(){

/* Adding year to archive headline - jhs@linkfactory.dk - Start */
var pathname = location.pathname.split("/");
if(pathname[1] == "servicenavigation" && pathname[2] == "nyheder" && pathname[3] == "arkiv" && pathname[4] == "archive") {
	$('#content>h1').each(function(){
		var archiveText = $(this).text();
		if(archiveText == "Arkiv") {
			$(this).text("Arkiv "+pathname[5]);
		}
	});
}
/* Adding year to archive headline - jhs@linkfactory.dk - End */

//----
// Accesskeys
//----

$('#accesskeys a').focus(function(){
		$('#accesskeys').attr("style","left: 10px; z-index:999;");
});
$('#accesskeys a').blur(function(){
		$('#accesskeys').attr("style","left: -9999px; z-index:0;");
});

// Carousel - start
	$('.gallery .jcarousel-skin-green').jcarousel({
		scroll: 1,
		itemLastInCallback: mycarousel_itemLastInCallback		
	});
	$('.gallery #mycarousel li:last-child').addClass('jcarousel-item-last');
	
// Drop-down
	$('#dropdown').appendTo('#main');
	
	$('#nav li a').each(function(i){
		$(this).click(function(){
		        $('#nav li').removeClass('over');
			$('#dropdown .dropdown-inner').hide();
			$('#dropdown .dropdown-inner').eq(i).show();
			$(this).parent("li").addClass('over');
		return false;		
		});		
	});
	
	$('#dropdown .dropdown-inner').each(function(i){
		$(this).addClass('no-'+i);		
	});
	
	$('#dropdown .dropdown-inner .close').click(function(){
		$('#dropdown .dropdown-inner').hide();
		$('#nav li').removeClass('over');
		return false;		
	});

//empty input felter on focus, but not if we have a lf_namelists_pi2 form on page
if ($('#tx_lfnamelists_pi2_form').size()==0) {

	$("#searchField").each(function(){
	
	 var ov = $(this).val();
	 $(this).focus(function(){
	 var fv = $(this).val();
	 if (fv == ov) {
	 $(this).val('');
	 }
	 });
	
	 $(this).blur(function(){
	 var bv = $(this).val();
	 if (bv.length < 1) {
	 $(this).val(ov);
	 }
	 });
	});
}
 
$("textarea").each(function(){
 var ov = $(this).val();
 $(this).focus(function(){
 var fv = $(this).val();
 if (fv == ov) {
 $(this).val('');
 }
 });

 $(this).blur(function(){
 var bv = $(this).val();
 if (bv.length < 1) {
 $(this).val(ov);
 }
 });
}); 
		
//----- 	
// Switch stylesheets starts
//-----

var c = readCookie('style');

if (c) {
	
	switchStylestyle(c);
	 
	if (c == "font_11px") {
        	$('.big').each(function(i) {
			$(this).attr({
				rel: "font_12px"
        		});
        	});
        	$('.small').each(function(i) {
			$(this).attr({
				rel: "font_11px"
        		});
        	});
        } else if (c == "font_12px") {
        	$('.big').each(function(i) {
			$(this).attr({
				rel: "font_13px"
        		});
        	});
        	$('.small').each(function(i) {
			$(this).attr({
				rel: "font_11px"
        		});
        	});
        } else if (c == "font_13px") {
        	$('.big').each(function(i) {
			$(this).attr({
				rel: "font_13px"
        		});
        	});
        	$('.small').each(function(i) {
			$(this).attr({
				rel: "font_12px"
        		});
        	});
        }
}
	
$('.big').click(function() {

	var relatt = this.getAttribute("rel");
        
       	switchStylestyle(relatt);
        
        if (relatt == "font_12px") {
		$(this).attr({
			rel: "font_13px"
        	});
        }
        if (relatt == "font_13px") {	
        	$('.small').attr({
			rel: "font_12px"
        	});
	}
        return false;	

}); 	

$('.small').click(function() {

	var relatt = this.getAttribute("rel");
        
       	switchStylestyle(relatt);
        
        if (relatt == "font_12px") {
		$(this).attr({
			rel: "font_11px"
        	});
        }
        if (relatt == "font_11px") {	
        	$('.big').attr({
			rel: "font_12px"
        	});
	}
	
        return false;
}); 	

//-----
// Switch stylesheets ends
//-----

	// set target="_blank" on all links to PDF-files
	$('a[href$=.pdf]').attr('target', '_blank');
	// set target="_blank" on all links to retsinfo
	$('a[href*=retsinfo.dk],a[href*=retsinformation.dk]').attr('target', '_blank');
		
	//show more/less

	$('.more-less').each(function(){
		var e=$(this);
		var fp = e.find('p:first-child'); var te=fp.text(); 
		if (te.length > 380){
			fp.empty();
			$('<span/>').text(te.substring(0,350)).appendTo(fp);
			var dots=$('<span/>').text(' ...').appendTo(fp);
			var mc=$('<span/>').text(te.substring(380)).appendTo(fp).hide();
		} else {
			var mc=$('<span/>');
			var dots=$('<span/>');
			if (e.find('p').size()<=1) e.find('.more').hide();
		}
		
		e.find('p').not(':first-child').hide();
		
		e.find('.more').click(function(){
			if (e.hasClass('expanded')) {
				e.removeClass('expanded');
				mc.hide(); dots.show();
				e.find('p:not(:first-child)').hide();
				$(this).text("Læs mere").attr('title','Læs mere');

			} else {
				e.addClass('expanded');
				mc.show(); dots.hide();
				e.find('p').show();
				$(this).text("Minimer").attr('title','Minimer');
			}
			return false;
		});
	});
	
	$('.file').each(function(){
		if($(this).find('a').length) {
		$(this).show();
		}else {
		$(this).hide();
		}
	});

	// Collapsable items for "Spørgsmål og svar"
	var url=window.location.href;
	var p=url;
	if (url.indexOf('#')!=-1) {
		var anchor=url.substr(url.indexOf('#')+1);
		var url_uid=parseInt(anchor.substr(3));
		p = url.substr(0, url.indexOf('#')); 
	}
	$('.news-list-container.expandable .news-list-item').each(function(){
		var e=$(this);
		var c=e.find('.news-list-content').hide();
		var news_uid=e.attr('news_uid');
		if (news_uid==url_uid) { lf_sporgogsvar_expand (news_uid); }		
		e.find('.news-list-title,.expand').click(function(){ 
			lf_sporgogsvar_expand (news_uid);						
		});
		e.find('.collapse').click(function(){ 
			e.removeClass('expanded');
			c.slideUp(); 
		});			
	});
	
	$('.news-list-most-container .news-list-most-item').each(function(){
		var e=$(this);
		var news_uid=e.attr('news_uid');
		e.find('a[href^=' + location.pathname + ']').click(function(){ 
			lf_sporgogsvar_expand (news_uid);
		});
	});

	// Collapsable items for "Lovgivning"		
	$('.showall').click(function(){ 
		var e=$(this);
		if (e.hasClass('expanded')) {
			$('.news-list-item:not(:first-child)').hide(); 
			$(this).text('Vis al lovgivning'); 
			e.removeClass('expanded');
			$('.show-hide').text('Vis alle');
			$('.end-link').hide();
			$('.law-list-item').removeClass('expanded');
	
		} else {
			$('.news-list-item').show(); 
			$(this).text('Fold al lovgivning sammen'); 
			e.addClass('expanded');
			$('.show-hide').text('Fold');
			$('.end-link').show();
			$('.law-list-item').addClass('expanded');
		}
		return false; 
	});
	$('.law-list-item').each(function(){		
		var e=$(this);
		e.find('.news-list-item:not(:first-child)').hide();
		if (e.find('.news-list-item').size()<2) e.find('.more').hide();
		e.find('.more').click(function(){
			if (e.hasClass('expanded')) {
				e.find('.news-list-item:not(:first-child)').hide();			
				e.removeClass('expanded');
				e.find('.show-hide').text('Vis alle');
				e.find('.end-link').hide();
			}	else {
				e.find('.news-list-item:not(:first-child)').show();
				e.addClass('expanded');
				e.find('.show-hide').text('Fold');
				e.find('.end-link').show();
			}
			return false;
		});
	});
	
}); // End jQuery onReady
function lf_sporgogsvar_expand (news_uid) {
	$('.news-list-container.expandable .news-list-item.expanded').removeClass('expanded').find('.news-list-content').slideUp();
	$('.news-list-container.expandable .news-list-item[news_uid=' + news_uid + ']').addClass('expanded').find(' .news-list-content').slideDown('', function(){ lf_mostpopular_ajaxcounter_count(news_uid); });
}

function lf_mostpopular_ajaxcounter_count (news_uid) {
					var cookie = readCookie('newsread'); if (cookie==null) cookie='';
					if (cookie.indexOf(news_uid)==-1) {
						$.getJSON('index.php?eID=lf_mostpopular_ajaxcounter&news_uid=' + news_uid);
						createCookie('newsread', cookie + news_uid + ',', 1);  // expire after 24h
					}
}

/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 */
function mycarousel_itemLastInCallback(carousel, item, idx, state) {
	// jhs@linkfactory.dk && jens@linkfactory.dk - carousel hack - start
	var width = parseInt($('#mycarousel').css('width'));
	$('#mycarousel').css('width', (width+40)+'px');
	// jhs@linkfactory.dk && jens@linkfactory.dk - carousel hack - end
 };

function buildButtons(carousel, state) {
	// Build buttons
	var carId = carousel.list.context.id;
	var carObj = jQuery('#' + carId);
	var buttonsNo = carousel.options.size;
	var howMany = carousel.options.scroll;
	
	if(buttonsNo > howMany){
	
		// Find containing div, and make a ul inside
		carObj.parents('.jcarousel-skin-green').prepend('<div class="jcarousel-control ' + carId + '"></div>');
		
		// Append controls to div
		for(button = 1; button <= buttonsNo; button = button+howMany){
			jQuery('.' + carId).append('<a href="#"><span>'+(button)+'</span>&nbsp;</a>');
		}
		// Bind click function to buttons
	 	jQuery('.' + carId + ' a').bind('click', function() {
			var scrollTo = jQuery.jcarousel.intval(jQuery(this).text());
			carousel.scroll(scrollTo);
		    return false;
		});
	} else {
		carObj.parents('.jcarousel-skin-green').find('.jcarousel-prev, .jcarousel-next').hide();
	}
}

function updateButtons(carousel, item, idx, state) {
	// Update the position of the buttons
	var position = Math.ceil(idx / carousel.options.scroll);
	var carId = carousel.list.context.id;
	var carObj = jQuery('#' + carId);

	// Find the div, then find controls for that div and then update the active button
	carObj.parents('.jcarousel-skin-green').find('.jcarousel-control a').removeClass('active').eq(position-1).addClass('active');
}


function switchStylestyle(styleName)
{
        $('link[rel*=style][title]').each(function(i)        
        {
                this.disabled = true;
                if (this.getAttribute('title') == styleName) this.disabled = false;
        });
        createCookie('style', styleName, 365);
}

// Ends all jquery



// cookie functions http://www.quirksmode.org/js/cookies.html

function createCookie(name,value,days){
	if (days)	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++){
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name){
	createCookie(name,"",-1);
}

// cookie functions end


