/*
Generic Util Function
Version 1.0
Last Update: 11/10/11
*/



$(document).ready(function () {

  /** THIS IS A HOMEPAGE FIX FOR THE SLIDER IN IE8 **/
  $('#testimonial-slider li:nth-child(2n+1)').addClass('nth-child-jquery');

  var userStatus = 0;
  if ($('#ctl00_LinkButton_Logout').length > 0) {
    userStatus = 1;
  }

  //Setup right adjustments
  var twitter = "145px";
  var facebook = "118px";
  var googleplus = "91px";

  switch (userStatus) {
    case 1:
      twitter = "155px"
      facebook = "128px"
      googleplus = "101px"
      break;
  }

  //Social display box gubbins
  $('.twitter').mouseenter(function () {
    $('.twitter_social').show()
    $('.social_overlay_arrow').attr('style', 'right:' + twitter);
    $('.social_overlay_arrow').show();
  }).mouseleave(function () {
    $('.twitter_social').hide()
    $('.social_overlay_arrow').hide();
  });

  $('.twitter_social').mouseenter(function () {
    $('.twitter_social').show();
    $('.social_overlay_arrow').attr('style', 'right:' + twitter);
    $('.social_overlay_arrow').show();
  }).mouseleave(function () {
    $('.twitter_social').hide();
    $('.social_overlay_arrow').hide();
  });


  $('.facebook').mouseenter(function () {
    $('.facebook_social').show();
    $('.social_overlay_arrow').attr('style', 'right:' + facebook);
    $('.social_overlay_arrow').show();
  }).mouseleave(function () {
    $('.facebook_social').hide()
    $('.social_overlay_arrow').hide();
  });

  $('.facebook_social').mouseenter(function () {
    $('.facebook_social').show();
    $('.social_overlay_arrow').attr('style', 'right:' + facebook);
    $('.social_overlay_arrow').show();
  }).mouseleave(function () {
    $('.facebook_social').hide();
    $('.social_overlay_arrow').hide();
  });

  $('.googleplus').mouseenter(function () {
    $('.googleplus_social').show();
    $('.social_overlay_arrow').attr('style', 'right:' + googleplus);
    $('.social_overlay_arrow').show();
  }).mouseleave(function () {
    $('.googleplus_social').hide()
    $('.social_overlay_arrow').hide();
  });

  $('.googleplus_social').mouseenter(function () {
    $('.googleplus_social').show();
    $('.social_overlay_arrow').attr('style', 'right:' + googleplus);
    $('.social_overlay_arrow').show();
  }).mouseleave(function () {
    $('.googleplus_social').hide()
    $('.social_overlay_arrow').hide();
  });

  //arrow hide/show
  $('.social_overlay_arrow').mouseenter(function () {

    //lets check the right attr
    switch ($('.social_overlay_arrow').css('right')) {
      case "91px":
      case "101px":
        $('.googleplus_social').show();
        $('.social_overlay_arrow').show();
        $('.social_overlay_arrow').mouseleave(function () {
          $('.googleplus_social').hide();
          $('.social_overlay_arrow').hide();
        });
        break;
      case "145px":
      case "155px":
        $('.twitter_social').show();
        $('.social_overlay_arrow').show();
        $('.social_overlay_arrow').mouseleave(function () {
          $('.twitter_social').hide();
          $('.social_overlay_arrow').hide();
        });
        break;
      case "118px":
      case "128px":
        $('.facebook_social').show();
        $('.social_overlay_arrow').show();
        $('.social_overlay_arrow').mouseleave(function () {
          $('.facebook_social').hide();
          $('.social_overlay_arrow').hide();
        });
        break;
    }
  });

  // To detect native support for the HTML5 placeholder attribute
  var fakeInput = document.createElement("input"),
    placeHolderSupport = ("placeholder" in fakeInput);

  // Applies placeholder attribute behavior in web browsers that don't support it
  if (!placeHolderSupport) {

    $('[placeholder]').focus(function () {
      var input = $(this);
      if (input.val() == input.attr('placeholder')) {
        input.val('');
        input.removeClass('placeholder');
      }
    }).blur(function () {
      var input = $(this);
      if (input.val() == '') {
        input.addClass('placeholder');
        input.val(input.attr('placeholder'));
      }
    }).blur().parents('form').submit(function () {
      $(this).find('[placeholder]').each(function () { //line 20
        var input = $(this);
      });
    });
  }

  //This stuff is for the sliders
  $("#slider-paging").show();
  $("#slider-paging li:first a:first").addClass("active");

  var sliderWidth = $(".sliding_window").width();
  var sliderItems = $("#testimonial-slider li").size();
  var sliderReelWidth = sliderWidth * sliderItems;

  //Adjust the image reel to its new size
  $("#testimonial-slider").css({ 'width': sliderReelWidth });

  rotateSwitch(); //Run function on launch

  //On Click
  $("#slider-paging li a").click(function () {
    $active = $(this); //Activate the clicked paging
    //Reset Timer
    clearInterval(play); //Stop the rotation
    rotate(); //Trigger rotation immediately
    //rotateSwitch(); // Resume rotation timer
    return false; //Prevent browser jump to link anchor
  });


});



//Paging  and Slider Function
function rotate() {

  //Get size of the image, how many images there are, then determin the size of the image reel.
  var sliderWidth = $(".sliding_window").width();
  var sliderItems = $("#testimonial-slider li").size();
  var sliderReelWidth = sliderWidth * sliderItems;

  var triggerID = $active.attr("rel") - 1; //Get number of times to slide
  var image_reelPosition = triggerID * sliderWidth; //Determines the distance the image reel needs to slide

  $("#slider-paging li a").removeClass('active'); //Remove all active class
  $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

  //Slider Animation
  $("ul.slider").animate({
    left: -image_reelPosition
  }, 500);

};

//Rotation  and Timing Event
 function rotateSwitch() {
  play = setInterval(function () { //Set timer - this will repeat itself every 7 seconds
    $active = $('#slider-paging li a.active').parent().next().find("a"); //Move to the next paging

    if ($active.length === 0) { //If paging reaches the end...
      $active = $('#slider-paging li:first a:first'); //go back to first
    }
    rotate(); //Trigger the paging and slider function
  }, 10000); //Timer speed in milliseconds (7 seconds)
};

