var downtown = function() {
	var
	menuButton,
	menuList,
	butikker,
	butikkerIsShowing = false,
	activeButikk = "",
	
	tooltipTemplate = 
		"<strong>%name%</strong> <br />"+
		"<strong>%branch%</strong><br />"+
		"%shorttext% <br />"+
		"%extras%",
	qtipConfig = {
 	   	show: {
			delay: 10,
			when: { event: 'mouseover' },
			effect: { length: 0 }
		},
 	   	position: {
 	   		corner: {
 	   			target: 'rightTop',
 	   			tooltip: 'leftMiddleMiddle'
 	   		},
 	   		adjust: {
 	   			screen: true,
 	   			y: 7,
 	   			x: 5
 	   		}
 	   	},
        style: { 
        	padding: 5,
        	background: '#FFFFFF',
        	color: 'black',
           	textAlign: 'left',
           	border: {
        		width: 3,
        		radius: 3,
        		color: '#444444'
        	},
        	tip: true,
        	name: 'dark'
       },
 	   hide: {
    	   fixed: true,
    	   delay: 40,
    	   effect: { length: 0 }
       }
	},
	
	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");
			}
		});
	},
	
	handleMenuMouseOver = function() {
		$("#menu .button").css("width", "700px");
		menuList.stop(true, false).animate({ 
			top: "0px"
		}, {queue: false, duration: 240});
	},
	
	handleMenuMouseOut = function() {
		menuList.stop(true, false).animate({ 
			top: "-50px"
		}, {queue: false, duration: 640});
		$("#menu .button").css("width", "150px");
	},
	
	toggleButikker = function() {
		butikkerIsShowing = !butikkerIsShowing;
		
		if(butikkerIsShowing)  {
			butikker.show();
		} else {
			butikker.hide();	
		}
		
		$("#butikker .kart-overlay").css("display", "none");
	},
	
	handleKartClick = function() {
		var $this = $(this);
		var overlay = $("#butikker .kart-overlay");
		if(overlay.css("display") == "none" || activeButikk != this.id) {
			overlay.html($("<img />", {src: "/img/" + this.id + "_stor.jpg"}))
						.css("display", "block");
			activeButikk = this.id;
			
		} else {
			overlay.css("display", "none");
		}
	},
	
	init = function() {
		menuButton	= $("#menu .button");
		menuList 	= $("#menu .list-wrapper .content");
		logo	 	= $("#menu-button");
		butikker 	= $("#butikker");
		
		menuButton	.bind("mouseover", handleMenuMouseOver)
					.bind("mouseout", handleMenuMouseOut);
		menuList	.bind("mouseover", handleMenuMouseOver)
					.bind("mouseout", handleMenuMouseOut);
		
		$("#kart1, #kart2").bind("click", handleKartClick);
		
		$("#butikker-button").bind("click", toggleButikker);
		$("#butikker-link").bind("click", toggleButikker);
		$("#frontpage-flash").bind("mouseover", function() {
			var $this = $(this).find(".click");
			if($.browser.msie) {
				$this.css("display", "block");
			} else {
				$this.stop(true, true).fadeIn(400);
			}
            $this.css("z-index", "999");
		});
		$("#frontpage-flash .click").bind("mouseout", function() {
			var $this = $(this);
			if($.browser.msie) {
				$this.css("display", "none");
			} else {
				$this.stop(true, true).fadeOut(200);
			}

		});
		
		$('#butikker li a').each(function()
	    {
	    	var content = tooltipTemplate;
	    	var shopid = this.id.replace("butikk", "");
	    	
	    	content = content.replace("%name%", butikkData[shopid].title);
	    	content = content.replace("%branch%", butikkData[shopid].branch);
	    	content = content.replace("%shorttext%", butikkData[shopid].shorttext);
	    	
	    	var extras = [];
	    	
	    	if(butikkData[shopid].telephone != "") {
	    		extras.push("<strong>Tlf:</strong> " + butikkData[shopid].telephone);
	    	}
	    	if(butikkData[shopid].email != "") {
	    		extras.push("<a href='mailto: " + butikkData[shopid].email + "'>E-post</a>");
	    	}
	    	if(butikkData[shopid].website != "") {
	    		extras.push("<a href='" + butikkData[shopid].website + "' target='_blank'>Nettside</a>");
	    	}
	    	
	    	content = content.replace("%extras%", extras.join(" | "));
	    	qtipConfig.content = content;
	    	$(this).qtip(qtipConfig);
	    	qtipConfig.content = "";
	    });
	}
	;

	return {
		init: init
	};
}();

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