/* ---------------------------------------------------
   Load Function  ------------------------------------ */

    function loadFct(){
        
        // Open link with the class "external" in a new window
		$().externalLinks();		
		
		//Button "Read more"
		$("a.b_read").bind("click", function(){
			$(this).hide().parent().next().show();		
			return false;
		});
		
		//Add mouseover on the subNav
		$("#subNav li > a").hover(
		    function(){ $(this).parents("li:first").css("backgroundPosition", "0 -39px") },
			function(){ $(this).parents("li:first").css("background-position", ""); }
		);
		
		//Animate the mouseover on the button "Learn more"
		$(".b_learn")
		.css({ backgroundPosition: "0 50%"})
		.hover(
		    function(){ $(this).stop().animate({ backgroundPosition: "100% 50%"}); },
			function(){ $(this).stop().animate({ backgroundPosition: "0 50%"}); }
		);
		
		//Add and bind the Share
		$(addUtilities())
		    .appendTo("#header")
		    .find("#b_stf")
		    .bind("click", function(){
				$.get(
			        $(this).attr('href'), 
			        { stf_url: "" + encodeURIComponent(document.location.href) + "", stf_title: "" + encodeURIComponent(document.title) +""  },
				    function(resp){ 
						animatePopin(resp);	
				    }
			    );
				
			    return false;
		    });
				
        return;
    };
    
/*  ------------------------------------------------------------------
	STF -------------------------------------------------------------- */		

	function addUtilities() {
	    var utilitiesHTML = '<ul class="utilities">\n';
	    utilitiesHTML += '	<li class="hide"><a href="#main">Go to content<\/a><\/li>\n'
		if($("body").is("#p_1")){
	        utilitiesHTML += '  <li><a href="child-development-model.php">Learn about the Child Development Model<\/a><\/li>\n';
	    }
	    utilitiesHTML += '	<li><a href="stf.php" id="b_stf">Share with a friend<\/a><\/li>\n';
	    utilitiesHTML += '<\/ul>\n';
	        
		return utilitiesHTML;
	};
		
	function animatePopin(msg){
	    var popin = $("<div class='popin'></div>");
        return popin
	           .modal({
		            closeClass:'b_close',
					overlayId:'overlay',
		            overlay: 35,
		            containerId:'popin',
		            appendToId: 'container',
		            onShow: function (dialog) {
						dialog.data.append(msg).find(":input").not(":image, :submit").hint();
				        dialog.data.find("#stf_name").focus();
						// Add javascript share
						dialog.data.find("#share_btn a").bind("click", function(event){
							var size, which = $(this).attr("class").replace('b_', '');
							switch (which) {
								case 'facebook': size = "width=620,height=436"; break;
								case 'delicious': size = "width=725,height=400"; break;
								case 'myspace': size = "width=800,height=600"; break;
								case 'twitter': size = "width=620,height=480"; break;
								default: size = "width=500,height=500";
							}					
							var my_popup = window.open($(this).attr("href"), "share_"+which, "scrollbars=yes,resizable=yes,toolbar=no,location=no,status=no,"+size);
							if (my_popup) {my_popup.focus();}
							return !my_popup;
						});

						dialog.container.animate({Shift: 10, top: getTopPosition(dialog)}, 1200, 'backout');
					}
		        });
	};
	
	// Find the top position
	function getTopPosition(dialog){
	   	var dialogHeight = dialog.container.height();
	    var winHeight =  $(window).height();
	    var top;
		if ($.browser.msie) { top = document.body.scrollTop || document.documentElement.scrollTop;} 
		else { top = window.pageYOffset; }
		top = top + winHeight/2 - dialogHeight/2 - 50;
		if ( top < 0 ) { top = 10 }
		return top + 'px';
	};
	    
    // Close Popin, called from the flash 
    function closePopin(which){
        $.modal.close();
	};
 
/*  -----------------------------------------------------------------------
    Easing ---------------------------------------------------------------- */
  	
	(function($) {
		$.easing.backout = function(x, t, b, c, d){
			var s=1.20158;
			return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
		};
    })(jQuery);	
	
/*  -----------------------------------------------------------------------
    Validation ------------------------------------------------------------ */

    function checkEmail(value) { return value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/i);  };
	function checkPhone(value) { return value.match(/^((1)?(\s)?(\+)?(\s)?(\()?\d{3}(\))?\s?-?\d{3}\s?-?\d{4}((\s)+(ext|x|extension|#)?(\s)?(\d)+)?)?$/i);  };

/*  -----------------------------------------------------------------------
    Submit Form ----------------------------------------------------------- */
    
    function checkFields(objForm){
        var error = 0;
        $(".v-mandatory", objForm).each( function(){
            if(!$(this).val()){	error++; $("label[@for=" + this.id + "]").addClass("error"); }
            else{ $("label[@for=" + this.id + "]").removeClass("error"); }	
		});
		$(".v-mandatory.v-email", objForm).each( function(){
            if(!checkEmail($(this).val())){	error++; $("label[@for=" + this.id + "]").addClass("error"); }
            else{ $("label[@for=" + this.id + "]").removeClass("error"); }	
		});
		
		return error == 0 ? true : false;
	};
	
	function clearForm(objForm){
		$('#b_submit', objForm).attr({ disabled: "", value: "Send Message"});
		$(":input", objForm).not(":image, :submit").val('');
		$("label.error", objForm).removeClass("error");
	} 
    
	function ajaxSubmit(objForm){
    	var data_params = '';
	   	
		$('#b_submit', objForm).attr({ disabled: "disabled", value: "Please wait..."});
		
		data_params += "stf_url= " + encodeURIComponent(document.location.href) + "&" ;
		$(":input", objForm).not(":image, :submit").each( function(){
			data_params += $(this).attr("name").split("_")[1] + "=" + $(this).val() + "&" ;
		});
		
		$.post(
			$(objForm).attr("action"), 
			data_params,
			function(resp){ 
				$("fieldset.toggleSuccess", objForm).toggle();
				clearForm(objForm); 			        
			}
		);
    };    
    
    function submitForm(objForm){
		if(checkFields(objForm)) {
			if ($(objForm).is(".noajax")) {
				return true;				   
			}else{ 
				ajaxSubmit(objForm); 
				return false; 
			}				
		} else { return false; }
	};
 
/*  ------------------------------------------------------------------
	External Link ---------------------------------------------------- */   
 
    (function() {
		jQuery.fn.externalLinks = function() {
			var container = this;
			
			return this.each( function() {
				jQuery("a.external", this)
				.bind("click", function(){ this.target='_blank'; /*return !window.open(this.href);*/ });
			});				
		};
    })(jQuery);	
	

/*  ------------------------------------------------------------------
	External Link ---------------------------------------------------- */  

    (function() {
		jQuery.fn.equalizeCols = function(){
			var height = 0;
	  
			return this
			.css("height", "auto")
			.each(function() { height = Math.max(height, this.offsetHeight); })
			.css("height", height)
			.each(function() {
				var h = this.offsetHeight;
				if (h > height) {
					jQuery(this).css("min-height", height - (h - height));
				};
			});
		};
    })(jQuery);	
 
 /*  ------------------------------------------------------------------
	Hint -------------------------------------------------------------- */
 
    (function() {
		jQuery.fn.hint = function(settings) {
			
			// defaults settings
			settings = jQuery.extend({  
				classFocus: "focus",  
				classOver: "hover",
				classLabelError: "error"
			}, settings);
			
			return this.each( function() {
				var elm = jQuery(this); 
				var title = elm.attr('title');
							
				elm // on focus, set value to blank if current value matches title attr
				.bind("focus", function(){ 
					//if(settings.classLabelError && elm.is(".mandatory")) { jQuery("label[@for=" + elm.attr("id") + "]").removeClass(settings.classLabelError); }
					if (title && elm.val() == title) { elm.val(''); } 
					elm.addClass(settings.classFocus); 
				})
				.bind("blur", function(){ 
					if (title && elm.val() == '') { elm.val(title); } 
					elm.removeClass(settings.classFocus);  
				})
				.addHover(settings.classOver)
				.blur();// now change all inputs 
			});				
		};
	})(jQuery);	
 
/*  ------------------------------------------------------------------
	Hover function --------------------------------------------------- */
	
	(function() {
		jQuery.fn.addHover = function(classOver) {
			var classOver = classOver || "over";
			return this.hover(
				function(){ jQuery(this).addClass(classOver); },
				function(){ jQuery(this).removeClass(classOver); }
			)
		};
	})(jQuery);	   
 
 /*  ------------------------------------------------------------------
	Hover - Fix IE6 -------------------------------------------------- */	
	try {
	  document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}  