﻿var mainScrollSelector = "html";
var navigationHideTimeoutId = -1;
var verticalNavigationElementIds = new Array();
var horizontalNavigationElementIds = new Array();
var keyboardNavigationIndicatorsUpdateEnabled = true;

$( document ).ready( function() {
  
  var fancyBoxShowOverlay = true;
  
  if( $.browser.webkit ) {
    mainScrollSelector = "body";
  }
  
  if( $.browser.msie && parseInt( $.browser.version ) == 6 ) {
    $( ".jFlowPrev, .jFlowNext, .transparent_png_graphic" ).each( function() {
      var src = $( this ).attr( "src" );
      $( this ).attr( "src", "images/empty.gif" );
      $( this ).css( "filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='image')" );
    });
    $( ".jFlowPrev, .jFlowNext" ).parent().css( "cursor", "pointer" )
    $( ".jFlowPrev, .jFlowNext" ).parent().click( function( event ) {
      if( !( $( event.target ).is( ".jFlowPrev, .jFlowNext" ) ) ) {
        $( this ).find( ".jFlowPrev, .jFlowNext" ).triggerHandler( "click" );
      }
    });
    fancyBoxShowOverlay = false;
  }
  
  $( "#solutions_lin_graphic" ).fancybox({
    overlayShow: fancyBoxShowOverlay,
    overlayColor: "#000000",
    overlayOpacity: 0.75,
    transitionIn: "elastic",
    transitionOut: "elastic",
    hideOnOverlayClick: true,
    hideOnContentClick: true,
    showCloseButton: true,
    showNavArrows: false,
    enableEscapeButton: true,
    enableKeyboardNav: false
  });
  
  verticalNavigationElementIds = [ "top_bar_wrapper", "solutions", "services", "process", "media", "contact" ];
  
  if( $( "#media .horizontal_navigation_indicator_wrapper" ).size() > 0 ) {
    horizontalNavigationElementIds = [ "solutions"/*, "services"*/, "media" ];
  } else {
    horizontalNavigationElementIds = [ "solutions"/*, "services"*/ ];
  }
  
  $( window ).resize( function() {
    updateHorizontalNavigations();
  });
  
  $( "#top_bar_navigation .top_bar_navigation_item" ).each( function() {
    $( this ).click( function() {
      
      var navigationId = $( this ).attr( "id" );
      var id = navigationId.substring( 11 );
      
      scrollTo( id );
      
    });
  });
  
  $( "#bottom_bar_navigation .bottom_bar_navigation_item" ).each( function() {
    $( this ).click( function() {
      
      var navigationId = $( this ).attr( "id" );
      var id = navigationId.substring( 18 );
      
      scrollTo( id );
      
    });
  });
  
  $( "#solutions" ).jFlow({
    slides: ".slider_content_wrapper",
    width: "960px",
    height: "620px",
    duration: 250,
    easing: "easeInOutExpo",
    hidePrevNext: false,
    slideChanged: function( slideIndex ) {
      $( "#solutions .header_info" ).hide();
      $( "#solutions .header_info_slide_" + slideIndex ).show();
    }
  });
  
  /*
  $( "#services" ).jFlow({
    slides: ".slider_content_wrapper",
    width: "960px",
    height: "620px",
    duration: 250,
    easing: "easeInOutExpo",
    hidePrevNext: false,
    slideChanged: function( slideIndex ) {
      $( "#services .header_info" ).hide();
      $( "#services .header_info_slide_" + slideIndex ).show();
    }
  });
  */
  
  if( $( "#media .horizontal_navigation_indicator_wrapper" ).size() > 0 ) {
    
    $( "#media" ).jFlow({
      slides: ".slider_content_wrapper",
      width: "960px",
      height: "620px",
      duration: 250,
      easing: "easeInOutExpo",
      hidePrevNext: false,
      slideChanged: function( slideIndex ) {
        $( "#media .header_info" ).hide();
        $( "#media .header_info_slide_" + slideIndex ).show();
      }
    });
    
  }
  
  if( $.browser.mobile ) {
    
    $( "html" ).addClass( "mobile" );
    
    $( window ).scroll( function() {
      hideNavigation();
      showNavigationAfterAWhile( 500 );
    });
    
    $( ".contact_form_field_input, .contact_form_field_textarea" ).bind( "blur", function() {
      hideNavigation();
      showNavigationAfterAWhile( 500 );
    });
    
    $( mainScrollSelector ).bind( "touchstart", function( event ) {
      
      if( !( $( event.target ).is( ".top_bar_navigation_item" ) ) ) {
        hideNavigation();
      }
      
    });
    
    $( mainScrollSelector ).bind( "touchend", function() {
      showNavigationAfterAWhile( 1000 );
    });
    
    $( "#solutions .content_wrapper" ).swipe({
      fingers: 1,
      swipeLeft: function() {
        if( $( "#solutions .horizontal_navigation_right_button_wrapper .jFlowNext" ).is( ":visible" ) ) {
          $( "#solutions .horizontal_navigation_right_button_wrapper .jFlowNext" ).click();
        } else if( $( "#solutions .horizontal_navigation_button_right_small .jFlowNext" ).is( ":visible" ) ) {
          $( "#solutions .horizontal_navigation_button_right_small .jFlowNext" ).click();
        }
      },
      swipeRight: function() {
        if( $( "#solutions .horizontal_navigation_left_button_wrapper .jFlowPrev" ).is( ":visible" ) ) {
          $( "#solutions .horizontal_navigation_left_button_wrapper .jFlowPrev" ).click();
        } else if( $( "#solutions .horizontal_navigation_button_left_small .jFlowPrev" ).is( ":visible" ) ) {
          $( "#solutions .horizontal_navigation_button_left_small .jFlowPrev" ).click();
        }
      }
    });
    
    /*
    $( "#services .content_wrapper" ).swipe({
      fingers: 1,
      swipeLeft: function() {
        if( $( "#services .horizontal_navigation_right_button_wrapper .jFlowNext" ).is( ":visible" ) ) {
          $( "#services .horizontal_navigation_right_button_wrapper .jFlowNext" ).click();
        } else if( $( "#services .horizontal_navigation_button_right_small .jFlowNext" ).is( ":visible" ) ) {
          $( "#services .horizontal_navigation_button_right_small .jFlowNext" ).click();
        }
      },
      swipeRight: function() {
        if( $( "#services .horizontal_navigation_left_button_wrapper .jFlowPrev" ).is( ":visible" ) ) {
          $( "#services .horizontal_navigation_left_button_wrapper .jFlowPrev" ).click();
        } else if( $( "#services .horizontal_navigation_button_left_small .jFlowPrev" ).is( ":visible" ) ) {
          $( "#services .horizontal_navigation_button_left_small .jFlowPrev" ).click();
        }
      }
    });
    */
    
    if( $( "#media .horizontal_navigation_indicator_wrapper" ).size() > 0 ) {
      
      $( "#media .content_wrapper" ).swipe({
        fingers: 1,
        swipeLeft: function() {
          if( $( "#media .horizontal_navigation_right_button_wrapper .jFlowNext" ).is( ":visible" ) ) {
            $( "#media .horizontal_navigation_right_button_wrapper .jFlowNext" ).click();
          } else if( $( "#media .horizontal_navigation_button_right_small .jFlowNext" ).is( ":visible" ) ) {
            $( "#media .horizontal_navigation_button_right_small .jFlowNext" ).click();
          }
        },
        swipeRight: function() {
          if( $( "#media .horizontal_navigation_left_button_wrapper .jFlowPrev" ).is( ":visible" ) ) {
            $( "#media .horizontal_navigation_left_button_wrapper .jFlowPrev" ).click();
          } else if( $( "#media .horizontal_navigation_button_left_small .jFlowPrev" ).is( ":visible" ) ) {
            $( "#media .horizontal_navigation_button_left_small .jFlowPrev" ).click();
          }
        }
      });
      
    }
    
    hideNavigation();
    showNavigationAfterAWhile( 1000 );
    
  } else {
    
    $( "#top_bar_navigation" ).removeClass( "top_bar_navigation_without_keyboard_navigation_indicator" );
    $( "#top_bar_navigation" ).addClass( "top_bar_navigation_with_keyboard_navigation_indicator" );
    $( "#top_bar_keyboard_navigation_indicator" ).show();
    
    $( window ).resize( function() {
      updateKeyboardNavigationIndicators();
    });
    
    $( window ).scroll( function() {
      updateNavigationMargin();
      updateKeyboardNavigationIndicators();
    });
    
    $( document ).keydown( handleKeyDown );
    
    $( document ).keyup( handleKeyUp );
    
    if( $( mainScrollSelector ).scrollTop() > 0 ) {
      updateNavigationMargin();
    }
    
    updateKeyboardNavigationIndicators();
    
  }
  
  updateHorizontalNavigations();
  
  enableContactFormSubmitButton();
  
});

function updateHorizontalNavigations() {
  
  var windowWidth = $( window ).width();
  
  if( windowWidth < 1254 ) {
    
    $( ".horizontal_navigation_left_button_wrapper" ).hide();
    $( ".horizontal_navigation_right_button_wrapper" ).hide();
    
    $( ".horizontal_navigation_small_arrows_wrapper" ).show();
    
  } else {
    
    $( ".horizontal_navigation_small_arrows_wrapper" ).hide();
    
    $( ".horizontal_navigation_left_button_wrapper" ).show();
    $( ".horizontal_navigation_right_button_wrapper" ).show();
    
  }
  
}

function scrollTo( id ) {
  
  var windowHeight = $( window ).height();
  var documentHeight = $( document ).height();
  var currentScrollTop = $( mainScrollSelector ).scrollTop();
  var elementTop = $( "#" + id ).offset().top;
  var elementHeight = $( "#" + id ).height();
  
  var newScrollTop = elementTop;
  
  if( elementHeight < windowHeight ) {
    newScrollTop -= ( windowHeight - elementHeight ) / 2;
  } else {
    newScrollTop -= 40;
  }
  
  if( newScrollTop < 0 ) {
    newScrollTop = 0;
  } else if( newScrollTop > ( documentHeight - windowHeight ) ) {
    newScrollTop = documentHeight - windowHeight;
  }
  
  if( Math.abs( newScrollTop - currentScrollTop ) > 2 ) {
    
    if( $.browser.mobile ) {
      hideNavigation();
    }
    
    $( mainScrollSelector ).stop();
    
    if( !( $.browser.mobile ) ) {
      disableKeyboardNavigation();
    }
    
    $( mainScrollSelector ).animate( { scrollTop : newScrollTop }, 400, "easeInOutExpo", function() {
      
      if( !( $.browser.mobile ) ) {
        enableKeyboardNavigation();
      }
      
    });
    
  }
  
}

function handleKeyDown( event ) {
  
  if( event.which == 38 || event.which == 40 ) {
    event.preventDefault();
  }
  
}

function handleKeyUp( event ) {
  
  if( event.which == 38 || event.which == 40 ) {
    handleVerticalArrowKeys( event.which );
  }
  
  if( event.which == 37 || event.which == 39 ) {
    handleHorizontalArrowKeys( event.which );
  }
  
}

function handleVerticalArrowKeys( keyCode ) {
  
  var previousVerticalNavigationElementId = null;
  var nextVerticalNavigationElementId = null;
  
  var windowHeight = $( window ).height();
  var scrollTop = $( mainScrollSelector ).scrollTop();
  
  var referenceLine = scrollTop + windowHeight / 2;
  
  var found = false;
  
  for( var i = 0; !found && i < verticalNavigationElementIds.length; i++ ) {
    
    var verticalNavigationElementId = verticalNavigationElementIds[i];
    var verticalNavigationElementTop = $( "#" + verticalNavigationElementId ).offset().top;
    
    if( verticalNavigationElementTop < scrollTop - 2 ) {
      previousVerticalNavigationElementId = verticalNavigationElementId;
    }
    
    if( verticalNavigationElementTop > referenceLine ) {
      nextVerticalNavigationElementId = verticalNavigationElementId;
      found = true;
    }
    
  }
  
  if( keyCode == 38 ) {
    
    if( previousVerticalNavigationElementId != null ) {
      scrollTo( previousVerticalNavigationElementId );
    }
    
  } else if( keyCode == 40 ) {
    
    if( nextVerticalNavigationElementId != null ) {
      scrollTo( nextVerticalNavigationElementId );
    }
    
  }
  
}

function handleHorizontalArrowKeys( keyCode ) {
  
  var activeHorizontalNavigationElementId = null;
  
  var windowHeight = $( window ).height();
  var scrollTop = $( mainScrollSelector ).scrollTop();
  
  var referenceLine = scrollTop + windowHeight / 2;
  
  var found = false;
  
  for( var i = 0; !found && i < horizontalNavigationElementIds.length; i++ ) {
    
    var horizontalNavigationElementId = horizontalNavigationElementIds[i];
    var horizontalNavigationElementTop = $( "#" + horizontalNavigationElementId ).offset().top;
    var horizontalNavigationElementHeight = $( "#" + horizontalNavigationElementId ).height();
    
    if( horizontalNavigationElementTop < referenceLine && horizontalNavigationElementTop + horizontalNavigationElementHeight > referenceLine ) {
      activeHorizontalNavigationElementId = horizontalNavigationElementId;
      found = true;
    }
    
  }
  
  if( activeHorizontalNavigationElementId != null ) {
    
    if( keyCode == 37 ) {
      $( "#" + activeHorizontalNavigationElementId ).find( ".horizontal_navigation_button_left_small .jFlowPrev" ).click();
    } else if( keyCode == 39 ) {
      $( "#" + activeHorizontalNavigationElementId ).find( ".horizontal_navigation_button_right_small .jFlowNext" ).click();
    }
    
  }
  
}

function updateNavigationMargin() {
  
  var newMarginTop = 0;
  
  if( $( mainScrollSelector ).scrollTop() < 10 ) {
    newMarginTop = -( $( mainScrollSelector ).scrollTop() );
  } else {
    newMarginTop = -10;
  }
  
  $( "#top_bar_navigation" ).css( { "margin-top" : newMarginTop } );
  
}

function disableKeyboardNavigation() {
  
  keyboardNavigationIndicatorsUpdateEnabled = false;
  
  $( ".keyboard_navigation_indicator_arrow" ).removeClass( "keyboard_navigation_indicator_arrow_active" );
  
  $( document ).unbind( "keydown", handleKeyDown );
  
  $( document ).unbind( "keyup", handleKeyUp );
  
}

function enableKeyboardNavigation() {
  
  $( document ).keydown( handleKeyDown );
  
  $( document ).keyup( handleKeyUp );
  
  keyboardNavigationIndicatorsUpdateEnabled = true;
  
  updateKeyboardNavigationIndicators();
  
}

function updateKeyboardNavigationIndicators() {
  
  if( keyboardNavigationIndicatorsUpdateEnabled ) {
    
    var windowHeight = $( window ).height();
    var documentHeight = $( document ).height();
    var scrollTop = $( mainScrollSelector ).scrollTop();
    
    var referenceLine = scrollTop + windowHeight / 2;
    
    $( ".keyboard_navigation_indicator_arrow" ).removeClass( "keyboard_navigation_indicator_arrow_active" );
    
    if( $( "#" + verticalNavigationElementIds[0] ).offset().top < scrollTop ) {
      $( "#keyboard_navigation_indicator_up_arrow" ).addClass( "keyboard_navigation_indicator_arrow_active" );
    }
    
    if( $( "#" + verticalNavigationElementIds[verticalNavigationElementIds.length - 1] ).offset().top > referenceLine ) {
      $( "#keyboard_navigation_indicator_down_arrow" ).addClass( "keyboard_navigation_indicator_arrow_active" );
    }
    
    var activateSideArrows = false;
    
    for( var i = 0; !activateSideArrows && i < horizontalNavigationElementIds.length; i++ ) {
      
      var horizontalNavigationElementId = horizontalNavigationElementIds[i];
      var horizontalNavigationElementTop = $( "#" + horizontalNavigationElementId ).offset().top;
      var horizontalNavigationElementHeight = $( "#" + horizontalNavigationElementId ).height();
      
      if( horizontalNavigationElementTop < referenceLine && horizontalNavigationElementTop + horizontalNavigationElementHeight > referenceLine ) {
        activateSideArrows = true;
      }
      
    }
    
    if( activateSideArrows ) {
      $( "#keyboard_navigation_indicator_left_arrow" ).addClass( "keyboard_navigation_indicator_arrow_active" );
      $( "#keyboard_navigation_indicator_right_arrow" ).addClass( "keyboard_navigation_indicator_arrow_active" );
    }
    
  }
  
}

function hideNavigation() {
  
  $( "#top_bar_navigation" ).stop();
  $( "#top_bar_navigation" ).hide();
  
}

function showNavigationAfterAWhile( time ) {
  
  if( navigationHideTimeoutId != -1 ) {
    clearTimeout( navigationHideTimeoutId );
    navigationHideTimeoutId = -1;
  }
  
  navigationHideTimeoutId = setTimeout( navigationHideTimeoutElapsed, time );
  
}

function navigationHideTimeoutElapsed() {
  
  navigationHideTimeoutId = -1;
  
  if( $( "#top_bar_navigation" ).is( ":hidden" ) ) {
    
    var topValueHide = $( mainScrollSelector ).scrollTop() - 50;
    var topValueShow = $( mainScrollSelector ).scrollTop();
    
    if( $( mainScrollSelector ).scrollTop() < 10 ) {
      topValueHide += ( 10 - $( mainScrollSelector ).scrollTop() );
      topValueShow -= $( mainScrollSelector ).scrollTop();
    } else {
      topValueShow -= 10;
    }
    
    $( "#top_bar_navigation" ).stop();
    $( "#top_bar_navigation" ).css( { "top" : topValueHide } );
    $( "#top_bar_navigation" ).show();
    $( "#top_bar_navigation" ).animate( { "top" : topValueShow }, 200, "swing" );
    
  }
  
}

function enableContactFormSubmitButton() {
  
  $( "#contact_form_submit_button" ).bind( "click", function() {
    clearContactFormMessages();
    sendContactForm();
  });
  
  $( "#contact_form_submit_button" ).removeClass( "contact_form_button_disabled" );
  
}

function disableContactFormSubmitButton() {
  
  $( "#contact_form_submit_button" ).unbind( "click" );
  
  $( "#contact_form_submit_button" ).addClass( "contact_form_button_disabled" );
  
}

function clearContactFormMessages() {
  
  $( ".contact_form_field_validate_failed" ).hide();
  
  $( "#contact_send_validation_failed" ).hide();
  $( "#contact_send_succeeded" ).hide();
  $( "#contact_send_failed" ).hide();
  
}

function sendContactForm() {
  
  var doSubmit = validateContactForm();
  
  if( doSubmit ) {
    
    disableContactFormSubmitButton();
    
    contactFormShowProgressInfo();
    
    $.ajax({
      url: "actions.php",
      type: "POST",
      data: {
        action : document.contact_form.action.value,
        name : document.contact_form.name.value,
        title : document.contact_form.title.value,
        company : document.contact_form.company.value,
        country : document.contact_form.country.value,
        email : document.contact_form.email.value,
        phone : document.contact_form.phone.value,
        message : document.contact_form.message.value
      },
      success: contactFormSent,
      error: contactFormSentFailed
    });
    
  } else {
    
    contactFormShowValidationFailedInfo();
    
  }
  
}

function validateContactForm() {
  
  var pass = true;
  
  var emailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  
  if( document.contact_form.name.value == "" ) {
    
    $( ".contact_form_wrapper .contact_form_field_validate_failed" ).eq( 0 ).show();
    
    pass = false;
    
  }
  
  if( document.contact_form.email.value == "" || !( emailFilter.test( document.contact_form.email.value ) ) ) {
    
    $( ".contact_form_wrapper .contact_form_field_validate_failed" ).eq( 4 ).show();
    
    pass = false;
    
  }
  
  return pass;
  
}

function contactFormSent( data ) {
  
  if( data != null ) {
    
    if( data == "success" ) {
      
      contactFormShowSuccessInfo();
      
      document.contact_form.reset();
      
    } else {
      
      contactFormShowFailedInfo();
      
    }
    
  } else {
    
    contactFormShowFailedInfo();
    
  }
  
  enableContactFormSubmitButton();
  
}

function contactFormSentFailed() {
  
  contactFormShowFailedInfo();
  
  enableContactFormSubmitButton();
  
}

function contactFormShowValidationFailedInfo() {
  
  $( "#contact_send_validation_failed" ).show();
  
}

function contactFormShowProgressInfo() {
  
  $( "#contact_send_progress" ).show();
  
}

function contactFormShowSuccessInfo() {
  
  $( "#contact_send_progress" ).hide();
  $( "#contact_send_succeeded" ).show();
  
  setTimeout( function() {
    $( "#contact_send_succeeded" ).hide();
  }, 4000 );
  
}

function contactFormShowFailedInfo() {
  
  $( "#contact_send_progress" ).hide();
  $( "#contact_send_failed" ).show();
  
  setTimeout( function() {
    $( "#contact_send_failed" ).hide();
  }, 4000 );
  
}
