$(document).ready(function(){
  
  /**
   * Set and Clear Default Text Input and Textarea Values
   * ----------
   * All placeholders are converted to value attributes for
   * legacy browser compatibility and ease of styling. The 
   * 'placeholder' class is appended when the placeholder
   * text is displayed.
   * 
   * Written by Kevin Thompson <kevin@kevinthompson.info> with additions/modifications by Tyler Martin
   */

  $('input[type="text"],textarea').each(function(){
  	var def = $(this).attr('placeholder') != undefined && $(this).attr('placeholder') != '' ? $(this).attr('placeholder') : '';
  	if($(this).val() == '') $(this).val(def).attr({'placeholder':'','data-placeholder':def}).addClass('placeholder');
  	$(this).focus(function(){
  		if($(this).val() == def){
  			$(this).removeClass('placeholder').val('');
  		}
  	}).blur(function(){
  		if($(this).val() == ''){
  			$(this).addClass('placeholder').val(def);
  		}
  	});
  });
   

  /**
   * Clear Placeholder Values on Form Submission
   */
  $('form').submit(function(){
    $(this).find('input[type="text"],textarea').each(function(i,el){
      $(el).val( $(el).val() == $(el).attr('data-placeholder') ? '' : $(el).val() );
    });
  });
  
  // ------------------------------------------------------------
  
    //style select
	$('#recipes-products').sSelect();
	$('.form-sel').sSelect();

	$(".chzn-select").chosen();
	$('#top-search-box').one("focus", function() {
		$('#top-search div.SSContainerDivWrapper').show();
		$(document).unbind('click.sSelect');
	});
	
	
	$('#top-search').submit(function(e){
		e.preventDefault();
		var keywords = $('#top-search-box').val().replace(/[-[\]{}()\'*+?.,\\^$|#\s]/g, " ")
		var section = $('input:radio[name=recipes-products]:checked').val()
		var keyword_is_null = (keywords == "" || keywords == null || keywords == 'Enter a keyword here')

		switch(section) {
			case 'products' :
				if (!keyword_is_null) {
					window.location = window.location.protocol+'//'+window.location.host+'/SRCH.html?Search='+keywords
				}
				else {
					window.location = window.location.protocol+'//'+window.location.host+'/SRCH.html?Search='
				}
				break;
			
			case 'recipes' :
				if (!keyword_is_null) {
					window.location = window.location.protocol+'//'+window.location.host+'/search-results/'+section+'/keywords:'+keywords+'/all/'
				}
				else {
					window.location = window.location.protocol+'//'+window.location.host+'/search-results/'+section+'/keywords:/all/'
				}
				break;
		}
	})
	
	$( "#top-search-box" ).autocomplete({
		source: $('#top-search-options option').map(function(i, e){return $(e).text()}).toArray()
	});	
	
	// function set_zindex(selector){
	// 	var z_index = 2000;
	// 	jQuery.each($(selector), function() {
	// 		 z_index = z_index -1;	
	// 		 $(this).css('z-index', z_index) 
	//    		});
	// 	}
	// set_zindex('header nav > ul > li > ul > li > ul > li');
	// 
	//for popup in shopping cart
	(function(){ 
	$('.product-item > .preview > a').mouseover(function(e){
										$(this).parents('.product-item').css('z-index','100');	
										$(this).siblings('div.popup-box').show();
										var cursorX = e.pageX;
										var window_width = document.body.clientWidth;
										if(cursorX > window_width/2) {
										$(this).siblings('div.popup-box').find('.popup-triangle').css({
											left : 'auto',
											right : '-25px',
											backgroundPosition: '0 -42px'
											});
										$(this).siblings('div.popup-box').css({
											left : '-475px'
											});
										}
									  }).mouseout(function(){
											$(this).siblings('div.popup-box').stop().hide();
											$(this).parents('.product-item').css('z-index','0');
									  });
	})();
	
	// click event for recipe boxes images
	$('.white-panel-inner-smaller .preview label').live('click', function(e){
		if ($(e.target).is('label')) {
			e.preventDefault();
			window.location = $(e.target).find('a').attr('href')
		}
	})
	
	//zoom for products 
	$('#products-slider-container ul.panes li > div > ul > li > div').mouseenter(function(){
		$(this).find('label').css({
					display: 'block'
					});
		$(this).find('span.label').css({
					fontSize: '16px'
			});
		$(this).find('img').animate({
				width: '182px',
				marginLeft: '-15px',
				marginTop: '-10px'
				}, 200);
		}).mouseleave(function(){
			$(this).find('span.label').css({
					fontSize: '14px'
			});
			$(this).find('label').css({
					display: 'none'
					});
			$(this).find('img').animate({
				width: '150px',
				marginLeft: '0',
				marginTop: '0'
				}, 100);
		});

	//centered link 
	function style_center_link(selector){
		jQuery.each($(selector), function() {
		  var width = $(this).innerWidth()/2;
		 $(this).css('margin-left', - width - 10);
   		});
		}
		style_center_link('article.banner div.link a');
		style_center_link('.center-me');		
		
/*For Drop down menu	*/
	$('li.drop-down').mouseenter(function(){
			$(this).addClass('current');
		}).mouseleave(function(){
			$(this).removeClass('current');
			});
			
	$('li.drop-down ul li').mouseenter(function(){
			$(this).addClass('current');
		}).mouseleave(function(){
			$(this).removeClass('current');
			});
		
	$('li.drop-down li').has('ul').children('a').css('background-position','230px 25px');
	$('.time-list li:last-child').addClass('time-list-last');
	$('.category-links li:last-child').addClass('category-links-last');
	
	//For video player
	$('.video-grids ul li a').click(function(){
			var video_url = $(this).attr('href');
			$('.video-box iframe').attr('src', video_url + '&wmode=transparent');
			$('#video-description-viewport').children().not('h3').remove()
			$('#video-description-viewport h3').after($($(this).attr('data-description')))
			return false;
		});
		
	//For Jobs Center	
	$('a.job-toggle').each(function(){
	  	$(this).click(function(e){
			e.preventDefault();
			$($(this).attr('href')).toggleClass('hide');
			$($(this).children('div')).toggleClass('hide');		
		});
	});
	
	$("#search_recipe_form").submit(function(e){
		e.preventDefault()
		var keywords = '';
		var keyword_acc = [];
		var section = '{segment_2}';
		$('.search-choice span').each(function(i, el){
			keyword_acc.push($(el).text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, ''))
		})
		var cat = $("#category_select option:selected").val();

		cat = cat =='' ? 'all': cat;
		keywords = keyword_acc.join('+').replace(/[-[\]{}()\'*?.,\\^$|#\s]/g, " ")
		window.location = window.location.protocol+'//'+window.location.host+'/search-results/recipes/keywords:'+keywords+'/'+cat+'/';
										
	})
	$('a.print-btn').each(function(){
	  	$(this).click(function(e){
			window.print();
		})
	})
	
});

