$(function() {
	container = $('#shopList');
	if (!container.length) { return; }

/*	
	// Init
	$('.activeOverlay').hide();
	
	// Setupt Containment
	var listHeight = $('#shopList').height();
	$('.list-containment').css("height",listHeight*2 -615);
	$('.list-containment').css("margin-top",-listHeight + 615);
	$('#shopList').css("margin-top", listHeight);
	$('.list-containment').css("position", 'absolute');
	//
*/
	$('.adress').each(function(){
			var colors = new Array('#00afa1','#ee8100','#96b20b','#c4528d','#1cae7c','#bd4fb1','#c288ae','#61a393','#c78d8c','#b6bd6d','#bd7b7a');
			$(this).css('color', colors[Math.floor(Math.random()*colors.length)]);
	});
	
	// List OverOut
	$('#shopList li').mouseover(function(){
		$(this).find('.butiksnamn').css('text-decoration', 'underline');
		$(this).find('.adress').css('text-decoration', 'underline');
	});
	$('#shopList li').mouseout(function(){
		$(this).find('.butiksnamn').css('text-decoration', 'none');
		$(this).find('.adress').css('text-decoration', 'none');
	});

	//NYTT HOVER:
	$('#letterList li').mouseover(function(){
		$('.active').css('color', '#444');
		$('li.active').removeClass('active');
		$(this).addClass('active');
		
		var colors = new Array('#00afa1','#ee8100','#96b20b','#c4528d','#1cae7c','#bd4fb1','#c288ae','#61a393','#c78d8c','#b6bd6d','#bd7b7a');
		$('.active').css('color', colors[Math.floor(Math.random()*colors.length)]);	
		var id = $(this).html();
		if (id == "#") {id = "_";}
		var pos = $('#letter' + id).position();
		if (pos) {
			var scrollTo = pos.top + $('#shopList').scrollTop();
			$('#shopList').scrollTop(scrollTo);  
		}
	});

	$('#letterList li').mouseleave(function(){
		$('.active').css('color', '#444');
		$('.active').removeClass('active');
	});


/*	$('#letterList').mouseleave(function(){
		$('.activeOverlay').hide();
	});
	
	// Mouse Over on Letters
	// Mouse Over and Click on Letters
	function scrollToLetter(){
		var selectedLetter = $('.activeOverlay').text();
		$("#shopList li").each(function(){
			var shopTitle = $(this).find('.butiksnamn').text().charAt(0);
			if(selectedLetter == "#")
			{
				$("#shopList").stop();
				$("#shopList").animate({top: (-listHeight)}, 500, 'easeOutQuad' );
				return false;
			}
			else if(selectedLetter == shopTitle)
			{
				var letterPosition = $(this).position();
				$("#shopList").stop();
				$("#shopList").animate({top: Math.min(Math.max(-listHeight, (-letterPosition.top - 615 - 2)), -615)}, 1000, 'easeOutQuad' );
				return false;
			}
		})
	}
	$('#letterList li').not('.arrowUp').not('.arrowDown').mouseover(function(){
		$('li.active').removeClass('active');
		$(this).addClass('active');
		
		updateActiveLetter();
	})
	$('.activeOverlay').click(function(){
		scrollToLetter();
	})
	$('.arrowDown').click(function(){
		if($('li.active').text() != '#')
		{
			var selectedArray = new Array()
			selectedArray.push($('li.active'));

			$('li.active').next().addClass('active');
			$(selectedArray[0]).removeClass('active');
			updateActiveLetter();
			scrollToLetter();
		}
	});
	$('.arrowUp').click(function(){
		if($('li.active').text() != 'A')
		{
			var selectedArray = new Array()
			selectedArray.push($('li.active'));

			$('li.active').prev().addClass('active');
			$(selectedArray[0]).removeClass('active');
			updateActiveLetter();
			scrollToLetter();
		}
	});
	$('.arrowUp').mouseover(function(){
		var colors = new Array('#00afa1','#ee8100','#96b20b','#c4528d','#1cae7c','#bd4fb1','#c288ae','#61a393','#c78d8c','#b6bd6d','#bd7b7a');
		$(this).css('background-color', colors[Math.floor(Math.random()*colors.length)]);
	})
	$('.arrowUp').mouseout(function(){
		$(this).css('background-color', '#FFF');
	})
	$('.arrowDown').mouseover(function(){
		var colors = new Array('#00afa1','#ee8100','#96b20b','#c4528d','#1cae7c','#bd4fb1','#c288ae','#61a393','#c78d8c','#b6bd6d','#bd7b7a');
		$(this).css('background-color', colors[Math.floor(Math.random()*colors.length)]);
	})
	$('.arrowDown').mouseout(function(){
		$(this).css('background-color', '#FFF');
	})
*/	
	// Toggle Search Layer
	$("#searchOff").mouseover(function () { 
		$(this).css('text-decoration','underline');
		$(this).css('color','#262626');
	});
	$("#searchOff").mouseout(function () { 
		$(this).css('text-decoration','none');
		$(this).css('color','#ae48a3');
	});
	$("#searchOff").click(function () { 
		$("#searchOn").show();
		$(".searchInput").focus();
	});
	$('html').click(function(e){
		e = $(e.target);
		if(!e.is('#searchOff') && !e.is('.searchInput') && !e.is('#searchBtn') && $("#searchOn").is(':visible')){
			$("#searchOn").hide();
	    	$(".searchInput").val("");
	  		updateSearch();
		}
	});
	$(window).keydown(function(event){
  		switch (event.keyCode) {
  			case 27:
  				$("#searchOn").hide();
     			$(".searchInput").val("");
     			updateSearch();
     		break;
  		}
	});
	
	// Filter List
	$(".searchInput").keyup(function () {
	    updateSearch();
	});
/*	
	// Update Active Letter
	function updateActiveLetter() {
		
		$('.activeOverlay').show();
		$('.activeOverlay').css('margin-top', '0px');
		
		var offsetVar = $('li.active').position();
		var heightVar = $('#letterList').height();
		var activePosition = offsetVar.top - heightVar + ($('.activeOverlay').height() + 50)
		
		$('.activeOverlay').css('margin-top', activePosition+'px');
		$('.activeOverlay').html($('li.active').html());
		
		var colors = new Array('#00afa1','#ee8100','#96b20b','#c4528d','#1cae7c','#bd4fb1','#c288ae','#61a393','#c78d8c','#b6bd6d','#bd7b7a');
		$('.activeOverlay').css('color', colors[Math.floor(Math.random()*colors.length)])
	}
	
	// Make list scrollable
	$("#shopList").animate({top: (0 - Math.random()*$("#shopList").height()/2 -615)}, 2000, 'easeOutQuad' );
	$("#shopList").draggable({
		axis: 'y',
		zIndex: 1,
		containment: '.list-containment',

		start: function(){
			$("#shopList").stop();
			startY = $("#shopList").position().top;
		},

		drag : function(){
			$('.activeOverlay').hide();
		},

		stop : function(){
			stopY = $("#shopList").position().top;
			if((stopY - startY) >= 0) {
				$("#shopList").animate({top: (Math.min(Math.max(-listHeight, (stopY + 45)), -615))}, 500, 'easeOutCirc' );
			}else {
				$("#shopList").animate({top: (Math.min(Math.max(-listHeight, (stopY - 45)), -615))}, 500, 'easeOutCirc' );
			}
	}});
*/	
	// Update Search
	function updateSearch() {
		//$("#shopList").animate({top: (0 - 615 - 2)}, 500, 'easeOutQuad' );
		$("#shopList").scrollTop(0);
		var filter = $('.searchInput').val(), count = 0;
	    $("#shopList li").each(function () {
	        if ($(this).find('.butiksnamn').text().search(new RegExp(filter, "i")) < 0) {
	            $(this).addClass("hidden");
	        } else {
	            $(this).removeClass("hidden");
	            count++;
	        }
    	});
	};
	
	var listHeight = $('#shopList ul').height();
	var randStart = Math.floor(Math.random()*listHeight);
	$('#shopList').scrollTop(randStart);
	if (listHeight - randStart < 1150) { var offset = -500 } else { var offset = 500; }
	$('#shopList').animate({ scrollTop: randStart + offset }, 1500, function() {});
	//slump med bšrjan pŒ typ 1000 och negscroll mot A saknas.
});
