$(document).ready(function() {
	
	var treatment_list = $('#treatment');
	treatment_list.hide();
	$('#treatment_wrap h3 a').click(function() {
		if(treatment_list.hasClass('open')) {
			treatment_list.slideUp(500).removeClass('open');
		} else {
			treatment_list.slideDown(500).addClass('open');
			$(this).focus();
		}
	});
	$('#treatment_wrap h3 a').blur(function() {
		window.setTimeout(function() {
			treatment_list.slideUp(500).removeClass('open');
		}, 300);
	});
	var concern_list = $('#concern');
	concern_list.hide();
	$('#concern_wrap h3 a').click(function() {
		if(concern_list.hasClass('open')) {
			concern_list.slideUp(500).removeClass('open');
		} else {
			concern_list.slideDown(500).addClass('open');
			$(this).focus();
		}
	});
	$('#concern_wrap h3 a').blur(function() {
		window.setTimeout(function() {
			concern_list.slideUp(500).removeClass('open');
		}, 300);
	});


	$('#treatment_select').change(function() {

		$('label.comments').html('Comments');

		if ($(this).attr('value') == 'Other') {
			$('label.comments').append(' *');
		};
	});

	$('a.shop').click(function() {
		alert('This portion of the site is not yet operational.');
	});


	$('.storycontent p img').parents('a').fancybox();

	$('.fancy_video').fancybox({
		'padding'		: 0,
		'autoScale'		: false,
		'height':  385,
		'width':   640,
		'frameHeight':  385,
		'frameWidth':   640,
		'type'			: 'iframe',
		'hideOnContentClick': false
	});



	$('#appointment_form').submit(function() {
		var $this = $(this);
		var data = $this.serialize();
  
		$.ajax({
		  url: "/appointment/submit",
		  type: "POST",
		  dataType: "html",
		  data: data,
  
		  success: function(data) {
				$('body').append('<div class="appointment_screener"></div>');
				$('body').append('<div class="appointment_popup">'+data+'</div>');

				$('.appointment_screener').click(function() {
					$(this).remove();
					$('.appointment_popup').remove();
				});

				$('.appointment_popup').click(function() {
					$(this).remove();
					$('.appointment_screener').remove();
				});
		 },
  
		  error: function(data) {
		  }
		});
  
		return false;
	});


	$('#skin_analysis_form').submit(function() {
		var $this = $(this);
	  var data = $this.serialize();
  
		$.ajax({
		  url: "/skin_analysis/submit",
		  type: "POST",
		  dataType: "html",
		  data: data,
  
		  success: function(data) {
				$('body').append('<div class="skin_analysis_screener"></div>');
				$('body').append('<div class="skin_analysis_popup">'+data+'</div>');

				$('.skin_analysis_screener').click(function() {
					$(this).remove();
					$('.skin_analysis_popup').remove();
				});

				$('.skin_analysis_popup').click(function() {
					$(this).remove();
					$('.skin_analysis_screener').remove();
				});
		 },
  
		  error: function(data) {
		  }
		});
  
		return false;
	});



	$("#datepicker").datepicker();

	$('.lightning_box').fancybox();

	$('.scroll-pane').jScrollPane();

	$('.nav').superfish();

	$('.callout').css('opacity', '0.5');


	var config = {    
	     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
	     interval: 0, // number = milliseconds for onMouseOver polling interval    
	     over: function(){$(this).animate({opacity:1}, 'fast', function() {$(this).children('.callout_bubble').fadeIn('fast');});}, // function = onMouseOver callback (REQUIRED)    
	     timeout: 500, // number = milliseconds delay before onMouseOut    
	     out: function(){$(this).children('.callout_bubble').fadeOut('fast');$(this).animate({opacity:0.5}, 'fast');} // function = onMouseOut callback (REQUIRED)    
	};
	
	$(".callout").hoverIntent( config );

	//$('.callout').hoverIntent(function() {
	//	$(this).animate({opacity:1}, 'fast', function() {$(this).children('.callout_bubble').fadeIn('fast');});
	//}, function() {
	//	$(this).children('.callout_bubble').fadeOut('fast');$(this).animate({opacity:0.5}, 'fast');
	//});

	$('.team_select_box').click(function() {
		$('body').append('<div class="screener"></div>');
		$('ul.team_list').show();
		
		$('.screener').click(function() {
			$(this).remove();
			$('ul.team_list').hide();
		});
	});

	if ($('.team_member_info').height() < 250) {
		var img_height = $('.team_member_info').children('img.team_member_image').css('height');
		$('.team_member_info').height(img_height);
	};

	$('.before_after_gallery').cycle({
		fx:      'fade', 
    next:   '.arrow_right', 
    prev:   '.arrow_left',
		speed: 	500,
		timeout:0
	});


	$('.showcase').cycle({
		fx:      'fade', 
    next:   '.arrow_right', 
    prev:   '.arrow_left',
		pager:  '.pager',
		speed: 	2000,
		timeout:10000
	});


	$('.newsletter_open').click(function() {
		$('#newsletter_box').show('fast');
	});
	
	$('.newsletter_close').click(function() {
		$('#newsletter_box').hide('fast');
	});
	
	
	$('#newsletter_input').focus(function() {
		var content = $(this).val();

		if (content == "YOUR EMAIL HERE") {
			$(this).val('');
		};

	});


	$('.facility_thumb img').click(function() {
		var bigger = $(this).attr('alt');
		//$('.facility_img img').attr('src', bigger);
			$('.facility_img').html('<img src="'+bigger+'" />');
	});


	$('li.dropper a').click(function() {
		$(this).toggleClass('hover');
		$(this).parent('li.dropper').children('ul').slideToggle('fast');
	});


	$('.side_container.side_nav a').hover(function() {
		if ($(this).attr('class') != 'hover') {
			$(this).animate(
				{paddingLeft: '10px',
				backgroundPosition:'0px 0'}, 
				'fast');
		};
	}, function() {
		if ($(this).attr('class') != 'hover') {
			$(this).animate(
				{paddingLeft: '0',
				backgroundPosition:'-10px 0'}, 
				'fast');
		};
	});



	//AREAS OF CONCERN DROPDOWN
  $('.treatments_dropdown_button').click(function() {

		if ($('.treatment_dropdown_list:hidden')) {
     	$('body').append('<div class="screener"></div>');
     	$('.treatment_dropdown_list').slideDown('fast');

     	$('.screener').click(function() {
	      $(this).remove();
	      $('.treatment_dropdown_list').slideUp('fast');
      });

    } else {
      $('.screener').remove();
      $('.treatment_dropdown_list').slideUp('fast');
    };

  });



	//FACILITIES SCROLL
	var scrolling = false;

    var end_pos = 0;

    $('.facility_slider_inner').children('.facility_thumb').each(function(index) {
      var this_width = $(this).width();
      end_pos = end_pos+this_width+4;
    });


  jQuery(function($){
      $(".facilities_right").mousedown(function(){

        var end_pos = 0;
        $('.facility_slider_inner').children('.facility_thumb').each(function(index) {
          var this_width = $(this).width();
          end_pos = end_pos+this_width+4;
        });

        var pos = $(".facility_slider_inner").position();

        if (pos.left <= -(end_pos-645)) {
          return false;
        } else {
          scrolling = false;
          startScrolling($(".facility_slider_inner"), "-=645px");
        };

      }).mouseup(function(){
          scrolling = false;
      });
  });

  jQuery(function($){
      $(".facilities_left").mousedown(function(){
          var pos = $(".facility_slider_inner").position();
          if (pos.left >= 0) {
            return false;
          } else {
            scrolling = false;
            startScrolling($(".facility_slider_inner"), "+=645px");
          };
      }).mouseup(function(){
          scrolling = false;
      });
  });

  function startScrolling(obj, param)
  {
      obj.animate({"left": param}, "fast", function(){
          if (scrolling)
          {
                  startScrolling(obj, param);
          }
      });
  }


  $('a').click(function() {
    if ($(this).attr('href') == '#') {return false;};
  });


	$('.video_thumb img').click(function() {	

		var video_id = $(this).attr('video');
	
		$(".video").load("/videos/get_video/"+video_id, function(response, status, xhr) {
		  if (status == "error") {
		    var msg = "Sorry but there was an error: ";
		    $(".video").html(msg + xhr.status + " " + xhr.statusText);
		  }
		});
		
	});



	//VIDEO SCROLL
	var scrolling = false;

    var end_pos = 0;

    $('.video_slider_inner').children('.video_thumb').each(function(index) {
      var this_width = $(this).width();
      end_pos = end_pos+this_width+4;
    });


  jQuery(function($){
      $(".videos_right").mousedown(function(){

        var end_pos = 0;
        $('.video_slider_inner').children('.video_thumb').each(function(index) {
          var this_width = $(this).width();
          end_pos = end_pos+this_width+4;
        });

        var pos = $(".video_slider_inner").position();

        if (pos.left <= -(end_pos-645)) {
          return false;
        } else {
          scrolling = false;
          startScrolling($(".video_slider_inner"), "-=645px");
        };

      }).mouseup(function(){
          scrolling = false;
      });
  });

  jQuery(function($){
      $(".videos_left").mousedown(function(){
          var pos = $(".video_slider_inner").position();
          if (pos.left >= 0) {
            return false;
          } else {
            scrolling = false;
            startScrolling($(".video_slider_inner"), "+=645px");
          };
      }).mouseup(function(){
          scrolling = false;
      });
  });


	//PRODUCTS SCROLL
	var scrolling = false;

    var end_pos = 0;

    $('.product_slider_inner').children('.product_thumb').each(function(index) {
      var this_width = $(this).width();
      end_pos = end_pos+this_width+4;
    });


  jQuery(function($){
      $(".products_right").mousedown(function(){

        var end_pos = 0;
        $('.product_slider_inner').children('.product_thumb').each(function(index) {
          var this_width = $(this).width();
          end_pos = end_pos+this_width+4;
        });

        var pos = $(".product_slider_inner").position();

        if (pos.left <= -(end_pos-645)) {
          return false;
        } else {
          scrolling = false;
          startScrolling($(".product_slider_inner"), "-=645px");
        };

      }).mouseup(function(){
          scrolling = false;
      });
  });

  jQuery(function($){
      $(".products_left").mousedown(function(){
          var pos = $(".product_slider_inner").position();
          if (pos.left >= 0) {
            return false;
          } else {
            scrolling = false;
            startScrolling($(".product_slider_inner"), "+=645px");
          };
      }).mouseup(function(){
          scrolling = false;
      });
  });


	

	$('.product_thumb img').click(function() {	

		var product_slug = $(this).attr('product');
		var ch = $('#page_content').height();
		
			$("#page_content").load("/products/get_product/"+product_slug, function(response, status, xhr) {

				if (status == "success") {

					$('#page_content').height(ch);
					$('#page_content').css('background', '#FFF url("/images/structure/ajax-loader.gif") no-repeat center center');

					setTimeout(function() {
						$('#page_content').css('background-image', 'none');
						$('.product_img').fadeIn('fast');
						$('#product_description').fadeIn('fast');
						$('#page_content').css('height', 'auto');
					}, 800);
				}

			  if (status == "error") {
			    var msg = "Sorry but there was an error: ";
			    $("#page_content").html(msg + xhr.status + " " + xhr.statusText);
			  }
			});

	});



	$('#the_newsletter').submit(function() {

		var $this = $(this);
    var data = $this.serialize();

		$.ajax({
		  url: "/newsletter/submit",
		  type: "POST",
		  dataType: "html",
		  data: data,
	
		  success: function(data) {
						$('#newsletter_form').html(data);
		 },

		  error: function(data) {
				    $('#newsletter_form').html(data);
		  }

		});

		return false;

	});


	//$('#the_newsletter').submit(function() {
	//    var $this = $(this);
	//    var data = $this.serialize();
  //
	//    $.ajax({
	//        url:        'http://www.emergemedicalspas.com/newsletter/submit',
	//        dataType:   'jsonp',
	//        data:        data,
	//        processData: false,
	//        success:     function (data) {
	//					$('#newsletter_form').html(data);
	//        },
	//				error:     function (data) {
	//					$('#newsletter_form').html(data);
	//        }
	//    });
	//    return false;
  //
	//});



	$('.send_to_a_friend_open').click(function() {
		$('#send_to_a_friend_box').show('fast');
	});
	
	$('.send_to_a_friend_close').click(function() {
		$('#send_to_a_friend_box').hide('fast');
	});
	
	
	$('#send_to_a_friend_input').focus(function() {
		var content = $(this).val();

		if (content == "YOUR FRIEND'S EMAIL HERE") {
			$(this).val('');
		};

	});



	$('#the_send_to_a_friend').submit(function() {
		var $this = $(this);
    var data = $this.serialize();

		$.ajax({
		  url: "http://www.emergemedicalspas.com/send_to_a_friend/submit",
		  type: "POST",
		  dataType: "html",
		  data: data,
	
		  success: function(data) {
						$('#send_to_a_friend_form').html(data);
		 },

		  error: function(data) {
				    $('#send_to_a_friend_form').html(data);
		  }

		});

		return false;

	});


});