var JSONFeed = {};
var bolScrubberDown = false;
var numScroll = 0;
var IE7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;

var Menu = {
    "show" : function (language,mobilebrowser) {
		$.get((language=="_no"?"/xml/info_restaurant_no.xml?language=5":"/xml/info_restaurant.xml"), function (data) {
		    data = data.replace(/\<\!\[CDATA\[/gi, "");
		    data = data.replace(/]]>/gi, "");
		    data = replaceSpecialChars(data)
		    
		    JSONFeed = xml2json.parser(data);

		    if (language != "_no") {
		    	$(".chooseRestaurantButton #btnCity span").html('<a href="'+JSONFeed.config.restaurant.url+'" target="'+JSONFeed.config.restaurant.target+'">'+replaceSpecialChars(JSONFeed.config.restaurant.city)+'</a>');
		    	$(".chooseRestaurantButton #btnStreet span").html('<a href="'+JSONFeed.config.restaurant.url+'" target="'+JSONFeed.config.restaurant.target+'">'+replaceSpecialChars(JSONFeed.config.restaurant.street)+'</a>');
		    } else {
		    	$(".chooseRestaurantButton #btnCity span").html('<a href="#">'+replaceSpecialChars(JSONFeed.config.restaurant.city)+'</a>');
		    	$(".chooseRestaurantButton #btnStreet span").html('<a href="#">'+replaceSpecialChars(JSONFeed.config.restaurant.street)+'</a>');
		    }
		    
		    $(".chooseRestaurantButton #btnCity span").css({"font-size": 22-Math.round(JSONFeed.config.restaurant.city.toString().length/2)});
		    $(".chooseRestaurantButton #btnCity").css({"height": 24-Math.round(JSONFeed.config.restaurant.city.toString().length/2), "padding-top": Math.round(15+Math.round(JSONFeed.config.restaurant.city.toString().length/2)/2)});
		    
		    $(".chooseRestaurantButton #btnStreet span").css({"font-size": 16-Math.round(JSONFeed.config.restaurant.street.toString().length/4)});
		    
		    if (language != "_no") {
			    if (JSONFeed.config.restaurant.openings) {
			    	var openings = "";
			    	$("#openingName span").html(replaceSpecialChars(JSONFeed.config.restaurant.openings.name));
			    	if (JSONFeed.config.restaurant.openings.opening.length) {
				    	$.each(JSONFeed.config.restaurant.openings.opening, function(key, value) {
				    		openings += '<div class="desc"><div class="openingDesc">'+value.day+'</div><div class="openingValue">'+value.time+'</div></div><div class="clear"></div>';
				    	});
			    	} else {
			    		openings += '<div class="desc"><div class="openingDesc">'+JSONFeed.config.restaurant.openings.opening.day+'</div><div class="openingValue"></div></div><div class="clear"></div>';
			    	}
			    	
			    	$("#openingsContainer").html(openings);
			    }
		    } else {
		    	$("#openingsContainer").html("<br />");
		    	$("#openingName").hide();
		    	$("#openingsContainer").hide();
		    	$("#shopMenu .mainContent #infoDesc").css({"margin-bottom": "50px"});
		    }
		    
		    $("#infoName span").html(replaceSpecialChars(JSONFeed.config.restaurant.info.name));
	    	
			var tmpDescription = "";
			if(JSONFeed.config.restaurant.info.description!=0) {
				tmpDescription = replaceSpecialChars(JSONFeed.config.restaurant.info.description);
			}
			
			tmpDescription = tmpDescription.split("/").join("<br />");
			if(JSONFeed.config.restaurant.info.phone_nr.length > 0) {
				var tmpPhone = JSONFeed.config.restaurant.info.phone_nr;
				if(mobilebrowser==true) {
					tmpPhone = tmpPhone.replace(/ /g,'');
					tmpDescription += '<br/>Tlf.: <a href="tel:'+tmpPhone+'">'+tmpPhone+'</a>';
				} else {
					tmpDescription += '<br/>Tlf.: '+tmpPhone;
				}
			}
			
			$("#infoDesc").html(tmpDescription);

			if (JSONFeed.config.restaurant.buttons.button) {
		    	var buttons = "";
		    	
		    	$.each(JSONFeed.config.restaurant.buttons.button, function(key, value) {
		    		if (value.image) {
		    			buttons += '<div class="buttonItem"><a href="'+value.url+'" target="'+value.target+'"><img src="'+value.image+'" alt="" /></a></div>';
		    		}
		    	});
		    	
		    	$(".buttonContainer").html(buttons);
		    }
	    	
	    	if (JSONFeed.config.list) {
		    	var restaurants = "";
		    	var region_count = 0;
				var restaurants_counter = 0;
		    	
		    	if (JSONFeed.config.list.region.length) {
			    	$.each(JSONFeed.config.list.region, function(key, value) {
			    		if (value.name && language != "_se") {
			    			restaurants += '<div class="region'+(region_count==0?' _first':'')+'">'+value.name+'&nbsp;</div>';
			    		} else if (language == "_se") {
			    			restaurants += '<div class="region'+(region_count==0?' _first':'')+'">&nbsp;</div>';
			    		}
			    		
				    	if (value.restaurant) {
				    		$.each(value.restaurant, function(key, value) {
				    			if (value) {
									restaurants_counter++;
				    				restaurants += '<div class="restaurant"><a href="'+value.link+'" target="'+value.target+'">'+value.name+'</a></div>';
				    			}
				    		});
			    		}
			    		
			    		region_count++;
			    	});
		    	} else {
		    		if (language != "_se") {
		    			restaurants += '<div class="region _first">'+(JSONFeed.config.list.region.name.toString() != "[object Object]"?JSONFeed.config.list.region.name:'')+'</div>';
		    		} else {
		    			restaurants += '<div class="region _first">&nbsp;</div>';
		    		}
		    		
		    		if (JSONFeed.config.list.region.restaurant.length) {
			    		$.each(JSONFeed.config.list.region.restaurant, function(key, value) {
			    			if (value) {
								restaurants_counter++;
			    				restaurants += '<div class="restaurant"><a href="'+value.link+'" target="'+value.target+'">'+value.name+'</a></div>';
			    			}
			    		});
		    		} else {
						restaurants_counter++;
		    			restaurants += '<div class="restaurant"><a href="'+JSONFeed.config.list.region.restaurant.link+'" target="'+JSONFeed.config.list.region.restaurant.target+'">'+JSONFeed.config.list.region.restaurant.name+'</a></div>';
		    		}
		    	}
				
				if(restaurants_counter <= 9) {
					$(".scrollbarContainer").css({
						display: "none"
					});
				}
		    	
		    	$(".dropdownContainer .dropdown .dropdownContent .restaurantContainer").html(restaurants);
		    }
	    	
	    	if (!IE7) {
		    	$(".buttonContainer .buttonItem").mouseover( function() {
		    		$(this).find("img").css("opacity", 0.85);
		    	});
		    	
		    	$(".buttonContainer .buttonItem").mouseout( function() {
		    		$(this).find("img").css("opacity", 1);
		    	});
	    	}

	    	if (language == "_se") {
	    		$(".restaurantMask").css({"width": 200});
	    		$(".region").css({"border-bottom": "0px", "padding-bottom": "0px", "padding-top": "0px", "margin-bottom": "0px"});
	    	}
	    	
	    	
	    	$(".chooseRestaurantButton #btnDropdown").click( function() {
	    		if (language == "_no") {
	    			
	    		} else if (language == "_de") {
	    			window.location.href = JSONFeed.config.list.region.restaurant.link;
	    		} else if (language == "_se") {
	    			$(this).parent().toggleClass("buttonOpen");
		    		
		    		if ($(this).parent().hasClass("buttonOpen")) {
		    			$(".scrollbarContainer").css("height", 168);
		    			$(".dropdownContainer .dropdown").css("top", -188);
		    	    	$(".dropdownContainer .dropdown .dropdownContent").css("height", 170);
		    	    	$(".dropdownContainer .dropdown .dropdownContent .restaurantMask").css("height", 170);
		    			$(".dropdownContainer .dropdownMask").css("height", 208);
		    			$(".dropdownContainer .dropdown").stop().animate({"top": 0});
		    		} else {
		    			
		    			$(".dropdownContainer .dropdown").stop().animate({"top": -188}, function() {
		    				$(".dropdownContainer .dropdownMask").css("height", 0);	
		    			});
		    		}
	    		} else {
	    			if (isMobile()) {
	    				$(this).parent().toggleClass("buttonOpen");
	    				$(".scrollbarContainer").hide();
	    				
			    		if ($(this).parent().hasClass("buttonOpen")) {
			    			$(".scrollbarContainer").css("height", 718);
			    			$(".dropdownContainer .dropdown").css("top", -738);
			    	    	$(".dropdownContainer .dropdown .dropdownContent").css("height", 714);
			    	    	$(".dropdownContainer .dropdown .dropdownContent .restaurantMask").css("height", 714);
			    			$(".dropdownContainer .dropdownMask").css("height", 738);
			    			$(".dropdownContainer .dropdown").stop().animate({"top": 0});
			    		} else {
			    			
			    			$(".dropdownContainer .dropdown").stop().animate({"top": -738}, function() {
			    				$(".dropdownContainer .dropdownMask").css("height", 0);	
			    			});
			    		}
	    			} else {
			    		$(this).parent().toggleClass("buttonOpen");
			    		
			    		if ($(this).parent().hasClass("buttonOpen")) {
			    			$(".scrollbarContainer").css("height", ($("#shopMenu").height()-133));
			    			$(".dropdownContainer .dropdown").css("top", -($("#shopMenu").height()-115));
			    	    	$(".dropdownContainer .dropdown .dropdownContent").css("height", ($("#shopMenu").height()-137));
			    	    	$(".dropdownContainer .dropdown .dropdownContent .restaurantMask").css("height", ($("#shopMenu").height()-133));
			    			$(".dropdownContainer .dropdownMask").css("height", $("#shopMenu").height()-115);
			    			$(".dropdownContainer .dropdown").stop().animate({"top": 0});
			    		} else {
			    			
			    			$(".dropdownContainer .dropdown").stop().animate({"top": -($("#shopMenu").height()-115)}, function() {
			    				$(".dropdownContainer .dropdownMask").css("height", 0);	
			    			});
			    		}
	    			}
	    		}
	    	});
	    	
	    	if (!isMobile()) {
		    	$(".chooseRestaurantButton #btnInfo").mouseover( function() {
		    		if (language != "_no") {
		    			$("#infoboxGotoRestaurant").show();
		    		}
		    	});
		    	
			$(".chooseRestaurantButton #btnInfo").mouseout( function() {
				if (language != "_no") {
					$("#infoboxGotoRestaurant").hide();
				}
			});
			
			$(".chooseRestaurantButton #btnDropdown").mouseover( function() {
				if (language != "_no") {
					$("#infoboxChooseRestaurant").show();
				}
			});
			
			$(".chooseRestaurantButton #btnDropdown").mouseout( function() {
				if (language != "_no") {
					$("#infoboxChooseRestaurant").hide();
				}
			});
	    	}
			
			$(".dropdown .dropdownBottom").click( function() {
				$(".chooseRestaurantButton #btnDropdown").click();
			});
	    	
	    	$("#scrubber").mousedown( function(e) {
	    		e.stopPropagation();
	    		bolScrubberDown = true;
	    	});
	    	
	    	$(document).mouseup( function(e) {
	    		e.stopPropagation();
	    		bolScrubberDown = false;
	    	});
	    	
	    	$(".dropdownContainer .restaurantMask").mousewheel(function(event, delta) {
			if (delta > 0) {
				if (numScroll-10 < 1) {
					numScroll = 1; 
				} else {
					numScroll -= 25;
				}
			} else if (delta < 0) {
				if ((numScroll+25)+$("#scrubber").height() > $("#scrubber").parent().height()) {
					numScroll = $("#scrubber").parent().height()-$("#scrubber").height()-1;
				} else {
					numScroll += 25;
				}
			}

			$("#scrubber").css("top", numScroll);
    			$(".dropdownContainer .restaurantContainer").css("top", -($(".dropdownContainer .restaurantContainer").height()-$(".dropdownContainer .restaurantMask").height())/($(".scrollbarContainer").height()-$("#scrubber").height())*$("#scrubber").position().top);
				
			return false;
		});
	    	
	    	$(document).mousemove( function(e) {
	    		e.stopPropagation();
	    		if (bolScrubberDown) {
	    			if (e.pageY-$("#scrubber").parent().offset().top-($("#scrubber").height()/2) < 1) {
	    				numScroll = 1; 
	    			} else if (e.pageY+($("#scrubber").height()/2)-$("#scrubber").parent().offset().top > $("#scrubber").parent().height()-1) {
	    				numScroll = $("#scrubber").parent().height()-$("#scrubber").height()-1;
	    			} else {
	    				numScroll = e.pageY-$("#scrubber").parent().offset().top-($("#scrubber").height()/2);
	    			}
	    			
    				$("#scrubber").css("top", numScroll);
	    			$(".dropdownContainer .restaurantContainer").css("top", -($(".dropdownContainer .restaurantContainer").height()-$(".dropdownContainer .restaurantMask").height())/($(".scrollbarContainer").height()-$("#scrubber").height())*$("#scrubber").position().top);
	    		}
	    		
	    		return false;
	    	});
	    	
	    	$(".scrollbarContainer").mousedown( function(e) {
	    		e.stopPropagation();
    			if (e.pageY-$("#scrubber").parent().offset().top-($("#scrubber").height()/2) < 1) {
    				numScroll = 1; 
    			} else if (e.pageY+($("#scrubber").height()/2)-$("#scrubber").parent().offset().top > $("#scrubber").parent().height()-1) {
    				numScroll = $("#scrubber").parent().height()-$("#scrubber").height()-1;
    			} else {
    				numScroll = e.pageY-$("#scrubber").parent().offset().top-($("#scrubber").height()/2);
    			}
    			
				$("#scrubber").css("top", numScroll);
    			$(".dropdownContainer .restaurantContainer").css("top", -($(".dropdownContainer .restaurantContainer").height()-$(".dropdownContainer .restaurantMask").height())/($(".scrollbarContainer").height()-$("#scrubber").height())*$("#scrubber").position().top);
	    		
	    		return false;
	    	});
	    	
	    	$("#shopMenu").css("visibility", "visible");
	    	Cufon.replace("#infoName span, #openingName span, .chooseRestaurantButton #btnCity span, .chooseRestaurantButton #btnStreet span", { fontFamily: "insigna", hover: true, hoverables: { span: true, div: true, a: true }  });
	    	
		});
    }
};

function isMobile() {
	var index = navigator.appVersion.indexOf("Mobile");
	return (index > -1);
}

function replaceSpecialChars(string) {
	return string.split("Å").join("&Aring;").split("å").join("&aring;").split("Æ").join("&Aelig;").split("æ").join("&aelig;").split("Ø").join("&Oslash;").split("ø").join("&oslash;");
}
