/************************************************
*
* Author: Keith Hanson
* Email: seraphimrhapsody@gmail.com
* Date: Wednesday, September 17, 2008
* Project: Trauma Survivors Network
* Client: Ken Hanson
*
************************************************/


/************************************************
* Required Files:
* jquery-1.2.6.min.js (include before this file.)
* 
* INSTRUCTIONS:
* 	Include this file on any page where you
*	need FauxRadio or FauxCheckbox functionality.
*	
* NOTES:
*	For FauxRadio functionality, ensure that you
*	name the underlying radio buttons the same
*	name for meaningful groupings.
*************************************************/


//Call this whenever new controls on the page get loaded in from AJAX
function WireUp()
{
	jQuery.noConflict();
	
	initOverLabels();
	
	//find all of the fake checkboxes and modify their onClick
	jQuery("a.fauxCheck").click(function(){
		
		//If the checkbox is checked...
		if(jQuery(this).hasClass("on"))
		{
			//ensure that it does not have the 'on' class
			jQuery(this).removeClass("on");
			//mark the faux checkbox as off
			jQuery(this).addClass("off");
			//mark the real checkbox as off
			jQuery(this).children(":checkbox").attr("checked", false);
		}
		
		//If the checkbox is not checked...
		else
		{
			//ensure that we remove the 'off' class...
			jQuery(this).removeClass("off");
			//mark the checkbox as on
			jQuery(this).addClass("on");
			//mark the real checkbox as on
			jQuery(this).children(":checkbox").attr("checked", true);
		}
		
		//stop the link from actually going anywhere.
		return false;
	});
	
	jQuery("a.selectAllCheckboxes").click(function(){
	  jQuery("a.fauxCheck").each(function(index) {
	   if (jQuery(this).hasClass("off")){
	     jQuery(this).click();
	   }
	  })
	});
	
	jQuery("a.selectNoCheckboxes").click(function() {
	  jQuery("a.fauxCheck").each(function(index) {
	   if (jQuery(this).hasClass("on")){
	     jQuery(this).click();
	   }
	  })
	});
	
	
	//Allow labels of FauxRadios to be able to be clicked.
	jQuery("label.radioLabel").click(function(){
		jQuery(this).siblings("a").click();
	});
	
	//grab all the fauxRadio buttons...
	jQuery("a.fauxRadio").click(function(){
		//look for all of the other FauxRadio buttons with child radio button
		//elements that have the same name as this element's child...
		
		//RAILS SUPPORT
		//we need to split out the separate parts for the name, since the brackets are
		//JQuery selectors. We filter the items now by ensuring the name has both the object name
		//and the property name present...
		var nameArray = jQuery(this).children(":radio").attr("name").split("[");
		
		//This is the object name...
		var firstPart = nameArray[0];
		
		//This is the property name...
		var secondPart = nameArray[1].split("]")[0];
		
		//Grab all radio buttons which have at least the first part...
		jQuery("a.fauxRadio > :radio[name*="+ firstPart + "]").each(function(){
			
			//Narrow down our results and ensure that they have the exact name of the second page
			//No fuzzy business here...
			if(jQuery(this).attr("name").split("[")[1].split("]")[0] == secondPart)
			{
				//for each of these, set their checked status to false.
				jQuery(this).attr("checked", false);
				//make sure that the FauxRadio button is off...
				jQuery(this).parent().removeClass("fauxRadioOn");
				jQuery(this).parent().addClass("fauxRadioOff");
			}
		});
		
		//look for the clicked element's radio button... check it.
		jQuery(this).children(":radio").attr("checked", true);
		jQuery(this).children(":radio").click();
		//make sure the FauxRadio button is on.
		jQuery(this).removeClass("fauxRadioOff");
		jQuery(this).addClass("fauxRadioOn");
		
		//stop the link from actually doing anything.
		return false;
	});
	
	/********************************************
	*
	*  The following is basic show/hide logic.
	*
	********************************************/
	jQuery("h4.collapsableTrigger").click(function(){
		jQuery(this).siblings().toggle();
		if(jQuery(this).hasClass("collapseIcon"))
		{
			jQuery(this).removeClass("collapseIcon");
			jQuery(this).addClass("expandIcon");
		}
		else
		{
			jQuery(this).removeClass("expandIcon");
			jQuery(this).addClass("collapseIcon");
		}
		
		return false;
	});
	
	jQuery("a.collapsableButton").click(function(){
		jQuery(this).parent().siblings().toggle();
		if(jQuery(this).hasClass("collapseIcon"))
		{
			jQuery(this).removeClass("collapseIcon");
			jQuery(this).addClass("expandIcon");
		}
		else
		{
			jQuery(this).removeClass("expandIcon");
			jQuery(this).addClass("collapseIcon");
		}
		
		return false;
	});
	
	jQuery(".shareIcon").click(function(){
	  jQuery(".emailForm").hide();
		
		if(jQuery(".shareList").css("display") == "none")
		{
			jQuery(".shareList").show();
		}
		else
		{
			jQuery(".shareList").hide();
		}
		
		return false;
	});
	
	
	jQuery(".emailIcon").click(function(){
	  jQuery(".shareList").hide();
		
		if(jQuery(".emailForm").css("display") == "none")
		{
			jQuery(".emailForm").show();
		}
    else
		{
			jQuery(".emailForm").hide();
		}
		return false;
	});
	
	jQuery(".emailForm .closeLink").click(function(){

		jQuery(".shareList").hide();
		jQuery(".emailForm").hide();
		
		jQuery(".to_name").val("");
		jQuery(".to_email").val("");
		jQuery(".from_name").val("");
		jQuery(".from_email").val("");
		jQuery(".comments").val("");

		return false;
	});

	jQuery(".shareList .closeLink").click(function(){

		jQuery(".shareList").hide();
		jQuery(".emailForm").hide();
		
		jQuery(".to_name").val("");
		jQuery(".to_email").val("");
		jQuery(".from_name").val("");
		jQuery(".from_email").val("");
		jQuery(".comments").val("");

		return false;
	});


	jQuery(".emailForm .cancelLink").click(function(){
		
		jQuery(".shareList").hide();
		jQuery(".emailForm").hide();
		
		jQuery(".to_name").val("");
		jQuery(".to_email").val("");
		jQuery(".from_name").val("");
		jQuery(".from_email").val("");
		jQuery(".comments").val("");

		return false;
	});
  
	
	
	/* THIS IS SCREWING UP THE FORM SUBMIT
	jQuery(".emailButton").click(function(){	
		jQuery(".shareList").hide();
		jQuery(".emailForm").hide();
		
		jQuery(".to_name").val("");
		jQuery(".to_email").val("");
		jQuery(".from_name").val("");
		jQuery(".from_email").val("");
		jQuery(".comments").val("");
		
		return false;
	});
	*/

	/********************************************
	*
	*  			Mini Menu Hover Work
	*
	********************************************/
	
  jQuery(".miniMenuList li.shootout").hover(function(){
		
		jQuery(this).addClass('over');
		jQuery(".miniMenuList li li").removeClass('over');
	}, function(){
	
		jQuery(this).removeClass('over');
		
	});

	jQuery("#mainMenu li").hover(function(){

		jQuery(this).addClass('over');
	}, function(){

		jQuery(this).removeClass('over');

	});

	jQuery(".tabs li").hover(function(){

		jQuery(this).addClass('over');
	}, function(){

		jQuery(this).removeClass('over');

	});
	
	jQuery(".miniMenuFlyoutWrapper ul:last-child").addClass('last');

}

jQuery(document).ready(function(){
	WireUp();
});