jQuery.noConflict();

// WHEN THE DOM IS READY...
jQuery(document).ready(function () {
	// TOOL TIPS
	jQuery(function(){
		jQuery("#global ul li ul li a").tipTip({defaultPosition: "bottom", edgeOffset: 0});
		jQuery("#process sup img").tipTip({defaultPosition: "top"});
		jQuery(".download").tipTip({defaultPosition: "top"});
	});
	
	// HEADER TOGGLES
	jQuery('#universal a.contact').click(function(){
		jQuery('#contact').slideToggle("fast");
		jQuery(this).toggleClass("on off");
	});
	jQuery('#contact a.contact').click(function(){
		jQuery('#contact').slideToggle("fast");
		jQuery('#universal a.contact').toggleClass("on off");
	});
	
	// GLOBAL NAVIGATION TOGGLES
	// ON LOAD, APPEND THE CSS NAVIGATION ELEMENTS
	//jQuery(function(){
		//jQuery('#global a').append('<div class="arrow"></div><div class="arrow-border"></div>');
	//});
	// ON LOAD, REMOVE THE ANNOYING TOOLTIPS FOR THE PARENTS
	// AND ADD A CLASS TO LET US KNOW IF THEY HAVE CHILDREN OR NOT
	jQuery(function(){
		jQuery('#global .primary li').children('a').attr('title', '');
		jQuery('#global .primary li').children('ul').parent('li').addClass('page-has-children');
	});
	// ON LOAD, DETERMINE IF WE'RE ON A TOP-LEVEL PAGE AND TARGE THE MENU APPROPRIATELY
	jQuery(function(){
		jQuery('#global .primary li.current_page_item').children('a').children('div.arrow').addClass('on');
		jQuery('#global .primary li.current_page_item').children('a').children('div.arrow-border').addClass('dine');
		jQuery('#global .primary li.current_page_item ul.children').addClass('open');
	});
	// ON HOVER, COLLAPSE ALL TOP-LEVEL EXPANSIONS
	jQuery('#global').hover(function(){
		jQuery('#global .primary li.current_page_item').children('ul').toggleClass('open');
		jQuery('#global .primary li.current_page_item').children('a').children('div.arrow').toggleClass('on over');
		jQuery('#global .primary li.current_page_item').children('a').children('div.arrow-border').toggleClass('dine over');
	});
	// ON LOAD, DETERMINE IF WE'RE ON A SUB-LEVEL AND TARGE THE MENU APPROPRIATELY
	jQuery(function(){
		jQuery('#global .primary li ul.children li.current_page_item').parent('ul').addClass('open');
		// TURN THE CURRENT CHILD ON
		jQuery('#global .primary li ul.children li.current_page_item').children('a').children('div.arrow').addClass('on');
		jQuery('#global .primary li ul.children li.current_page_item').children('a').children('div.arrow-border').addClass('dine');
		// TURN THE PARENT ON
		jQuery('#global .primary li ul.children li.current_page_item').parent('ul').parent('li').children('a').children('div.arrow').addClass('on');
		jQuery('#global .primary li ul.children li.current_page_item').parent('ul').parent('li').children('a').children('div.arrow-border').addClass('dine');
	});
	// ON HOVER, COLLAPSE ALL SUB-LEVEL EXPANSIONS
	jQuery('#global').hover(function(){
		jQuery('#global .primary li ul.children li.current_page_item').parent('ul').toggleClass('open');
		// TURN THE CURRENT CHILD ON
		jQuery('#global .primary li ul.children li.current_page_item').children('a').children('div.arrow').toggleClass('on');
		jQuery('#global .primary li ul.children li.current_page_item').children('a').children('div.arrow-border').toggleClass('dine');
		// TURN THE PARENT ON
		jQuery('#global .primary li ul.children li.current_page_item').parent('ul').parent('li').children('a').children('div.arrow').toggleClass('on');
		jQuery('#global .primary li ul.children li.current_page_item').parent('ul').parent('li').children('a').children('div.arrow-border').toggleClass('dine');
	});
	// ON HOVER TURN ON THE CSS NAVIGATION ELEMENTS
	jQuery('#global a').hover(function(){
		if ( jQuery(this).parent('li').hasClass('current_page_item') ) { } else {
			jQuery(this).children('div.arrow').toggleClass('on');
			jQuery(this).children('div.arrow-border').toggleClass('dine');
		}
	});
	jQuery('#global li ul li a').hover(function(){
		jQuery(this).parent('li').parent('ul').parent('li').children('a').children('div.arrow').toggleClass('on');
		jQuery(this).parent('li').parent('ul').parent('li').children('a').children('div.arrow-border').toggleClass('dine');
	});
	
	// PROCESS TOGGLES
	// DEFAULT ACTION
	jQuery(".section_content").hide(); // HIDE ALL CONTENT
	jQuery(".section:first").addClass("on"); // ACTIVATE FIRST TAB
	jQuery(".section_content:first").show(); // SHOW FIRST TAB CONTENT
	
	// On Click Event
	jQuery(".section").click(function() {
		jQuery(".section").removeClass("on"); //Remove any "active" class
		jQuery(this).addClass("on"); //Add "active" class to selected tab
		jQuery(".section_content").hide(); //Hide all tab content
		var activeTab = jQuery(this).attr("title");	//Find the rel attribute value to identify the active tab + content
		jQuery('#'+activeTab).fadeIn("slow"); //Fade in the active content
		clearInterval(refreshIntervalId);
		return false;
	});
	jQuery(".mail-follow").click(function() {
		jQuery('#mail-list').addClass('followed').fadeIn("slow");
	});
	
	// HOMEPAGE CAROUSEL	
	jQuery.fn.timer = function() {
		if ( !jQuery(".section_content:last", this).is(":visible") ) {
			jQuery(".section_content:visible", this).hide().next(".section_content").show().fadeIn("slow");
			jQuery(".section.on", this).removeClass("on").next(".section").addClass("on");
		} else {
			jQuery(".section_content:visible", this).hide();
			jQuery(".section.on", this).removeClass("on");
			jQuery(".section_content:first", this).show().fadeIn("slow");
			jQuery(".section:first", this).addClass("on");
		}
	}
	var refreshIntervalId = setInterval(function() {
		jQuery("#homecary").timer();
	}, 3700);
	window.refreshIntervalID;
	
	// SIDEBAR TOGGLES
	jQuery('#sidebar .butt').click(function() {
		jQuery(this).toggleClass('down');
		jQuery(this).parent('li').children('.children').toggle('fast');
	});
	
	/* R&P SELF SUBMIT
	jQuery("#sortby").change(function() {
		//$('#searchPilots').addClass('blue');
		var action = jQuery(this).val();
		//jQuery("#sort_form").attr("action", action);
		this.form.submit();
	});
	*/
	// R&P MANUAL SUBMIT
	jQuery("#refine").click(function() {
		jQuery('#sort_form').submit();
	});
	// R&P MANUAL RESET & SUBMIT
	jQuery("#reset").click(function() {
		jQuery('#sortby option[value=""]').attr("selected", "selected");
		jQuery('#orderby option[value=""]').attr("selected", "selected");
		jQuery('#order option[value=""]').attr("selected", "selected");
		jQuery('#sort_form').submit();
	});
	// R&P TOGGLE
	jQuery('#r_and_p_sort a.toggle').click(function(){
		jQuery('#randp_sort_form').slideToggle("fast");
		jQuery(this).toggleClass("on off");
	});
	
	// READ MORE LINKS
	jQuery("a.more-link").parent("p").addClass("more-link");
	
	// MAILLIST TOGGLES
	jQuery('#mail-firstname').focus(function(){
		jQuery(this).val('');
	});
	jQuery('#mail-firstname').blur(function(){
		if(jQuery(this).val() === "")
		jQuery(this).val('First Name');
	});
	jQuery('#mail-lastname').focus(function(){
		jQuery(this).val('');
	});
	jQuery('#mail-lastname').blur(function(){
		if(jQuery(this).val() === "")
		jQuery(this).val('Last Name');
	});
	jQuery('#mail-org').focus(function(){
		jQuery(this).val('');
	});
	jQuery('#mail-org').blur(function(){
		if(jQuery(this).val() === "")
		jQuery(this).val('Organization');
	});
	jQuery('#mail-title').focus(function(){
		jQuery(this).val('');
	});
	jQuery('#mail-title').blur(function(){
		if(jQuery(this).val() === "")
		jQuery(this).val('Profession');
	});
	jQuery('#mail-email').focus(function(){
		jQuery(this).val('');
	});
	jQuery('#mail-email').blur(function(){
		if(jQuery(this).val() === "")
		jQuery(this).val('Email Address');
	});
		
	// MAILLIST PROCESS
	function checkEmail(email) { 
	  var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	  var emailVal = jQuery("#" + email).val();
	  return pattern.test(emailVal);
	}
	
	jQuery(function() {
	  jQuery("#mail-list-signup input:submit").click(function() { 
	    
	    // DISABLE THE FORM
	    jQuery('form#mail-list-signup').submit(function() { return false; });
	    jQuery('#confirmation span.retname').append(jQuery('#mail-firstname').val());
	    
	    // ACQUIRE THE ACTION
	    var formAction = jQuery("form#mail-list-signup").attr("action");
	    
	    // ID FOR MAIL FIELD
	    var id = "mail-email";
	    var emailId = id;
	    
	    // VALIDATE WITH REGEX
	    if (!checkEmail(emailId)) {
	      alert("Please enter a valid email address");
	      return;
	    }
	    
	    // SERIALIZE FORM VARIABLES FOR POST SUBMISSION
	    var str = jQuery("form#mail-list-signup").serialize();
	    
	    // ADD FORM ACTION TO END OF VARIABLES
	    // CDATA IS USED TO AVOID VALIDATION ERRORS
	    //<![CDATA[
	    var serialized = str + "&action=" + formAction;
	    // ]]>
	    
	    // AJAX SUBMISSION
	    jQuery.ajax({
	      url: "http://flamboyanfoundation.org/wp/wp-content/themes/flamboyan/j/proxy.php",
	      type: "POST",
	      data: serialized,
	      success: function(data){
	        // SERVER-SIDE VALIDATION
	        if (data.search(/invalid/i) != -1) {
	          alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
	        }
	        else
	        {
	          jQuery("#mail-list-signup").hide(); // If successfully submitted hides the form
	          jQuery("#confirmation").html(data).slideDown("slow");  // Shows "Thanks for subscribing" div
	          jQuery("#confirmation").tabIndex = -1;
	          jQuery("#confirmation").focus(); // For screen reader accessibility
	          // GOOGLE ANALYTICS FAKE PAGE VIEW
	          //var pageTracker = _gat._getTracker("UA-XXXXX-X");
	          //pageTracker._trackPageview("/newsletter_signup");
	        }
	      }
	    });
	  });
	});	
	
});
