/**
* KSCMS TEMPLATE PLUGINS
*/

// Popup med informasjon om utvikler
jQuery.fn.brand = function(){

	$('a[rel*=ksw_element]').click(function(){
 		$('.ksw_cont').fadeIn('fast');
		return false;
	});
			
	$('a[rel*=ksw_element_close]').click(function(){
		$('.ksw_cont').fadeOut('fast');
		return false;
	});
}

/**
* JQUERY NEWSFLASH ROTATOR
*
* @author	: Kim Sandvold
*/
jQuery.fn.newsflash = function(nf_params){

	nf_settings = $.extend({
		target_cont: '#nf',
		news_limit: 10,
		overlay_color: '#555',
		fade_speed : 'normal',
		interval_speed : 1000

	}, nf_params);
	
	var count = 0;
	var nf_count = 0;
	
	$('#newsflash-overlay').css({backgroundColor: '#ffffff'});

	if(count == 0){
	
		$(nf_settings.target_cont).append('<div id="nf_loader"></div>');
		$.ajax({
		
			url: 'modules/mod_newsflash_ajax.php?',
			data: 'limit='+nf_settings.news_limit+'&width='+nf_settings.max_width+'&count='+nf_settings.news_limit,
			cache: false,
			success: function(nf_result){
			
				$('#nf_loader').remove();		
				$(nf_settings.target_cont).append(nf_result);
				$(nf_settings.target_cont+' img').eq(0).fadeIn(nf_settings.fade_speed, function(){
					get_related_text( $(this).attr('title'), 0);
					init_rotator();
				});
			},
			error: function(){
			
				alert('Finner ingen data. Kontakt webadministrator!');
				
			}
		});
	}
	
	var init_rotator = function(){
		setTimeout(function(){ 
			get_next_case(); 
		}, nf_settings.interval_speed );
	}

	var get_next_case = function(){
		$('.overlay-text').fadeOut('fast');
		$(nf_settings.target_cont+' img').eq(0).fadeOut(nf_settings.fade_speed, function(){
			$(nf_settings.target_cont+' img').eq(1).fadeIn(nf_settings.fade_speed, function(){
			get_related_text( $(this).attr('title'), 1  );
			});
		});
		
		setInterval(function(){ 
		
			nf_count++;
			if(nf_count <= (nf_settings.news_limit -2)){
			$('.overlay-text').fadeOut('fast');
				$(nf_settings.target_cont+' img').eq(parseInt(nf_count)).fadeOut(nf_settings.fade_speed, function(){
					$(nf_settings.target_cont+' img').eq(parseInt(nf_count + 1)).fadeIn(nf_settings.fade_speed, function(){
					
						get_related_text( $(this).attr('title'), parseInt(nf_count + 1)  );
					});
				});
			
			}else{
			$('.overlay-text').fadeOut('fast');
				$(nf_settings.target_cont+' img').eq(parseInt(nf_count)).fadeOut(nf_settings.fade_speed, function(){
					$(nf_settings.target_cont+' img').eq(0).fadeIn(nf_settings.fade_speed, function(){
					
						get_related_text( $(this).attr('title') , 0 );
					});
				});
				nf_count = -1;
				
			}
		}, nf_settings.interval_speed );
		
	}

	/*
	$('a[rel*=newsflash_nav]').live('click', function(){
		count++;
		
		direction = $(this).attr('href').substring(1);
	
		
		dir = (direction == 'back') ? parseInt(count -1): count;
		$('.overlay-text').fadeOut(nf_settings.fade_speed);
		
		$(nf_settings.target_cont+' img').eq(parseInt(dir -1)).fadeOut(nf_settings.fade_speed, function(){
			$(nf_settings.target_cont+' img').eq(parseInt(dir)).fadeIn(nf_settings.fade_speed, function(){
				get_related_text( $(this).attr('title') );
			});
		});
		return false;
	});
	*/
	
	var get_related_text = function(current_element, current_count){
		$('.newsflash-overlay').css({'background': '#000000', opacity: 0.5});
		text = current_element.split('-/@-');
	
		$('.overlay-text').html('<div class="nf_heading">'+text[0]+'</div><div class="nf_ingress">'+text[1].substring(0, 160)+'</div>').fadeIn(nf_settings.fade_speed);
		
	}
	
	
}

// Når man er logget inn fungerer dette på brukermenyen
jQuery.fn.accordion = function(options){

	settings = $.extend({
		affect_id: '#list',
		speed: 'normal'
	}, options);
	
	$(settings.affect_id+' ul').hide();
	$(settings.affect_id+' li a').click(function() {
				
		var checkElement = $(this).next();
	
		if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			return false;
		}
		
		if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			$(settings.affect_id+' ul:visible').slideUp(settings.speed);
			checkElement.slideDown(settings.speed);
			return false;
		}
	});
}

// Animere alle Hash
jQuery.fn.slideHash = function(i){

	settings = $.extend({
		speed: 'normal'
	}, i);
	
	$('a[href*=#]').click(function() {
	
		if(location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {	
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			
			if ($target.length) {	
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, settings.speed);
				return false;
			}
		}	
	});
}

// Tooltip
jQuery.fn.tooltip = function(tt_opt){

	tt_settings = $.extend({
		xOffset: 10,
		yOffset: 10
	}, tt_opt);
	

	$("a.tooltip").hover( function(e){	

		splitted_title = this.title.split('-');
		this.t = splitted_title[0];
		this.title = "";
													  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - tt_settings.xOffset) + "px")
			.css("left",(e.pageX + tt_settings.yOffset) + "px")
			.fadeIn("fast");		
	},
	function(){
		splitted_title = this.title.split('-');
		this.t = splitted_title[0];		
		$("#tooltip").remove();
	});	
	$("a.tooltip").mousemove(function(e){
		
		$("#tooltip")
			.css("top",(e.pageY - tt_settings.xOffset) + "px")
			.css("left",(e.pageX + tt_settings.yOffset) + "px");
	});
}



