function slugify(text) {
	text = text.replace(/[^-a-zA-Z0-9,&\s]+/ig, '');
	text = text.replace(/-/gi, "_");
	text = text.replace(/\s/gi, "-");
	return text;
}

$(document).ready(function() {
	
	if ( $('#blogg').length != 0 ) {
		
		$('.post').each(function() {
				
			var id = $(this).attr("id");
			
			if ( $('#'+id+'.post').find('p:first img').length != 0 ) {
				
				var heading = $('#'+id+'.post').children('.heading').clone();
				
				$('#'+id+'.post').children('.heading').remove();
				
				$('#'+id+'.post p:first').after(heading);
				
			}
		
		});
		
	}	
		
	if ( $('#tjanster').length != 0 ) {
		
		$('#tjanster .post.left.col strong').each(function() {								
			var txt = $(this).text();
			txt = txt.trim();
			var slug = slugify(txt);
			$(this).attr('id', slug);			
		});
		
		/*
		
		$('#tjanster-illu a').click(function(){	
			var txt = $(this).text();
			txt = txt.trim();
			var slug = slugify(txt);
			console.log(slug);
			var pos = $('strong#'+slug).offset();
			$(window)._scrollable().scrollTo((pos.top-20), 1000 );
			return false
		});
		
		*/	
	
	}
		
	if ( $('#start').length != 0 ) {
			
		$('#slide-buttons').css({'display':'block'});
		
		$('#start #start-banners .slide:not(.active)').css({'display':'none',opacity:0});
		
		$.preloadCssImages();
	}
	
	if ( $('#aterforsaljare').length != 0 ) {
		
		$('table').before('<p>Sortera efter stad: </p>');
		
		var create = '<select id="stad">';
		$('td.stad').each(function() {
								   
			var city = $(this).text();
												
			if ( $('#stad option[value="'+city+'"]').length == 0 ){
				create += '<option value="'+city+'">'+city+'</option>';
			}
		
		});
		create += '</select>';
		
		$('#aterforsaljare .post p:last').append(create);
		
		var a = new Array(); 
        $("#stad").children("option").each(function(x){ 
        	test = false; 
            b = a[x] = $(this).val(); 
            for (i=0;i<a.length-1;i++){ 
            	if (b ==a[i]) test =true; 
        } 
        if (test) $(this).remove(); 
        });
		
		var soptions = $.makeArray($('select#stad option')).sort(function(a, b) {
			return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
		});
		$('select#stad').html(soptions).prepend('<option value="choose">&ndash;</option>');
		
		$('#stad option:first').attr('selected', 'selected');
		
		$('#stad').change(function() {
								   
			var selected = $('select#stad option:selected').val();
			
			if ( selected != $('select#stad option:first').val() ) {		
				$('td.stad').each(function() {													
					if ( $(this).text() != selected) {
						$(this).parent('tr').hide();
					} else {
						$(this).parent('tr').show();
					}
				
				});			
			} else {
				$('td.stad').each(function() {
					$(this).parent('tr').show();				
				});				
			}
		});
		
	}
	
	if ( $.browser.mozilla ) {
		$('ul.links li').css({'float':'none','margin':'0'});
		$('ul.links').css({'margin':'0 10px','width':940,'-moz-column-count':4,'-webkit-column-count':4,'-moz-column-width':'220px','-webkit-column-width':'220px','-moz-column-gap':'20px','-webkit-column-gap':'20px'});
	}
		
	$('a.arrow').click(function(){	
		var string = $(this).attr('href');
		var contains = string.indexOf('#');
		if ( contains != -1 ) {			
			var mySplitResult = string.split("#");
			var id = '#'+mySplitResult[1];
			var pos = $(id).offset();
			if ( pos.top < 21) {
				var scrollHere = 0;	
			} else {
				var scrollHere = pos.top-20;				
			}
			$(window)._scrollable().scrollTo(scrollHere, 1500 );
			return false
		}
	});	
	
});

$(window).load(function(){  

	/*
	if ( $('.masonry').length != 0 ) {
		$('.utbildningar').masonry({ columnWidth: 330, itemSelector: '.masonry'});	
	}
	*/

	if ( $('#start-banners').length != 0 ) {

		var run = window.setInterval(function(){
		var $active = $(".slide.active");		
		var $next;
								
		if ( $("#slide-1.active").length != 0 ) {
			$next = $('#slide-2');
		} else if ( $("#slide-2.active").length != 0 ) {
			$next = $('#slide-3');
		} else if ( $("#slide-3.active").length != 0 ) {
			$next = $('#slide-1');				
		}
					
		$active.addClass('lastActive');			
						
		$active.css({'z-index':'9'});
		$next.css({opacity: 0.0,'display':'block','z-index':'10'}).addClass("active").animate({opacity: 1.0}, 1000, function(){
			$active.removeClass('active lastActive').css({'z-index':'8','display':'none'}).css({opacity:0});
					if ( $("#slide-2.active").length != 0 ) {
						$('#slide-button-one').removeClass('active');
						$('#slide-button-two').addClass('active');
						$('body').removeClass('one');
						$('body').addClass('two');					
					} else if ( $("#slide-3.active").length != 0 ) {
						$('#slide-button-two').removeClass('active');
						$('#slide-button-three').addClass('active');
						$('body').removeClass('two');
						$('body').addClass('three');	
					} else if ( $("#slide-1.active").length != 0 ) {
						$('#slide-button-three').removeClass('active');
						$('#slide-button-one').addClass('active');
						$('body').removeClass('three');
						$('body').addClass('one');			
					}				
				});
														 
			}, 4000);		
				
			$('a#slide-button-one').click(function(){
				window.clearInterval(run);
				var $active = $(".slide.active");
				$next = $('#slide-1');
				$active.addClass('lastActive').css({'z-index':'9'}).animate({opacity: 0}, 0);
				$next.css({opacity: 0.0,'z-index':'10','display':'block'}).addClass("active").animate({opacity: 1.0}, 0);
				$active.removeClass('active lastActive').css({'z-index':'8','display':'none'});
				$('.slide-button').removeClass('active');
				$('#slide-button-one').addClass('active');
				$('body').removeClass('two');
				$('body').removeClass('three');
				$('body').addClass('one');		
			});
				
			$('a#slide-button-two').click(function(){
				window.clearInterval(run);
				var $active = $(".slide.active");
				$next = $('#slide-2');
				$active.addClass('lastActive').css({'z-index':'9'}).animate({opacity: 0}, 0);
				$next.css({opacity: 0.0,'z-index':'10','display':'block'}).addClass("active").animate({opacity: 1.0}, 0);
				$active.removeClass('active lastActive').css({'z-index':'8','display':'none'});
				$('.slide-button').removeClass('active');
				$('#slide-button-two').addClass('active');
				$('body').removeClass('one');
				$('body').removeClass('three');
				$('body').addClass('two');
			});
				
			$('a#slide-button-three').click(function(){
				window.clearInterval(run);
				var $active = $(".slide.active");
				$next = $('#slide-3');
				$active.addClass('lastActive').css({'z-index':'9'}).animate({opacity: 0}, 0);
				$next.css({opacity: 0.0,'z-index':'10','display':'block'}).addClass("active").animate({opacity: 1.0}, 0);
				$active.removeClass('active lastActive').css({'z-index':'8','display':'none'});
				$('.slide-button').removeClass('active');
				$('#slide-button-three').addClass('active');
				$('body').removeClass('two');
				$('body').removeClass('one');
				$('body').addClass('three');
			});
	}
});
