$(function () {

  // variables

  $.config = {
    browser : "",
    category : "windows",
    categoryMax : 0,
    categoryPos : 1,
    housePlay : true,
    navigationHover : false,
    galleryMax : 0,
    galleryPos : 0,
    baseURL : "/"
  }

  // browser

  $.each($.browser, function(i, val) {
    if (i=='safari' && val==true) { $.config.browser='safari'; };
    if (i=='opera' && val==true) { $.config.browser='opera'; };
    if (i=='msie' && val==true) { $.config.browser='msie'; };
    if (i=='mozilla' && val==true) { $.config.browser='mozilla'; };
  });

	if($.browser.msie && $.browser.version=="6.0") {
		
		$nav = $('ul#nav_categories');
		$links = $nav.find('li');
		$sublinks = $nav.find('li li');
		
		$links.hover(function() {
			$(this).find('ul:first').show();
		}, function() {
			$(this).find('ul:first').hide();
		});
		
		$sublinks.hover(function() {
			$(this).css('background', '#222');
		}, function() {
			$(this).css('background', 'transparent');
		});
		
	};

  // lightbox

  $('a.lightbox').lightBox({fixedNavigation:true});

  // gallery

  $("div.gallery div.button.next").show();
  $("div.gallery div.button.next").click(function () {
		$.config.galleryMax = Math.floor($("div.gallery div.wrap").width()/580);
    $.config.galleryPos = $.config.galleryPos++ < $.config.galleryMax ? $.config.galleryPos++ : $.config.galleryMax;
    $.config.galleryPos > 0 ? $("div.gallery div.button.prev").show() : $("div.gallery div.button.prev").hide();
    $.config.galleryPos == $.config.galleryMax ? $("div.gallery div.button.next").hide() : $("div.gallery div.button.next").show();
    $("div.gallery div.slides").animate({left : $.config.galleryPos*-580+"px"}, 300, "easeOutQuad");
  });
  $("div.gallery div.button.prev").click(function () {
		$.config.galleryMax = Math.floor($("div.gallery div.wrap").width()/580);
    $.config.galleryPos = $.config.galleryPos-- > 0 ? $.config.galleryPos-- : 0;
    $.config.galleryPos > 0 ? $("div.gallery div.button.prev").show() : $("div.gallery div.button.prev").hide();
    $.config.galleryPos == $.config.galleryMax ? $("div.gallery div.button.next").hide() : $("div.gallery div.button.next").show();
    $("div.gallery div.slides").animate({left : $.config.galleryPos*-580+"px"}, 300, "easeOutQuad");
  });

  // house

  $("body#home #house").hide().fadeIn(800);

  $.config.categoryMax = $("#house ul.menu li").size();

  $("#house ul li a").click(function () {
    //$.config.housePlay = false;
    $("#house ul li").removeClass("active");
    $.config.category = $(this).parent().attr("class");
    $.config.categoryPos = parseInt($(this).parent().attr("pos"));
    $("#house ul.menu li:nth-child(" + $.config.categoryPos + ")").addClass("active");
    $("#house .overlay").hide();
    $("#house .overlay." + $.config.category).fadeIn(400);
    return false;
  });

   $("#house").hover(function() {
   	$.config.housePlay = false;
   }, function() {
   	$.config.housePlay = true;
   });
  
   setInterval(function () {
     if($.config.housePlay) {
       $.config.categoryPos = $.config.categoryPos + 1 > $.config.categoryMax ? 1 : $.config.categoryPos + 1;
       $("#house ul.menu li").removeClass("active");
       $.config.category = $("#house ul.menu li:nth-child(" + $.config.categoryPos + ")").attr("class");
       $("#house ul.menu li:nth-child(" + $.config.categoryPos + ")").addClass("active");
       $("#house .overlay").hide();
       $("#house .overlay." + $.config.category).fadeIn(400);
     };
     }, 4000);
  
    $("#house .overlay").hide();
    $("#house .overlay." + $.config.category).fadeIn(1000);

    // Form - ENQUIRIES

    $('input.reset-enquiry').click(function() {
      $('form#enquiries span.error').fadeOut(500, function() {
        $(this).remove();
      });
      $("form#enquiries p.flash").hide();
    });

    $("input.submit-enquiry").click(function () {
      var hasError = false;
      $('form#enquiries span.error').remove();

      var nameVal = $("input.name").val();
      if(nameVal == '') {
        $("label.name").after('<span class="error">Name missing!</span>');
        hasError = true;
      }

      var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

      var emailVal = $("input.email").val();
      if(emailVal == '') {
        $("label.email").after('<span class="error">Email missing!</span>');
        hasError = true;
      } else if(!emailReg.test(emailVal)) {
        $("label.email").after('<span class="error">Email invalid!</span>');
        hasError = true;
      }

      var telephoneVal = $("input.telephone").val();
      if(telephoneVal == '') {
        $("label.telephone").after('<span class="error">Telephone missing!</span>');
        hasError = true;
      }

      var enquiryVal = $("textarea.enquiry").val();
      if(enquiryVal == '') {
        $("label.enquiry").after('<span class="error">ENQUIRY missing!</span>');
        hasError = true;
      }

      if(hasError) {
        alert("Please check the form for errors!");
      } else {
        var data = $("form#enquiries").serialize();
        $.post($.config.baseURL + "contact/post", { data: data }, function(data){ $("form#enquiries p.flash").hide().text("Thank you for your enquiry - a member of our team will contact you ASAP").slideDown(500) } );
        $("form#enquiries :input").not(':button, :submit, :reset, :hidden').val('');
      }
      return false;
    });
   
    // Form - FREE ENERGY

    $('input.reset-free-energy').click(function() {
      $('form#free-energy span.error').fadeOut(500, function() {
        $(this).remove();
      });
      $("form#free-energy p.flash").hide();
    });

    $("input.submit-free-energy").click(function () {
      var hasError = false;
      $('form#free-energy span.error').remove();

      var nameVal = $("input.name").val();
      if(nameVal == '') {
        $("label.name").after('<span class="error">Name missing!</span>');
        hasError = true;
      }
      
      var addressVal = $("input.address1").val();
      if(addressVal == '') {
        $("label.address1").after('<span class="error">Address missing!</span>');
        hasError = true;
      }
      
      var postcodeVal = $("input.postcode").val();
      if(postcodeVal == '') {
        $("label.postcode").after('<span class="error">Postcode missing!</span>');
        hasError = true;
      }

      var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

      var emailVal = $("input.email").val();
      if(emailVal == '') {
        $("label.email").after('<span class="error">Email missing!</span>');
        hasError = true;
      } else if(!emailReg.test(emailVal)) {
        $("label.email").after('<span class="error">Email invalid!</span>');
        hasError = true;
      }

      var telephoneVal = $("input.telephone").val();
      if(telephoneVal == '') {
        $("label.telephone").after('<span class="error">Telephone missing!</span>');
        hasError = true;
      }
      
      var mobileVal = $("input.mobile").val();
      if(mobileVal == '') {
        $("label.mobile").after('<span class="error">Mobile missing!</span>');
        hasError = true;
      }

      var requirementsVal = $("input.requirements").val();
      if(requirementsVal == '') {
        $("label.requirements").after('<span class="error">Requirements missing!</span>');
        hasError = true;
      }

      if(hasError) {
        alert("Please check the form for errors!");
      } else {
        var data = $("form#free-energy").serialize();
        $.post($.config.baseURL + "free-energy/post", { data: data }, function(data){ $("form#free-energy p.flash").hide().text("Thank you for your enquiry - a member of our team will contact you ASAP").slideDown(500) } );
        $("form#free-energy :input").not(':button, :submit, :reset, :hidden').val('');
      }
      return false;
    });

  });
