$(function(){

    $.fn.qtip.styles.tipAbove = { // Last part is the name of the style
        background:'#f8f4de',
        border:{width:2,color:'#ece1a5',radius:2},
        color:'#c6a05d',
        fontSize:'11px',
        fontWeight:'bold',
        padding:'2px',
        textAlign:'center',
        tip:'bottomMiddle'
  };

  $('#nav-main ul').superfish({ 
    delay:       1000,                            // one second delay on mouseout 
    animation:   {opacity:'show',height:'show'},  // fade-in, slide-down animation 
    speed:       'fast',                          // faster animation speed 
    autoArrows:  false,                           // disable arrow mark-up 
    dropShadows: true                             // disable drop shadows 
  });

  $('#promo-tabs-container') 
  .after('<ul id="promo-tabs-selectors">') 
  .cycle({ 
    cleartype:  1, 
    timeout: 8000, 
    effect: 'fade',
    speed: 500,
    pause: true,
    pager:  '#promo-tabs-selectors',
    pagerEvent:   'click',
    pagerAnchorBuilder: function(idx, slide) { 
      if(jQuery(slide).next().length > 0){
        /*jQuery(slide).next().attr("")*/
        return '<li class="' + jQuery(slide).attr("id") + '"><a href="#"><img src="' + jQuery(slide).find("div.image img").attr("src").replace(/960x250.jpg/,"70x50.png") + '"></a></li>';
      }
      else{
        return '<li class="' + jQuery(slide).attr("id") + '"><a href="#"><img src="' + jQuery(slide).find("div.image img").attr("src").replace(/960x250.jpg/,"70x50.png") + '"></a></li>';
      }
    },
    after: function(currSlideElement, nextSlideElement, options, forwardFlag){
      if($('#promo-tabs-next-item').length == 0){
        $('#promo-tabs-selectors').append('<li id="promo-tabs-next-item"><h2></h2></li>');
      }
    
      if($(nextSlideElement).next().find('div.promo-text h3').length > 0){
        $('#promo-tabs-next-item h2').text('Next: ' + $(nextSlideElement).next().find('div.promo-text h3').text());
      } else {
        $('#promo-tabs-next-item h2').text('Next: ' + $(nextSlideElement).parent('div:first').find('#promo-content1 div.promo-text h3').text());
      }
    }
  });

  $('.promo-text').hover(function(){
    $('#promo-tabs-container').cycle("pause");
  }, function(){
    $('#promo-tabs-container').cycle("resume");
  });
  
  $('#promo ul').hover(function(){
    $('#promo-tabs-container').cycle("pause");
  }, function(){
    $('#promo-tabs-container').cycle("resume");
  });
  
  $('#home #content-sub1').tabs();
  $('#tabs.call-to-action').tabs();

// add hover to service items

  $('.featured-services li').hover(function(){
    $(this).addClass('service-hover');
  }, function(){
    $(this).removeClass('service-hover');
  }).click(function() {
    window.location = $(this).find('h3 a').attr('href'); // find the h3 a of each service 
  });
  
  $('.form').validate();
  $('form .phone').mask('(999) 999-9999',{placeholder:""});

  $('#site-search a').qtip({
    width:{max:200},
    style:'tipAbove',
    position:{corner:{target:'topMiddle',tooltip:'bottomMiddle'}}
  });

  $('#home #health-encyclopedia a').qtip({
    width:{max:200},
    style:'tipAbove',
    position:{corner:{target:'topMiddle',tooltip:'bottomMiddle'}}
  });
  
  $('#interior #health-encyclopedia a').qtip({
    width:{max:200},
    style:'tipAbove',
    position:{corner:{target:'topMiddle',tooltip:'bottomMiddle'}}
  });
 
  /* content and social media share */
  $(window).bind('scroll',function(){
    if($(window).scrollTop() > 335){
      $("#social-share-panel").css("border-bottom","1px #c6c6c6 solid");
      $("#social-share-panel").css("width","100%");
      $("#social-share-panel").css("position","fixed");
      $("#social-share-panel").css("top","0%");
      $("#social-share-panel").css("left","0%");
      $("#social-share-panel").css("padding-top","6px");
      $("#social-share-panel").css("z-index","999");
      $("#social-share-panel").css("background","#fff");
      $("#social-share-buttons").css("margin","0 auto");
      $("#social-share-buttons").css("width","940px");
    } else{
      $("#social-share-panel").css("border","");
      $("#social-share-panel").css("width","");
      $("#social-share-panel").css("position","");
      $("#social-share-panel").css("top","");
      $("#social-share-panel").css("left","");
      $("#social-share-panel").css("padding-top","0");
      $("#social-share-panel").css("z-index","");
      $("#social-share-buttons").css("margin","");
      $("#social-share-buttons").css("width","");
    }
  });

});
