//JQuery Setup
$(function(){
 
//Important functions to fire first!
//-------------------------------------------------------------------------------

	//IE6 duct tape
	$('#nav ul li, #utility-nav ul li').bind('mouseenter mouseleave', function(){
		$(this).toggleClass('sfhover');
	});
	
	//IE6 Upgrade Warning
	//set cookie handler
	var displayAlertBox = $.cookie('sc_ie_alert');
	//if cookie val 'false' then hide message - user has already seen this message!
	if(displayAlertBox == 'false')
	{
		$('#ie_alert').css('display', 'none');
	}
	else
	{
		$('#ie_alert').css('display', 'block');
	}
	//click to close behavior
	$('.ie-alert-link').click(function(){
		$('#ie_alert').slideToggle("slow");
		$.cookie('sc_ie_alert', 'false', { expires: 14 });
	});
	
	//png fix
	//must reference jquery.pngFix1.2.js in order to work
	//$(document).pngFix();

// Utilities
//-------------------------------------------------------------------------------
	
	//jcIR
	//check if images are enabled then run replacement
	// $('<img src="images/home-aao.gif'+ '#' + Math.random() + '"/>').load(function() {
	//	$('.jcir').jcIR({
	//		image_dir: 'headings',//no slashes
	//		image_ext: '.jpg',//file extention of replacement image
	//		elem_width: '648',//width of element being replaced
	//		elem_height: '69'//height of element being replaced
	//	});
	// });

	//Fancybox
	// $("a.fancy").fancybox({
	//	'hideOnContentClick': true
	// });
	
 //Google Analytics
	// $.jGoogleAnalytics(
	// 	'', // Your GA tracker code
	// 	{
	// 		anchorClick: true, // adds click tracking to *all* anchors
	// 		domainName: 'www.wellsorthodontics.com', // e.g. 'nottingham.ac.uk'
	// 		pageViewsEnabled: true // can be disabled e.g. if only tracking e.g. click events
	// 	}
	// );
	
	// Cycle
	$('#slides').cycle({
		timeout: 8000,
		cleartypeNoBg: true
	});
	
	$('#marquee').cycle({
		fx: 'scrollRight',
		timeout: 8000,
		cleartypeNoBg: true
	});

// Standard Flash Modules
//-------------------------------------------------------------------------------
	
	// Define the default configuation values
	// followed by individual module configuration.
	// Values for individual modules will override the defaults
	var flashModules = {
		
		defaults: {
			width: 500,
			height: 300,
			wmode: 'transparent',
			flashvars: {
				autoPlay: false, // change value to 'true' to play on start
				thisColor: '0x' + '91aa03' // change global hex color (default=639CCE)
			},
			pluginOptions: {version: 9}
		},
	
	modules: [
		// Define the configuration values for each flash module.
		// Change item values in any 'flashvars' section to false to omit,
		// Add any value to override the default value in flashModuleDefaults.
		
		{name: 'homepage', config: {width:330, height:330}},
		{name: 'braces-diagram', config: {height: 375}},
		{name: 'braces-diagram-espanol', config: {height: 375}},
		{name: 'braces-diagram-iberian', config: {height: 375}},
		{name: 'brace-painter', config: {height: 430, wmode:'opaque'}},
		{name: 'brace-painter-espanol', config: {height: 430, wmode:'opaque'}},
		{name: 'brace-painter-iberian', config: {height: 430, wmode:'opaque'}},
		
			
		{name: 'brushing-and-flossing-ortho',
			config: {
				flashvars: {
					brushing: true,
					flossing: true
				}
			}
		},

		{name: 'brushing-and-flossing-dental',
			config: {
				flashvars: {
					brushing: true,
					flossing: true
				}
			}
		},

		{name: 'clarity', config: {width: 520, height: 390}},

		{name: 'clarity-smart-clip', config: {width: 520, height: 390}},
		
		{name: 'color-your-retainer', config: {width: 300, height: 300, flashvars: {autoPlay: true}}},
		
		{name: 'common-treatments',
			config: {
				flashvars: {
					crowding: true,
					openbite: true,
					deepOverbite: true,
					missing: true,
					underbite: true,
					spacing: true,
					overbite: true,
					nonBraces: true,
					phaseI: true
				}
			}
		},

		{name: 'common-treatments-espanol',
			config: {
				flashvars: {
					crowding: true,
					openbite: true,
					deepOverbite: true,
					missing: true,
					underbite: true,
					spacing: true,
					overbite: true,
					nonBraces: true,
					phaseI: true
				}
			}
		},

		{name: 'common-treatments-iberian',
			config: {
				flashvars: {
					crowding: true,
					openbite: true,
					deepOverbite: true,
					missing: true,
					underbite: true,
					spacing: true,
					overbite: true,
					nonBraces: true,
					phaseI: true
				}
			}
		},
		
		{name: 'damon-system-comparison', config: {width:250, height:165, wmode:'opaque'}},
		
		{name: 'emergency-care',
			config: {
				flashvars: {
					pokingWire: true,
					bracket: true,
					looseWire: true,
					appliance: true,
					headgear: true,
					soreness: true
				}
			}
		},

		{name: 'emergency-care-espanol',
			config: {
				flashvars: {
					pokingWire: true,
					bracket: true,
					looseWire: true,
					appliance: true,
					headgear: true,
					soreness: true
				}
			}
		},

		{name: 'emergency-care-iberian',
			config: {
				flashvars: {
					pokingWire: true,
					bracket: true,
					looseWire: true,
					appliance: true,
					headgear: true,
					soreness: true
				}
			}
		},

		
		{name: 'ibraces-logo', config: {width:200, height:135, wmode:'opaque'}},
		
		{name: 'know-your-teeth', config: {width:500, height:400}},
		
		{name: 'eastside-office-tour',
			config: {width:500, height:375, wmode:'opaque',
			flashvars: { 
			caption01: 'Office Building',
			caption02: 'Reception Desk',
			caption03: 'Patient Finger-touch Check-in Station',
			caption04: 'Waiting Area',
			caption05: 'Brushing Station',
			caption06: 'New Patient Exam Room',
			caption07: 'Patient Exam Area',
			caption08: 'Patient Exam Area',
			caption09: 'Exam Chairs',
			caption10: 'Consultation Room',
			caption11: '',
			caption12: '',
			caption13: '',
			caption14: '',
			caption15: '',
			caption16: ''
				}
			}
		},
		
		{name: 'westside-office-tour',
			config: {width:500, height:375, wmode:'opaque',
			flashvars: { 
			caption01: 'Office Building',
			caption02: 'Reception Desk',
			caption03: 'Waiting Area',
			caption04: 'Waiting Area',
			caption05: 'Hall Leading to Exam Rooms',
			caption06: 'Exam Room',
			caption07: 'Exam Room',
			caption08: 'Exam Room',
			caption09: 'Lab',
			caption10: 'Patient Consultation Room',
			caption11: '',
			caption12: '',
			caption13: '',
			caption14: '',
			caption15: '',
			caption16: ''
				}
			}
		},
	
		{name: 'palatal-expander', config: {width: 200, height: 150}},
		{name: 'patient-care', config: {height: 375}},

		{name: 'smile-gallery',
			config: {
				flashvars: {
					bonding: true,
					bridges: true,
					crowns: true,
					fillings: true,
					implants: true,
					invisalign: true,
					veneers: true,
					whitening: true
				}
			}
		},

		{name: 'smile-gallery-espanol',
			config: {
				flashvars: {
					bonding: true,
					bridges: true,
					crowns: true,
					fillings: true,
					implants: true,
					invisalign: true,
					veneers: true,
					whitening: true
				}
			}
		},
		
		{name: 'types-of-appliances',
			config: {
				flashvars: {
					elastics: true,
					headgear: true,
					herbst: true,
					palatal: true,
					positioners: true,
					separators: true
				}
			}
		},

		{name: 'types-of-appliances-espanol',
			config: {
				flashvars: {
					elastics: true,
					headgear: true,
					herbst: true,
					palatal: true,
					positioners: true,
					separators: true
				}
			}
		},

		{name: 'types-of-appliances-iberian',
			config: {
				flashvars: {
					elastics: true,
					headgear: true,
					herbst: true,
					palatal: true,
					positioners: true,
					separators: true
				}
			}
		},
		
		{name: 'types-of-braces',
			config: {
				flashvars: {
					metal: true,
					gold: false,
					ceramic: true,
					invisible: true,
					lingual: true
				}
			}
		},

		{name: 'types-of-braces-espanol',
			config: {
				flashvars: {
					metal: true,
					gold: true,
					ceramic: true,
					invisible: true,
					lingual: true
				}
			}
		},

		{name: 'types-of-braces-iberian',
			config: {
				flashvars: {
					metal: true,
					gold: true,
					ceramic: true,
					invisible: true,
					lingual: true
				}
			}
		}
	]
	};
	
	// Loop through the defined modules 
	// and do flash replacement for any that are on the current page
	for (var j = flashModules.modules.length - 1; j >= 0; j--){
		var module = flashModules.modules[j];
		// combine default config settings with individual module config settings
		var modConfig = $.extend({}, flashModules.defaults, module.config);
		// combine default flashvars with module flashvars
		modConfig.flashvars = $.extend({}, flashModules.defaults.flashvars, module.config.flashvars);
		modConfig.src = 'flash/' + module.name + '.swf';
		$('#flash-' + module.name).flash(modConfig, flashModules.defaults.pluginOptions);
	}

// Flash Video functions
//-------------------------------------------------------------------------------

var flashVideos = {
	
		defaults: {
			// Define the configuation values applied to each module.
			// Each module in flashModules can override these values.
			
			src: 'video/flvPlayer.swf',
			width: 320,
			height: 266,
			wmode: 'transparent',
			menu: false,
			allowFullScreen: true,
			flashvars: {
				autoStart: false, // change value to true to play on start
				showScaleModes: false, //set to false to disable scale modes menu
				smoothVideo: true, //set to false to disable video smoothing 
				hiddenGui: false
			},
			pluginOptions: {version: 9}
		},
	
		videos: [
			{name: 'the-damon-system',config:{width: 240, height: 206}},
			{name: 'the-damon-system-2',config:{width: 400, height: 330}},
			{name: 'in-ovation',config:{}},
			{name: 'invisalign',config:{width: 360}},
			{name: 'ibraces-1',config:{}},
			{name: 'ibraces-2', config: {width: 352}},
			{name: 'ibraces-3', config: {width: 352}},
			{name: 'opalescence', config: {width: 400, height: 251}},
			{name: 'your-video', config: {width: 600, height: 364, flashvars: {hiddenGui: false}}},
			{name: 'suresmile', config: {height: 206}},
			{name: 'suresmile-robot', config: {height: 242, flashvars: {startImage: 'none', autoStart: true}}}
		]
	};
	for (var j=0; j < flashVideos.videos.length; j++) {
		var video = flashVideos.videos[j];
		// combine default config settings with module config settings
		var vidConfig = $.extend({}, flashVideos.defaults, video.config);
		// combine default flashvars with module flashvars
		vidConfig.flashvars = $.extend({}, flashVideos.defaults.flashvars, video.config.flashvars);
		vidConfig.flashvars.flvToPlay = video.name + '.flv';
		vidConfig.flashvars.startImage = 'video/' + video.name + '.jpg';
		$('#video-' + video.name).flash(vidConfig, flashVideos.defaults.pluginOptions);
	};

// Game room
//-------------------------------------------------------------------------------
	
	// list the games that we want activated on click
	var games = Array('asteroids', 'frogger', 'pong', 'simon', 'snake', 'space-invaders', 'tetris', 'tic-tac-toe');
	
	// set the click behavior for a game link
	var setGameClick = function(game){
	$("a." + game).click(function(){
		$('#sesame-game').flash( // #sesame-game is the empty div that will contain the active game
			{
			src: 'games/' + game + '.swf',
			width: 400,
			height: 300,
			wmode: 'transparent'
			},
			{ version: 8 }
		);
	});
	};
	
	// loop through the games array and
	// set the click behavior for each game link
	for (var j=0; j<games.length; j++){
	setGameClick(games[j]);
	};

//Toggle functions
//---------------------------------------------------------------------------------
	
	// Show only when javascript is available
	$('.toggle-link').css('display','block');

	//hide lists first!
	$('#toggle-content div').css('display','none'); 

	//toggle
	function toggleInfoContent(id){
		if($('#' + id).css('display') != 'none'){
			$('#' + id).hide();
		}else{
			$("#toggle-content div").hide();
			$('#' + id).show();
		}
	}
	$(".toggle-anatomy").click(function(){
			toggleInfoContent('anatomy-of-a-tooth');
			return false;
	});
	
	$(".toggle-teeth").click(function(){
			toggleInfoContent('flash-know-your-teeth');
			return false;
	});

	$(".toggle-suresmile").click(function(){
			toggleInfoContent('video-suresmile-robot');
			return false;
	});
	//------
	
	//Teeth for life
	$('.anatomy-of-a-tooth-link').click(function() {
		$('#anatomy-of-a-tooth').slideToggle("slow");
	});
	$('.flash-know-your-teeth-link').click(function() {
		$('#flash-know-your-teeth').slideToggle("slow");
	});

	//Ortho Dictionary
	$('.parts-of-braces-link').click(function() {
		$('#parts-of-braces').slideToggle("slow");
	});
	$('.ortho-procedures-link').click(function() {
		$('#ortho-procedures').slideToggle("slow");
	});

//Basic HTML functions
//---------------------------------------------------------------------------------

	// add 'back to top' link when the content is taller than the window
	// this has to be done after flash replacement because the content height changes
	if ($('#content').height() > $(window).height()) {
		$('.back-to-top').removeClass('hide');
	}
	
	// Some effects rely on an element to be initially hidden,
	// but we only hide them if the user has javascript
	$('.jshide').addClass('hide');
	
	
	//clear form fields
	$('.clearme').one("focus", function() {
		$(this).val("");
	});

	// Open external links in new windows
	$('a[href^="http://"]').not('[href*="orthosesame.com"]').addClass('external').attr('target', '_blank');
	
	// Open pdf links in new windows
	$('a[href$=".pdf"]').addClass('pdf').attr('target', '_blank');

	// Anti-spam email obfuscator
	//-- requires <script type="text/javascript" src="http://10.scripts.sesamehost.com/scripts/jquery.jcAntiSpam.js"></script> 
	//-- change the ('#ID OF CONTAINER') to to the id of the container which holds your email
	// $('.email-container').jcAntiSpam({
	// 	emailCommonName: '',//Name displayed in link text, e.g., 'Email So-And-So'
	// 	emailName: '',//First half of primary email address; before "@" symbol
	// 	emailDomain: '',// Full Primary email domain name
	// 	emailCcName: '',//First half of CC email address; before "@" symbol
	// 	emailCcDomain: ''//CC email domain name
	// });

	//Form Functions
	//---------------------------------------------------------------------------------
			//Hide all items with class of "hidden-content"
			$('.hidden-content').css('display', 'none');

			//Sequential numbering of comment form li's
			$(".sequential-list li").each( function(i) {
			    i = i+1;
			    $(this).prepend('<span class="comment-number">' + i + '. </span>');
			});

			//toggle hidden fields on Appointment Request Form
			//New Patient Toggle...
			$("input[name^='Current_Patient']").click( function() {
				 if ($("input[name^='Current_Patient']:checked").val() == 'No')
				 	//not current patient, ask where they found us
				 	$('.hidden-content').fadeIn('slow');
				else 
					//current patient, hide content
					$('.hidden-content').fadeOut();
						if($('#found-other').css('display') != 'none') {
							$('#found-other').fadeOut();
						}
			});
			//Where did you hear about us toggle 
			$('#Found').change(function() {
				var selected = $('#Found option:selected');
				if(selected.val() == 'Other') {
					$('#found-other').fadeIn('slow');
				} else {
					$('#found-other').fadeOut();
				}
			});  

			//Comment form "May We Contact You" toggle
			$("input[name^='Contact_Me']").click( function() {
				 if ($("input[name^='Contact_Me']:checked").val() == 'Yes')
				 	//not current patient, ask where they found us
				 	$('.hidden-content')
				 		.fadeIn('slow');
				else 
					//current patient, hide content
					$('.hidden-content').fadeOut();
			});

			//Referral form "Radiographs Sent" toggle
			$("input[name^='Radiographs_Sent']").click( function() {
				 if ($("input[name^='Radiographs_Sent']:checked").val() == 'Yes')
				 	//not current patient, ask where they found us
				 	$('.hidden-content')
				 		.fadeIn('slow');
				else 
					//current patient, hide content
					$('.hidden-content').fadeOut();
			});

			/* Toggle Submit after Agreeing to Terms */
			$('input[name^="Disclaimer"]').click( function() {
				 if ($("input[name^='Disclaimer']:checked").val() == 'I Agree')
				 	//not current patient, ask where they found us
				 	$('.hidden-content').fadeIn('slow');
				else 
					//current patient, hide content
					$('.hidden-content').fadeOut();
			});

			//clear form fields with "clearme" class when clicked
			$('.clearme').one("focus", function() {
					$(this).val("");
			});

			//add red asterisk to label of required fields
			$('label.required').each(function(i) {
				$(this).append('<em> * </em>');
			});

			//Form validation
			//---------------------------------------------------------------------------------

			//masked input
			$(".date-mask").mask("99/99/9999");
			$(".phone-mask").mask("(999) 999-9999");

			//additional validation methods
			$.validator.addMethod("phone", 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");

			//validator
			$(".validate").each(function() {
				$(this).validate({
					errorElement: "div",
						success: function(label) {
							label.text("ok!").addClass("success");
						},
					rules: {
						First_Name: {
							required:true,
							minlength: 2
						},
						Last_Name: {
							required:true,
							minlength: 2
						},
						Doctor_First_Name: {
							required:true,
							minlength: 2
						},
						Doctor_Last_Name: {
							required:true,
							minlength: 2
						},
						Patient_First_Name: {
							required:true,
							minlength: 2
						},
						Patient_Last_Name: {
							required:true,
							minlength: 2
						},
						Patient_Email: {
							required: true,
							email:true
						},
						Referred_Patient_First_Name: {
							required:true,
							minlength: 5
						},
						Referred_Patient_Last_Name: {
							required:true,
							minlength: 5
						},
						Doctor_Patient_Referring: {
							required:true,
							minlength: 5
						},
						Doctor_Email: {
							required: true,
							email:true
						},
						Appointment_Email: {
							required: true,
							email:true
						},
						Daytime_Phone: {
							required: true,
							phone: true	
						},
						Alternate_Phone: {
							phone: true
						},
						Patient_Phone: {
							required: true,
							phone: true	
						},
						Message: {
							required:true,
							minlength: 12
						},
						Would_Like_To: {
							required: true
						},
						User_Name: {
							required: true
						},
						Password: {
							required: true
						},
						Question_1: {
							required: true
						},
						Question_2: {
							required: true
						},
						Question_3: {
							required: true
						},
						Question_4: {
							required: true
						},
						Question_5: {
							required: true
						},
						Question_6: {
							required: true
						},
						Question_7: {
							required: true
						},
						Question_8: {
							required: true
						},
						Question_9: {
							required: true
						},
						Question_10: {
							required: true
						},
						Patient_Name: {
							minlength: 5,
							required: "#Contact_Me_Yes:checked"
						},
						Email: {
							email:true,
							required: "#Contact_Me_Yes:checked"
						},
						txtNumber: {
							required:true,
							minlength: 5
						}
					}
				});
			});

});//end document.ready
