var ect = function() {
	var
	slideshowCurrentID = 0,
	slideshowNumElements = 0,
	referanserMenu,
    referanserDiv,
    slideshowIntervalInstance,
	currentReferanseCategory = "bygg",
	lastCategory = -1,
	
	checkCurrent = function() {
		var that = $(this);
		
		if(typeof topmenupoint == "undefined") {
			topmenupoint = "none-";
		}
		
		that.find("a").each(function() {
			if($(this).attr("href") == window.location.pathname.toString() || $(this).attr("href").indexOf(topmenupoint) != -1) {
				$(this).addClass("current");
			}
		});
	},
	
	initSlideshow = function() {
		var slideshowDiv = $("#forside-slideshow");
		
		if(slideshowDiv.length == 0) 
			return;
		
		var images = slideshowDiv.find("img");
		
		slideshowNumElements = images.length;
		
		$("#forside-slideshow-menu").empty();
		
		buildSlideshowMenu();
		
		var images = slideshowDiv.html().split(/\s{2,}/mg);
		slideshowDiv.empty();
		
		for(var i = 0; i < images.length; i++) {
			slideshowDiv.append(images[i]);
		}
			    
	    slideshowIntervalInstance = setInterval(handleSlideshowShift, 5000);
	},
	
	initReferanserMenu = function() {
		referanserMenu = $("#referanser-menu");
		referanserDiv = $("#referanser-wrapper");		
		if(referanserMenu.length == 0) {
			return;
		}
		referanserMenu.find(".tabs li").bind("click", handleReferanserTabClick);
		referanserMenu.find(".referanser-category li").bind("click", handleReferanserCategoryClick);
	},
	
	handleReferanserTabClick = function() {
		var $this = $(this);
		
		var referanseCategory = $this.attr("id").replace("referanse-", "");
		var tabs = referanserMenu.find(".tabs li");
		tabs.removeClass("active");
		$this.addClass("active");
		
		var categories = referanserMenu.find(".referanser-category");
		categories.removeClass("active");
		referanserMenu.find("." + referanseCategory).addClass("active");
	},
	
	handleReferanserCategoryClick = function() {
		var idParts = this.id.split("-");
		var id = idParts[1];
		lastCategory = id;
		$(".referanser-category li").css("font-weight", "");
		$(this).css("font-weight", "bold");
		$.get("/referanser/ajax/category/" + id, function(data) {
			referanserDiv.html(data);
			bindAjaxPaging();
		});
	},
		
	bindAjaxPaging = function() {
		$(".paging a").each(function() {
			var $this = $(this);
			var parts = $this.attr("href").split("page=");
			var page = parts[1];
			$this.removeAttr("href");
			$this.parent().bind("click", function() {
				$.get("/referanser/ajax/category/" + lastCategory + "?page=" + page, function(data) {
					referanserDiv.html(data);
					bindAjaxPaging();
				});
			});
		});
	},
	
	buildSlideshowMenu = function() {		
		$("#forside-slideshow-menu").empty();
		
		for(var i = 0; i < slideshowNumElements; i++) {
			if(i == slideshowCurrentID) {
				$("#forside-slideshow-menu").append('<img src="/images/slideshow-button-active.png" />');
			} else {
				$("#forside-slideshow-menu").append('<img src="/images/slideshow-button.png" />');
			}
		}
		
		$("#forside-slideshow-menu img").bind("click", handleSlideshowClick);
	},
	
	handleSlideshowClick = function() {
		var $this = $(this);
		var menuPoints = $this.parent().find("img");
		var posID = 0;
		
		for(var i = 0; i < menuPoints.length; i++) {
			if(this == menuPoints[i]) {
				posID = i;
			}
		}
		slideshowCurrentID = posID;
		clearInterval(slideshowIntervalInstance);
		slideshowGoTo(posID);
	},
	
	slideshowGoTo = function(posID) {
		///$("#slideshow-menu img").removeClass("active");
		//$("#slideshow-menu img").eq(posID).addClass("active");
		var text = $("#forside-slideshow img").eq(posID).attr("title");
		var link = $("#forside-slideshow img").eq(posID).parent().attr("href");
		text = "<a href='" + link + "'>" + text + "</a>"; 
		
		$("#forside-slideshow-text").html(text);
		$("#forside-slideshow-text").removeClass("sIFR-replaced");
		sIFR.replace(centurycothic, {
		  selector: '#forside-slideshow-text'
		  ,css: [
		    '.sIFR-root {text-align: left; color:#222222; font-size:14px; font-weight: bold; } a { color:#222222; text-decoration: none; } a:hover { color:#222222; text-decoration: none; }'
		  ]
		  ,wmode: "transparent"
		});
		
		var left = posID * 602;
		
		$("#forside-slideshow").animate(
			{left: -left + "px"},
			{queue: false, duration: 600}
		);
		
		buildSlideshowMenu();
	},
	
	handleSlideshowShift = function() {
		slideshowCurrentID++;
		
		if(slideshowCurrentID >= slideshowNumElements) {
			slideshowCurrentID = 0;
		}
		
		slideshowGoTo(slideshowCurrentID);
	},
		
	init = function() {
		
		$("#menu li").each(checkCurrent);
		$(".left-col .menu-wrapper li").each(checkCurrent);
	    
		$("#menu li a").bind("mouseover", function() {
			var $this = $(this);
			
			$this.find(".default").css("display", "none");
		});
		
		$("#menu li a").bind("mouseout", function() {
			var $this = $(this);
			
			$this.find(".default").css("display", "block");
		});
		
		initSlideshow();
		initReferanserMenu();
		bindAjaxPaging();	
		slideshowGoTo(0);
	}
	;

	return {
		init: init
	};
}();


$(document).ready(function() {
	ect.init();
});
