//JQuery Setup
$(document).ready(function(){
 
	// hack hack hack
	var filterSubnav = Array('about-children', 'about-adults', 'accelerated-orthodontics', 'advanced-orthodontics', 'braces-101', 'general-information', 'community-outreach');
	var pageID = $('body').attr('class');
	if ($.inArray(pageID, filterSubnav) > -1)
	{
		var $li = $('#subnav li');
		$li.hide()
		   .filter('.'+pageID)
		   .show()
		   .children().children().show();
		$('#subnav').show();
	}

	// ***** 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) {
		// Hover fix, adds .sfhover class on rollover
		$('#nav li').bind('mouseenter mouseleave', function(){
			$(this).toggleClass('sfhover');
		});
		// Add other IE6 only code here
	}
	
	// ***** 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 (remove '@' for jquery v.1.3 and up)
	$('a[href^="http://"]').addClass('external').attr('target', '_blank');
	
	// Open pdf links in new windows (remove '@' for jquery v.1.3 and up)
	$('a[href$=".pdf"]').addClass('pdf').attr('target', '_blank');

	// replaced fancybox with prettyphoto because
	// fancybox wasn't styling properly in IE
	$('a.fancybox').prettyPhoto();

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

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

	// ***** 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' + '471f3a' // change global hex color (default=639CCE)
			},
			pluginOptions: {version: 8}
		},
		
		modules: [
			// Change item values in any 'flashvars' section to false to omit,
			// Add any value to override the default.
			
			{name: 'home', config: {width:745, height:284}},
			
			{name: 'before-after',
				config: {
					width: 475,
					height: 285,
					flashvars: {
						caption01: 'Crowding',
						caption02: 'Spacing',
						caption03: 'Excess Overbite and Overjet',
						caption04: 'Deep Bite/Excess Overbite',
						caption05: 'Class III Crowding',
						caption06: 'Class III Anterior Crossbite',
						caption07: 'Posterior Crossbite',
						caption08: 'Anterior Open Bite'
					}
				}
			},
			
			
			{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: {
					width: 475,
					height: 285,
					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
				}
			},

			{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',
				width: 450,
				height: 270,
				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);
	}

});//end document.ready