//depends on Sugar.js
$(function(){
	if(!$('.carousel').toArray().isEmpty()){
		
	window.carouselSettings = {};
	window.carouselStar = function( options ) {

		var setCarouselDimensions = function(){
			$(".carousel-item").css("width", window.carouselSettings.width);
		};

		var slideCarousel = function(){
			currentCarouselItem = window.carouselSettings.currentItem;
			var item = $(".carousel .nav li a[data-id="+currentCarouselItem+"]");
			$(".carousel-items").animate({
				left: (-1 * currentCarouselItem * window.carouselSettings.width)
			}, "slow", function(){
				item.parent().parent().find(".active").removeClass("active");
				item.addClass("active");
			});
		};

		var autoSlideCarousel = function(){
			(window.carouselSettings.currentItem < $(".carousel .nav A").length) ? window.carouselSettings.currentItem++ : window.carouselSettings.currentItem = 0;
			if (window.carouselSettings.currentItem ==  $(".carousel .nav A").length) {window.carouselSettings.currentItem = 0};
			slideCarousel();
		};

		var init = function(options){
				window.carouselSettings = {
					'width'         : $(".carousel").css("width").toNumber(),
					'currentItem'   : 0,
					'refreshIntervalId' : undefined,
					'carouselInterval' : 7000
				};

				if ( options ) { 
					$.extend( window.carouselSettings, options );
				}

				setCarouselDimensions();
				window.carouselSettings.refreshIntervalId = window.setInterval(autoSlideCarousel,  window.carouselSettings.carouselInterval);

				$(".carousel .nav A").click(function(){
					window.carouselSettings.currentItem = $(this).attr("data-id").toNumber();
					slideCarousel();
				});

				$(".carousel").hover(
					function(){
						window.carouselSettings.refreshIntervalId = window.clearInterval(window.carouselSettings.refreshIntervalId);
					},
					function(){
						window.clearInterval(window.carouselSettings.refreshIntervalId);
						window.carouselSettings.refreshIntervalId = window.setInterval(autoSlideCarousel, window.carouselSettings.carouselInterval);
					}
				);
			};

	init();

  };


  window.carouselStop = function( options ) {

		var stop = function(){
				window.carouselSettings.refreshIntervalId = window.clearInterval(window.carouselSettings.refreshIntervalId);
				$(".carousel .nav A").unbind();
				$(".carousel").unbind();
		};

		stop();
  };

	$(window).resize(function(){
		this.carouselStop();
		window.carouselSettings.currentItem = 0;
		this.carouselStar();
	});

	window.carouselStop();
	window.carouselStar();
	}

  //navigation
  $(".nav H2 A").bind("click", function(){
    if ($(this).parent().parent().parent().parent().hasClass("active")){
      //$(this).parent().parent().parent().parent().removeClass("active");
    } else {
      $(".nav").removeClass("active");
      $(this).parent().parent().parent().parent().addClass("active");
      $(".nav H2 A").removeClass("active");
      $(this).addClass("active");
    }
  });
  
  $("html, .close").click(function(event) {
    event.stopPropagation();
    $('.navigation-bar').removeClass("active");
  });

  $(".navigation-bar").not('.close').click(function(event){
      event.stopPropagation();
			$(this).addClass("active");
  });
  
  //adds "active" class to the right section button
  $(".navigation-bar .nav." + $('.page').attr('class').split(' ')[1]).addClass("active");
  
  //adds "active" class to the right page button
  $(".navigation-bar ." + $('.page').attr('class').split(' ')[2]).addClass("active");
  
  
  //home page banner
  window.areProfuctsClickable = true;
  
  $(".products .nav LI A").bind('click',function(event){
    //console.log('clicked');
    if (! window.areProfuctsClickable) {return false};
    if ($(this).hasClass("active")) {return false};
    var item = $(this).attr("href");
    item = item.slice(1);
    
    makeTransition(item,$(this));
    window.areProfuctsClickable = false;
  });
  
  function makeTransition(item, button){
    var hasAnimationEnded = false;

    var mt1 = function () {      
      $(".products A.product.active").fadeTo(800, 0, function(){
      //callback
      $(this).css("display", "none");
      if (hasAnimationEnded) {
        activeSwitch(item);
      } else {
        hasAnimationEnded = true;
      }
    });
    }
    
    var mt2 = function(){$(".products A.product." + item).css({
      opacity: 0,
      display: "block"
      }).fadeTo(500, 1, function(){
      //callback
      if (hasAnimationEnded) {
        activeSwitch(item);
      } else {
        hasAnimationEnded = true;
      }
    });
    }
    
    mt1();
    mt2();
  };
  
  function activeSwitch(item){
    //console.log('switch on!')    
    $(".products A.active").removeClass("active");
    $(".products A." + item).addClass("active");
    window.areProfuctsClickable = true;
  }
}
);

//Contact form validation
$(document).ready(function(){
	$("#form-contact").submit(function(){
		if( !validateField($("#name"), "")){
			$(".feedback").html('<p class="validation-message">Please fill in your name</p>');
			return false;
		};
		if( !validateField($("#email"), "")){
			$(".feedback").html('<p class="validation-message">Please fill in your e-mail</p>');
			return false;
		}	
		else if( !validateEmail($("#email").val())){
			$(".feedback").html('<p class="validation-message">Please fill in a valid e-mail address</p>');
			$("#email").addClass("error");
			$("#email").focus();
			return false;
		}
		else{
			$("#email").removeClass("error");
		};
		if( !validateField($("#message"), "")){
			$(".feedback").html('<p class="validation-message">Please fill in a message</p>');
			return false;
		};
	});

	function validateEmail(elementValue){  
       var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
       return emailPattern.test(elementValue);  
    } 

	function validateField(el,val){
		var isValid = true;
		if (el.val() == val) {
		 el.addClass("error");
		 el.focus();
		 isValid = false;
		} else {
		 el.removeClass("error");
		 isValid = true;
		}
		return isValid;
	}; 
});

