$(document).ready(function(){

	// Remove focus outline on all links when clicked
	$("a").focus(function(){
	  $(this).blur();
	});
	

	$('#menu li').hover(function() {
		$('ul',this).css('display','block');
	},function() {
		$('ul',this).css('display','none');
	});
	
	
	/**
	 * 
	 * Scroller buttons
	 * 
	 */
	
	function needWrap(list,direction) {
		listX = list.offset().left - parseInt(list.css('marginLeft'));
		width = list.parent().width();
		
		var found = 0;
		
		if(direction == 'right') {
			$('li',list).each(function() {
				itemX = $(this).offset().left;
				
				if(itemX >= listX + width) {
					found++;
					// return false;
				}
			});
			
		}else{
			$('li',list).each(function() {
				itemX = $(this).offset().left;
				
				if(itemX < listX) {
					found++;
					// return false;
				}
			});
		}

		return !(found >= requiredCount);
	}
	
	function isVisible(item,list) {
		itemX = item.offset().left;
		listX = list.offset().left - parseInt(list.css('marginLeft'));
		return itemX >= listX;
	}
	
	$('.scroller .prev-link').click(function() {
		$scroller = $('ul',$(this).parent());
		$scroller.stop(false,true);
		
		distance = $scroller.parent().width();
		requiredCount = parseInt(distance / ($('li',$scroller).width() + parseInt($('li',$scroller).css('marginLeft'))));
		itemCount = $('li',$scroller).size();
		
		if(needWrap($scroller,'left')) {
			if($scroller.hasClass('noWrapScroll')) {
				return false;
			}
			// console.log('Not enough elements to left');
						
			if(itemCount >= requiredCount*2) {
				// Move all elements from the right of current view to the left, and alter the margin so same item(s) are shown
				var $lastItem = null;
				var movedCount = 0;
				$('li',$scroller).each(function() {
					itemX = $(this).offset().left;
					if(itemX >= listX + distance) {
						if($lastItem) {
							$lastItem.after($(this));
						}else{
							$scroller.prepend($(this));
						}
						$lastItem = $(this);
						$scroller.css('marginLeft','-=' + (distance/requiredCount));

						movedCount++;
					}
				});
				
				// console.log('moved ' + movedCount + ' element(s)');
			}
		}
		
		/* Only animate if there's anything left to show */
		marginInt = parseInt($scroller.css('marginLeft'));

		if(marginInt < 0) {
			$($scroller).animate({
					marginLeft: '+='+distance
				},1000
			);
		}

	});
	
	
	$('.scroller .next-link').click(function() {
		$scroller = $('ul',$(this).parent());
		$scroller.stop(false,true);
		
		distance = $scroller.parent().width();
		requiredCount = parseInt(distance / ($('li',$scroller).width() + parseInt($('li',$scroller).css('marginLeft'))));
		itemCount = $('li',$scroller).size();
		
		if(needWrap($scroller,'right')) {
			if($scroller.hasClass('noWrapScroll')) {
				return false;
			}
			
			// console.log('No elements to right');
			
			
			// console.log(requiredCount + ' : ' + listX);
						
			if(itemCount >= requiredCount * 2) {
				// Move all elements from the right of current view to the left, and alter the margin so same item(s) are shown
				// var movedCount = 0;
				$('li',$scroller).each(function() {
					itemX = $(this).offset().left;
					// console.log(itemX);
					if(itemX < listX) {
						$scroller.append($(this));
						$scroller.css('marginLeft','+=' + (distance/requiredCount));
						// movedCount++;
					}
				});
				
				// console.log('moved ' + movedCount + ' element(s)');
			}
		}
		
		marginInt = parseInt($scroller.css('marginLeft'));
		widthInt = $('li',$scroller).width() * itemCount * -1;
		// console.log('Margin: ' + marginInt + ' -- Width: ' + widthInt);
		if(marginInt - distance > widthInt) {
			$($scroller).animate({
					marginLeft: '-='+distance
				},1000
			);
		}
	});
	 	
 });

