//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');
	});	
	
	//png fix
	$(document).pngFix();	
	

	//Image Replacement	
	//jcIR - oon this site used for everything except headings
	//check if images are enabled then run replacement
	$('<img src="images/pixel.gif'+ '#' + Math.random() + '"/>').load(function() {
		$('h1.jcir').jcIR({
			image_dir: 'titles',//no slashes
			image_ext: '.png',//file extention of replacement image
			elem_width: '550',//width of element being replaced
			elem_height: '45'//height of el;ement being replaces
		});
		$('.l-column h1.jcir').jcIR({
			image_dir: 'titles',//no slashes
			image_ext: '.png',//file extention of replacement image
			elem_width: '550',//width of element being replaced
			elem_height: '45'//height of el;ement being replaces
		});
		$('.r-column h1.jcir').jcIR({
			image_dir: 'titles',//no slashes
			image_ext: '.png',//file extention of replacement image
			elem_width: '332',//width of element being replaced
			elem_height: '45'//height of el;ement being replaces
		});
		$('.r-column h2.jcir').jcIR({
			image_dir: 'titles',//no slashes
			image_ext: '.png',//file extention of replacement image
			elem_width: '332',//width of element being replaced
			elem_height: '45'//height of el;ement being replaces
		});
		$('#nav-column dt.jcir').jcIR({
			image_dir: 'titles/nav',//no slashes
			image_ext: '.png',//file extention of replacement image
			elem_width: '308',//width of element being replaced
			elem_height: '45'//height of el;ement being replaces
		});
	}); 

	//Slideshows
	$('.controls').css('display','block');//shows prev/next controls when js is enabled
	

	//sets a class to the slideshow to set dimensions
	$('#testimonial-slider, .slideshow-wrapper, .caption').addClass('jq');


	//Testimonial Slider
	//--------------------------------
    
    //truncate testimonial text
    $('#testimonial-slider p.copy span').truncate( 200, {
        chars: /\s/,
        trail: [ '...', '' ]
    });
    

	$('#testimonial-slider').cycle({ 
    	fx:     'scrollHorz', 
    	timeout: 10000, 
    	delay:  5000,
    	next:   '#testimonial-next',
    	prev:   '#testimonial-prev'
 	});
 	
 	//Office Tour
	$(function() {	
	    $('#tour-slideshow').cycle({
	        fx:     'blindY',
	        speed:   'slow',
	    	timeout: 5000, 
	        next:   '#tour-next',
	        prev:   '#tour-prev',
	        pause:  'true',
	       	before: function() {  
	            $('#tour-caption').html(this.alt).css('display','none'); 
	        },
	       	after: function() {  
	            $('#tour-caption').html(this.alt).fadeIn('fast'); 
	        }
	    });
		$('#tour2-slideshow').cycle({
	        fx:     'blindY',
	        speed:   'slow',
	    	timeout: 5000, 
	        next:   '#tour2-next',
	        prev:   '#tour2-prev',
	        pause:  'true',
	       	before: function() {  
	            $('#tour2-caption').html(this.alt).css('display','none'); 
	        },
	       	after: function() {  
	            $('#tour2-caption').html(this.alt).fadeIn('fast'); 
	        }
	    });
	});
 	
 	//slideshows
	$(function() {	
	    $('#skating-slideshow').cycle({
	        fx:     'blindY',
	        speed:   'slow',
	        timeout: 0,
	        next:   '#skating-next',
	        prev:   '#skating-prev',
	        pause:  'true',
	       	before: function() {  
	            $('#skate-caption').html(this.alt).css('display','none'); 
	        },
	       	after: function() {  
	            $('#skate-caption').html(this.alt).fadeIn('fast'); 
	        }
	    });
	});
	$(function() {	
	    $('#skating09-slideshow').cycle({
	        fx:     'blindY',
	        speed:   'slow',
	        timeout: 0,
	        next:   '#skating09-next',
	        prev:   '#skating09-prev',
	        pause:  'true',
	       	before: function() {  
	            $('#skate09-caption').html(this.alt).css('display','none'); 
	        },
	       	after: function() {  
	            $('#skate09-caption').html(this.alt).fadeIn('fast'); 
	        }
	    });
	});
	$(function() {	
	    $('#hygiene-slideshow').cycle({
	        fx:     'blindY',
	        speed:   'slow',
	        timeout: 0,
	        next:   '#hygiene-next',
	        prev:   '#hygiene-prev',
	        pause:  'true',
	       	before: function() {  
	            $('#hygiene-caption').html(this.alt).css('display','none'); 
	        },
	       	after: function() {  
	            $('#hygiene-caption').html(this.alt).fadeIn('fast'); 
	        }
	    });
	});

	//jQuery Tabs
	$('#tabs dl').hide(); // Hide all divs
	$('#tabs dl:first').show(); // Show the first div
	$('#tabs ul li:first').addClass('active'); // Set the class of the first link to active

	$('#tabs ul li a').click(function(){ //When any link is clicked
		$('#tabs ul li').removeClass('active'); // Remove active class from all links
		$(this).parent().addClass('active'); //Set clicked link class to active
		var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
		$('#tabs dl').fadeOut(); // Hide all divs
		$(currentTab).fadeIn('slow'); // Show div with id equal to variable currentTab
	return false;
	});
//---------------------------------------------------------------------------------


//Image Functions
//---------------------------------------------------------------------------------

	// get dimensions of image and add nested span to show enlarged version is available with graphic button
	
	$('.img-wrap-enlarge img').each(function() {
		var $this = $(this);
		var width = $this.attr('width') + 4;
		var height = $this.attr('height') + 4;
		
		$('.img-wrap-enlarge a')
			.css('width',width)
			.css('height',height)
			.append('<span></span>');
	});

	//fancybox
	$("a.fancy").fancybox({ 
		'overlayOpacity': .5,
 		'zoomSpeedIn': 500, 
 		'zoomSpeedOut': 500, 
 		'overlayShow': true,	
		'easingIn': 'easeOutBack',
		'easingOut': 'easeInBack',
 		'hideOnContentClick': false
 	}); 
//---------------------------------------------------------------------------------

//Flash and Video Functions
//---------------------------------------------------------------------------------
	
	//Flash
	var flashModules = {
	
	defaults: {
	  // Define the configuation values applied to each module.
	  // Each module in flashModules can override these values.
	  
	  width: 500,
	  height: 300,
	  wmode: 'transparent',
	  flashvars: {
	    autoPlay: 'false', // change value to true to play on start
	    thisColor: '0x' + '005fa3' // change global hex color (default=639CCE)
	  },
	  pluginOptions: {
	  	version: 9,
	  	update: false
	  }
	},
	
	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: 'banner', config: {width:407, height:128}},
	  {name: 'braces-diagram', config: {height: 375}},
	  {name: 'brace-painter', config: {height: 430}},
	  
	  	
	  {name: 'brushing-and-flossing',
	    config: {
	      flashvars: {
	        brushing: 'true',
	        flossing: 'true'
	      }
	    }
	  },
	  
	  {name: 'color-your-retainer', config: {width: 300, height: 300}},
	  
	  {name: 'common-treatments',
	    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: 'ibraces-logo', config: {width:200, height:135, wmode:'opaque'}},
	  
	  {name: 'know-your-teeth', config: {width:500, height:400}},
	  
	  {name: 'smile-gallery',
	    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-braces',
	    config: {
	      flashvars: {
	        metal: 'true',
	        gold: 'true',
	        ceramic: 'true',
	        invisible: 'true',
	        lingual: 'true'
	      }
	    }
	  },
	  
	  {name: 'office-tour',
	    config: {width:500, height:375,	 
		  flashvars: { 
			caption01: '',
			caption02: '',
			caption03: '',
			caption04: '',
			caption05: '',
			caption06: '',		  
			caption07: '',		  
			caption08: '',		  
			caption09: '',		  
			caption10: ''		  
	      }	  
	    }
	  },
	
	  {name: 'palatal-expander', config: {width: 200, height: 150}},
	  {name: 'patient-care', config: {height: 375}}
	]
	};
	
	for (var j=0; j < flashModules.modules.length; j++) {
	var module = flashModules.modules[j];
	// combine default config settings with 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);
	}

	//Video
	var flashVideos = {
					
						defaults: {
						  // Define the configuation values applied to each module.
						  // Each module in flashModules can override these values.
						  
						  'src': 'video/flvPlayer.swf',
						  'dir': 'http://media.sesamehost.com/video/',//location of video and image files
						  '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 
						  }
						},
					
						videos: [
							{name: 'invisalign-best-friends', config: {'width': 320, 'height': 206}},
							{name: 'invisalign-news-travels-fast', config: {'width': 320, 'height': 206}},
							{name: 'invisalign-lobby', config:{}},
							{name: 'the-damon-system',config:{'width': 240, 'height': 206}},
							{name: 'the-damon-system-2',config:{'width': 400, 'height': 330}},
							{name: 'in-ovation',config:{}},
							{name: 'opalescence', config: {'width': 400, 'height': 251}},
							{name: 'invisalign', 
								config: {
									//dir: 'video/',//override location of video and image file
									'width': 360
								}
							},
							{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 = vidConfig.dir + video.name + '.flv';
						vidConfig.flashvars.startImage = vidConfig.dir + 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 li div').css('display','none');	

	//toggle
	function toggleInfoContent(id){
		if($('#' + id).css('display') != 'none'){
		  $('#' + id).hide();
		}else{
		  $("#toggle-content li 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;
	});
	//------
	
	
	//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
//---------------------------------------------------------------------------------
	
	//PrettyPhoto modal windows
	//---------------------------------------------------------------------------------
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'}); 
	
	// jQuery Cycle Before and After //-------------------------------------------------
	$('#before-after-cycle') // Give this ID a unique name if more than one office tour is needed on a page
	.before('<div class="before-after-nav">') // Ditto for this ID
	.cycle({
	cleartype: true, // true if clearType corrections should be applied (for IE)
	cleartypeNoBg: true, // Set to true to disable extra cleartype fixing (leave false to force background color setting on slides)
	speed: 500, // This controls speed of transition
	timeout: 0, // This controls delay between slides. Set to 0 if more than one office tour on a page, so they don't auto-play
	pager: '.before-after-nav',
	before: function() {
	$('.cycle-detail span').fadeOut(); // Give this ID a unique name if more than one office tour is needed on a page
	},
	after: function(curr, next, opts) {
	var cycle_alt = $(next).find('img').attr('alt'); // This grabs the image alt text
	$('.cycle-detail span').html(cycle_alt).fadeIn(); // This puts alt text into the caption span
	}
	}); 
	// 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 ($('body.two-col #content').height() > $(window).height()) {
	$('#content').append('<p class="top-link"><a href="#container">Back to top &uarr;</a></p>');
	}
	
	// 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://"]').addClass('external').attr('target', '_blank');
	
	// Open pdf links in new windows
	$('a[href$=".pdf"]').not('.noicon').addClass('pdf').attr('target', '_blank');

	// wrap img.caption in a div and use the title attr as a caption  
	$("#content img.caption").each(function (i) {
		var $this = $(this);
		var caption = $this.attr('alt');
		var width = $this.attr('width');
		$this.wrap('<div class="inset" style="width:'+width+'px;"></div>').after("<p>" + caption + "</p>");
	});	
	
	$('p.copy').append('<img src="images/rdquo.jpg" alt="end quote" class="quote" />');
	
	// Anti-spam email
	//-- change the ('#ID OF CONTAINER') to to the id of the container which holds your email
	$('#anti-spam').jcAntiSpam({
		emailCommonName: 'Non-Emergency Email',//Name displayed in link text, e.g., 'Email So-And-So'
		emailName: 		 'contactus',//First half of primary email address; before "@" symbol
		emailDomain: 	 'myamazingsmile.com',//Primary email domain name 
		emailCcName: 	 '',//First half of CC email address; before "@" symbol
		emailCcDomain:   ''//CC email domain name
	});

//---------------------------------------------------------------------------------


//Form Functions
//---------------------------------------------------------------------------------

 	//toggle hidden form elements - JQ 1.2.6 only!
	//$("#Found").change(onSelectChange);
	//function onSelectChange(){  
	//	var selected = $("#Found option:selected");       
	//	if(selected.val() == 'Other'){  
    //        //show the hidden field
    //        $("#found-other").toggle('slow');
	//	} else {
	//		//otherwise, hide it 
    //        $("#found-other").toggle('fast');		
	//	} 
	//}
	
	$('#Found').change(function() {
		var selected = $('#Found option:selected');
		if(selected.val() == 'Other') {
			$('#found-other').toggle();
		} else {
			$('#found-other').hide();
		}
	});  

//Form validation
//---------------------------------------------------------------------------------
	
	//masked input
	$(".date-mask").mask("99/99/9999");
	$(".phone-mask").mask("(999) 999-9999");

	//additional 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: {
				Name: {
					required:true,
					minlength: 5
				},
				Doctor_Name: {
					required:true,
					minlength: 5
				},
				Patient_Name: {
					required:true,
					minlength: 5
				},
				Patient_Patient_Referring: {
					required:true,
					minlength: 5
				},
				Doctor_Patient_Referring: {
					required:true,
					minlength: 5
				},
				Doctor_Email: {
					required: true,
					email:true
				},
				Appointment_Email: {
					required: true,
					email:true
				},
				Email: {
					email:true
				},
				Daytime_Phone: {
					required: true,
					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
				},
				txtNumber: {
					required:true,
					minlength: 5
				}
			}
		});
	});
});//end document.ready

