/**** Add application wide javascripts below this point  ******/
$(document).ready(function(){
  
  carousel = $('#carousel');
  if(carousel.length > 0) {
    
  }
  
  // submit to salesforce
  
//  $("#enquiry").submit(function() {
// alert($('#name').attr('value'));
// 
//    // we want to store the values from the form input box, then send via ajax below
//     // var fname     = $('#fname').attr('value');
//     // var lname     = $('#lname').attr('value'); 
//     
//     var oid = '00D200000007m3e';
//     var first_name = $('#name').attr('value');
//     var last_name = $('#name').attr('value');
//     var email = $('#email').attr('value');
//     var company = $('#company').attr('value');
//     var var00N20000001bFaa = "";
//     var var00N20000001bFaG = "";
//     var lead_source = "Website Enquiry";
// 
//     // $.post("https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8", { name: "John", time: "2pm" } );
//     // return false;
//     
//      $.ajax({
//          type: 'POST',
//          url: "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8",
//          data: { 'name':'john', 'time':'2pm'},
//          dataType: 'json',
//          jsonp:'jsonp_callback',
//          success: function(data) {
//            alert(data);
//          },
//        });
// 
//       
//       // $.post("https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8", { oid: $oid, first_name: $first_name, last_name: $last_name, email: $email, company: $company, 00N20000001bFaa: $var00N20000001bFaa, 00N20000001bFaG: $var00N20000001bFaG, lead_source: $lead_source } );
//   
// 
//  });
  
  // show casestudy if needed
  
  // hide customer_box_more
  for(var i=1;i<19;i++) {
    $('.customer_box_more'+i).hide();  
  }
  
  //showmorecustomers
  $('#show_more_customers').hide();
  //$('#more_customers').slideToggle('slow');
  
  $('.showmorecustomers').click(function(){ 
    // show more_customers and hide first_customers i.e. toggle and change link text?
    //$('#more_customers').slideToggle('slow');
    $('#first_customers').slideToggle('slow');
    //,function(){
      $('#show_more_customers').slideToggle('slow');
    //});
  });
  
  //when click customer_box (arrow is just bg image) show it's child with toggleSlide
  // $('.box').hover(function(){ // could use mouseover and mouseout instead.
  //   // toggleslide!
  //   var more = '.'+$(this).attr('rel');
  //   var case_study = '#'+$(this).attr('rel')+'_case_study';
  //   if($(case_study).length!=0) {
  //     $(case_study).hide(); // hide case study link
  //   }
  //   $(more).slideToggle('fast',function(){
  //     if($(case_study).length!=0) {
  //       var current = $(case_study).css('margin-top').replace("px","");
  //       var h = $(more).height();  // height is not there until it's fully output so delay?
  //       return casestudy(current,case_study,more);      }    
  //   });
  // });
  $('.box').hover(
    function(){ 
      //alert('over');
      var more = '.'+$(this).attr('rel');
      var case_study = '#'+$(this).attr('rel')+'_case_study';
      if($(case_study).length!=0) {
        $(case_study).hide(); // hide case study link
      }
      $(more).slideToggle('fast',function(){
        if($(case_study).length!=0) {
          var current = $(case_study).css('margin-top').replace("px","");
          var h = $(more).height();  // height is not there until it's fully output so delay?
          return casestudy(current,case_study,more);      
        }
      });    
    },
    function(){
      //alert('out');
      var more = '.'+$(this).attr('rel');
      var case_study = '#'+$(this).attr('rel')+'_case_study';
      if($(case_study).length!=0) {
        $(case_study).hide(); // hide case study link
      }
      $(more).slideToggle('fast',function(){
        if($(case_study).length!=0) {
          var current = $(case_study).css('margin-top').replace("px","");
          var h = $(more).height();  // height is not there until it's fully output so delay?
          return casestudy(current,case_study,more);      
        }
      });      
    }
  );
    
  function casestudy(cs, cs_link,more) {
    // delay?
    
    var h = $(more).height();  // height is not there until it's fully output so delay?
    //alert(cs + cs_link + more);
    if(cs==165) {
      //alert('t');
      //alert(h);
      h=h+20;
      var mt = 165-h;
      $(cs_link).css('margin-top',mt);      
    } else {    
      $(cs_link).css('margin-top',165);    
    }
    $(cs_link).show();
    
  }
  
  
   // open external links into new window.  
   function externalLinks(base_url) {
     return $('a[href^=http]:not("[href^=' + base_url + ']"):not(":has(\'img\')")');
   }
   
   function externalLinksPdf() {
     return $('a[href$=pdf]');
   }
   
   $(document).ready(function() { externalLinks('this.hostname').addClass("external"); });
   $(document).ready(function() { externalLinksPdf('this.hostname').addClass("external"); });

   $('.external').click(function(){
   this.target = "_blank";
   });   
  
});
