jQuery.fn.fadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle'}, speed, easing, callback);  
};

$('#tellfriend').hide();


// wrap children - for videos page
(function($){

   $.fn.wrapChildren = function(options) {

    var options = $.extend({
                              childElem : undefined,
                              sets : 1,
                              wrapper : 'div'
                            }, options || {});
    if (options.childElem === undefined) return this;

 return this.each(function() {
  var elems = $(this).children(options.childElem);
  var arr = [];

  elems.each(function(i,value) {
    arr.push(value);
    if (((i + 1) % options.sets === 0) || (i === elems.length -1))
   {
     var set = $(arr);
     arr = [];
     set.wrapAll($("<" + options.wrapper + ">"));
   }
  });
    });

  }

})(jQuery);

$(function() {   
  $('.video_cat_wrap').wrapChildren({ 
             childElem : '.video_list_wrap' , 
             sets: 3, 
             wrapper: 'div class="group"'
  });   
});


$(document).ready(function() {

	/* Footer slidedown */
	$('.footer_wrap h6').siblings().hide();
	$('.footer_wrap h6').hover(function() {
		$('.footer_wrap h6').siblings().slideDown();
	}); 
	
	/* Homepage slideshow */
	$('.slideshow').cycle({
		fx: 'fade',
		cleartype:  true,
		cleartypeNoBg:  true
	});
	
	/* BU red quote slideshow */
	$('.redQuoteSlideshow').cycle({
		fx: 'fade',
		cleartype:  true,
		cleartypeNoBg:  true
	});
	
	


  $('a.email, #tellfriend a.close').click(function() {
  	$("#tellfriend").fadeToggle('slow');
  });
	$('.subnav li:first').addClass('landing');
	$('.subnav li:last').addClass('last');
		
	$("#product_search_form").submit(function() {
		var searchLink = "http://www.memjet.com/partners/product-search/";
		var selectedRegion = $("#product_search_region option:selected").val();
		var selectedType= $("#product_search_type option:selected").val();
		
		if ((selectedType == "") && (selectedRegion == "")) {
			$('#product_search_type').after('<p class="form_error">Please choose a region or product type</p>');
			return false;
		}
			$(".product_search_choice option:selected").each(function () {
				if ($(this).val() != '') {
					searchLink += $(this).attr('id');
					searchLink += '/';
				}
	    });
	    $('#product_search_form').attr('action', searchLink);
      return true;
		});
		
		//Leadership slidedowns
		$('.leadership_content').hide();
		
		$('.leadership_wrap h3').click( function(e) {
			e.preventDefault();
			var $tgt = $(e.target);
			if ($tgt.is('.leadership_wrap a.view')) {
				$('.leadership_wrap a').addClass('view').removeClass('hide');
				$(this).siblings('.leadership_content').slideDown('slow');
				$(this).children('a').addClass('hide').removeClass('view');	
			} else {
				$('.leadership_content').slideUp('normal');
				$(this).children('a').addClass('view').removeClass('hide');
			} 
		});	
		
		//Tell a friend slidedowns
		$('.tellfriend_wrap').hide();
		
		$('.tellfriend_link').click( function(e) {
			e.preventDefault();
			var $tgt = $(e.target);
			if ($tgt.is('.tellfriend_link .view')) {
				$('.tellfriend_wrap').slideDown('slow');
				$(this).children('a').addClass('hide').removeClass('view');	
			} else {
				$('.tellfriend_wrap').slideUp('normal');
				$(this).children('a').addClass('view').removeClass('hide');
			} 
		});	
		
		// colorbox popups
		// add 2px to width to fixe scrollbar overflow auto problem
		$("#printhead1_link").colorbox({width:"602px", inline:true, href:"#printhead1"});
		$("#printhead2_link").colorbox({width:"602px", inline:true, href:"#printhead2"});
		$("#printhead3_link").colorbox({width:"602px", inline:true, href:"#printhead3"});
		$("a[rel='gallery-nozzle']").colorbox();
		$("#printhead_movement_link").colorbox({width:"578px", inline:true, href:"#printhead_movement"});
		$("#printgap_link").colorbox({width:"427px", inline:true, href:"#printgap"});
		
		$("#broll_video_link").colorbox({width:"642px", inline:true, href:"#broll_video"});		
		// user message on contact forms
		$('div.user_message_wrap').delay(10000).slideUp('slow');
		
			
});

