//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 ul li').bind('mouseenter mouseleave', function(){
			$(this).toggleClass('sfhover');
		});	
	};	
	//png fix
	$(document).pngFix();	
	
	// Some effects rely on an element to be initially hidden,
	// but we only hide them if the user has javascript
	$('.jshide').addClass('hide');
	
	// Show only when javascript is available
	$('.toggle-link').css('display','block');

	
	//clear form fields
	$('.clearme').one("focus", function() {
			$(this).val("");
	});
	
	//Office Tour
	$('#slideshow').before('<ul id="slideshow-nav">').cycle({ 
	    fx:     'fade', 
	    speed:  'slow', 
	    timeout: 5000, 
	    before: function() {  
            $('#caption').html(this.alt); 
        }, 
	     
	    // callback fn that creates a thumbnail to use as pager anchor 
	    pagerAnchorBuilder: function(idx, slide) { 
	        return '<li><a href="#"><img src="' + slide.src + '" width="72" height="54" /></a></li>'; 
	    } 
	});


	//jcIR
	//check if images are enabled then run replacement
	$('<img src="images/required_note.gif'+ '#' + Math.random() + '"/>').load(function() {
		$('.jcir').jcIR({
			image_dir: 'titles',//no slashes
			image_ext: '.png',//file extention of replacement image
			elem_width: '400',//width of element being replaced
			elem_height: '36'//height of element being replaced
		});
		$('.jcir.home').jcIR({
			image_dir: 'titles',//no slashes
			image_ext: '.png',//file extention of replacement image
			elem_width: '400',//width of element being replaced
			elem_height: '63'//height of element being replaced
		});
	}); 
		
	//transparent bg
	if ( $.browser.msie ) {
		$('#content').css('background-image','url(images/trans.png)').pngFix();
	}
	

	
	//toggle functions

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

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

 	//toggle hidden form elements
	$("#Found").change(onSelectChange);
	

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

	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' + '88a82a' // change global hex color (default=639CCE)
	  },
	  pluginOptions: {version: 9}
	},
	
	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:228, height:243}},
	  {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: 'false',
	        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);
	}
  


	var flashVideos = {
	
		defaults: {
		  // Define the configuation values applied to each module.
		  // Each module in flashModules can override these values.
		  
		  src: 'video/flvPlayer.swf',
		  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 
		  },
		  pluginOptions: {version: 9}
		},
	
		videos: [
			{name: 'the-damon-system',config:{}},
			{name: 'in-ovation',config:{}},
			{name: 'ibraces-1',config:{}},
			{name: 'ibraces-2', config: {width: 352}},
			{name: 'ibraces-3', config: {width: 352}},
			{name: 'suresmile-01', config: {height: 206}}
		]
	};
	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 = video.name + '.flv';
	vidConfig.flashvars.startImage = 'video/' + 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]);
	};

});
