$(document).ready(function(){

 /* ---------- adding link to the homepage banner by jquery ----------- */
$('#views_slideshow_thumbnailhover_div_slideshow-block_1_0 .views-field-field-slide-show-image-fid .views-content-field-slide-show-image-fid img').click(function() {
		var link = $('#views_slideshow_thumbnailhover_div_slideshow-block_1_0 .views-field-field-description-value .views-content-field-description-value h2 a').attr('href');
		//var link =  '/news/nga-waka';
		window.location.href = link;
});

$('#views_slideshow_thumbnailhover_div_slideshow-block_1_1 .views-field-field-slide-show-image-fid .views-content-field-slide-show-image-fid img').click(function() {
		var link = $('#views_slideshow_thumbnailhover_div_slideshow-block_1_1 .views-field-field-description-value .views-content-field-description-value h2 a').attr('href');
		//var link =  '/news/focus-dr-mike-peterkin-pierro-wines';
		window.location.href = link;
});

$('#views_slideshow_thumbnailhover_div_slideshow-block_1_2 .views-field-field-slide-show-image-fid .views-content-field-slide-show-image-fid img').click(function() {
		var link = $('#views_slideshow_thumbnailhover_div_slideshow-block_1_2 .views-field-field-description-value .views-content-field-description-value h2 a').attr('href');
		//var link =  '/content/why-laytons-0';
		window.location.href = link;
});

$('#views_slideshow_thumbnailhover_div_slideshow-block_1_3 .views-field-field-slide-show-image-fid .views-content-field-slide-show-image-fid img').click(function() {
		var link = $('#views_slideshow_thumbnailhover_div_slideshow-block_1_3 .views-field-field-description-value .views-content-field-description-value h2 a').attr('href');
		//var link =  '/news/new-vintages';
		window.location.href = link;
});
/* ===================================================================== */

// Remove the '[Popup]' text that Popups adds.
$('a.popups-processed').each(function() {
$(this).attr('title', $(this).attr('title').replace('[Popup]', ''));
});
 
	/*************** function for grey out the screen ************** 
	 $(".js-cat .js-taxo-list a").click(function() {
		//var buttonname = $(this).attr("class");		
 
			$('body').append('<div id="test"><span class="loaderb"></span></div>');
			$('#test')
			//	.css('opacity','0.8')
				.css('position','fixed')
				.css('color','#FFF')
				.css('z-index','200000')
				.css('padding-left', '50%')
				.css('padding-top', '20%')
				.css('height','80%')
				.css('width','50%')
				.css('top','0px')
		//		.css('background-color','#000')
				.css('left','0px');
			$('#timeimage')
				.css('position','fixed')
				.css('z-index','1111111')
				.css('top','50%')
				.css('left','50%')
				.css('margin-top','-50px')
				.css('margin-left','-100px');
	});
*/

	/*input clearing*/
   if($('input[type="text"]').length>0) {
	   $('input[type="text"]').each(function () {
		   var needle = $(this).val();
		   $(this).attr('needle', needle);
		   if(needle) {
			   $(this).bind('focus', function() {
				  if($(this).val()==needle) {
					  $(this).val('');
				  }
			   });
			   $(this).bind('blur', function() {
				  if($(this).val() == '') {
					  $(this).val(needle);
				  }
			   });
		   }
	   });
   }
	/*textarea clearing*/
   if($('textarea').length>0) {
	   $('textarea').each(function () {
		   var needle = $(this).text();
		   $(this).attr('needle', needle);
		   if(needle) {
			   $(this).bind('focus', function() {
				  if($(this).text()==needle) {
					  $(this).text('');
				  }
			   });
			   $(this).bind('blur', function() {
				  if($(this).text() == '') {
					  $(this).text(needle);
				  }
			   });
		   }
	   });
   }

   /*left side search javascript*/
   if($('.js-taxo-list').length>0) {
	   //* toggle siled #more#
	   $('.js-more').click(function() {
		   var container = $(this).parents('.js-taxo-list');
		   container.find('.js-more-block').slideToggle('', function() {
			   if(container.find('.js-more-block').is(':hidden')) {
				   container.find('.js-more a').text('more...');
			   }else{
				   container.find('.js-more a').text('less...');
			   }
		   });
	   });
	   //* populate selection value to correct hidden fields
	   var selectionBox = $('.search-selection');
	   $('.js-taxo-item>a').click(function() {
		    
		   var taxoID = $(this).attr('rel');
		   //* Add to hidden fields
		   var field = '';
		   var catMark = '';
		   if($(this).parents('.js-cat').hasClass('color_categories')){
			   field = $('input[name="submitted[colour]"]');
			   catMark = 'colour';
		   }
		   if($(this).parents('.js-cat').hasClass('country_categories')){
			   field = $('input[name="submitted[country]"]');
			   catMark = 'country';
		   }
		   if($(this).parents('.js-cat').hasClass('grape_variety_categories')){
			   field = $('input[name="submitted[grape]"]');
			   catMark = 'grape';
		   }
		   var presetted_taxoID_arr = field.val().split(' ');

		   if(!in_array(taxoID, presetted_taxoID_arr)) {
			 
			  //* hasn't been added
			  var val = field.val();
			  if(val) val += ' ';
			  val += taxoID;
			  field.val(val);
			  selectionBox.find('h4').append('<div class="ajax-loader selectionBoxLoader"></div>');
			  selectionBox.removeClass('hidden').show();
			  //* Pull item to selection box
			  var selected = '<li class="js-selection-added"><span>'+$(this).text()+'</span><a href="javascript:void(0);" rel="'+taxoID+'" cat="'+catMark+'"></a></li>';
			  selectionBox.find('ul').append(selected);
			  
			  $('.js-selection-added').slideDown('', function() {
					$(this).removeClass('js-selection-added');
					selectionBox.parents('.webform-client-form').submit();
					//removethe_links();
					/****************** */
					greyscreen();
					/******************/
					
									
					
				 /*setTimeout(function() {					 
					 selectionBox.parents('.webform-client-form').submit();
					}, 500); //set time out is to prevent the gif not playing
				*/
			  });
			  /*re-bind, banned as it submits the form once a click happend
			  //* delete function
			   $('.search-selection ul li>a').bind('click', function() {
				   //* remove from hidden field
				   var taxoID = $(this).attr('rel');

				   //* Add to hidden fields
				   var field = $('input[name="submitted['+$(this).attr('cat')+']"]');
				   var presetted_taxoID_arr = field.val().split(' ');
				   presetted_taxoID_arr = removeByElement(presetted_taxoID_arr, taxoID);
				   var val = presetted_taxoID_arr.toString().replace(',', ' ');
				   field.val(val);

				   //*remove from the list
				   $(this).parent().slideUp('', function() {
					   $(this).remove();
					   if($('.search-selection ul li').length == 0) {
						   $('.search-selection').slideUp('', function() {
							   $(this).addClass('hidden');
						   });
					   }
				   });
			   });
			   */
		   }else{
 
			   return;
		   }
	   });

	   //* delete function
	   $('.search-selection ul li>a').bind('click', function() {
		   greyscreen();
		   //* remove from hidden field
		   var taxoID = $(this).attr('rel');
		   selectionBox.find('h4').append('<div class="ajax-loader selectionBoxLoader"></div>');
		   
		   //* Add to hidden fields
		   var field = $('input[name="submitted['+$(this).attr('cat')+']"]');
		   var presetted_taxoID_arr = field.val().split(' ');
		   presetted_taxoID_arr = removeByElement(presetted_taxoID_arr, taxoID);
		   var val = presetted_taxoID_arr.toString().replace(',', ' ');
		   field.val(val);

		   //*remove from the list
		   $(this).parent().slideUp('', function() {
			   $(this).remove();
			   /*
			   if($('.search-selection ul li').length == 0) {
				   $('.search-selection').slideUp('', function() {
					   $(this).addClass('hidden');
				   });
			   }*/
			   selectionBox.parents('.webform-client-form').submit();
		   });
	   });

	   //* clear all function
	   $('.clearall>a').click(function() {
			selectionBox.find('ul').empty();
			selectionBox.find('h4').append('<div class="ajax-loader selectionBoxLoader"></div>');
			$('input[name="submitted[colour]"]').val('');
			$('input[name="submitted[country]"]').val('');
			$('input[name="submitted[grape]"]').val('');
			
			var han = Drupal.settings.basePath + 'clear_search_filter';
				$.ajax({
					type: "POST",
					url: han,
					dataType: 'json',
					//data: { cname : countryName},
					success: function(theResponse) {
					 
					}
			});
			$(".search-selection").attr("style", "display:block");
			$(".search-selection").hide();
			//selectionBox.parents('.webform-client-form').submit();
	   });
   }

   //* sort by function
   if($('.sort_selection_box').length>0) {
	   $('select[name="sort_order"]').change(function() {
		   var select_val = $(this).val();
		   switch (select_val) {
			   case "1":
				   $(this).parents('form').find('input[name="sort_by"]').val('country');
				   $(this).parents('form').find('input[name="order"]').val('ASC');
			   break;
			   case "2":
				   $(this).parents('form').find('input[name="sort_by"]').val('country');
				   $(this).parents('form').find('input[name="order"]').val('DESC');
			   break;
			   case "3":
				   $(this).parents('form').find('input[name="sort_by"]').val('region');
				   $(this).parents('form').find('input[name="order"]').val('ASC');
			   break;
			   case "4":
				   $(this).parents('form').find('input[name="sort_by"]').val('region');
				   $(this).parents('form').find('input[name="order"]').val('DESC');
			   break;
		   }
		   $(this).parents('form').submit();
	   });
   }
   if($('#block-block-9, #block-block-10, #block-block-11').length>0) {
	   $('#block-block-9, #block-block-10, #block-block-11').each(function() {
		   $(this).click(function() {
				var dest = $(this).find('.image_area').find('a').attr('href');
				window.location = dest;
		   });
	   })
   }
   if($('#webform-client-form-51').length>0) {
	   $('#webform-client-form-51 .form-required').each(function() {
			$(this).parents('.form-item').append('<span title="This field is required." class="form-required">*</span>');
	   });
   }
   /*********** for the home page banner link ***************/
	/*$('#views_slideshow_thumbnailhover_div_breakout_teaser_slideshow-block_1_0').click(function() {
		var alink = $('#views_slideshow_thumbnailhover_div_breakout_teaser_slideshow-block_1_0 div div a').attr('href');
		window.location = alink;
	});
	$('#views_slideshow_thumbnailhover_div_breakout_teaser_slideshow-block_1_1').click(function() {
		var alink = $('#views_slideshow_thumbnailhover_div_breakout_teaser_slideshow-block_1_1 div div a').attr('href');
		window.location = alink;
	});
	$('#views_slideshow_thumbnailhover_div_breakout_teaser_slideshow-block_1_2').click(function() {
		var alink = $('#views_slideshow_thumbnailhover_div_breakout_teaser_slideshow-block_1_2 div div a').attr('href');
		window.location = alink;
	});
	$('#views_slideshow_thumbnailhover_div_breakout_teaser_slideshow-block_1_3').click(function() {
		var alink = $('#views_slideshow_thumbnailhover_div_breakout_teaser_slideshow-block_1_3 div div a').attr('href');
		window.location = alink;
	});*/

	/*********************************************************/
	/************* for the other field display ************/
	$('#edit-submitted-where-did-you-hear-about-us').change(function(){
		var valsel = $('#edit-submitted-where-did-you-hear-about-us option:selected').val();
		 
		if(valsel == 'Other'){
			$("#webform-component-other").attr("style", "display:block");
		}
		if(valsel != 'Other'){
			$("#webform-component-other").attr("style", "display:none");
			$("#edit-submitted-other").val('');
		}
	});
	var valsel = $('#edit-submitted-where-did-you-hear-about-us option:selected').val();
		 
	if(valsel == 'Other'){
			$("#webform-component-other").attr("style", "display:block");
	}
	/***********************************************/
	

	/***____________________________________________________________**/
});

function removethe_links(){
	//alert('sdfsdf');
	 $('.js-taxo-item>a').attr('rel','');
	 $('.js-taxo-item>a').attr('href','');
	 return;
}

function in_array(b, d, a) {
    var e = "";
    if (a) for (e in d) {
        if (d[e] === b) return !0
    } else for (e in d) if (d[e] == b) return !0;
    return !1
}

function removeByElement(arrayName,arrayElement) {
	for(var i=0; i<arrayName.length;i++ ) {
		if(arrayName[i]==arrayElement)
		arrayName.splice(i,1);
	}
	return arrayName;
}

function greyscreen(){
	$('body').append('<div id="test"><span class="loaderb"></span></div>');
					$('#test')
					 //	.css('opacity','0.8')
						.css('position','fixed')
						.css('color','#FFF')
						.css('z-index','200000')
						.css('padding-left', '50%')
						.css('padding-top', '20%')
						.css('height','80%')
						.css('width','50%')
						.css('top','0px')
				 		//.css('background-color','#000')
						.css('left','0px');
					$('#timeimage')
						.css('position','fixed')
						.css('z-index','1111111')
						.css('top','50%')
						.css('left','50%')
						.css('margin-top','-50px')
						.css('margin-left','-100px');	
	return;
}
