// JavaScript Document
(function($) {
	$(document).ready(function(){
		function sliderResize() {
			var slideshowPosition =  $('#slideshow').offset();
			var startingY = slideshowPosition.top;
			var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();
			sliderHeight = viewportHeight - startingY;
			//viewportWidth = 1152;
			var viewportWidth = window.innerWidth ? window.innerWidth : $(window).width();
			sliderWidth = viewportWidth;
			$('#slidesContainer').css({
				'width' : sliderWidth,
				'height' : sliderHeight
			});
			$('.slide').css({
				'width' : sliderWidth,
				'height' : sliderHeight
			});
			$('.textHolder').css('width',864);
			sliderSetup();
		};
		function imageFullscreenSetup(){
			var scaleX = sliderWidth/1024;
			var scaleY = sliderHeight/590;
			if (scaleX >= scaleY){
				scaleFactor = scaleX;
			}else{
				scaleFactor = scaleY;
			}
			var newWidth = scaleFactor*1024;
			var marginLeft = 0;//(newWidth-864)/2
			//VIsta IE 7 fix
			/*$('.homeFeatureBg img').css({
				'width' : scaleFactor*1024,
				'height' : scaleFactor*590
			});*/
			//$('.homeFeatureBg img').attr("width", scaleFactor*1024);
			//$('.homeFeatureBg img').attr("height", scaleFactor*590);
			$('.attachment-homeBackground').attr("width", scaleFactor*1024);
			$('.attachment-homeBackground').attr("height", scaleFactor*590);

			
			//$('.homeFeatureBg img').css("margin-left", marginLeft);
			//var imgWidth=$('.homeFeatureBg img').width();
		}
		function sliderSetup() {
			homeSliderPosition = 0;
			slideWidthHome = sliderWidth;
			var slides = $('.slide');
			numberOfHomeSlides = slides.length;
			// Remove scrollbar in JS
			$('#slidesContainer').css('overflow', 'hidden');
			// Wrap all .slides with #slideInner div
			// Float left to display horizontally, readjust .slides width
			slides.wrapAll('<div id="slideInner"></div>').css({
				'float' : 'left'
				//,				'width' : slideWidthHome
			});
			// Set #slideInner width equal to total width of all slides
			$('#slideInner').css('width', slideWidthHome * numberOfHomeSlides);
			// Insert left and right arrow controls in the DOM
			$('#slideshow')
				.prepend('<span class="control" id="leftControl">Move left</span>')
				.append('<span class="control" id="rightControl">Move right</span>');
			// Hide left arrow control on first load
			manageControls(homeSliderPosition);
			// Create event listeners for .controls clicks
			$('.control').bind('click', function(){
				slideTimerStop();
				// Determine new position
				homeSliderPosition = ($(this).attr('id')=='rightControl')
					? homeSliderPosition+1 : homeSliderPosition-1;
				// Hide / show controls
				manageControls(homeSliderPosition);
				// Move slideInner using margin-left
				$('#slideInner').animate({
					'marginLeft' : slideWidthHome*(-homeSliderPosition)
				},2500,'easeInOutCubic');
			});
			// manageControls: Hides and shows controls depending on homeSliderPosition
			function manageControls(position){
				// Hide left arrow if position is first slide
				if(position==0){ $('#leftControl').hide() }
				else{ $('#leftControl').show() }
				// Hide right arrow if position is last slide
				if(position==numberOfHomeSlides-1){ $('#rightControl').hide() }
				else{ $('#rightControl').show() }
			}
			imageFullscreenSetup();
			ReadMoreButtonSetup();
			ReadLessButtonSetup();
			slideTimerStart();
		};
		function ReadMoreButtonSetup(){
			$('.more').bind('click', function() {
				slideTimerStop();
				$(this).parent().find('.intro').animate({opacity: 0 },200);
				$(this).animate({ opacity: 0 }, 200, function(){
					$(this).parent().find('.intro').hide();
					$(this).hide();
					$(this).parent().find('.less').show();
					$(this).parent().find('.less').css({ opacity: 1 });
					$(this).parent().parent().find('.introBox').animate({ width: 332, height: 367 }, 1000, function(){
						$(this).parent().find('.moreText').show();
						$(this).parent().find('.moreText').css({ opacity: 1 });
						$(this).parent().find('.moreText').jScrollPane({scrollbarWidth:17});
						$(this).parent().find('.jScrollPaneContainer').show();
						$(this).parent().find('.jScrollPaneContainer').css({ opacity: 1 });
					});
				});
			});
		};
		function ReadLessButtonSetup(){
			$('.less').bind('click', function() {
				$(this).parent().find('.moreText').animate({opacity: 0 },200);
				//if there's a scrollPane hide that too
				$(this).parent().find('.jScrollPaneContainer').animate({opacity: 0 },200);
				$(this).animate({ opacity: 0 }, 200, function(){
					$(this).parent().find('.moreText').hide();
					$(this).parent().find('.jScrollPaneContainer').hide();
					$(this).hide();
					$(this).parent().find('.more').show();
					$(this).parent().find('.more').css({ opacity: 1 });
					//css({'visibility':'visible'});
					$(this).parent().parent().find('.introBox').animate({ width: 586, height: 134 }, 1000, function(){
						$(this).parent().find('.intro').show();
						$(this).parent().find('.intro').animate({opacity: 100 },0);
					});
				});
			});
		};
		function slideTimerStart(){
			slideTimerIntervalId = setInterval(function(){
				homeSliderPosition++;
				if(homeSliderPosition==numberOfHomeSlides-1){
					homeSliderPosition=0;
				};
				// Move slideInner using margin-left
				$('#slideInner').animate({
					'marginLeft' : slideWidthHome*(-homeSliderPosition)
				},2500,'easeInOutCubic');
				// Hide / show controls
				//manageControls(homeSliderPosition);
					// Hide left arrow if position is first slide
					if(homeSliderPosition==0){
						$('#leftControl').hide();
					}else{
						$('#leftControl').show();
					};
					// Hide right arrow if position is last slide
					if(homeSliderPosition==numberOfHomeSlides-2){
						$('#rightControl').hide();
					}else{
						$('#rightControl').show();
					};
			}, 10000);
		};
		function slideTimerStop (){
			clearInterval(slideTimerIntervalId);
		};
		sliderResize();
	});
})(jQuery);
