$(function(){
	
	if($('html').hasClass('msie') == true){
		$('div.col3').children('article:eq(2)').addClass('last-child');
	}
	
	
	var $galleryShow = $('#galleryShowHome');
	if($galleryShow.length){
		
		var $gallUl = $('ul', $galleryShow);
		var $galLi = $('li', $gallUl);
		var $galLeft = $('#galleryLeft');
		var $galRight = $('#galleryRight');
		var liWidth = $galLi.width() + parseInt($galLi.css('margin-right'));
		var fullUlWidth = liWidth*$galLi.length;
		var $bbInfo = 	$('#billboardInfo');
		var $photoImg = $('#billboard .largeImage img');
		var $p2 = 		$('p.p2', $bbInfo);
		var $title = 	$('p.title', $bbInfo);
		var $readMore = $('a.readMore', $bbInfo);
		var moveToX = 0;
		var currentML;
		var ulLength = fullUlWidth
		var move = $galleryShow.width() / liWidth;
		var canItMove = false;
		
		var timeToSwitch = 8000; // 3000 = 3 seconds 
		var slideTimer = {};
		
		
		if($galLi.length > 3){
			canItMove = true;	
		}else{
			$galLeft.addClass('inactive');
			$galRight.addClass('inactive');
		}
		
	
		$galLi.eq(0).children('a').addClass('active');
		
		$gallUl.width(fullUlWidth);
		$galLi.children('a').click(function(){
			$.clearTimer(slideTimer);
			var $this = $(this);
			var photo = {
				alt: 		$this.attr('data-alt'),
				image:		$this.attr('data-image'),
				readMore:	$this.attr('data-read-more'),
				title:		$this.attr('data-title'),
				p2:			$this.attr('data-p2')
				
			}
			
			$this.addClass('active').parent().siblings().children('a').removeClass('active');
			$photoImg.attr({'src':photo.image, 'alt': photo.alt});
			$p2.text(photo.p2);
			$title.text(photo.title);
			$readMore.attr('href', photo.readMore);
			return false;
		});
		
		
		$galLeft.click(function(){	
			$.clearTimer(slideTimer);
			if(canItMove){
				canItMove = false;
				$('#galleryShowHome > ul >li:last-child').insertBefore('#galleryShowHome > ul >li:first-child');
				currentML = parseInt($gallUl.css('margin-left')) - liWidth;
				$gallUl.css('margin-left', currentML);
				var moveToX = (currentML+= liWidth);
				
				$gallUl.animate({marginLeft:moveToX},500,function(){canItMove = true;});	
			}
			return false;									
		});
		
		$galRight.click(function(){	
			$.clearTimer(slideTimer);
			if(canItMove){
				canItMove = false;
				currentML = parseInt($gallUl.css('margin-left'));
				var moveToX = (currentML-= liWidth);
				$gallUl.animate({marginLeft:moveToX},500
					,function(){
					currentML = parseInt($gallUl.css('margin-left')) + liWidth;
					$('#galleryShowHome > ul >li:first-child').insertAfter('#galleryShowHome > ul >li:last-child');
					$gallUl.css('margin-left', currentML);
					canItMove = true;
				});
			}
			return false;									
		});
		
		
		
		
		function fullOnSlider(){
			slideTimer = $.timer(timeToSwitch, function() {
				if(canItMove){
					canItMove = false;
					currentML = parseInt($gallUl.css('margin-left'));
					var moveToX = (currentML-= liWidth);
					$gallUl.animate({marginLeft:moveToX},500
						,function(){
						currentML = parseInt($gallUl.css('margin-left')) + liWidth;
						$('#galleryShowHome > ul >li:first-child').insertAfter('#galleryShowHome > ul >li:last-child');
						$gallUl.css('margin-left', currentML);
						canItMove = true;
					});
					
					
					var $this = $($galLi.children('a.active').parent('li').next().children('a'));
					var photo = {
						alt: 		$this.attr('data-alt'),
						image:		$this.attr('data-image'),
						readMore:	$this.attr('data-read-more'),
						title:		$this.attr('data-title'),
						p2:			$this.attr('data-p2')
						
					}
					
					$this.addClass('active').parent().siblings().children('a').removeClass('active');
					$photoImg.attr({'src':photo.image, 'alt': photo.alt});
					$p2.text(photo.p2);
					$title.text(photo.title);
					$readMore.attr('href', photo.readMore);
					
				}	
				fullOnSlider();
			});
		}
		fullOnSlider();
	}
	
	$(window).load(function(){
		$galLi.removeClass('last').css('margin-right','4px');
	});
});
