$(document).ready(function() {
	$('.service-info:eq(0)').show();
	$('#service-list li a:eq(0)').addClass('curr');
	$("#service-list li a").hover(
		function(){
			$('#service-list li a').removeClass('curr');
			$(this).addClass('curr');
			target = '#'+$(this).attr('rel');
			$('.service-info').hide();
			$(target).show();
		},
		function () {
			
		}
	);
	
	
});

$(document).ready(function() {
	var current_banner = 1, banner_count = $('#banner_list li').size();
	
	$('#banner_bullets').width(banner_count * 32);
	
	function intfunc() {
		$('#banner_bullets li:eq(' + current_banner + ')').click();
	}
	
	$('#banner_bullets li').click(function() {
	
		clearTimeout(interval);
		$('#banner_bullets li').removeClass('current'); $(this).addClass('current');
		
		bannerIndex = parseInt($(this).attr('rel').replace(/[^0-9]/g, '')) - 1;
		targetMargin = -1 * (bannerIndex * 960);
	
		$('#banner_list').animate( {
			'marginLeft' : targetMargin
		}, 500, function() {
			interval = setTimeout(intfunc, 8000);
		});
	
		current_banner = (current_banner + 1) % banner_count;
	});

	interval = setTimeout(intfunc, 4000);
	}
);


$(document).ready(function() {
	$('.contact-info:eq(0)').show();
	$('#region-list li a:eq(0)').addClass('curr');
	$("#region-list li a").click(
			function(){
				$('#region-list li a').removeClass('curr');
				$(this).addClass('curr');
				target = '#'+$(this).attr('rel');
				$('.contact-info').hide();
				$(target).show();
			},
			function () {
				
			}
	);
	
	
	
	/**
	 * 
	 * Scroller buttons
	 * 
	 */
	
	function needWrap(list,direction) {
		listX = list.offset().left - parseInt(list.css('marginLeft'));
		width = list.parent().width();
		
		var found = false;
		
		if(direction == 'right') {
			$('li',list).each(function() {
				itemX = $(this).offset().left;
				
				if(itemX >= listX + width) {
					found = true;
					return false;
				}
			});
			
		}else{
			$('li',list).each(function() {
				itemX = $(this).offset().left;
				
				if(itemX < listX) {
					found = true;
					return false;
				}
			});
		}
		
		return !found;
	}
	
	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'))));
		
		if(needWrap($scroller,'left')) {
			if($scroller.hasClass('noWrapScroll')) {
				return false;
			}
			console.log('No elements to left');
			itemCount = $('li',$scroller).size();
			
			
			console.log(requiredCount);
						
			if(itemCount > requiredCount) {
				// 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;
				$('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);
					}
				});
				
				console.log('moved element(s)');
			}else{
				return false;
			}
		}
		
		$($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'))));
		
		if(needWrap($scroller,'right')) {
			if($scroller.hasClass('noWrapScroll')) {
				return false;
			}
			
			console.log('No elements to right');
			itemCount = $('li',$scroller).size();
			
			
			console.log(requiredCount + ' : ' + listX);
						
			if(itemCount > requiredCount) {
				// Move all elements from the right of current view to the left, and alter the margin so same item(s) are shown
				$('li',$scroller).each(function() {
					itemX = $(this).offset().left;
					if(itemX < listX) {
						$scroller.append($(this));
						$scroller.css('marginLeft','+=' + distance);
					}
				});
				
				console.log('moved element(s)');
			}else{
				return false;
			}
		}
		
		$($scroller).animate({
				marginLeft: '-='+distance
			},1000
		);
		
		
	});
	
	
	
	
});
