jQuery.noConflict();

// methods for ingredient search //
SNI.RZ = {
	ingreds:function(){
		jQuery("#ButtonSearch").attr({disabled:"disabled"}); // Default to being disabled.
		jQuery(".CheckBoxen").click(function() {              // If any check boxes are clicked on...
			var anyChecked = false;	
			jQuery(".CheckBoxen").each(function() {           // Look at all checkboxes...
				if( jQuery(this).attr("checked") ) {          //   if the checkbox is checked...
					anyChecked = true;                        //   then at least one checkbox is checked
				}
			});
			if (anyChecked == true) {                            // If any of the checkboxes are checked...
				jQuery("#ButtonSearch").removeAttr("disabled");  // Remove the disable from the submit button
			}
			else {                                                     // Otherwise, all checkboxes are empty...
				jQuery("#ButtonSearch").attr({disabled:"disabled"});  // and therefore the button should be disabled.
			}
		});	
	}
};


if(typeof(SNI.RZ.RecipeCollections)=="undefined"){SNI.RZ.RecipeCollections={};}

SNI.RZ.RecipeCollections={
	hoverCity:function(element){
		jQuery('#rz-rec-col-groups ol > li').bind("mouseenter mouseleave",function(e){jQuery(this).toggleClass('hoverCity');});
		//SNI.Food.Omniture.ClickTrack("#rec-col-groups .group-wrap","Food:Site: Recipe Collections Groups","Recipe_Collections-Groups");
	}
	,rightRail:function(element){
		// hide all the props
		jQuery('#rec-col-wrap .more-colls').hide(); // hide the second tab
		jQuery('a.more-colls-cta').hide(); // hide the call to action
		jQuery('a.more-all').hide(); // hide the call to action
		
		var protectedCount = jQuery('.more-colls-cta span').text();
		var allCollsCount = jQuery('.status a em').text();
		var collectionName = jQuery('.dpl-pop-tabs h5:first').text(); // get the name of the collection
		collectionName = jQuery.trim(collectionName);
		var topicLink = jQuery('a.topic-link').attr('href');
		var collLink = jQuery('a.exp-all').attr('href');
		jQuery('.status a em').wrapInner('<span class="collectionCount"></span>');
		jQuery('.status a em').append('<span class="collectionName">&nbsp;'+collectionName+'</span>'); // update the status count em place holder
		
		var totalCount = jQuery('.status a .collectionCount').text(); // get the total collections
		var visibleCount = jQuery('#rec-col-wrap li.rec-item:visible').length; // count the visible
		
		var collectionGroupLink = jQuery('.status p a').attr("href"); // Get the link to the collection group page for switching.
			
		if(totalCount > 2) { 
			visibleCount = 3;	
			jQuery('a.cta[rel="more"]').show(); 
			jQuery('a.more-colls-cta').hide(); // otherwise show and hide these
		} else {
			jQuery('a.more-colls-cta').hide();
		};
	
		if(totalCount < 2) {
			jQuery('a.cta[rel="more"]').hide();  // hide the more button
		} else {
			jQuery('a.cta[rel="more"]').show();
		};
		
		if(totalCount <= 2) {
			var moduleHeight = totalCount * 100; // The module should be 85pixels for every collection
		} else {
			var moduleHeight = 270;
		}
		
		moduleHeight = moduleHeight + 'px'; // Set the string for the module height based on number of collections.
			
		jQuery('a.cta[rel="more"]').click(function() {
			visibleCount = totalCount;
		});  
					
		jQuery('#status-items').text('1-'+visibleCount); // change the status text
		
		if(jQuery('#more-collections-tab').hasClass('ui-tabs-selected')) { // if the second tab is active on load
			jQuery('ul.ingredients').hide(); // hide the first ul
			jQuery('ul.more-colls').show(); // show the second one
			jQuery('.collectionName').hide(); // get ride of the collection name
			jQuery('a.more-colls-cta').show(); // hide the call to action
			visibleCount = jQuery('ul.active-list').children().length; // count the items
			jQuery('.status a .collectionCount').text(protectedCount); // place the updated count
			jQuery('#status-items').text('1-'+visibleCount); // display updated text
			jQuery('.status p a').attr("href", '/recipe-collections/'); // Set to the home page for all collections
		} else { // Primary tab displayed, modify height.
			if(totalCount <= 2) {
				jQuery('#rec-col-wrap').animate({ // animate the expansion 
					height: moduleHeight // height
				}, 'slow' ); // speed
			}
		};
		
		if(jQuery('ul:visible').hasClass('more-colls')) { // if we are on the second tab
			jQuery('a.exp-all').show(); // show the all link
			jQuery('a.cta[rel="more"]').hide();
			jQuery('a.more-colls-cta').show();
			jQuery('.topic-link').attr({
				href: collLink
			});
			visibleCount = jQuery('ul.more-colls li').length; // count the items
		} else { 
			jQuery('a.exp-all').hide(); // hide the all link
			visibleCount = 3; // update the count
		};
		
		jQuery('#rec-col-wrap li.rec-item').each(function(recIndex){
			if(recIndex > 2) { jQuery(this).hide() }; // hide all but the first 3 items
		});
			
		jQuery('.ui-tabs-nav h5').click(function(){ // tab click

			if(jQuery(this).hasClass('ui-tabs-selected')) {} else { //if you click the active tab

				var visibleUL = jQuery('#rec-col-wrap ul.active-list') // define the visible ul variable
				jQuery(visibleUL).hide().removeClass('active-list'); // remove active state
				jQuery(visibleUL).siblings().show().addClass('active-list'); // show and name the new active ul
			};
			
			if(jQuery('ul:visible').hasClass('more-colls')) { // if the second tab is active
			
				jQuery('a.exp-all').show(); // show the all link
				jQuery('.status a .collectionCount').text(protectedCount); // place the updated count
				jQuery('a.cta[rel="more"]').hide(); // hide the expand link
				jQuery('a.more-colls-cta').show(); // show the more link
				jQuery('.status p a').attr("href", '/recipe-collections/'); // Set to the home page for all collections
				visibleCount = jQuery('ul.more-colls li').length; // count the items
				jQuery('#status-items').text('1-'+visibleCount); // display updated text
				jQuery('#rec-col-wrap').animate({ // animate the expansion 
					height: "270px" // height
				}, 'slow' ); // speed
			} else {
				jQuery('.collectionName').show(); // show the collection name
				jQuery('.more-colls-cta').hide();
				jQuery('a.cta').show();
				jQuery('a.exp-all').hide(); // hide the more link
				visibleCount = jQuery('#rec-col-wrap li.rec-item:visible').length; // update the count
				// totalCount = jQuery('#rec-col-wrap li.rec-item').length; // update the count
				jQuery('.status a .collectionCount').text(totalCount); // place the updated count
				if(totalCount > 2) {
					jQuery('a.cta[rel="more"]').show(); 
					jQuery('a.more-colls-cta').hide(); // otherwise show and hide these
				} else {
					jQuery('a.more-colls-cta').hide();
				};
				jQuery('#status-items').text('1-'+visibleCount); // change the status text back
				jQuery('.status p a').attr("href", collectionGroupLink); // Reset the collection group link.
				if(totalCount <= 2) {
					jQuery('#rec-col-wrap').animate({ // animate the expansion 
						height: moduleHeight // height
					}, 'slow' ); // speed
				}
			};
				
			if(jQuery('.exp-all').is(':visible')) { // if the you can the all link
				jQuery('.collectionName').hide(); // hide collection name
				visibleCount = jQuery('ul.more-colls li').length; // update the count
				jQuery('.topic-link').attr({
					href: collLink
				});
			} else {
				jQuery('.collectionName').show(); // show the collection name
				jQuery('.topic-link').attr({
					href: topicLink
				});
			};
			
			
			if(jQuery('ul:visible').hasClass('more-colls')) { // if tab 2 is active
				
				
			};
			
			if(jQuery('a.cta[rel="more"]').hasClass('expanded')) { // if the box is expanded
				jQuery('a.cta[rel="more"]').removeClass('expanded'); // remove the expanded class
				jQuery('a.cta[rel="more"]').hide(); // hide the expand link
				jQuery('a.more-all').hide(); // hide the tab 2 link
				jQuery('#rec-col-wrap li.rec-item').each(function(recIndex){
					if(recIndex > 2) { jQuery(this).fadeOut('slow') }; // hide all but the first 3 items
				});
				jQuery('#status-items').text('1-'+visibleCount); // status text
			};
					
			jQuery(this).siblings().removeClass('ui-tabs-selected'); // remove active tab state
			jQuery(this).addClass('ui-tabs-selected'); // add active tab state to the new tab
			return false;
		});
		
		jQuery('a.cta[rel="top-3"]').click(function(){ // click a top-3 link
			jQuery(this).hide(); // hide the action link
			jQuery(this).next().slideDown('slow'); // reveal the contents
			return false;
		});
		
		jQuery('a.cta[rel="more"]').click(function(){ // click the "more" link
			if(jQuery(this).hasClass('expanded')) { // if its already expanded
				jQuery(this).removeClass('expanded'); // remove the class
				jQuery(this).text('Show More'); // change the text
				jQuery('#rec-col-wrap li.rec-item').each(function(recIndex){
					if(recIndex > 2) { jQuery(this).fadeOut('slow') }; // hide all but the first 3
				});
				if(totalCount <= 2) {
					jQuery('#rec-col-wrap').animate({ // animate transistion
						height: "270px"
					}, 'slow' );
				}
			} else { // otherwise
				jQuery(this).addClass('expanded'); // give it the marker class
				jQuery('#rec-col-wrap li.rec-item').fadeIn('slow'); // fade in the content
				jQuery(this).hide(); // hide what was clicked
				jQuery('a.more-all').show(); // show tab 2 link
				visibleCount = jQuery('ul.active-list').children().length; // count the items
				jQuery('#status-items').text('1-'+visibleCount); // change status text
				if(visibleCount > 3) { // Only animate the expansion if there are more than 3 collections to show.
					jQuery('#rec-col-wrap').animate({ // animate the transition 
						height: "415px"
					}, 'slow' );
				}
				
			};
			return false;
		});
	}
	,init:function(){
		SNI.RZ.RecipeCollections.hoverCity();
	}
};


