/* replace the default selector engine of jQuery with Peppy (http://jamesdonaghue.com/static/peppy/) */
// jQuery.find = peppy.query;

/* rest of base JS */

function isValidEmail(strEmail){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	bReturnValue = false;

	// search email text for regular exp matches
	if (strEmail.search(validRegExp) != -1) {
		/*alert('A valid e-mail address is required.\nPlease amend and retry');
		return false;*/
		bReturnValue = true;
	}

	return bReturnValue;
}

function validateField(objField) {
	//console.log('validateField.id = ' + jQuery(objField).attr('id'));
	var checkType = jQuery(objField).attr('rel');
	var bReturn = false;
	var strErrorMsg = '';

	switch(checkType) {
		case 'mail-address': /* min length: a@xy.tv, req. chars: '@', '.' */
			if(isValidEmail(jQuery(objField).val()) == false || jQuery(objField).val() == '') {
				jQuery(objField).addClass('form-field-invalid');
				strErrorMsg += 'E-Mail-Adresse ';
				if(isValidEmail(jQuery(objField).val()) == false) {
					strErrorMsg += 'falsch';
				} else {
					strErrorMsg += 'fehlt';
				}
				strErrorMsg += ".\n";
			} else {
				jQuery(objField).removeClass('form-field-invalid');
				bReturn = true;
			}
			break;
		case 'full-name': /* min length: 6 chars, consists of: 'first_name last_name', req. chars: ' ' */
			/*if(jQuery(objField).val().length < 6) {
				jQuery(objField).addClass('form-field-invalid');
			} else {
				jQuery(objField).removeClass('form-field-invalid');
			}*/
			break;
		case 'message': /* min-length: 10 chars */
			//console.log('length = ' + jQuery(objField).val().length);

			if(jQuery(objField).val().length < 10) {
				jQuery(objField).addClass('form-field-invalid');
				strErrorMsg += 'Mitteilung zu kurz.' + "\n";
			} else {
				jQuery(objField).removeClass('form-field-invalid');
				bReturn = true;
			}

			break;
		default: /* standard input field, min length: 3 chars */
			break;
	}
	return bReturn;
}

jQuery(function() {
	jQuery('#navigation').localScroll({
		duration:100,
		queue:true
	});


	var photoBgPos = jQuery('#photo').css('background-position');
	/*console.log('photoBgPos = ' + photoBgPos);*/

	jQuery('#photo-half-left a').hover(
		function() { /* over */
			jQuery('#photo').css('background-position', 'top left');
		}, function() { /* out */
			jQuery('#photo').css('background-position', photoBgPos);
		}
	);

	jQuery('#photo-half-right a').hover(
		function() { /* over */
			jQuery('#photo').css('background-position', '-200px 0');
		}, function() { /* out */
			jQuery('#photo').css('background-position', photoBgPos);
		}
	);

	jQuery('.flag-germany').replaceWith('<img src="http://i.f2w.de/icon/famfamfam/flag/png/de.png" alt="Germany" /> ');

	jQuery('#top small').hover(function() {
		jQuery(this).addClass('cursorMagnifier').css({fontSize:'1.1em', color:'#eee', fontWeight:'bold'});
	}, function() {
		jQuery(this).removeClass('cursorMagnifier').css({fontSize:'0.75em', color:'#777', fontWeight:'normal'});
	});

	jQuery('.close-message-window').replaceWith('<div class="message-window-x-container"><img class="button-msg-close" src="http://fwolf.info/6.5/images/triangle-button.png" alt="x" title="Fenster schlie&szlig;en" /></div><div class="message-window-close-container"><button type="button" class="button-msg-close">Fenster schlie&szlig;en</button></div>');

	jQuery('.button-msg-close').click(function() {
		jQuery('#message-window').addClass('hideTag');
	});

	jQuery('div#navigation').append('<ul id="nav-translation"><li id="translate-english"><a href="#translate-english"><img src="http://i.f2w.de/icon/famfamfam/flag/png/gb.png" alt="English" title="Translate content partially to english" /></a></li><li id="translate-german"><a href="#translate-german" title="Anzeige auf Deutsch (Standard)"><img src="http://i.f2w.de/icon/famfamfam/flag/png/de.png" /></a></li></ul>');

	checkPartTimeTranslation();

	jQuery('#translate-english').click(function() {
		addPartTimeTranslation('en');
	});

	jQuery('#translate-german').click(function() {
		removePartTimeTranslation();
	});



	/* usability enhancements for mobile platforms */

	/* netbook 10" = 1024 x 600 (viewport about 520 - 540px) */

// 	if(jQuery(window).height() < 700) {
	if(jQuery(window).height() < 670) {
		jQuery('#photo').addClass('photoRedux');
		jQuery('#header').css('margin-bottom', '5px');
		jQuery('#navigation ul ul').addClass('subnav').addClass('hideTag');

		jQuery('#navigation ul li').hover(
			function() {
				jQuery(this).children('.subnav').slideDown('100').removeClass('hideTag');
			}, function() {
				jQuery(this).children('.subnav').slideUp('500', function() {
					jQuery(this).addClass('hideTag');
				});
			}
		);
	}

	/* Form validation */

	jQuery('.field-required').change(function() {
// 			console.log('field = ' + this);
			validateField(this);
		}
	);

	jQuery('#cf form, #body-offer form').submit(
		function() {
			var bValidForm = false;
// 			console.log('#cf form length:' + jQuery('#cf form .field-required').length);
// 			console.log('#body-offer form length:' + jQuery('#body-offer form .field-required').length);
			jQuery('#cf form .field-required, #body-offer form .field-required').each(
				function() {
					bValidForm = validateField(this);
				}
			);
			return bValidForm;
			//return validateField(this);
		}
	);
});