$(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){
        return '<li class="' + jQuery(slide).attr("id") + '"><a href="#">'+$(slide).find("<div.tab-text h2").text()+'</a></li>';
      } else {
        return '<li class="' + jQuery(slide).attr("id") + '"><a href="#">'+$(slide).find("<div.tab-text h2").text()+'</a></li>';
      }
    }
  });

  $('.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();

  // get videos from Youtube
  $("#events-news .yplaylist ul").youtubeFeed({
    "feedtype" : "author",
    "results"  : "list",
    "url"      : "http://gdata.youtube.com/feeds/api/videos?alt=jsonc&v=2&author=HCAWestFloridaHealth&max-results=4&orderby=published&callback=?"
  });
  
// 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","");
    }
  });
  
  $(".custom-select a").click(function(){
    $('#promo-tabs-container').cycle('pause');
    var this_id = $(this).parent().attr("id");
    var this_class = $(this).parent().attr("class");
    $('#'+this_class+'-'+this_id).toggle();
  });
  $(".custom-listing ul li").click(function(){
    var selectedValue = $(this).text();
    var this_id = $(this).closest("div").attr("id").replace(/custom-select-/,"");
    $("#"+this_id+".custom-select").find("a span").text(selectedValue);
    $("#"+this_id+".custom-select").find("input[name=keywords]").val(selectedValue);
    $(this).closest(".custom-listing").css("display","none");
  });
  $(".custom-listing").mouseleave(function(){
    if($(".custom-listing").is(":visible")){
      $(this).toggle();
    }
  });
  
});
