/**
 * General javascripts
 * @version $Id: scripts.js 716 2011-04-04 13:45:32Z rutger $
 */


/**
 * Placeholder class based on jQuery
 * @description: Make placeholder attributed work on all browsers
 */
var PlaceHoldIt = {

	addPlaceholders: function(props) {

		// default configuration
		var config = {
			'class': 'placeholder' // apply CSS classes to an input displaying a placeholder
		};

		// if any properties were supplied, apply them to the config object
		for (var key in props) {
			if (config.hasOwnProperty(key)) {
				config[key] = props[key];
			}
		}

		// create a basic form for testing support
		var testform = $('<form><input type="text"></form>');

		// extend jQuery.support() for testing "placeholder" attribute support
		$.extend($.support, {
			placeHolder: !!($('input', testform)[0].placeholder !== undefined || $('input', testform)[0].placeHolder !== undefined)
		});

		// create placeholders by using "value" attribute
		if (!$.support.placeHolder) {
			$('input[placeholder]').each(function() {
				var placeholder = $(this).attr('placeholder');
				$(this).bind({
					'focus': function() {
						if ($(this).val() === placeholder) {
							$(this).val('');
							$(this).removeClass(config['class']);
						}
						$(this).attr('data-focused', 'yes');
					},
					'blur': function() {
						if ($(this).val() === '') {
							$(this).val(placeholder);
							$(this).addClass(config['class']);
						}
						$(this).removeAttr('data-focused');
					}
				});
				// only add placeholder on load when value is empty or placeholder or input is not focused (focus is preserved while reloading/XHR)
				if ((($(this).val() === '') || ($(this).val() === placeholder)) && (!$(this).attr('data-focused'))) {
					$(this).val(placeholder);
					$(this).addClass(config['class']);
				}
			});
		}

	}

};

$(document).ready(function() {
	PlaceHoldIt.addPlaceholders();

  var langIsDutch   = false;

  // Determine language of page
  if($("body").hasClass('lang-nl')) {
    langIsDutch = true;
  }

	// Search form validation
	$("#searchform").submit(function(){
	  var field         = $("input:first");
	  var fieldValue    = field.val();
	  var fieldLength   = fieldValue.length;
    var minValLength  = 3;
    
    // If field has a value, is longer than minimum length and it is not the placeholder
	  if(fieldValue != "" && fieldLength >= minValLength && !field.hasClass('placeholder')) {
	    // Go on with form submission
      return true;
	  } else {

	    // Give message based on language set
      if(langIsDutch) alert("Geef een zoek waarde van minimaal 3 karakters.");
      else alert("Please enter a search query of 3 characters minimum.");
    
      // Focus in the search field
	    field.focus();
	  }
	  
	  // Always return false if validation is not met
	  return false;

	});
	
	// Newsletter form submission
	$('#subForm').submit(function(){
	  var fieldName = $("#name").val();
	  var fieldEmail = $("#xurllt-xurllt").val();
	  
    // If name and email field are not empty
	  if(fieldName && fieldEmail){
	    return true;
    } else {
	    // Give message based on language set
      if(langIsDutch) alert("Voer alstublieft alle velden in.");
      else alert("Please enter all fields.");
      
      // Focus in name field
      $("#name").focus();
	  }
	  
	  // Always return false if validation is not met	  
    return false;	  
	});
	
});
