//JQuery Setup
$(document).ready(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) {
		// Hover fix, adds .sfhover class on rollover
		$('#nav li').bind('mouseenter mouseleave', function(){
			$(this).toggleClass('sfhover');
		});
		//IE6 Upgrade Warning
		//set cookie handler
		var alertBox = $.cookie('ie_alert');
		//if cookie val "noshow" then hide message - user has already seen this message!
		if(alertBox == 'noshow') {
		$('#alert').css('display', 'none');
		}else{
		$('#alert').css('display', 'block');
		}
		
		//click to close behavior
		$('.ie-alert-link').click(function() {
		$('#alert').slideToggle("slow");
		$.cookie('ie_alert', 'noshow', { expires: 14 });
		});
		// 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://"]').not('[href*="cdofpalisades.com"]').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');

	// stretch the content area to match the expanded navigation column
  $('#content').css('min-height', $('#nav-container').height());

	// Add 'back to top' link when the content is taller than the window
	if ($('#content').height() > $(window).height()) {
		$('#content').append('<p class="clear"><a href="#hd">Back to top</a> &uarr;</p>');
	}

	// wrap img.caption in a div and use the title attr as a caption  
	$("#content img.caption").each(function(i) {
		var $img = $(this);
		var caption = $img.attr('title');
		var width = $img.attr('width') + 10;
		$img.wrap('<div class="inset" style="width:'+width+'px;"></div>').after("<p>" + caption + "</p>");
	});

	// ***** flash headlines *****
	$('#content h1').flash(
		{
			src: 'flash/headlines.swf',
			width: 430,
			height: 30,
			wmode: 'transparent',
			flashvars: {'h1': $('#content h1').html()}
		},
		{
			version: 8,
			update: false // if flash not installed, don't change anything
		}
	);
	
	// ***** Teeth 101 *****
  $('.anatomy-of-a-tooth-link').click(function() {
    $('#anatomy-of-a-tooth').toggle();
  });

  $('.flash-know-your-teeth-link').click(function() {
    $('#flash-know-your-teeth').toggle();
  });

  $('.flash-brushing-and-flossing-link').click(function() {
    $('#flash-brushing-and-flossing-dental').toggle();
  });

	// ***** 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 }
		);
	});



	// ***** 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' + 'DC3185' // 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: 'brushing-and-flossing-dental',
				config: {
					flashvars: {
						brushing: 'true',
						flossing: 'true'
					}
				}
			},
			
			{name: 'know-your-teeth', config: {width:500, height:400}}
		]
	};


	// 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
