/* Author: WonderBrand AB */

$(document).ready(function() {
	$('table').attr({
		cellSpacing: "0" // must have camelCase to work
	 });
	
	$("img").error(function(){
	        $(this).hide();
	});
	
	// ==============
	// = File icons =
	// ==============
	
	$("a[href^='mailto']").addClass("mail");
		$("a[href$='.vcf']").addClass("veecard");
		$("a[href$='.pdf']").addClass("pdf");
		$("a[href$='.zip']").addClass("zip");
		$("a[href$='.doc']").addClass("doc");
		$("a[href$='.ppt']").addClass("ppt");
		$("a[href$='.xls']").addClass("xls");
	
	// ===========
	// = Submenu =
	// ===========
		$("#globalnav > li a.hassub").click(function(){
				$(this).next(".submenu-wrapper").slideToggle("fast");
				$(this).parent().toggleClass("active");
				return false;
			});
	
	// ===========
	// = Plugins =
	// ===========
	
	/*--- multiAccordion() – allows multiple items to be opened---*/
	
	(function( $ ){
		$.fn.multiAccordion = function( options ) {
		
		// 		var settings = {};
		// 		if ( options ) {  $.extend( settings, options );  }
		
		return this.click(function() {
			var $this = $(this);
			$this.next().slideToggle("fast");
			$this.hasClass("expanded") ? $this.removeClass("expanded") : $this.addClass("expanded");
			return false;
		}).next().slideToggle();
			
	};
	  
	})( jQuery );
	
	/* -- clickableArea() - extracts a link from inside a parent DOM element, makes entire element hoverable and clickable --*/
	
	(function( $ ){
		$.fn.clickableArea = function() {

		return this.click(function() {
			
			var $this = $(this);
			var linkloc = $this.find("a").attr("href");
			var blank = $this.find("a").attr("target"); /* for external links */
			
			if ( blank ) {
				window.open(linkloc);
			    return false;
			} else if ( !linkloc ) {
				return false;
			} else if ( $this.hasClass("lightbox")) {
				$this.find("a").trigger("click");
			} else {
				window.location=linkloc;
			    return false;
			}
		});
		
	};
	  
	})( jQuery );
	
	/* ------ reveal() - visar text under läs mer ------*/
	
	(function( $ ){
		$.fn.reveal = function() {
			return this.click(function() {
			
			var $this = $(this);
			
			$el = $this;
			$p  = $el.parent();
			$up = $p.parent();
			
			// measure how tall inside should be by adding together heights of all inside paragraphs (except read-more paragraph)
			$up.each(function() {
			        totalHeight += $(this).outerHeight();
			});
			
			$p.css({
				// Set height to prevent instant jumpdown when max height is removed
				"height": $up.height(),
				"max-height": 9999
			})
			.animate({
				"height": totalHeight
			});
			
			// fade out read-more
			$p.fadeOut();
			
			// prevent jump-down
			return false;
			});
			
	};
	  
	})( jQuery );
	
	// ========
	// = Home =
	// ========	
	
	// nyheter accordion
	$('.accordion dt').multiAccordion();
	
	// rollover area
	$("#senaste li").clickableArea();
	$("#projects ol li").clickableArea();
	
	$("#senaste li.link").mouseover(function(){
		$(this).css({
			background: "#f9f9f9",
			cursor:"pointer"
		});
		$(this).find("h4").css("text-decoration", "underline");
		}).mouseout(function(){
			$(this).css({
				background: "transparent",
				cursor:"default"
			});
			$(this).find("h4").css("text-decoration", "none");
		});
		
	    $("#projects ol li").mouseover(function(){
	   					$(this).css({
	   						background: "#f9f9f9",
	   						cursor:"pointer"
	   					});			
	   					}).mouseout(function(){
	   						$(this).css({
	   							background: "transparent",
	   							cursor:"default"
	   						});
	   					});

	if($(".medarbetare li a").length > 0){
			$(".medarbetare li a").fancybox({
				'scrolling'			:	'no',
				'margin'			:	0,
				'padding' 			:	0,
				'height'			:	540,
				'width'				:	900,
				'hideOnContentClick':	false,
				'showNavArrows'		:	true,
				'onStart'			:	function(all, cur, opts){
					window.location.hash = $(".medarbetare li a:eq("+cur+")").attr('data-user');
				}
			});
			
			setTimeout(function(){
				if(window.location.hash != ''){
					var user = $(".medarbetare li").find('a[data-user="'+window.location.hash.substr(1)+'"]');
					user.trigger('click');
				}
			}, 2000);
			
	}

});

