// JavaScript Document
(function () {
$.fn.infiniteCarousel = function () {
        function repeat(str, n) {
            return new Array( n + 1 ).join(str);
        }
        
        return this.each(function () {
            
            var $wrapper = $('> div', this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul').width(9999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first')
                
                singleWidth = $single.outerWidth(),
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                currentPage = 1,
                //pages = Math.ceil($items.length / visible);
                //var currLogo is the number of items to be cycled through. 
                //this must be manually changed based on the number of items you have to be rotated. 
                pages = 20,
                currLogo = 6,
                $testiOutput = $('#testiOutput');
                
            
            // 2. create the carousel padding on left and right (cloned)
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $slider.find('> li');
            
            // 3. reset scroll
            $wrapper.scrollLeft(singleWidth * visible);
            
            // 4. paging function
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    //left = singleWidth * dir * visible * n;
                    left = singleWidth;
                    
                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
                  
                     if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * visible * pages);
                    }
                    
                    currentPage = page;
                    
                    var newTesti = $('#testi' + currLogo).html();
                    $testiOutput.empty().html(newTesti);
                    
                    $('.infiniteCarousel li').each(function(){
                    var src = $(this).find("img").attr("src").replace("-color", "-bw");
                        $(this).find('img').attr('src', src);
                    });
                 
                    $('.' + currLogo).each(function(){
                    var src = $(this).find("img").attr("src").replace("-bw", "-color");
                        $(this).find('img').attr('src', src);
                    });

                    //var currLogo is the number of items to be cycled through. 
                    //this must be manually changed based on the number of items you have to be rotated.                     
                    if (currLogo < 20 ){
                        currLogo = currLogo + 1;
                    }else{
                        currLogo = 1;
                    }
                    
                });
            }
            
            $(this).bind('goto', function (event, page) {
                gotoPage(page);
            });
            
            $(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });
        });
    };
})(jQuery);

$(document).ready(function () {

    var autoscrolling = true;
    
    $('.infiniteCarousel').infiniteCarousel().mouseover(function () {
        autoscrolling = false;
    }).mouseout(function () {
        autoscrolling = true;
    });
    
    setInterval(function () {
        if (autoscrolling) {
            $('.infiniteCarousel').trigger('next');
        }
    }, 6000);
    
    $('img').hover(function(){
        var src = $(this).attr("src").replace("-bw", "-color");
        $(this).attr('src', src);
    }, function(){
        var src = $(this).attr("src").replace("-color", "-bw");
        $(this).attr('src', src);
    });

var theWindow = $(window),
$container = $('body');

function changeLay() {

                if ( theWindow.width() < 1360 ) {
                        $container.removeClass('over1365');
                } else {
                        $container.addClass('over1365');
                }
}

theWindow.resize(function() {
                changeLay();
}).trigger("resize");


/* This code hides the side chatbar */
var theWidth = $(window),
$chatTab = $('.chatTab');

function hideChat() {

                if ( theWidth.width() < 1111 ) {
                        $chatTab.hide();
                } else {
                        $chatTab.show();
                }
}

theWidth.resize(function() {
                hideChat();
}).trigger("resize");


/* simply slides the slider on the homepage down after being hidden. */
$(".slider").slideDown("slow");
});

/**
 * The Following is an Ajax handler for the Newsletter Form.
 *
 *
**/
$(function(){
    
 $('input[title].newsletterinput').each(function() {
  if($(this).val() === '') {
   $(this).val($(this).attr('title')); 
  }
  });
  
  $('input[title].newsletterinput').focus(function() {
   if($(this).val() === $(this).attr('title')) {
    $(this).val('').addClass('focused'); 
   }
  });
  
  $('input[title].newsletterinput').blur(function() {
   if($(this).val() === '') {
    $(this).removeClass('focused'); 
   }
  });
 
 // ajax form
    $(".newsletterbox form").submit(function(){
    
            var str = $(this).serialize();
            
     $.ajax({
         type: "POST",
         url: "/newsletter.php",
         data: str,
         success: function(msg){
    
                $(".newsletterbox form").ajaxComplete(function(event, request, settings){
                    if(msg == 'ok') // Message Sent? Show the 'Thank You' message and hide the form
                    {
                        result = '<p>Thank you!</p>';
                        $(".newsletterbox").fadeOut();
                    } else {
                        result = msg;
                    }
                    $('.output').html(result);
                });
            }
        });
        return false;
    });

});


$(function(){
    var submitname = $("p.submitBtn input[type='image']").attr("alt");
    var location = $("p.submitBtn input[type='image']").attr("alt");

    $("p.submitBtn").append('<p>' + submitname + '</p>');
    $("p.submitBtn").css({ 'float' : 'right', 'width' : '200px', 'position' : 'relative'});
    $("p.submitBtn p").css({ 'cursor' : 'pointer', 'color' : '#FFFFFF', 'font-weight' : 'bold', 'right' : '10px', 'margin' : '0 auto', 'position' : 'absolute', 'top' : '7px', 'font-family' : '\'CenturyGothicRegular\',sans-serif'});

    $("p.submitBtn p").click(function(){
        $(".quote-form").submit();
    });

});





/* The Custom form field erros and such are below, 
as well as the code for form validation. */
$(function(){


                                                     
  /*Custom Method Declaration. see: http://docs.jquery.com/Plugins/Validation/CustomMethods/phoneUS */
  jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
      phone_number = phone_number.replace(/\s+/g, ""); 
      return this.optional(element) || phone_number.length > 9 &&
          phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
  }, "Please specify a valid phone number");
                                                       
  /* Validating rules for the questionaires */
  $(".quote-form").validate({
    rules: {
      phone: {
          required: true,
          phoneUS: true               
        },
      name: {
          required: true
        },
      email: {
          required: true
        }
      },
    messages: {
      required: "We kinda need you to fill this out.",
      name: "Sorry, but we need at least a first name.",
      email: {
          required: "Without your email we will never be able to talk to you.",
          email: "Your email address must be in the format of name@domain.com"
        },
      phone: {
          required: "We really need your number.",
          phoneUS: "Phone Numbers Must be in the format of, 555-123-4567."            
        }
    }
  });


  $(".newsletterbox form").validate({
    rules: {
      email: {
          required: true
        }
      },
    messages: {
      required: "We kinda need you to fill this out.",
      email: {
          required: "Without your email we will never be able to talk to you.",
          email: "Your email address must be in the format of name@domain.com"
        }
    }
  });





});
