$(document).ready(function() {
    // Check for hash value in URL
    
    $('.default-tag').addClass('active-tag');
    
	var links = $('a.tag-link');
	var innerObj = $('.inner');
    var hash = window.location.toString().match(/#(\S+)/i);
    hash = hash != null ? hash[1] : '';
    
    var href = $('.tag_list li a').each(function() {
        var new_hash = $(this).attr('href').split('/');
        new_hash.pop(); // Remove the empty array item at the end
        new_hash = new_hash.pop();
		
		if ( hash == new_hash ) {
            links.removeClass('active-tag');
            $(this).addClass('active-tag');
            
			var toLoad = '/tag/'+hash+' .inner';
			innerObj.load(toLoad)
	    }
    });

    $('.tag_list li a').live('click', function(event) {
		event.preventDefault();
   		var toLoad = $(this).attr('href')+' .inner';
	    loadContent();
	    links.removeClass('active-tag');
	    $(this).addClass('active-tag');
        
        var new_hash = $(this).attr('href').split('/');
        new_hash.pop(); // Remove the empty array item at the end
        new_hash = new_hash.pop();
	    window.location.hash = new_hash;
	    
        function loadContent() {
	    	innerObj.fadeOut('5').load(toLoad, '', showNewContent)
	    }
	    function showNewContent() {
			innerObj.fadeOut('10');
	    	setTimeout(function() {
				innerObj.fadeIn('5',hideLoader());
			}, 100);
	    }
	    function hideLoader() {
	    	//$('#load').fadeOut('normal');
	    }
	    return false;

    });

    $('li.tag_button a').live('click', function(event){
		event.preventDefault();
   		var toLoad = $(this).attr('href')+' .inner';
	    loadContent();
        var new_hash = $(this).attr('href').split('/');
        new_hash.pop(); // Remove the empty array item at the end
        new_hash = new_hash.pop();
	    window.location.hash = new_hash;
        
        $('.tag_list li a').each(function() {
            var tag_hash = $(this).attr('href').split('/');
            tag_hash.pop(); // Remove the empty array item at the end
            tag_hash = tag_hash.pop();
            
            if ( new_hash == tag_hash ) {
                links.removeClass('active-tag');
                $(this).addClass('active-tag');
            }
        });

	    function loadContent() {
	    	innerObj.fadeOut('5').load(toLoad, '', showNewContent)
	    }
	    function showNewContent() {
			innerObj.fadeOut('5');
	    	setTimeout(function() {
				innerObj.fadeIn('10',hideLoader());
			}, 100);
	    }
	    function hideLoader() {
	    	//$('#load').fadeOut('normal');
	    }
	    return false;

    });
    
    // Fix for news toggle

 
	// TOGGLE SCRIPT
	$(".vanish").hide().css({ width: 460 });
	
	$("a.more-link").live("click", function(event){
		var obj = $(this);
		$("a.more-link").parent('p').css({ margin: 0 });
		obj.parents(".article").find(".vanish").slideDown("normal");
		obj.removeClass('article-closed');
		obj.addClass('article-open');
		obj.text('Dölj');

	});
    // END TOGGLE
 		
	$("a.article-open").live("click", function(event){
 		var obj = $(this);
		obj.parents(".article").find(".vanish").slideUp("normal");
		obj.removeClass('article-open');
		obj.addClass('article-closed');
		obj.text('Visa hela artikeln');
	});

	var max = 0;
	$('.tag_list li em').each(function() {
		if ($(this).width() > max) max = $(this).width();
	});
	$('.tag_list li em').width(max);
	
 		
   }); // END ready function
