
get_current_section = function()
{
 
      return parseInt(($(window).scrollTop() + $(window).height() /2 ) / $(window).height());

  }

  get_current_text_section = function()
{
      return parseInt($(window).scrollTop() / $(window).height());
}

get_scroll_text_up_point = function()
{
    
  return Math.max($(window).scrollTop() - $(window).height() , 0);
}

var arrow_coordinates = new Array('-850px','-614px', '-465px', '-297px', '-168px', '-27px' );



 $(document).ready(function() {

  $.localScroll.defaults.axis = 'y';
 hash= window.location.hash;


  $('.sections').bind('inview', function(event, visible) {
	  if(visible == true) {
	  	$(this).addClass('inview');
	  } else {
	  	$(this).removeClass('inview');
	  }
	});

        $('#top_menu a').click(function(){
           $("body").css("overflow-y", "auto");
        });



  

$('.top_menu, .flyout_content').localScroll();


//initialize
window_height = $(window).height();
var current_section;
var current_text_section;
var scroll_text_up_point;
var sections= $('.section');
sections.css('height',  window_height);
sections.text_scrolled_down=false;
sections.text_scrolled_up=false;
sections.data('text_scrolled_down', false);
sections.data('text_scrolled_up', true);

text_scroll_distance='120px';
text_scroll_speed=1200;
if(!(navigator.userAgent.match(/iPad/i)))//no scroll animation on ipad so don't move text up'
    sections.find('.section_text').css('margin-top', '-=' +  text_scroll_distance );



$('#privacy_policy').click(function(){
    $('#privacy_policy_popup').fadeIn('fast');
});

$('html').click(function(e){
     if(
           ((e.target.nodeName != undefined && e.target.nodeName.toLowerCase() == 'a' ) &&
             ($(e.target).attr('href') != undefined && $(e.target).attr('href') != '')) ){
               return true;
           }
           if ($(e.target).parents('a').attr('href') != undefined && $(e.target).parents('a').attr('href') != '#'){
               return true;
           }
           if( ($(e.target).parents('#privacy_policy_popup').length > 0 || $(e.target).attr('id') == 'privacy_policy') &&  $(e.target).attr('class') != 'info_close')
                return false;

          e.preventDefault();
    $('#privacy_policy_popup').fadeOut('fast');
});

//set section height to window size
$(window).resize(function(){
    sections.css('height',   $(this).height());
    $(window).scrollTo($(".section:eq(" + current_section +")").offset())
    
   
  });
 
  if (hash=="#about"  || hash=="#services" ||  hash=="#news" || hash=="#portfolio" || hash=="#contact" )
      {
          
          //$(html, body).scrollTo(hash);
          $('html,body').animate({scrollTop: $(hash).offset().top}, 1000, 'easeOutCubic');
      }



      //slide text down on refresh
            pixels_down = $(window).scrollTop();

       sections.each(function(){
          if(pixels_down >= ($(this).offset().top - 200) &&  ($(this).data('text_scrolled_up'))) {
                if(!(navigator.userAgent.match(/iPad/i)))
                     $(this).find('.section_text').animate({'margin-top' :'+=' + text_scroll_distance}, text_scroll_speed, function(){
                         var timer=-100;//no delay for 1st link
                         $(this).find('.text').each(function(){
                             var text = $(this);
                             timer+=100;
                            setTimeout(function(){
                               text.slideLeftShow('fast');
                            }, timer);

                     });
               });
                      $(this).data('text_scrolled_up', false);
          }

          if(pixels_down >= ($(this).offset().top - 200) && pixels_down <= ($(this).offset().top + 200) )
              {
                    //$('#top_menu_line').attr('src', 'images/home_lines/' + $(this).attr('id') + '_line.png');

              }


           if(pixels_down == $('#contact').offset().top)
                        {
                              // Contact page twinkle of locations


                                        var location_timer = 0;
                                        var num_locations;
                                        setTimeout( function(){
                                            num_locations = $('#contact .locations>li:not("#first")').length;
                                               $('#contact .locations>li:not("#first")').each(function(index){
                                                          var location = $(this);
                                                          location_timer+=100;
                                                          setTimeout( function(){
                                                                $('#contact .active').removeClass('active');
                                                                location.addClass('active').fadeIn('slow');

                                                                if( (num_locations-1) == index ){
                                                                    setTimeout( function(){$('#contact .locations>li:last').removeClass('active');}, 100 );
                                                                }

                                                            }, location_timer);
                                               });

                                        }, 100 );



                        }



           });

           //end slide text down on refresh




  $(window).scroll(function(){

        //dock ipad at bottom
      if((navigator.userAgent.match(/iPad/i)))
          {
      document.getElementById('flyout_container').style.top =(window.pageYOffset + window.innerHeight - 25) + 'px';
          }
 
      current_section = get_current_section();

      pixels_down = $(window).scrollTop();

       sections.each(function(){
          if(pixels_down >= ($(this).offset().top - 200) &&  ($(this).data('text_scrolled_up'))) {
                if(!(navigator.userAgent.match(/iPad/i)))
                     $(this).find('.section_text').animate({'margin-top' :'+=' + text_scroll_distance}, text_scroll_speed, function(){
                         var timer=-100;//no delay for 1st link
                         $(this).find('.text').each(function(){
                             var text = $(this);
                             timer+=100;
                            setTimeout(function(){
                               text.slideLeftShow('fast');
                            }, timer);

                     });
               });
                      $(this).data('text_scrolled_up', false);
          }

          if(pixels_down >= ($(this).offset().top - 200) && pixels_down <= ($(this).offset().top + 200) )
              {
                    //$('#top_menu_line').attr('src', 'images/home_lines/' + $(this).attr('id') + '_line.png');
                    
                      $('.arrow').stop().animate({'left': arrow_coordinates[current_section]});
                      
              }

         
           if(pixels_down == $('#contact').offset().top)
                        {
                              // Contact page twinkle of locations


                                        var location_timer = 0;
                                        var num_locations;
                                        setTimeout( function(){
                                            num_locations = $('#contact .locations>li:not("#first")').length;
                                               $('#contact .locations>li:not("#first")').each(function(index){
                                                          var location = $(this);
                                                          location_timer+=100;
                                                          setTimeout( function(){
                                                                $('#contact .active').removeClass('active');
                                                                location.addClass('active').fadeIn('slow');

                                                                if( (num_locations-1) == index ){
                                                                    setTimeout( function(){$('#contact .locations>li:last').removeClass('active');}, 100 );
                                                                }

                                                            }, location_timer);
                                               });

                                        }, 100 );



                        }
          
             
             
           });
      });



     
      
      
//locations functionality
(function($){
    $('.locations li').click(function(){
       $(this).addClass('active').siblings().removeClass('active').find('ul').fadeOut('100');
       $(this).find('ul').fadeIn('800');
       
    });
})(jQuery);


    pixels_down= $(window).scrollTop();


    
          current_section = get_current_section();




            

// Contact form validation
            function validateEmail(email){
                    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
                    return pattern.test(email);
            }
            
              $('#contact .error').live('focus', function(){
                  if( $(this).attr('id') != 'email_text')
                     $(this).val('').removeClass('error').css({'border': '0'});

                  if( $(this).val() == 'Please enter in your email' )
                      $(this).val('').removeClass('error').css({'border': '0'});
              });

              $('#contact .button').click(function(){
                 var error = '';
                $('#contact .required').each(function(){
                    var process = true;

                    /* One off for the email validation */
                    if( $(this).attr('id') == 'email_text'){
                    process = false; // set to false if you do not want the generic if below to validate as well (the one after this special case validation)
                        if( $(this).val() == '' ){
                            error += 'Error';
                            var field_name = $(this).attr('id');
                            field_name = field_name.split('_');
                            $(this).addClass('error').css({'border': '2px solid #B22222'}).val('Please enter in your '+field_name[0]);
                        }
                        else{
                            if( !validateEmail($(this).val()) ){
                                error += 'Error';
                                $(this).addClass('error').css({'border': '2px solid #B22222'});
                            }
                        }

                    }
                    /* End one off for the email validation */


                   if( process === true && ($(this).val() == '' || $(this).hasClass('error')) ){
                       error += 'Error';
                       var field_name = $(this).attr('id');
                       field_name = field_name.split('_');
                       $(this).addClass('error').css({'border': '2px solid #B22222'}).val('Please enter in your '+field_name[0]);

                   }
                });

                 if( error == '' ){
                     //var form_values = $('.contact_form').serialize();
                     var name = encodeURIComponent($('#name_text').val());
                     var email = encodeURIComponent($('#email_text').val());
                     var company = encodeURIComponent($('#company_text').val());
                     var phone = encodeURIComponent($('#phone_text').val());
                     var memo = encodeURIComponent($('#memo_text').val());
                     var second_name = encodeURIComponent($('#second_name').val());

                     $.ajax({
                           type: "POST",
                           url: "form_handler.php",
                           data: 'name='+name+'&email='+email+'&company='+company+'&phone='+phone+'&memo='+memo+'&second_name='+second_name,
                           success: function(msg){
                             $('#contact form input, #contact form textarea').val('');
                             $('.contact_popup').val(msg);
                             $('.contact_popup').fadeIn('slow');
                             setTimeout( function(){$('.contact_popup').fadeOut('slow');}, 5000 );
                           }
                         });
                 }
                   return false;
              });


 });

