var buskerud = function() {
	var
	
	tooltipTemplate = 
			"<strong>%name%</strong> <br />"+
			"<strong>Senter:</strong> %location%<br />"+
			"<strong>%branch%</strong><br />"+
			"%shorttext% <br />"+
			"%extras%"
	,
	
	handleNyhetMouseOver = function() {
		var $that = $(this).parent().find(".bilde");
		var $img = $(this).parent().find("img");
		
		$that.animate({ 
		 	height: "190px",
			top: "-130px"
		}, {queue: false, duration: 140});
		
		$img.animate({ 
			top: "0px"
		}, {queue: false, duration: 140} );
	},
	
	handleNyhetMouseOut = function() {
		var $that = $(this).parent().find(".bilde");
		var $img = $(this).parent().find("img");
		
		$that.animate({ 
		 	height: "60px",
			top: "0px"
		}, {queue: false, duration: 140} );
		
		$img.animate({ 
			top: "-40px"
		}, {queue: false, duration: 140} );
	},
	
	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");
			}
		});
	},
	
	init = function() {
		$("#forside-nyheter .overlay, #forside-nyheter .tekst-wrapper")
				.bind("mouseover", handleNyhetMouseOver)
				.bind("mouseout", handleNyhetMouseOut);

	    $('#butikker .list a').each(function()
	    {
	    	var content = tooltipTemplate;
	    	var shopid = this.id.replace("butikk", "");
	    	
	    	content = content.replace("%name%", buskerudJson[shopid].title);
	    	content = content.replace("%branch%", buskerudJson[shopid].branch);
	    	content = content.replace("%location%", buskerudJson[shopid].location);
	    	content = content.replace("%shorttext%", buskerudJson[shopid].shorttext);
	    	
	    	var extras = [];
	    	
	    	if(buskerudJson[shopid].telephone != "") {
	    		extras.push("<strong>Tlf:</strong> " + buskerudJson[shopid].telephone);
	    	}
	    	if(buskerudJson[shopid].email != "") {
	    		extras.push("<a href='mailto: " + buskerudJson[shopid].email + "'>E-post</a>");
	    	}
	    	if(buskerudJson[shopid].website != "") {
	    		extras.push("<a href='" + buskerudJson[shopid].website + "' target='_blank'>Nettside</a>");
	    	}
	    	
	    	content = content.replace("%extras%", extras.join(" | "));
	    	
	    	$(this).qtip({
	    		content: content,
	     	   	show: {
	    			delay: 10,
	    			when: { event: 'mouseover' },
	    			effect: { length: 0 }
	    		},
	     	   	position: {
	     	   		corner: {
	     	   			target: 'rightTop',
	     	   			tooltip: 'leftMiddleMiddle'
	     	   		},
	     	   		adjust: {
	     	   			screen: true,
	     	   			y: -13,
	     	   			x: 5
	     	   		}
	     	   	},

	            style: { 
	            	padding: 5,
	            	background: '#FFFFFF',
	            	color: 'black',
	               	textAlign: 'left',
	               	border: {
	            		width: 3,
	            		radius: 3,
	            		color: '#CCCCCC'
	            	},
	            	tip: true,
	            	name: 'dark'
	           },
	           
	     	   hide: {
	        	   fixed: true,
	        	   delay: 40,
	        	   effect: { length: 0 }
	           }
	      });
	    });
	    
	    $("#menu li").each(checkCurrent);
	    
	    $("#forside-nyheter .boks").each(function() {
	        var $this = $(this);
	        var link = $this.find("a").attr("href");
	        link = $("<a />").addClass("link").attr("href", link);
	        $this.find(".overlay").append(link);    
	    });
	    
	    if($("#background-image").length > 0) {
	    	$("#background-image").ezBgResize();
	    }

	    $(window).bind("resize", function(){
	    	if($("#background-image").length > 0) {
	    		$("#background-image").ezBgResize();
	    	}
	    });
	}
	;

	return {
		init: init
	};
}();


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