// JavaScript Document
$(document).ready(function() { // On Ready Functions
	// Get Total Width of all Slides
	var allSlideWidths = 0;
	var imgNum = 0;
	$('.caroSlides .slide').each(function() {
		var theSlideWidth = $(this).width();
		allSlideWidths = allSlideWidths+theSlideWidth;
		imgNum = imgNum+1;
	});
	//
	// Set Caro Variables
	var windowWidth = 788;
	var currentSlide = 0;
	// var slideNum = $('.caroSlides .slide').length; // Get number of 'Slides' - list items
	var slideNum = Math.ceil(allSlideWidths/windowWidth);
	var lastSlide = slideNum-1;
	//
	// Print Results - TEST
	//$('body').append('<p>Total Width: '+allSlideWidths+'</p><p>Total Viewable Sides: '+slideNum+'</p><p>Actual Total Images: '+imgNum+'</p>');
	//
	// Set Caro Slides Holder Width
	//var allSlideWidths = slideNum*slideWidth; // Multiply number of slides by slide width
	$('.caroSlides').css('width',allSlideWidths); // Set slide holder to width of all slide
	//
	// Show Elements
	if ( slideNum > 1 ) {
		$('#nextSlide').show(); // Any Page
		$('#prevSlide').show(); // Any Page
	}
	//
	// Home - Set News & Case Heights
	var theDif;
	var homePageH = $('#pageContent .page.clearfix').height();
	var homeLeftColH = $('#pageContent .page.clearfix .col2Full.floatL').height();
	var homeRightColH = $('#pageContent .page.clearfix .col2Full.floatR').height();
	if ( homePageH > homeLeftColH ) { 
		theDif = (homePageH - $('#homeIntro').outerHeight(true))-20;
		$('#homeNews').css('height',theDif)
	} else if ( homePageH > homeRightColH ) {
		theDif = (homePageH - $('.homeSections').outerHeight(true))-20;
		$('#homeCase').css('height',theDif)
	}
	//$('body').append('<p>PG: '+homePageH+'</p><p>L: '+homeLeftColH+'</p><p>R: '+homeRightColH+'</p>'); // TEST
	//
	// Product Sectors
	$('.sectorShowHide').show(); // Product Sectors
	// System Products
	$('.systemProdInfo').hide();
	//
	// Home Sections Info
	$('.homeSections .homeSectionsImg .title a').toggle(function(event){
		$(this).parent('p').parent('.title').parent('.homeSectionsImg').parent('.homeSections').children('.info').slideDown();
		event.preventDefault();
	},function(event){
		$(this).parent('p').parent('.title').parent('.homeSectionsImg').parent('.homeSections').children('.info').slideUp();
		event.preventDefault();
	});
	//
	// Product Sectors
	var sectorParaHeight = 0;
	$('.col2.floatL .sectorsCopy').each( function(){
		var headerH = $(this).children('h1').innerHeight();
		var paraH = $(this).children('p:first').innerHeight();
		var theHeight = headerH+paraH;
		$(this).css('height',theHeight);
	});
	//
	// Architects Testimonials
	$('#testimonials .txt').hide();
	//
	//	
	$(window).load(function() { // On Page Load Functions
	
		// Set Caro Variable
		var pageCaro = null;
		// Caro Rotate Function
		if ( $('.caroWindow').hasClass('rotate') ){
			pageCaro = window.setInterval(function() {
				if ( currentSlide != lastSlide ) {
					currentSlide = currentSlide+1; // Get Next Slide Number
					var newPos = 0-(currentSlide*windowWidth);
					$('.caroSlides').animate({'left': newPos}, 500);
				} else {
					currentSlide = 0;
					$('.caroSlides').animate({'left': '0px'}, 500);
				}
			},9000);
		}
		$('.caroWindow.rotate').mouseenter(function(){
			clearInterval(pageCaro);
		});
		$('.caroWindow.rotate').mouseleave(function(){
			pageCaro = window.setInterval(function() {
				if ( currentSlide != lastSlide ) {
					currentSlide = currentSlide+1; // Get Next Slide Number
					var newPos = 0-(currentSlide*windowWidth);
					$('.caroSlides').animate({'left': newPos}, 500);
				} else {
					currentSlide = 0;
					$('.caroSlides').animate({'left': '0px'}, 500);
				}
			},9000);
		});
	
		// Home Caro Buttons
		$('#nextSlide').click(function(event){
			if ( currentSlide != lastSlide ) {
				currentSlide = currentSlide+1; // Get Next Slide Number
				var newPos = 0-(currentSlide*windowWidth);
				$('.caroSlides').animate({'left': newPos}, 500);
			}
			event.preventDefault();
		});
		$('#prevSlide').click(function(event){
			if ( currentSlide != 0 ) {
				currentSlide = currentSlide-1; // Get Next Slide Number
				var newPos = 0-(currentSlide*windowWidth);
				$('.caroSlides').animate({'left': newPos}, 500);
			}
			event.preventDefault();
		});
		//
		// Procduct Sector
		$('.sectorShowHide').click(function(event){
			if ( $('.sectorShowHide').hasClass('closed') ) {
				$('.sectorsCopy').css('height','auto');
				$('.sectorShowHide').removeClass('closed');
				$('.sectorShowHide').addClass('open');
				$('.sectorShowHide').text('> Hide text');
			} else if ( $('.sectorShowHide').hasClass('open') ) {
				$('.col2.floatL .sectorsCopy').each( function(){
					var headerH = $(this).children('h1').innerHeight();
					var paraH = $(this).children('p:first').innerHeight();
					var theHeight = headerH+paraH;
					$(this).css('height',theHeight);
				});
				$('.sectorShowHide').removeClass('open');
				$('.sectorShowHide').addClass('closed');
				$('.sectorShowHide').text('> Show more');
			}
			event.preventDefault();
		});
		//
		// System Products 'More' link
		$('.systemProdTitle a.more').click(function(event) {
			$(this).parent('p').parent('.systemProdTitle').children('.systemProdInfo').slideToggle();
			event.preventDefault();
		});
		//
		// Architects Testimonials Show Details
		$('#testimonials .testImg').mouseenter( function(){
			$(this).children('.txt').fadeIn('fast');
		});
		$('#testimonials .testImg').mouseleave( function(){
			$(this).children('.txt').hide();
		});
	});
});
