(function($) { // Making $ accessible even in noConflict
  $.fn.simpleTabs = function(options){
    options = $.extend({
      buttonsContainer: '.buttons',
      sectionsContainer: '.sections'
    },options);
    
    return $(this).each(function(){
      var tabs = $(this);
      var buttons = tabs.find(options.buttonsContainer).children();
      var sections = tabs.find(options.sectionsContainer).children();

      buttons.bind('click', function(){
        buttons.add(sections).removeClass('selected');
        $(this).addClass('selected');
        var index = $(this).prevAll($(this).get(0).tagName).length;
        sections.eq(index).addClass('selected');
      });
      buttons.first().click();
    });
  };
})(jQuery);

(function($) { // Making $ accessible even in noConflict

  $(document).ready(function(){
    
    // Adding placeholders for form fields
    if($.fn.placeholder)
    {
      $(".placeholder").placeholder();
    }
    
    $('#contact-form').attr('action', '/cmail.php');
    
    // Setting up contact form
    /*
		if($.fn.TFContactForm)
    {
      $('#contact-form').TFContactForm();
    }
		*/
    
    if($.fn.fixDisplayTableCell)
    {
      $('#menu').fixDisplayTableCell();
    }
    
    if($.fn.simpleTabs)
    {
      $('.tabs').simpleTabs();
      $('.inner-tabs').simpleTabs();
      $('.ask-an-expert-tabs').simpleTabs();
    }

    $('.side-menu-dentures').each(function(){
      var links = $(this).find('a');
      links.bind('click', function(){
        var item = $(this).parent().prevAll().length;
        $('.denture-tabs ul.buttons > li').eq(item).click();
        return false;
      });
    });
    
    if($.fn.jcarousel)
    {
      $('.carousel-boxes').jcarousel({
        scroll: 1,
        animation: 200
      });
    }
    
    if($.fn.jScrollPane)  
    {
      $('.video-thumbnails').jScrollPane();
    }
		
		$('#clinic-location').change(function () {
      var section =  $(this).val();
      if (section != '')
      {
        window.location = '/clinics/' + section + '/index.html';
      }
    });
    
    var player = $('#buto-video-iframe');
    $('.video-link').bind('click', function(e){
      e.preventDefault();
      e.stopPropagation();
      var id = $(this).attr('href').replace('#','');
      player.attr('src', 'http://embed.buto.tv/' + id);
    });
		
		$('.view-photos').click(function () {
			var $this = $(this),
					photos = $('.patient-photos'),
					duration = 781;
					
			if (! $this.hasClass('open')) {
				$this.addClass('open');
				photos.css('opacity', 0);
				photos.animate({
					opacity: 1,
					height: 'toggle'
				}, duration);
				$this.html('Hide photos');
			}
			
			else {
				$this.removeClass('open');
				photos.animate({
					opacity: 0,
					height: 'toggle'
				}, duration);
				$this.html('View photos');
			}
			
			return false;
		});
	  
	  if ($.fn.fancybox) {
	  	$('.fancybox').fancybox({
  			'titlePosition': 'inside'	
  		});
			$('.genics-fancy').fancybox({
				autoDimensions: true,
				width: 460,
				height: 471
			});
  	}
		
		if ($.fn.accordion) {
		  $('.accordion').accordion();
	  }
		
		var inAnim = false,
		female = true;
		$('#masculine').mouseenter(function () {
			if (inAnim != true) {
				inAnim = true;
				$(this).animate({
					opacity: 0
				},
				500,
				function () {
					$('#masculine').css({
						'z-index': 1,
						'opacity': 1
					});
					$('#feminine').css({
						'z-index': 2
					});
					inAnim = false;
				});
				female = true;
			}
		});
		
		$('#feminine').mouseleave(function () {
			if (inAnim != true) {
				inAnim = true;
				$(this).animate({
					opacity: 0
				},
				500,
				function () {
					$('#feminine').css({
						'z-index': 1,
						'opacity': 1
					});
					$('#masculine').css({
						'z-index': 2
					});
					inAnim = false;
				});
				female = false;
			}
		});

		$('#masculine').click(function () {
			if (female == false) {
				$('#masculine').mouseenter();
			}
		});

		$('#feminine').click(function () {
			if (female == true) {
				$('#feminine').mouseleave();
			}
		});
		
		$('#compare-form').attr('action', '/cmail.php');
		$('#compare-form').compareClinics();
  });
})(jQuery);

$(window).load(function () {
	var slider = $('.slider');
	slider.cycle({
		fx: 'fade',
		speed: 'slow',
		timeout: 6250,
		pager: '.pager'
	});
});
