jQuery(document).ready(function($) {
	$("#def-ill").hide();
    $('#overlay_toggle').click(function(){
	$('#overlay').toggle();
    });
	var illpath = "/images/index/";
	number = Math.floor(Math.random()*5)+1;
	file = "VE_Index_425x200_0"+number+".png";
	$("#def-ill").attr("src",illpath+file).show();
	
	
	// TEMPORARY FRONTPAGE BULLSHIT
	if($("body").is("#front")){
		
		
		$(".introtext").each(function(){
			var $this = $(this);
			$intro = $this.children().clone();
			$ill = $this.next().find(".illustration").clone();
			$this.next().find(".text").prepend($intro);
			$this.css("cursor","pointer").hover(function(){
				$this.addClass("hovered");
			},function(){
				$this.removeClass("hovered");
			});
		});
		
		$("#mainstory").has(".more").css("cursor","pointer").hover(function(){
				$(this).addClass("hovered");
			},function(){
				$(this).removeClass("hovered");
			}).click(function(){
				window.location = $(this).find(".more").attr("href");
			});

		oh = $("#news_section").height()-20;
		$("#oldoverlay").css("height",oh+"px").css("opacity",0);
		nh = $("#main_section").height()-70;
		$("#newoverlay").css("height",nh+"px").css("opacity",0);

		$("#case3,#case4,#case5").each(function(){
			$(this).click(function(){
				$label = $(this).find("h3").text();
				$text  = $(this).find(".text").html();
				$image = $(this).find(".illustration").clone();
				$("#old_label").html($label);
				$("#oldcontent").html($text).append($image);
				$("#oldoverlay").stop().css("display","block");
				$("#oldcontent").columnize({width:280});
				$("#oldoverlay").animate({"opacity":1},500);
			});
		});
		$("#case2").click(function(){
			$label = $(this).find("h3").text();
			$text = $(this).find(".text").html();
			$image = $(this).find(".illustration").clone();
			$("#new_label").html($label);
			$("#newcontent").html($text).append($image);
			$("#newoverlay").stop().css("display","block");
			$("#newcontent").columnize({width:280});
			$("#newoverlay").animate({"opacity":1},500);
		});
		
		$(".overlay .close").attr("title","Klikk for å lukke artikkelen").live("click", function(){
			$(this).parent().filter(":not(:animated)").stop().animate({"opacity":0},200,function(){$(this).css("display","none");});
		});

	} // END TEMPORARY FRONTPAGE BULLSHIT
	
	
	// BODY WORKERS
	if($("body").is("#workers") && !($("#heroes").has(".cms_edit_container").length > 0)){
		
		// Turn off animations on page load
		jQuery.fx.off = true;
		
		// Make filter pretty witout cluttering markup
		// $(".filtersection").prepend("[ ").append(" ]").find("a:not(:last-child)").after(" | ");
		
		// Clone selected heroes
		$("#heroes .hero").clone().prependTo("#selectedheroes");
		
		// Clone and sort sorted heroes
		var sortedHeroes = $("#heroes .hero").clone();
		sortedHeroes.sort(function(a, b){
			var nameA=$(a).find(".name").text().toLowerCase(), nameB=$(b).find(".name").text().toLowerCase();
 			if (nameA < nameB) //sort string ascending
  				return -1;
 			if (nameA > nameB)
  				return 1;
 			return 0; //default return value (no sorting)
		});
		$(sortedHeroes).appendTo("#sortedheroes");
		
		// Masonry init and settings
		var 
		speed = 500,   // animation speed
		$wall = $('#heroes'),
		$selectwall = $('#selectedheroes'),
		$sortwall = $('#sortedheroes'),

		masonryOptions = {         
			singleMode: true, 
			itemSelector: '.hero:not(.invis)',
			animate: true,
			animationOptions: {
				duration: speed,
				queue: false
			}
		};
		
		
		// FRY ON THE BBQ BABY ///////////////////////////
		$(window).bind( 'hashchange', function(e) {
			
			// Did you filter while overlayin?
			if($("#herooverlay").is(":visible")){
				$("#herooverlay").add("#heropopup").fadeOut(100);
			}
			
			// Init the hash
			var url = e.fragment;
			e.fragment = "/"+url;
			if (url == ""){
				url = "init";
			}

			// Current the filter
			$( 'a.current' ).removeClass( 'current' );
			if (url != "init"){
				url && $( 'a[href="#' + url + '"]' ).addClass( 'current' );
				var filter = $("a.current").attr("rel");
			} else {
				url && $( 'a[href="#alle"]' ).addClass( 'current' );
				var filter = url;
			}
			// console.log(filter);
			
			// Show the masons
			$("#main_section").css("visibility","visible");
			
			// Check filter
			switch (filter){
				case "init" :
					$sortwall.hide();
					$selectwall.hide();
					
					$wall.masonry(masonryOptions);
				break;
				case "alle":
					$sortwall.slideUp();
					$selectwall.slideUp();
					
					$wall.slideDown();
					$wall.children('.invis').toggleClass('invis').fadeIn(speed);
					
					$wall.masonry(masonryOptions);
				break;
        case "bergen" : case "stavanger" : case "oslo" : case "administrasjon" : case "autorisert" : case "jurist" : case "regnskapsmedarbeidere" : case "nice" :
					$sortwall.slideUp();
					$selectwall.children(".hero").not(".invis").not("."+filter).toggleClass("invis").hide();
					$wall.children("."+filter).not('.invis').toggleClass("invis").hide();
					
					$selectwall.children("."+filter+'.invis').toggleClass('invis').fadeIn(speed);
					$wall.children('.invis').not("."+filter).toggleClass('invis').fadeIn(speed);
					
					$selectwall.slideDown().masonry(masonryOptions);
					$wall.slideDown().masonry(masonryOptions);
				break;
				case "alfa" :
					$selectwall.slideUp();
					$wall.slideUp();
					
					$sortwall.slideDown();
				break;
				
			}
			// Turn on animations
			jQuery.fx.off = false;
		});

		// Puff puff pass
		$(window).load(function(){
					// Initialize the masons while we hide from plain sight
					$sortwall.masonry(masonryOptions);
					$wall.masonry(masonryOptions);
					$selectwall.masonry(masonryOptions);
					$("#loading").fadeOut(1000,function(){$(this).remove();});
					$(window).trigger( 'hashchange' );
		});
		

		// Dont activate the activate
		$("a.current").live("click", function(){
			return false;
		});

		// OVERLAY FIELDS
		var 
		$overlay		=	$("#herooverlay"  	),
		$popup			=	$("#heropopup"	  	),
		$popupoffice   	=   $("#popupoffice"  	),
		$popupportrait 	=   $("#popupportrait"	),
		$popupinformal 	=   $("#popupinformal"	),
		$popupill      	=   $("#popupill"     	),
		$popupposition 	=   $("#popupposition"	),
		$popupname     	=   $("#popupname"    	),
		$popupdetails  	=   $("#popupinfo" 		),
		$popupcomptext 	=   $("#popupcomptext"	),
		$popupmail     	=   $("#popupmail"    	),
		$popupphone    	=   $("#popupphone"   	);
		
		// Overlay trigger
		$(".hero").css("cursor","pointer").live("click", function(){
			// remap
			$this = $(this);
			// get info
			$thisoffice   =  $this.find(".city").text();
			$thisportrait =  $this.find(".shot").html();
			$thisinformal =  $this.find(".informal").attr("rel");
			$thisill      =  $this.find(".illustration").html();
			$thisposition =  $this.find(".title").text();
			$thisname     =  $this.find(".name").text();
			$thisdetails  =  $this.find(".information").html();
			$thiscomptext =  $this.find(".skills").html();
			$thismail     =  $this.find(".mail").clone();
			$thisphone    =  $this.find(".directphone").text();
			// put info
			$popupoffice  .html($thisoffice  );
			$popupportrait.html($thisportrait);
			$popupinformal.attr("src",$thisinformal);
			$popupill     .html($thisill     );
			$popupposition.html($thisposition);
			$popupname    .html($thisname    );
			$popupdetails .html($thisdetails );
			$popupcomptext.html($thiscomptext);
			$popupmail    .html($thismail    );
			$popupphone   .html("Tlf: " + $thisphone   );
			// fade bg, callback fade overlay
			$overlay.fadeIn(200,function(){
				$("html, body").animate({scrollTop:($("#herofilter").offset().top-10)},500);
				$popup.fadeIn(500);
			});
		});
		
		// Click on overlay to close
		$("#herooverlay").css("cursor","pointer").add("#popupclose").attr("title","Klikk for å lukke").click(function(){
			$("#herooverlay").add("#heropopup").fadeOut(100);
		});
		
		
		// PIC OVERLAY
		$(".shot").hover(function(){
			$(this).find(".toys").stop(true,true).fadeIn(200);
		}, function(){
			$(this).find(".toys").stop(true,true).fadeOut(200);
		});
		
	} else if($("#heroes").has(".cms_edit_container").length > 0){
		$("#loading").remove();
		$("#main_section").css("visibility","visible");
		$("#herofilter").css("visibility","hidden");
		$(".hero").css({"position":"static", "float":"none"});
		$(".heroeslist .shot").css({
			height: "130px",
			left: "280px",
			overflow: "hidden",
			paddingBottom: "0px",
			position: "absolute",
			top: 0
			});
	}
	// BODY WORKERS END
	

	// REFERENCES
	if($("body").is("#references") && !($("#reflist").has(".cms_edit_container").length > 0)){
		
		// Init the masons on window load
		$(window).load(function(){
			$("#loading").fadeOut(1000,function(){$(this).remove();});
			$("#reflist").masonry({singleMode: true}).css({opacity:0,visibility:"visible"}).animate({opacity:1},500);
		}); 
		

		// OVERLAY FIELDS
		var 
		$overlay		=	$("#herooverlay"  	),
		$popup			=	$("#heropopup"	  	),
		$popuptype	 	=   $("#popupoffice"  	),
		$popuplogo	 	=   $("#popuplogo"		),
		$popupname     	=   $("#popupname"    	),
		$popupdetails  	=   $("#popupinfo" 		),
		$popupfield 	=   $("#popupfield"		),
		$popuplink     	=   $("#popupmail"    	);

		// Overlay trigger
		$("#reflist .reference").has(".over").hover(function(){
			$(this).addClass("hovered");
		},function(){
			$(this).removeClass("hovered");
		});
		$("#reflist .reference").has(".fulltext").css("cursor","pointer").bind("click", function(){
			// remap
			$this = $(this);
			// get info
			$thistype     =  $this.find(".type").text();
			$thislogo 	  =  $this.find(".biglogo").attr("src");
			$thisname     =  $this.find(".name").text();
			$thisintro	  =  $this.find(".introtext").html();
			$thisfull	  =  $this.find(".fulltext").html();
			$thisfield	  =  $this.find(".industry").html();
			$thislink     =  $this.find(".link").clone();

			// put info
			$popuptype    .html($thistype     );
			$popuplogo 	 .css("backgroundImage","url("+$thislogo+")" );
			$popupname    .html($thisname     );
			$popupdetails .html($thisintro).append($thisfull);
			$popupfield	 .html($thisfield	 );
			$popuplink    .html($thislink.addClass("mail")     );
			
			// fade bg, callback fade overlay
			$overlay.fadeIn(200,function(){
				$("html, body").animate({scrollTop:($("#main_section").offset().top-10)},500);
				$popup.fadeIn(500);
			});
		});
		
		// Click on overlay to close
		$("#herooverlay").css("cursor","pointer").add("#popupclose").attr("title","Klikk for å lukke").click(function(){
			$("#herooverlay").add("#heropopup").fadeOut(100);
		});
		
	} else if($("#reflist").has(".cms_edit_container").length > 0){
		$("#reflist").css("visibility","visible")
			.find(".cms_edit_connectable").css("float","left").find(":nth-child(5n)").css("clear","both").end().end()
			.find(".cms_edit_container").css("width","840px");
	}
	// REFERENCES END
	
	// Fix sideinfo
	$(".sideinfo:not(.first)").find("h3:first-child").css("borderTop","0");
	
	$("#month_cycle").cycle({
		fx: 'scrollHorz',
  		timeout:  0,
  		prev:    '#prev',
  		next:    '#next'
  	});
});














