/* Borland-Groover.com
   Main JavaScript Functions
   ----------------------------------------------------*/

jQuery.fn.exists = function(){return jQuery(this).length>0;}

/* cufon font replacement
   ----------------------------------------------------*/

Cufon.replace('h1', {
  hover: true
});
Cufon.replace('h2', {
  hover: true
});
Cufon.replace('h3', {
  hover: true
});
Cufon.replace('h4', {
  hover: true
});
Cufon.replace('h6', {
  hover: true
});
Cufon.replace('div#main_nav a', {
  hover: true
});
Cufon.replace('ul#physician_list li a span.full_name', {
  hover: true
});



/* value/class swap for search input
   ----------------------------------------------------*/
$(document).ready(function(){
  $('input[type="text"]').addClass("idle_field");
  $('input[type="text"]').focus(function() {
    $(this).removeClass("idle_field").addClass("focus_field");
      if (this.value == this.defaultValue){
        this.value = '';
      }
      if(this.value != this.defaultValue){
        this.select();
      }
  });
  $('input[type="text"]').blur(function() {
    $(this).removeClass("focus_field").addClass("idle_field");
    if ($.trim(this.value) == ''){
      this.value = (this.defaultValue ? this.defaultValue : '');
    }
  });
});



/* class swap for textarea
   ----------------------------------------------------*/
$(document).ready(function(){
  $('textarea').addClass("idle_field");
  $('textarea').focus(function() {
    $(this).removeClass("idle_field").addClass("focus_field");
  });
  $('textarea').blur(function() {
    $(this).removeClass("focus_field").addClass("idle_field");
  });
});



/* homepage featured content slider
   ----------------------------------------------------*/

$(document).ready(function() {
  if ($('#slider').exists()) {
    $('#slider section').cycle({
      after: onAfter,
      fx: 'fade',
  //    fx: 'scrollLeft',
  //    easing: 'easeInOutBack',
      pause: 1,
      speed: 10,
      timeout: 10000,
      next: '#next',
      prev: '#prev',
      pager: '#slide_controller',
      pagerAnchorBuilder: buildSlideControlls
    });
  }
});

// sets class for container specific to each slide
function onAfter(curr,next,opts){
  $('#slider').removeClass().addClass('slide_' + (opts.currSlide + 1) + '_bg');
};

// build links for slide controller
function buildSlideControlls(idx, slide) {
  return '<a href="#">'+(idx+1)+'</a>';
};


/* open links with rel="external" in a new window
   ----------------------------------------------------*/
$(function() {
	$('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});
});


/* open links with fully qualified URL in a new window
   ----------------------------------------------------*/

$(function() {
	$("a[href*='http://']:not([href*='"+location.hostname+"'])").click( function() {
		window.open(this.href);
		return false;
	});
});


/* set height for diagnosing disorders div on gi tract page so that bg image can be vertically aligned to the bottom
   ----------------------------------------------------*/

$(document).ready(function(){
  var div_height = $('body#gi_tract section#content section.main div.diagnosing_disorders').height();
  $('body#gi_tract section#content section.main div.diagnosing_disorders div.sandbag_1').css({'height' :
  div_height - 172});
});



/* initialize maps
   ----------------------------------------------------*/

$(document).ready(function(){
  if ($('#map').exists()) {
    initializeMap();
  }
  
  if ($('#distance_map').exists()) {
    initializeDistanceMap();
  }
});


/* change GI tract background image on GI education 
   index page when hovering over appropriate link
   ----------------------------------------------------*/




/* alpha sort on physician list
   ----------------------------------------------------*/

//$(document).ready(function() {
//  if ($('#physician_list').exists()) {
//    $('#physician_list').listnav({
//      initLetter: 'a',
//      includeNums: false,
//      noMatchText:'There are no matching physicians.'
//    });
//  }
//});



/* tabs - from 
   http://www.sohtanaka.com/web-design/simple-tabs-w-css-jquery/
   ----------------------------------------------------*/

$(document).ready(function() {
	//When page loads...
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).show(); //show the active ID content
    return false;
	});
});



/* animated scrolling - from
   http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links
   ----------------------------------------------------*/

$(document).ready(function() {
  function filterPath(string) {
  return string
    .replace(/^\//,'')
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
    .replace(/\/$/,'');
  }
  var locationPath = filterPath(location.pathname);
  $("a[href*=#]:not(a[href^='#tab'])").each(function() {
    var thisPath = filterPath(this.pathname) || locationPath;
    if (  locationPath == thisPath
    && (location.hostname == this.hostname || !this.hostname)
    && this.hash.replace(/#/,'') ) {
      var $target = $(this.hash), target = this.hash;
      if (target) {
        var targetOffset = $target.offset().top;
        $(this).click(function(event) {
          event.preventDefault();
          $('html, body').animate({scrollTop: targetOffset}, 300, function() {
            location.hash = target;
          });
        });
      }
    }
  });
});



/* form validation
   http://docs.jquery.com/Plugins/Validation
   ----------------------------------------------------*/

if ($('#contact_form').exists()) {
  jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
      phone_number = phone_number.replace(/\s+/g, ""); 
  	return this.optional(element) || phone_number.length > 9 &&
  		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
  }, "Please specify a valid phone number");
}

$(document).ready(function(){
  if ($('#contact_form').exists()) {
    //$("#phone").mask("(999) 999-9999");

	  // add * to required field labels
  	$('label.required').append('&nbsp;<strong>*</strong>&nbsp;');

    $('#contact_form').validate({
      rules: {
        phone: {
          required: function(element) {
            return "#email:blank";
          },
          phoneUS: true
        },
  			email: {
          required: function(element) {
  				  return "#phone:blank";
          },
  				email: true
  			},
  			captcha: "required"
      },
  		messages: {
  			name: "Please enter your name",
  			phone: "Please enter a valid phone number or email address in the box below",
  			email: "Please enter a valid email address or phone number in the box above",
  			recipient: "Select which best describes your message",
  			captcha: "Please enter the word from the image above",
  			message: "Tell us how we can assist you"
  		}
    });
  }
});

$(document).ready(function() {
  if ($('.submit_success').exists()) {
    $('.submit_success').fadeIn(1200);
  }
}); 
