$(document).ready(function() {
	
	/* General jQuery Starts */
	
	// Replacement for target="_blank" - initiate with class="new-window"
	$(function(){
		$('a.new-window').click(function(){
			window.open(this.href);
			return false;
		});
	});
	
	$(function(){
		$('#archive-listing a').click(function(){
			window.open(this.href);
			return false;
		});
	});
	
	// Creates a nice smooth scroll to the top of the page
	$('.toparrow').click(function(){
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});
	
	// Scroll to ID - initiate with class="scrolldown" - be sure to include the #href
	$('.scrolldown').click(function(){
		$(this).attr('href');
     	$('html,body').animate({scrollTop: $($(this).attr('href')).offset().top},'slow'); //Gets the HREF from the current link and scrolls to it
		return false;
	});


	// Hover code for TOP link chevrons
	$(".toparrow").hover(function() {
			
		$(this).css('background-position', 'left bottom'); // Rollover turns chevron blue
		$(this).find('a').css('color', '#ffaf00');
			
		}, function() {
					
		$(this).css('background-position', 'left top'); // Rollout turns chevron grey
		$(this).find('a').css('color', '#999');
			
	});

	// Code for the video menu - Uses jquery.selectboxes.js
	$(".videomenu").change(function() {
        var val = $(this).selectedValues();
        if (val != '') {
            location.href=val;
        }
    });
	
	// Code to toggle registration elements
	$('#toggleElement').click(function(){
    	if ($('#toggleElement').is(':checked')) {
       	 	$('.toggledisabled').attr('disabled', true);
			$('.toggledisabled').val('');
			$('.disabled').css('color', '#ccc');
			$('.toggleTable').css('display', 'none');
			$('.disabledstar').css('color', '#ccc');
   		} else {
        	$('.toggledisabled').removeAttr('disabled');
			$('.disabled').css('color', '#514141');
			$('.disabledstar').css('color', '#900');
			$('.toggleTable').removeAttr("style");
    	}   
	});
	
	// Code to toggle registration What is This
	
	$(".registration-what01").hide();
	$(".registration-what02").hide(); 

	$(".whatisthis01").click(function(){
		$(".registration-what01").toggle();
		$('.whatisthis01').text($('.whatisthis01').text() == 'Hide Message' ? 'What Is This?' : 'Hide Message');
		return false; //Prevent the browser jump to the link anchor
	});
	
	$(".whatisthis02").click(function(){
		$(".registration-what02").toggle();
		$('.whatisthis02').text($('.whatisthis02').text() == 'Hide Message' ? 'What Is This?' : 'Hide Message');
		return false; //Prevent the browser jump to the link anchor
	});
	
	// Code to show Save on Order div
	$('#vialsquantity').change(function(){
		if ($('#vialsquantity').val() >= 3 || $('#vialsquantity').val() == 0) {
			$('.divsavings').animate({opacity: 'hide', height: 'hide'}, 80); 
		} else {
			$('.divsavings').animate({opacity: 'show', height: 'show'}, 300); 
		}
	});
	
	
	// Code to show Public Listing div
	$('.whatispubliclistingclick').click(function(){
		$('.whatispubliclisting').slideToggle('fast');
		return false;
	});
	
	
	$('.readmore-betty').click(function(){
		$('.hidden-betty').slideToggle(100);
		$('.readmore-betty').text($('.readmore-betty').text() == 'Read More...' ? 'Read Less...' : 'Read More...');
		$('.hidethis').toggleClass("hidden");
		return false;
	});
	
	/* General jQuery Ends */
	
	/* Accordion Sidebar Starts */
	
	//Set default open/close settings
	$('.acc_container').hide(); // Hide/close all containers
	$('.acc_trigger:first').addClass('active01').next().show(); // Add "active" class to first trigger, then show/open the immediate next container
	
	//On Click
	$('.acc_trigger').click(function(){
		
		if ($(this).hasClass('active01') == true) { // If the div is active go no further, nothing needs to happen
			return false;
		} else { // Otherwise there's work to be done...
			$('.acc_trigger').css('background-position', 'left top'); // Turn all chevrons off, next step will turn the active on back on
			$('.acc_trigger').find('a').css('color', '#999');
			if( $(this).next().is(':hidden') ) { // If immediate next container is closed...
				$(this).css('background-position', 'left bottom'); // Current chevron back to blue
				$(this).find('a').css('color', '#ffaf00');
				$('.acc_trigger').removeClass('active01').next().slideUp("fast"); // Remove all "active" state and slide up the immediate next container
				$(this).toggleClass('active01').next().slideDown("fast"); // Add "active" state to clicked trigger and slide down the immediate next container
			}
		}
		return false; // Prevent the browser jump to the link anchor
	});
	
	// Hover code to get chevrons turning blue
	$(".acc_trigger").hover(function() {
			
		$(this).css('background-position', 'left bottom'); // Rollover turns chevron blue
		$(this).find('a').css('color', '#ffaf00');
			
		}, function() {
					
		var testactive = $(this).hasClass('active01'); // Check for active state
					
		if (testactive == false ) {  
			$(this).css('background-position', 'left top'); // If not active turn chevron grey
			$(this).find('a').css('color', '#999');
		} else {
			$(this).css('background-position', 'left bottom'); // Otherwise keep it blue
			$(this).find('a').css('color', '#ffaf00');
			
		}
			
	});
	
	/* Accordion Sidebar Ends */
	
	
	/* Image Rollovers */
	
	$('#nav-images .image01').mouseover(function() {
  		$('#nav .hover01').find('a').css({'background-color' : '#4d4d4d', 'color' : '#ffaf00'});
		$('#nav .hover01').find('ul').css({'display' : 'block'});
		$(this).find('img').css('opacity', '.6');
	}).mouseout(function(){
   		$('#nav .hover01').find('a').removeAttr("style");
		$('#nav .hover01').find('ul').removeAttr("style");
		$(this).find('img').css('opacity', '1');
  	});
	
	$('#nav-images .image02').mouseover(function() {
  		$('#nav .hover02').find('a').css({'background-color' : '#4d4d4d', 'color' : '#ffaf00'});
		$('#nav .hover02').find('ul').css({'display' : 'block'});
		$(this).find('img').css('opacity', '.6');
	}).mouseout(function(){
   		$('#nav .hover02').find('a').removeAttr("style");
		$('#nav .hover02').find('ul').removeAttr("style");
		$(this).find('img').css('opacity', '1');
  	});
	
	$('#nav-images .image03').mouseover(function() {
  		$('#nav .hover03').find('a').css({'background-color' : '#4d4d4d', 'color' : '#ffaf00'});
		$('#nav .hover03').find('ul').css({'display' : 'block'});
		$(this).find('img').css('opacity', '.6');
	}).mouseout(function(){
   		$('#nav .hover03').find('a').removeAttr("style");
		$('#nav .hover03').find('ul').removeAttr("style");
		$(this).find('img').css('opacity', '1');
  	});
	
	$('#nav-images .image04').mouseover(function() {
  		$('#nav .hover04').find('a').css({'background-color' : '#4d4d4d', 'color' : '#ffaf00'});
		$(this).find('img').css('opacity', '.6');
	}).mouseout(function(){
   		$('#nav .hover04').find('a').removeAttr("style")
		$(this).find('img').css('opacity', '1');
  	});
	
	$('#nav-images .image05').mouseover(function() {
  		$('#nav .hover05').find('a').css({'background-color' : '#4d4d4d', 'color' : '#ffaf00'});
		$(this).find('img').css('opacity', '.6');
	}).mouseout(function(){
   		$('#nav .hover05').find('a').removeAttr("style")
		$(this).find('img').css('opacity', '1');
  	});
	
	
	$('#nav .hover01').mouseover(function() {
		$('#nav-images .image01').find('img').css('opacity', '.6');
	}).mouseout(function(){
   		$('#nav-images .image01').find('img').css('opacity', '1');
  	});
	
	$('#nav .hover02').mouseover(function() {
		$('#nav-images .image02').find('img').css('opacity', '.6');
	}).mouseout(function(){
   		$('#nav-images .image02').find('img').css('opacity', '1');
  	});
	
	$('#nav .hover03').mouseover(function() {
		$('#nav-images .image03').find('img').css('opacity', '.6');
	}).mouseout(function(){
   		$('#nav-images .image03').find('img').css('opacity', '1');
  	});
	
	$('#nav .hover04').mouseover(function() {
		$('#nav-images .image04').find('img').css('opacity', '.6');
	}).mouseout(function(){
   		$('#nav-images .image04').find('img').css('opacity', '1');
  	});
	
	$('#nav .hover05').mouseover(function() {
		$('#nav-images .image05').find('img').css('opacity', '.6');
	}).mouseout(function(){
   		$('#nav-images .image05').find('img').css('opacity', '1');
  	});
	
	
	$(".closefancybox").click(function(){ 
		 $.fancybox.close();
		return false;
	});

	/* Fancybox Starts */
	
	// Basic popup window. Resizes to content, close button
	$(".popup-general").fancybox({ 
		'autoDimensions'		: true,
		'padding'				: 20,
		'transitionIn'			: 'fade',
		'transitionOut'			: 'fade',
		'hideOnOverlayClick'	: 'true',
		'overlayShow' 			: true
	});
	
	// Basic popup window. Resizes to content, close button
	$(".popup-modal").fancybox({ 
		'autoDimensions'		: true,
		'padding'				: 20,
		'transitionIn'			: 'fade',
		'transitionOut'			: 'fade',
		'centerOnScroll'		: true,
		'modal'					: true
	});
	
	// Basic popup window for endnotes. Resizes to content, close button
	$("a.endnote").fancybox({
		'autoDimensions'		: true,
		'padding'				: 20,
		'transitionIn'			: 'fade',
		'transitionOut'			: 'fade',
		'hideOnOverlayClick'	: 'true',
		'overlayShow' 			: true
	});
	
	// Image gallery. Resizes to content, close button, caption over image
	$("a[rel=imagegallery]").fancybox({
		'transitionIn'			: 'fade',
		'transitionOut'			: 'fade',
		'cyclic'				: 'true',
		'padding'				: 13,
		'titlePosition' 		: 'over',
		'titleFormat'			: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + ' - ' + (title.length ? '' + title : '') + '</span>';
			}
	});
	
	// Patient Video
	$(".video").fancybox({
		'height'			: 460,
        'width'		     	: 530,
		'titleShow'			: false,
        'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe'
	});
	
	// Patient Video UK
	$(".videouk").fancybox({
		'height'			: 370,
        'width'		     	: 530,
		'titleShow'			: false,
        'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe'
	});
	
	// Patient Video UK
	$(".videouk-tall").fancybox({
		'height'			: 440,
        'width'		     	: 530,
		'titleShow'			: false,
        'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe'
	});
	
	// Pro Video
	$(".provideo").fancybox({
		'height'			: 500,
        'width'		     	: 530,
		'titleShow'			: false,
        'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe'
	});
	
	// Pro Video UK
	$(".provideouk").fancybox({
		'height'			: 410,
        'width'		     	: 530,
		'titleShow'			: false,
        'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe'
	});
	
	// Location Form
	$(".location-pop").fancybox({
		'height'			: 250,
        'width'		     	: 250,
		'titleShow'			: false,
        'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe'
	});
	
	// Login Form
	$(".login-pop").fancybox({
		'height'			: 400,
        'width'		     	: 475,
		'titleShow'			: false,
        'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'scrolling'			: 'no',
		'type'				: 'iframe'
	});
	
	
	// License Form
	$(".license-pop").fancybox({
		'height'			: 275,
        'width'		     	: 400,
		'titleShow'			: false,
        'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'scrolling'			: 'no',
		'type'				: 'iframe'
	});
	
	// Registration Form
	$(".longform-pop").fancybox({
		'height'			: 725,
        'width'		     	: 625,
		'titleShow'			: false,
        'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe',
		'modal'				: 'true'
	});
	
	// Google Map
	$(".googlemap").fancybox({
		'height'			: 600,
        'width'		     	: 800,
		'titleShow'			: false,
        'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe'
	});
	
	// Dentist GDC # Check
	$(".dentist-pop").fancybox({
		'height'			: 260,
        'width'		     	: 260,
		'titleShow'			: false,
        'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe'
	});
	
	/* Fancybox Ends */
	
});

// Function to redirect Location
function closeFancyboxAndRedirectToUrl(url){
   	//$.fancybox.close();
   	parent.window.location = url;
}

// Function to redirect Location
function splashCloseFancyboxAndRedirectToUrl(url){
 	parent.$.fancybox.close();
   	parent.window.location = url;
}

