//JQuery Setup
$(function(){

//IE6 duct tape
	$('#util li').bind('mouseenter mouseleave', function(){
		$(this).toggleClass('sfhoverTop');
	});
	$('#nav li').bind('mouseenter mouseleave', function(){
			$(this).toggleClass('sfhover');
	});	
//pngfix
	$(document).pngFix();
	
// ***** Utilities *****
	
// 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"]').addClass('pdf').attr('target', '_blank');
	
// 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()) {
	$('#content').append('<p class="top-link"><a href="#container">Back to top &uarr;</a></p>');
	}

//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");
	});


//Toggle hidden form elements
	$(".toggle-field").change(onSelectChange);
	
//Hide div w/id extra
    $("#schedule-other").css("display","none");
    $("#found-other").css("display","none");

	function onSelectChange(){  
		var selected = $(this);       
		if(selected.val() == 'Other'){  
            //show the hidden field
            $(this).next(".other").css("display","block");
		} else {
			//otherwise, hide it 
            $(this).next(".other").css("display","none");		
		} 
	}

//jcIR
	//check if images are enabled then run replacement
	$('<img src="http://www.google.com/intl/en_ALL/images/logo.gif'+ '#' + Math.random() + '"/>').load(function() {
		$('#content h1').jcIR({
		image_dir: 'headings', // no slashes
		image_ext: '.gif',   // file extention of replacement image
		elem_width: '640',   // width of element being replaced
		elem_height: '60'    // height of element being replaced
		});
	});

// ***** Game Room *****
	$('ul#sesame-games a').click(function(e){
		var game = $(e.target).attr('class'); // infers the game name from the class of the <a> tag
		// not sure why, but if the link is an image,
		// we have to get the link's class name as the parent of the <img> tag
		game = game ? game : $(e.target).parent().attr('class');
		$('#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 }
		);
	});

	//form validation
	//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
				},
				Email: {
					required: true,
					email:true
				},
				Daytime_Phone: {
					required: true,
					phone: true	
				},
				Patient_Phone: {
					required: true,
					phone: true	
				},
				Message: {
					required:true,
					minlength: 12
				},
				Would_like_to: {
					required: true
				},
				txtNumber: {
					required:true,
					minlength: 5
				}
			}
		});
	});


// ***** 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' + '55542D' // change global hex color (default=639CCE)
			},
			pluginOptions: {version: 8}
		},
	
	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:689, height:258}},
	  {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: 'office-tour',
	    config: {width:500, height:375,	 
		  flashvars: { 
			caption01: 'Entrance',
			caption02: 'Reception Desk',
			caption03: 'Waiting Room',
			caption04: 'Consultation Room',
			caption05: 'Open-Bay Patient Area',
			caption06: 'Tooth Brushing Station',		  
			caption07: 'New Faces Without Braces',		  
			caption08: 'Kids Area',		  
			caption09: 'Our Patients',		  
			caption10: ''		  
	      }	  
	    }
	  },
	
	  {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: '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: 'false',
	        ceramic: 'true',
	        invisible: 'true',
	        lingual: 'false'
	      }
	    }
	  }
	]
	};
	
	// 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 *****
	// Enable flash video on any div whose id starts with "video-"
	// Infer the name of the video clip from the rest of the id
	// e.g. <div id="video-the-damon-system"> looks for the file "the-damon-system.flv"
	$("div[@id^='video-']").flash(null, { version: 8 }, function(opts){
		var $this = $(this);	
		var videoClip = $this.attr('id').replace(/^video-/,'');
		opts.src = 'video/flvPlayer.swf';
		opts.width = 350;
		opts.height = 280;
		opts.wmode = 'transparent',
		opts.menu = false,
		opts.flashvars = {
			flvToPlay: videoClip + ".flv",
			smoothVideo: true,
			showScaleModes: false,
			autoStart: false,
			startImage: "video/" + videoClip + ".jpg",
			allowFullScreen: false
		}
		this.innerHTML = '<div class="alt">'+this.innerHTML+'</div>';
		$this.addClass('flash-replaced').prepend($.fn.flash.transform(opts));
	});





});//end document.ready
