//JQuery Setup
$(function(){
 
	//IE6 duct tape
	$.browser.msie6 = $.browser.msie && /MSIE 6\.0/i.test(window.navigator.userAgent) && !/MSIE 7\.0/i.test(window.navigator.userAgent);
	if ($.browser.msie6) {
	$('#nav li').hover(
	  function() {
	    $(this).addClass("sfhover");
	  },
	  function() {
	    $(this).removeClass("sfhover");
	  }
	);
	// Add other IE6 only code here
	}
	
    
	// 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="#">Back to top &uarr;</a></p>');
	}
	$('.parts-of-braces-link').click(function() {
		$('#parts-of-braces').slideToggle("slow");
	});
	$('.ortho-procedures-link').click(function() {
		$('#ortho-procedures').slideToggle("slow");
	});

 	//toggle hidden form elements
	$("#Found").change(onSelectChange);
	
	//Hide div w/id extra
    $("#found-other").css("display","none");

	function onSelectChange(){  
		var selected = $("#Found option:selected");       
		if(selected.val() == 'Other'){  
            //show the hidden field
            $("#found-other").show("slow");
		} else {
			//otherwise, hide it 
            $("#found-other").hide("fast");		
		} 
	}  
 
	// 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('title');
		var width = $this.attr('width') + 10;
		$this.wrap('<div class="inset" style="width:'+width+'px;"></div>').after("<p>" + caption + "</p>");
	});
	
	//form validation
	//additional methods
	$("div.error").css("display","none");
	$.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
	var validator = $("#html-referral-form, #html-comment-form, #html-playlist-form, #html-contact-form").bind("invalid-form.validate", function() {
		$("#summary").html("Your form contains " + validator.numberOfInvalids() + " errors, see details below.");
	}).validate({
		errorElement: "div",
			success: function(label) {
				label.text("ok!").addClass("success");
			},
		rules: {
			Patient_Date: {
				date: true
			},
			Doctor_Date: {
				date: true
			},
			Appointment_Date: {
				required: true,
				date: true
			},
			Name: {
				required:true,
				minlength: 5
			},
			contact_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
			},
			Patient_Email: {
				required: true,
				email:true
			},
			Email: {
				email:true
			},
			contact_email: {
				required: true,
				email:true
			},
			Daytime_Phone: {
				required: true,
				phone: true	
			},
			Patient_Phone: {
				required: true,
				phone: true	
			},
			contact_phone: {
				required: true,
				phone: true	
			},
			contact_message: {
				required: true,
				minlength: 5
			},

			txtNumber: {
				required:true,
				minlength: 5
			}
		}
	});


	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' + '46469A' // 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: 'home', config: {width:528, height:234}},
	  {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: 'false'
	      }
	    }
	  },
	  {name: 'before-and-after',
	    config: {width:500, height:400,	 
		  flashvars: { 
			caption01: 'A 9 1/2 year old girl came to us because one of her front teeth had not grown in. The x-ray revealed that it had failed to erupt due to a lack of adequate space. With interceptive (or early) treatment, we were able to regain some space for the tooth and it was able to grow into her mouth. Once it had grown in a bracket was placed for a short time to bring the tooth into place.',
			caption02: 'A deep bite is when the front teeth overlap excessively. As you see in this photo, the lower front teeth are completely hidden by the upper front teeth. Over time, the result is often severe wearing of the teeth. This patient wore limited braces on the upper teeth only (braces on the front four teeth, anchored to the rear molars with bands) for about 12 months.',
			caption03: 'The before picture shows the teeth of an 11 year old boy who was still sucking his thumb. This habit results in his front teeth not touching; he has an open bite.  The after image shows the patient\'s teeth six months later. Note how the teeth have spontaneously come together once the habit was stopped.',
			caption04: 'This young man came to us with an open bite - his front teeth failed to touch. Proper alignment/overlap of the front teeth is important in order for the teeth and jaws to function properly. The front teeth guide the movements of the jaws when you chew. Over time, people with open bites tend to have excessive wear on their back teeth. This series shows the effect of braces and conscientious rubber band wear over a period of 24 months. The patient\'s teeth now function as they were intended and he has a beautiful smile as an added bonus!',
			caption05: 'This young lady came to us to correct a crossbite. She had a narrow upper jaw causing her lower back teeth to be outside her upper teeth. She was also missing a tooth on the top right. Her treatment consisted of widening her upper jaw with a "Rapid Palatal Expander" (or rapid Maxillary expander), the placement of braces to align her teeth, and the placement of a bridge by her dentist to replace the missing tooth. After the braces, her smile is nicer - broader, with better lip support.'		  
	      }	  
	    }
	  },
	
	  {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);
	}
  
});


// game room
$(function() {
	// 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]);
	};
});

// embeded flash video clips
$(function() {
	// define which video files to embed
	var clips = Array('the-damon-system', 'in-ovation', 'ibraces-1', 'ibraces-2', 'ibraces-3', 'suresmile-1', 'suresmile-2');
	
	var embedFlashVideo = function(clip, targetDiv){
	$(targetDiv).flash(
	  {
	  src: 'video/flvPlayer.swf',
	  width: 320,
	  height: 285,
	  wmode: 'transparent',
	  menu: false,
	  flashvars: {
	    flvToPlay: clip + ".flv", //link is relative to SkinPath, not site root
	    autostart: "false",
	    startImage: clip + ".jpg",//image will be name of movie + .jpg
	    allowFullScreen: "true" //set to false to disable full-screen
	  	}
	  },
	  { version: 8 }
	);
	};
	
	// enable each video clip
	for (var j=0; j<clips.length; j++){
	var video = clips[j];
	embedFlashVideo(video, 'div#content div#video-'+ video);
	};
	
// Quicktime Movie Clips
// assigned by class identifier. The href in the link on-page designates what file to play
$('.qt').media({ width: 400, height: 300, autoplay: false,caption: false}); 

	
});