// preload navi

function preload() {

    document.Vorladen = new Array();
 
    if(document.images) {
        for(var i = 0; i < preload.arguments.length; i++) {
            document.Vorladen[i] = new Image();
            document.Vorladen[i].src = preload.arguments[i];
        }
    }
}

preload('/images/bg_pad_on.gif', '/images/blog_navi_li_on.gif', '/images/navi_blog_32_14_off.gif', '/images/navi_blog_32_14_on.gif', '/images/navi_home_40_14_off.gif', '/images/navi_home_40_14_on.gif', '/images/navi_services_60_14_off.gif', '/images/navi_services_60_14_on.gif', '/images/navi_portfolio_59_14_off.gif', '/images/navi_portfolio_59_14_on.gif', '/images/navi_contact_55_14_off.gif', '/images/navi_contact_55_14_on.gif');

// Krücke für IE
sfHover = function() {
	var sfEls = document.getElementById("main-navi").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

$(document).ready(function(){
 
$('#main-navi li').bind('touchstart', function(){$(this).addClass('sfhover');})

});

//postcard order form

// Settings
var _createHiddenDiv = true;							// If set to false, you will need to add this to your template
var _redirectToThankYouTemplate = false;				// if you want to redirect to a thank you template path to redirect to
var _toggleThankYouDiv = true;							// set to true, the form will be replaced by the thank you message
var _thankYouTemplatePath = '/blog/validation_form';		// if _redirectToThankYou is set to true - thank you template path to redirect to
var _thankYouDiv = 'validationhidden';					// Div to display thank you message
var _formDiv = 'postcardform';								// Div containing the form
var _UseFormIDs = true;								// if true, the form_id attr needs to be added to the exp:freeform:form. False for just one from per page
var _formID = 'order_form'; 							// this is the ID of the freeform :: add attribute form_id="" to the exp:freeform:form
var _showErrorMessages = false;							// Show error messages generated by FreeForm
var _showErrorFormClass = false;							// Changes class on field to show an error
var _showErrorLabels = true;							// Changes class on label to show an error
var _errorClass = 'error'; 								// css class to show the form field had an error
var _errorLabelClass = 'errorlabel'; 					// css class to show the form field had an error
var _onSubmitMessage = 'send...';
var _validateField = 'rel';								// this is the customer attribute added to the form fields that is required
var _labelPrefix = 'label_'; 							// this is the id of the element to display the error message for that field
var _fieldErrorMessagePrefix = 'error_'; 				// this is the id of the element to display the error message for that field
var _hiddenDivWithFFErrors = 'ffajaxreturn';			// this is the hidden div that the ajax call will load the FreeForm errors into


$(document).ready(function(){
	// Create Array Of Required Fields
	var _formFieldsRequired = new Array();
	// Store Submit Button Value;
	var _submitButtonValueOrg = $('input[type=submit]').val();
	// Create Hidden Div for FreeForm Error Messages
	if (_createHiddenDiv) $('body').append('<div id="'+_hiddenDivWithFFErrors+'" style="display:none;"></div>');
	// Get required fields - build array		
	$(':input['+ _validateField +']').each(function(i) {
		_formFieldsRequired[i] = $(this).attr(_validateField);
	});		
	// Submit form
	if (_UseFormIDs) {_theFormIs='#'+_formID} else {_theFormIs='form'}
	$(_theFormIs).ajaxForm({  
		target: '#'+_hiddenDivWithFFErrors, 
		beforeSubmit:function() {			
			$(':input['+ _validateField +']').each(function(i) {
				$(this).removeClass(_errorClass);
				_field = $(this).attr(_validateField);
				$('#' + _labelPrefix + _field).removeClass(_errorLabelClass);
				$('#' + _fieldErrorMessagePrefix + _field).empty();
				// Disable Submit Button
				$('input[type=submit]').attr('disabled', 'disabled').val(_onSubmitMessage);
			});	
	},
	success:function(response) {
		if (response=="success") {
			$(_theFormIs).resetForm();
			if (_redirectToThankYouTemplate) {
				window.location = _thankYouTemplatePath;
			} else {
				if (_toggleThankYouDiv) {
					$('#'+_thankYouDiv).load(_thankYouTemplatePath,
						function () {
							$('#'+_formDiv).fadeOut('slow',
								function () {$('#'+_thankYouDiv).fadeIn('slow');}); 
					}); 
				} else {
					$('#'+_thankYouDiv).load(_thankYouTemplatePath);
					$('#'+_thankYouDiv).css({'display' : 'inline'});
				}
				
				
			}
			
		} else {
			// Loop through required fields
			_formFieldsRequired.reverse();
			$(_formFieldsRequired).each(function(e) {			
				// Set Field Checking
				var _check = _formFieldsRequired[e];
				var myRegExp = new RegExp(_check, 'gi');
				// Loop through error list from freeform
				$('#'+_hiddenDivWithFFErrors+' #content ul li').each(function(i) {
					// Compare Field to Error Message
					// 2009.12.11 : [FIX] the fix : added .replace(/ /g,'_') to $(this).html() to replace spaces with underscores so it can validate on multiple word labels				
					if ( $(this).html().replace(/ /g,'_').search(myRegExp) > 0 ) {
						// Show Errors on Form
						_errormessage = $(this).html();
						if (_showErrorFormClass) $('input[name='+_check+']').addClass(_errorClass).fadeIn('slow');
						if (_showErrorMessages) $('#' + _fieldErrorMessagePrefix + _check).html(_errormessage).fadeIn('slow');
						if (_showErrorLabels) $('#' + _labelPrefix + _check).addClass(_errorLabelClass).fadeIn('slow');
						return false;
					} else {
						_errormessage = $(this).html();
						if ( $(this).html().replace(/ /g,'_').search('E-Mail-Adresse') > 0 ) {
							if (_showErrorLabels) $('#' + _labelPrefix + 'email').addClass(_errorLabelClass).fadeIn('slow');
							if (_showErrorFormClass) $('input[name="email"]').addClass(_errorClass).fadeIn('slow');
						} else if ( $(this).html().replace(/ /g,'_').search('Sicherheitscode') > 0 ) {
							if (_showErrorLabels) $('#' + _labelPrefix + 'captcha').addClass(_errorLabelClass).fadeIn('slow');
						}
					}
				});
			});
			// Enable Submit Button
			$('input[type=submit]').attr('disabled', '').val(_submitButtonValueOrg);
			// Clear the hidden div
			$('#'+_hiddenDivWithFFErrors).empty();		
		}		
	  }
	});
});
