var slideWidth;
var currentPosition;
var slides;
var numberOfSlides;
var viewportwidth;
var viewportheight;
var feedbackSelect;
feedbackSelect = false;

window.onresize = changeviewport;

// Feedback Button JS

function changeFeedback() {
	viewport();
	
	point = viewportheight / 2;
	topFeedback = point - 325;
	topButton = point - 105;
		
	$('#feedback-form').css('top', topFeedback);
	$('#feedback').css('top', topButton);
	
	if(feedbackSelect == true) {
		
		point = viewportwidth / 2;
		centerFeedback = point - 350;
		centerButton = point + 350;

		$('#feedback-form').css('right', centerFeedback);
		$('#feedback').css('right', centerButton);
	}
}

//

function changeviewport() {
	changeFeedback();
	
	viewport();
	slideWidth = viewportwidth;
	slides = $('.slide');
	numberOfSlides = slides.length;
	
	// Wrap all .slides with #slideInner div
	slides
	.wrapAll('<div id="slideInner"></div>')
	// Float left to display horizontally, readjust .slides width
	.css({
	  'float' : 'left',
	  'width' : slideWidth
	});
	
	
	$('#slideInner').css('width', slideWidth * numberOfSlides);
	$('#slideInner').css('marginLeft', slideWidth*(-currentPosition));
	// 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(currentPosition);

  	// Create event listeners for .controls clicks
 // 	$('.control')
//    .bind('click', function(){
    // Determine new position
//      currentPosition = ($(this).attr('id')=='rightControl')
//    ? currentPosition+1 : currentPosition-1;

      // Hide / show controls
//      manageControls(currentPosition);
      // Move slideInner using margin-left
//      $('#slideInner').animate({
//        'marginLeft' : slideWidth*(-currentPosition)
//      });
//    });

  // manageControls: Hides and shows controls depending on currentPosition
//  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==numberOfSlides-1){ $('#rightControl').hide() }
//    else{ $('#rightControl').show() }
    }	

function viewport() {
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	 else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	 }
	 
	 // older versions of IE
	 else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 }
}


$(document).ready(function(){
	currentPosition = 0;
	changeviewport();
	
	slideheightStart = $("#slide-one").height() +'px' +20;
	$('.slide').css('height', slideheightStart);
	
	// OnClick event for the top menu to position the slider
	$('#one').click(function() {
		currentPosition = 0;
		slideFrame(currentPosition);
		slideheight = $("#slide-one").height() +'px' +20;
		$('.slide').css('height', slideheight);
	});

	$('#two').click(function() {
		currentPosition = 1;
		slideFrame(currentPosition);
		slideheight = $("#slide-two").height() +'px' +20;
		$('.slide').css('height', slideheight);
	});

	$('#three').click(function() {
		currentPosition = 2;
		slideFrame(currentPosition);
		slideheight = $("#slide-three").height() +'px' +20;
		$('.slide').css('height', slideheight);
	});

	$('#four').click(function() {
		currentPosition = 3;
		slideFrame(currentPosition);
		slideheight = $("#slide-four").height() +'px' +20;
		$('.slide').css('height', slideheight);
	});

	$('#five').click(function() {
		currentPosition = 4;
		slideFrame(currentPosition);
		slideheight = $("#slide-five").height() +'px' +20;
		$('.slide').css('height', slideheight);
	});
	
	// Slide animation function
	function slideFrame(currentPosition) {
		$('#slideInner').animate({
			'marginLeft' : slideWidth*(-currentPosition)
		});
	}
	
	
	
	// START JS for testimonials //
	
	tesPosition = 0;
	tesWidth = 270;
	tesslides = $('.teslide');
	tesnumberOfSlides = tesslides.length;
	
	// Remove scrollbar in JS
	$('#testimonialsContainer').css('overflow', 'hidden');
  
	// Wrap all .slides with #slideInner div
	tesslides
	.wrapAll('<div id="tesslideInner"></div>')
	// Float left to display horizontally, readjust .slides width
	.css({
	  'float' : 'left',
	  'width' : tesWidth
	});
	
	$('#tesslideInner').css('width', tesWidth * tesnumberOfSlides);
	$('#tesslideInner').css('marginLeft', tesWidth*(-tesPosition));
	
	$('#tesOne').css('background-image', "url(images/dot-up.png)");
		
	 $('#tesOne').click(function() {
		tesPosition = 0;
		$('#tesOne').css('background-image', "url(images/dot-up.png)");
		removeDot('tesOne');
		tesslideFrame(tesPosition);
	});

	$('#tesTwo').click(function() {
		tesPosition = 1;
		$('#tesTwo').css('background-image', "url(images/dot-up.png)");
		removeDot('tesTwo');
		tesslideFrame(tesPosition);
	});

	$('#tesThree').click(function() {
		tesPosition = 2;
		$('#tesThree').css('background-image', "url(images/dot-up.png)");
		removeDot('tesThree');
		tesslideFrame(tesPosition);
	});

	$('#tesFour').click(function() {
		tesPosition = 3;
		$('#tesFour').css('background-image', "url(images/dot-up.png)");
		removeDot('tesFour');
		tesslideFrame(tesPosition);
	});

	$('#tesFive').click(function() {
		tesPosition = 4;
		$('#tesFive').css('background-image', "url(images/dot-up.png)");
		removeDot('tesFive');
		tesslideFrame(tesPosition);
	});
	
	// Slide animation function
	function tesslideFrame(tesPosition) {
		$('#tesslideInner').animate({
			'marginLeft' : tesWidth*(-tesPosition)
		});
	}
	
	function removeDot(dot) {
		if(dot != 'tesOne') {
			$('#tesOne').css('background-image', "url(images/dot-down.png)"); 
		}
		if(dot != 'tesTwo') {
			$('#tesTwo').css('background-image', "url(images/dot-down.png)"); 
		}
		if(dot != 'tesThree') {
			$('#tesThree').css('background-image', "url(images/dot-down.png)"); 
		}
		if(dot != 'tesFour') {
			$('#tesFour').css('background-image', "url(images/dot-down.png)"); 
		}
		if(dot != 'tesFive') {
			$('#tesFive').css('background-image', "url(images/dot-down.png)"); 
		}
	}
	
	
	// Feedback button JS
	$('#feedback').click(function() {
		if(feedbackSelect == true) {
			feedbackSelect = false;

			$('#feedback').animate({
				'right' : 0
			});
			
			$('#feedback-form').animate({
				'right' : -702
			});
		} else {
			feedbackSelect = true;
			viewport()
			
			point =  viewportwidth / 2;
			centerFeedback = point - 350;
			centerButton = point + 350;

			$('#feedback').animate({
				'right' : centerButton
			});
			
			$('#feedback-form').animate({
				'right' : centerFeedback
			});
		}
	});
	
});
