var siteUrl = 'http://hullbusiness.co.uk/';

jQuery.fn.eachDelay = function(callback, speed){
  return jQuery.eachDelay( this, callback, speed)
}
jQuery.extend({
  eachDelay: function(object,callback, speed){ 
    var name, i = -1, length = object.length, $div = $('<div>'), id;
    if (length === undefined) { //not an array process as object
      var arr = [], x = -1;
      for (name in object) arr[++x] = name; 
      id = window.setInterval(function(){
       if( ++i === arr.length || callback.call(object[ arr[i] ], arr[i], object[ arr[i] ]) === false) 
         clearInterval(id);
      }, speed);  
    }
    else { //array-compatible element ie. [], jQuery Object
      id = window.setInterval(function(){ 
        if (++i === object.length || callback.call(object[i], i, object[i]) === false) 
          clearInterval(id);
      }, speed);
    }
    return object;
  }
});



$(document).ready(function() {

  switchTestimonial();
  homeVacanciesSetup();
  
  $('li.vacContent').each(function() {
     $(this).css({
                'height' : ($(this).parent().find('li.vacTitle').height() > 20) ? '77px' : '92px'
                });

  });   

});


function homeVacanciesSetup(type) {
  
  if(!type) {
    $.ajax({
       type: "POST",
       url: siteUrl + "apprentice_vacancies/page_count",
       success: function(pages){
         $('#vacancySliderPages').val(pages);
         if(parseInt(pages) >= 2) {
           type = 'right';
           setupInit();
         }
       }
    });     
  }
  else {
    setupInit();
  }
   
  function setupInit() {

    if(type == 'right' || type == 'both') {
      $('#vacanciesScrollRight').removeClass('vacancyNoHover');
      $('#vacanciesScrollRight img').attr('src', 'assets/shared/images/home-scroller-arrow-right.png');
      
      $('#vacanciesScrollRight').bind('click', function() {
        
          disableButtons();
          
          $('#homeVacanciesContent ul').animate({
                                'opacity' : 0
                                }, 500, function() {
                                    
          });
          setTimeout(function() {
            $('#homeVacanciesContent ul li').empty();
          }, 550);  
          setTimeout('getData(\'right\')', 600);
           
      });
      
    }


    
    if(type == 'left' || type == 'both') {
      $('#vacanciesScrollLeft').removeClass('vacancyNoHover');   
      $('#vacanciesScrollLeft img').attr('src', 'assets/shared/images/home-scroller-arrow-left.png');
      
      $('#vacanciesScrollLeft').bind('click', function() {
        
        disableButtons();
        
        $('#homeVacanciesContent ul').animate({
                              'opacity' : 0
                              }, 500, function() {
                      
        });
        setTimeout(function() {
          $('#homeVacanciesContent ul li').empty();
        }, 550);
        setTimeout('getData(\'left\')', 600);

      });
    }

  }
    
}



function getData(direction) {
  
  var sliderCount = parseInt($('#vacancySliderCount').val());
  var pageTotal   = parseInt($('#vacancySliderPages').val());
  
  switch(direction) {
    case 'right':
    $('#vacancySliderCount').val(sliderCount + 1);
      sliderCount++;
      break;
    case 'left':
    $('#vacancySliderCount').val(sliderCount - 1);
    sliderCount--;
    break;
  }
    
   $.ajax({
      url: siteUrl + "apprentice_vacancies/get_vacancies/" + (sliderCount),
      dataType: 'json',
      success: function(data) {
        var count = 0;
        $(data).each(function(index) {
          $('#vacUl' + count + ' li.vacTitle').append('<h3>' + this.job_title.substring(0,54) + '</h3>');

          $('#vacUl' + count + ' li.vacContent').css({
                                              'height' : ($('#vacUl' + count + ' li.vacTitle').height() > 20) ? '77px' : '92px'
                                              });
          
          var content = this.content.substring(3, this.content.indexOf('</p>'));
          var contentMaxLen = ($('#vacUl' + count + ' li.vacTitle').height() > 20) ? 140 : 150;
                                   
          if(content.length >= contentMaxLen) {
              $('#vacUl' + count + ' li.vacContent').append(content.substring(0, content.indexOf(' ', contentMaxLen - 10)) + '...');
            }
          else {
            $('#vacUl' + count + ' li.vacContent').append(content);
          }
          
          $('#vacUl' + count + ' li.vacDetailLink').append('<a href="' + siteUrl + 'apprentice_vacancies/show/' + this.job_title.split(" ").join("_") + '">Full Details</a>');
          
          $('#vacUl' + count).animate({
                                'opacity' : 1
                                }, 500);     
          count++;
        });
       
       
       if(sliderCount < pageTotal) {
         homeVacanciesSetup('right');
       }
       if(sliderCount > 1) {
         homeVacanciesSetup('left');
       }
       
      }
    });
      
}

function disableButtons() {
  
  $('#vacanciesScrollRight, #vacanciesScrollLeft').unbind('click');
  $('#vacanciesScrollLeft img').attr('src', 'assets/shared/images/home-scroller-arrow-left-out.png');
  $('#vacanciesScrollRight img').attr('src', 'assets/shared/images/home-scroller-arrow-right-out.png');
  
}


function switchTestimonial() {
  
  var content = '#testimonialsContent p';
  var client = '#testimonialsContent span';
  var noCache = Math.floor(Math.random()*1111111);
  
  $.ajax({
    url: siteUrl + 'latest_testimonials/json_list/' + noCache,
    dataType: 'json',
    success: function(data) {
      $.eachDelay(data, function(index, item) {
      
         $('#testimonialsContent span, div.testimonialInner').animate({
                                                      "opacity" : 0
                                              }, 500, function() { 
                                  addTestimonialData(item.client_name, item.content)
                  });  
                  
         if(index == data.length - 1) {
            switchTestimonial(); 
         }    
                                      
       }, 10000); 
    }
  });    
}


function addTestimonialData(client_name, content) {
  $('#testimonialsContent span').html(client_name);
  
    content = content.substring(3, content.indexOf('</p>', 3));
    if(content.length >= 140) {
      content = content.substring(0, content.indexOf(' ', 135)) +  '...'; 
    }
    $('div.testimonialInner').html('<p>' + content + '</p>');
    $('#testimonialsContent span, div.testimonialInner').delay(500).animate({
                                        "opacity" : 1
                                      }, 500, function() {
                                        // Animation complete.
                                                      }); 
                                                      
}

 
