//-- Sesame Design Global JavaScript --// 
//-- Created January 25th 2010 --//
//----------------------------------------//
//-- All plugin/jquery function additions should be added in the below format, within the 'globalLib' var, then initialized after the jQuery $(function(){
//
//		 	,COMMON_NAME_OF_LIBRARY_ITEM_TO_BE_INITIALIZED :
//			{
//				init : function()
//				{
//					YOUR CODE HERE
//				}
//			} -----Add comma here only if you add more rules; the last rule should not have a comma here	 

var globalLib =
{


//preload css images
//---------------------------------------------------------------------------------
	cssImage_preload :
	{
		init : function()
		{
			$.xLazyLoader({
			    js: 'http://15.scripts.sesamehost.com/scripts/jquery.preloadCssImages.min.js',
			    name: 'preloadCssImages',
			    success: function(){
			        $.preloadCssImages();
			    }
			});
		
		}//end init
	}//end image preloader
	
//Form Validation
//---------------------------------------------------------------------------------
	,form_validation :
	{
		init : function()
		{
			//Hide all items with class of "hidden-content"
			$('.hidden-content').css('display', 'none');
			
			//Sequential numbering of comment form li's
			$(".sequential-list li").each( function(i) {
			    i = i+1;
			    $(this).prepend('<span class="comment-number">' + i + '. </span>');
			});
			
			//toggle hidden fields on Appointment Request Form
			//New Patient Toggle...
			$("input[name^='Current_Patient']").click( function() {
				 if ($("input[name^='Current_Patient']:checked").val() == 'No')
				 	//not current patient, ask where they found us
				 	$('.hidden-content').fadeIn('slow');
				else 
					//current patient, hide content
					$('.hidden-content').fadeOut();
						if($('#found-other').css('display') != 'none') {
							$('#found-other').fadeOut();
						}
			});
			//Where did you hear about us toggle 
			$('#Found').change(function() {
				var selected = $('#Found option:selected');
				if(selected.val() == 'Other') {
					$('#found-other').fadeIn('slow');
				} else {
					$('#found-other').fadeOut();
				}
			});  
		
			//Comment form "May We Contact You" toggle
			$("input[name^='Contact_Me']").click( function() {
				 if ($("input[name^='Contact_Me']:checked").val() == 'Yes')
				 	//not current patient, ask where they found us
				 	$('.hidden-content')
				 		.fadeIn('slow');
				else 
					//current patient, hide content
					$('.hidden-content').fadeOut().find('input').val("");
			});
		
			//Referral form "Radiographs Sent" toggle
			$("input[name^='Radiographs_Sent']").click( function() {
				 if ($("input[name^='Radiographs_Sent']:checked").val() == 'Yes')
				 	//not current patient, ask where they found us
				 	$('.hidden-content')
				 		.fadeIn('slow');
				else 
					//current patient, hide content
					$('.hidden-content').fadeOut().find('input').val("");;
			});
			
			//clear form fields with "clearme" class when clicked
			$('.clearme').one("focus", function() {
					$(this).val("");
			});
		
			//add red asterisk to label of required fields
			$('label.required').each(function(i) {
				$(this).append('<em> * </em>');
			});
			
			
			//Form validation
			//---------------------------------------------------------------------------------
			$.xLazyLoader({
				js: ['http://20.scripts.sesamehost.com/scripts/jquery.validate.js',
					'http://13.scripts.sesamehost.com/scripts/jquery.maskedinput-1.2.2.min.js'],
				name: 'validate',
				success: function(){
					//masked input
					$(".date-mask").mask("99/99/9999");
					$(".phone-mask").mask("(999) 999-9999");
				
					//additional validation 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
					$("#bd form.validate").each(function() {
						$(this).validate({errorElement: "div",
							errorClass: "error",
							validClass: "success",
							success: function(label) {label.text("ok!").addClass("success");},
							rules: {
								First_Name: 					{required:true,minlength:2},
								Last_Name: 						{required:true,minlength:2},
								Doctor_First_Name: 				{required:true,minlength:2},
								Doctor_Last_Name: 				{required:true,minlength:2},
								Patient_First_Name: 			{required:true,minlength:2},
								Patient_Last_Name: 				{required:true,minlength:2},
								Patient_Email: 					{required:true,email:true},
								Referred_Patient_First_Name:	{required:true,minlength:5},
								Referred_Patient_Last_Name: 	{required:true,minlength:5},
								Doctor_Patient_Referring: 		{required:true,minlength:5},
								Doctor_Email: 					{required:true,email:true},
								Appointment_Email: 				{required:true,email:true},
								Daytime_Phone: 					{required:true,phone:true},
								Alternate_Phone: 				{phone:true},
								Patient_Phone: 					{required:true,phone:true},
								Message: 						{required:true,minlength:12},
								Would_Like_To: 					{required:true},
								Patient_Name: 					{required:"#Contact_Me_Yes:checked",minlength:5},
								Email: 							{required:"#Contact_Me_Yes:checked",email:true},
								txtNumber: 						{required:true,minlength: 5}
							}
						});
					});
				}
			});
		}//end form init()
		
	}//end form validation
	

//PrettyPhoto modal windows
//---------------------------------------------------------------------------------
	,modal_windows :
	{
		init : function() 
		{
			/*gets plugin then runs function*/
			$.xLazyLoader({
			    js: 'http://16.scripts.sesamehost.com/scripts/jquery.prettyPhoto.2.5.6.js',
			    css: 'css-main/prettyPhoto.css',
			    name: 'prettyPhoto',
			    success: function(){
					$("a[rel^='prettyPhoto']").prettyPhoto({
						animationSpeed: 'normal', 		/* fast/slow/normal */
						default_width: 900,
						default_height: 700,
						opacity: 0.65, 					/* Value betwee 0 and 1 */
						showTitle: false, 				/* true/false */
						allowresize: true, 				/* true/false */
						counter_separator_label: '/', 	/* The separator for the gallery counter 1 "of" 2 */
						theme: 'dark_rounded', 			/* light_rounded / dark_rounded / light_square / dark_square / facebook */
						hideflash: false, 				/* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
						wmode: 'transparent', 				/* Set the flash wmode attribute */
						autoplay: true, 				/* Automatically start videos: True/False */
						modal: false 					/* If set to true, only the close button will close the window */
					});    
				}
			});
					
		}//end modal init
	}//end modal windows

// Game room
//-------------------------------------------------------------------------------
	,games :  
	{
		init : function()
		{
			//Partial Sliding (Only show some of background)  
			$('ul#sesame-games li').hover(function(){  
			    $(".cover", this).stop().animate({top:'36px'},{queue:false,duration:160});  
			}, function() {  
			    $(".cover", this).stop().animate({top:'0px'},{queue:false,duration:160});  
			});  
		}//end games init
	}//end games
	

//Basic HTML and utility functions
//---------------------------------------------------------------------------------
	,html_functions :
	{
		init : function() 
		{
			// Some effects rely on an element to be initially hidden,
			// but we only hide them if the user has javascript
			$('.jshide').addClass('hide');

			//prepend "More in this Section" to sub_nav
			//$('p.sub_nav').prepend('<strong>More in this section: </strong>');
			
			// Open external links in new windows
			$('a[href^="http://"], a[href^="https://"]')
				.not('[href*="orthosesame.com"],[href*="sesameinteractive.com"], a[href$=".doc"], a[href$=".pdf"]')
				.addClass('external').attr('target', '_blank');
			
			// Open pdf links in new windows + add icon
			$('a[href$=".pdf"]').attr('target', '_blank').not('[class*="noicon"]').append('<span/>').addClass('pdf');
			
			// Open M$ doc links in new windows + add icon
			$('a[href$=".doc"]').attr('target', '_blank').not('[class*="noicon"]').append('<span/>').addClass('doc');
		
			// add icons to links which open in prettyPhoto
			$("a[rel^='prettyPhoto[flash]']").parent('li').append('<span/>').addClass('video-link');
		
		 
			//Ordered List Style
			$('ol.alpha, ol.numeric').addClass('js');
			$("ol li").each(function (i) {
			    $(this).wrapInner('<span/>');
		    });
		    
		    //fancy Q & A
		    $('ul.q-and-a li').each( function() {
		    	$(this).find('h2,h4,h3').prepend('<span>Q: </span>');
		    	$(this).find('div').prepend('<span>A: </span>');
			});

			//Toggle functions
			//---------------------------------------------------------------------------------
			// Show only when javascript is available
			$('#toggle-links').css('display','block');
			
			//hide lists first!
			$('#toggle-content dl').hide();
			
			//toggle
			function toggleInfoContent(id){
				if($(id).css('display') != 'none'){
				  $(id).fadeOut('fast');
		  		  $('.back-to-top').addClass('hide');
				}else{
				  $('#toggle-content dl').fadeOut('fast');
				  $(id).fadeIn('slow');
				  $('.back-to-top').removeClass('hide');
				}
			}
			$("#toggle-links").bind('click', function(e) {
				var target = e.target, // e.target grabs the node that triggered the event.
				$target = $(target);  // wraps the node in a jQuery object
				var id = $target.attr('href')
			    if (target.nodeName === 'A') {
			    	toggleInfoContent(id);
				}
			    return false;
			});
			
			// Emergency Care toggle //----------------------------------
			//hide items first!
			$('#toggle-emergency div p').hide();
			//toggle
			function toggleEmergency(id){
				if($(id).css('display') != 'none'){
					$(id).animate({opacity: 'toggle'}).parent().animate({width: '80px',height: '60px'});
				}else{
					$(id).animate({opacity: 'toggle'}).parent().animate({width: '200px',height: '150px'});
				}
			}
			$("a.toggle-div").bind('click', function(e) {
				var target = e.target, // e.target grabs the node that triggered the event.
				$target = $(target);  // wraps the node in a jQuery object
				var id = $target.attr('href')
				if (target.nodeName === 'A') {
					toggleEmergency(id);
				}
				return false;
			});
			//end toggle functions
			
			
	
			//Back to top
			if ($('#content').height() > $(window).height()) {
				$('.back-to-top').removeClass('hide');
			}
			
			//pt/dr login tooltip functions
			$('#pt-login-form a, #dr-login-form a').bind('mouseover', function(e){
				var target = e.target, // e.target grabs the node that triggered the event.
					$target = $(target);  // wraps the node in a jQuery object
					$target.animate({opacity: '.65'}, 400).bind('mouseout', function() {
						$target.animate({opacity: '1'}, 400);
					});
			});

			//fade sidebar callouts
			$('#sidebar img').hover( function() {
				$(this).animate({opacity: '.65'}, 400);
			}, function() {
				$(this).animate({opacity: '1'},400);
			});
		}//end HTML init
	}//end HTML functions


// slideshows	
//-------------------------------------------------------------------------------
	,gallery_view_tour :
	{
		init : function()
		{
			// jQuery GalleryView Office Tour //----------------------------------------------
			$.xLazyLoader({
			    js: ['http://11.scripts.sesamehost.com/scripts/jquery.galleryview-2.0.js',
			    	'http://7.scripts.sesamehost.com/scripts/jquery.easing.1.3.js',
			    	'http://19.scripts.sesamehost.com/scripts/jquery.timers-1.1.2.js'],
			    name: 'galleryView',
			    success: function(){
			    	//customize me
					$('#gallery').galleryView({
						panel_width: 460,
						panel_height: 350,
						frame_width: 80,
						frame_height: 60,
						transition_speed: 600,
						easing: 'easeInOutBack',
						transition_interval: 0,
						nav_theme: 'dark'
					});
					//end
			    }
		});

		}//end slideshow init
	}//end gallery view tour
	
// jQuery Cycle Office Tour //----------------------------------------------------
	,cycle_tour :
	{
		init : function()
		{
			// jQuery cycle slideshow
			$.xLazyLoader({
			    js: 'http://6.scripts.sesamehost.com/scripts/jquery.cycle.2.73.all.min.js',
			    name: 'cycleTour',
			    success: function(){
					// jQuery Cycle Plugin
					$('ul.slideshow').cycle({
						pause: true,
						wmode: 'transparent'
					});
				
					// Duplicate the following block of code to add extra office tours
					$('#cycle-office-tour') // Give this ID a unique name if more than one office tour is needed on a page
					.before('<div class="office-tour-nav" id="office-1">') // Ditto for this ID
					.cycle({
						cleartype: true, // true if clearType corrections should be applied (for IE)
						cleartypeNoBg: true, // Set to true to disable extra cleartype fixing (leave false to force background color setting on slides)
						speed: 500, // This controls speed of transition
						timeout: 5000, // This controls delay between slides. Set to 0 if more than one office tour on a page, so they don't auto-play
						pager:  '#office-1',
						before: function() {
							$('#caption span').fadeOut(); // Give this ID a unique name if more than one office tour is needed on a page
						},
						after: function(curr, next, opts) {
							var alt = $(next).find('img').attr('alt'); // This grabs the image alt text
							$('#caption span').html(alt).fadeIn(); // This puts alt text into the caption span
						}
					});
				}
			});
		
		}//end init
	}//end cycle tour

// jQuery Cycle Slideshow (homepage) //----------------------------------------------------
	,cycle_slideshow :
	{
		init : function()
		{
			// jQuery cycle slideshow
			$.xLazyLoader({
			    js: 'http://6.scripts.sesamehost.com/scripts/jquery.cycle.2.73.all.min.js',
			    name: 'cycleSlideshow',
			    success: function(){
					//Home Page Slideshow 
					var $ss = $('#slideshow-home'); 
				         
				    // add slides to slideshow (images 2-8) 
				    for (var i = 1; i < 11; i++) 
				        $ss.append('<img src="images/photos/slide'+i+'.png" width="872" height="162" alt="slide" />'); 
				 
				    // start the slideshow 
				    $('#slideshow-home img:first').fadeIn(1000, function() {
					    $ss.cycle({
					    	random:	1,
					        fx:     'turnDown',
					        speed:   '1000',
					        timeout: 7000
					    });
					});
			    }
			});
		}//end slideshow init
	}//end cycle slideshow

	
// jQuery Cycle Before and After //-------------------------------------------------
	,cycle_before_after :
	{
		init : function() 
		{
			$.xLazyLoader({
			    js: 'http://6.scripts.sesamehost.com/scripts/jquery.cycle.2.73.all.min.js',
			    name: 'cycleBeforeAfter',
			    success: function(){
			    	//customize me
					$('#before-after-cycle') // Give this ID a unique name if more than one office tour is needed on a page
					.before('<div class="before-after-nav">') // Ditto for this ID
					.cycle({
						cleartype: true, // true if clearType corrections should be applied (for IE)
						cleartypeNoBg: true, // Set to true to disable extra cleartype fixing (leave false to force background color setting on slides)
						speed: 500, // This controls speed of transition
						timeout: 0, // This controls delay between slides. Set to 0 if more than one office tour on a page, so they don't auto-play
						pager:  '.before-after-nav',
						before: function() {
							$('.cycle-detail span').fadeOut(); // Give this ID a unique name if more than one office tour is needed on a page
						},
						after: function(curr, next, opts) {
							var cycle_alt = $(next).find('img').attr('alt'); // This grabs the image alt text
							$('.cycle-detail span').html(cycle_alt).fadeIn(); // This puts alt text into the caption span
						}
					});
					//end
				}
			});
		}//end init
	}//end cycle before/after
	
// jQuery Before After Slider //-------------------------------------------------------
	,slider_before_after : 
	{
		init : function()
		{	
			$.xLazyLoader({
			    js: ['http://6.scripts.sesamehost.com/scripts/jquery.cycle.2.73.all.min.js',
			    	'http://3.scripts.sesamehost.com/scripts/jquery.beforeafter.js'],
			    name: 'sliderBeforeAfter',
			    success: function(){
			    	//customize me
					$('.before-after-slider').beforeAfter({
						animateIntro : false,
						showFullLinks : false
					});
					
					// Cycle plugin added to above Slider
					$('#slider-cycle')
					.before('<div class="slider-nav">')
					.cycle({
						cleartype: true, // true if clearType corrections should be applied (for IE)
						cleartypeNoBg: true, // Set to true to disable extra cleartype fixing (leave false to force background color setting on slides)
						speed: 500, // This controls speed of transition
						timeout: 0, // This controls delay between slides. Set to 0 if more than one office tour on a page, so they don't auto-play
						pager:  '.slider-nav',
						before: function() {
							$('.slider-detail span').fadeOut(); // Give this ID a unique name if more than one office tour is needed on a page
						},
						after: function(curr, next, opt) {
							var slider_alt = $(next).children('.before-after-slider').children('div').siblings().find('img').attr('alt'); // This grabs the image alt text
							$('.slider-detail span').html(slider_alt).fadeIn(); // This puts alt text into the caption span
						}
					});
					//end
				}
			});
		}//end init
	}// end slider before after
	

// IE6 Duct Tape	
//-------------------------------------------------------------------------------
	,ie_functions :
	{
		init : function()
		{
		  	//detects if browser is IE by checking leadingWhitespace property. 
		  	//See: http://docs.jquery.com/Utilities/jQuery.support
			var fancyHover = $.support.leadingWhitespace;
			if(fancyHover == false) 
			{//browser is IE, use script
				$('#nav ul li, #utility-nav ul li').bind('mouseenter mouseleave', function(){
					$(this).toggleClass('sfhover');
				});	
				//PNG fix for IE6
				//$.xLazyLoader({
				//	js: 'http://2.scripts.sesamehost.com/scripts/jquery.pngFix1.2.min.js',
				//	name: 'pngFix',
				//	success: function() { 
				//		$(document).pngFix();
				//	},
				//});
		 
				//click to close behavior
				if($('.ie-alert-link').length > 0) {
					$.xLazyLoader({
						js: 'http://5.scripts.sesamehost.com/scripts/jquery.cookie.js',
						name: 'cookie',
						success: function() { 
							$('.ie-alert-link').click(function() {
								$('#ie_alert').slideToggle("slow");
								$.cookie('iealert', 'noshow', { expires: 14 });
							});
							//set cookie handler
							var alertBox = $.cookie('iealert');
							//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');
							}
						}
					});
				}
				//-- /end ie6 warning
			}//end if/else
		}//end ie init		
	}//end ie_functions


// Image Replacement	
//-------------------------------------------------------------------------------
	// Cufon & FontAvailable Image Replacement Headings
	,cufon : 
	{
		init : function()
		{		
			//Cufon + jQuery
			//--- requiresin <head>:
			//--- <script src="http://9.scripts.sesamehost.com/scripts/jquery.fontavailable-1.1.min.js" type="text/javascript"></script>
			//if (!$.fontAvailable('CorbelBold')) {
				// load the cufon library
			//	$.xLazyLoader({
			//	    js: ['http://10.scripts.sesamehost.com/scripts/cufon-yui.js',
			//	    	'scripts/CorbelBold_700.font.js'],
			//	    name: 'cufon',
			//	    success: function(){
			//            Cufon.replace('#bd h1.headline',{
			//            	fontFamily:'CorbelBold'
			//            });
			//	    }
			//	});				
			//}
			//ADD YOUR CUSTOM TEXT REPLACEMENT METHODS HERE
		}//end init
	}//end image replacement
	
	// jcIR Image Replacement Headings	
	//-------------------------------------------------------------------------------
	,jcir :
	{
		init : function()
		{
			$.xLazyLoader({
				js: 'http://22.scripts.sesamehost.com/scripts/jquery.jcIR.js',
				img: 'images/pixel.gif',
				name: 'jcIR',
				success: function() {
					//jcIR
					//check if images are enabled then run replacement
					$('.jcir').jcIR({
						image_dir: 'headings',//no slashes
						image_ext: '.jpg',//file extention of replacement image
						elem_width: '600',//width of element being replaced
						elem_height: '50'//height of element being replaced
					});
				}//end success
			});
		}//end init
	}//end jcIR


// Anti-spam email obfuscator
//-------------------------------------------------------------------------------
	,anti_spam :
	{
		init : function()
		{
			$.xLazyLoader({
				js: 'http://12.scripts.sesamehost.com/scripts/jquery.jcAntiSpam.js',
				name: 'anti_spam',
				success: function() {
					$('.my-email-address').jcAntiSpam({
						emailCommonName: 'Email Dr. Sesame',//Name displayed in link text, e.g., 'Email So-And-So'<br/>
						emailName:       'samantha',//First half of primary email address; before "@" symbol<br/>
						emailDomain: 	 'sesamedental.com',// Full Primary email domain name<br/>
						emailCcName: 	 '',//First half of CC email address; before "@" symbol<br/>
						emailCcDomain:   ''//CC email domain name<br/>
					});
				}
			});
		}//end init
	}//end anti-spam


}//end globalLib


//JQuery Setup
$(function(){


//--initialize all required library items

	
	// LINE: 40 - unused
	//globalLib.cssImage_preload.init();
	
	// LINE: 500
	globalLib.html_functions.init();

	// LINE: 727
	globalLib.ie_functions.init();

	// LINE: 777
	//globalLib.cufon.init();

	
//--initialize on-demand library items

	// jcIR
	if($('.jcIR').length > 0) {//load jcIR Image Replacement
		globalLib.jcir.init();
	}
	// Anti-Spam
	if($('.my-email-address').length > 0) {//load Anti-Spam Obfusicator
		globalLib.anti_spam.init();
	}
	//slideshows
	if($('ul#gallery').length > 0) {//load galleryView slideshow
		//
		globalLib.gallery_view_tour.init();
	}
	
	if($('#slideshow-home').length > 0) {//load cycle slideshow
		//
		globalLib.cycle_slideshow.init();
	}
	
	if($('#cycle-office-tour').length > 0) {//load cycle office tour
		//
		globalLib.cycle_tour.init();
	}
	
	if($('#before-after-cycle').length > 0) {//load cycle before after
		//
		globalLib.cycle_before_after.init();
	}
	
	if($('#slider-cycle').length > 0){//load slider before after
		//
		globalLib.slider_before_after.init();
	}//end slideshows 
	
	if($('form').length > 0) {//load form functions
		// LINE: 57
		globalLib.form_validation.init();	
	}
	
	if($("a[rel^='prettyPhoto']").length > 0){//load prettyPhoto function
		// LINE: 192
		globalLib.modal_windows.init();
	}
	
	if($('ul#sesame-games').length > 0 ) {//load game code
		// LINE: 484
		globalLib.games.init();
	}	
});//end document.ready
