/************************************* <CUSTOMER> <PROJECT> script *****/
/************************** Copyright (c) <yyyy>-<yyyy> <CUSTOMER> *****/
/***********************************************************************/
/****************************************** inidvidual init script *****/
/***********************************************************************/
/************************************** author virtual identity AG *****/
/* $LastChangedDate: 2009-11-26 11:51:04 +0100 (Do, 26 Nov 2009) $ *****/

var USE_SIFR = true;
var DEBUG_SIFR = false;

$(document).ready(function() {
	if (DEBUG_SIFR) {
		$("body").click(function(){
	    	sIFR.debug();
			$("body").unbind( "click" );
			$("body").click(function(){
				initLayout_sifr();
				sIFR.debug.replaceNow();
			});
		});
	} else {
		setup_sifr();
		initLayout_sifr();
	}


	// init site navigation
	if(typeof $.fn.flyoutMenu == 'function') {
		$('#prim-nav').flyoutMenu( { envInfo: Info } );
		$('#generic-nav-zone').flyoutMenu( { envInfo: Info , slideDuration: 100} );
	}

	// init google maps
	if(typeof $.fn.googleMap == 'function') {
		if($('#map-community').length == 1) {
			if($('#map-community').length == 1) {
				$('#map-community').googleMap( {
					mapZoomLimits: { min: 2, max: 15 }
					, initialZoom: 4
					, xmlDataSource: PRODUCERS_PATH
					, detailDataSource: PRODUCER_DETAIL_PATH
					, imgPath: RESOURCES_PATH + '/css/import/img/'
				} );
			}
		}
		if($('#map-technicians').length == 1) {
			$('#map-form').show();
			var map;
			map = $('#map-technicians').googleMap( {
				  mapOptions: { mapTypes: [google.maps.NORMAL_MAP] }
				, initialZoom: 5
				, xmlDataSource: TECHNICIANS_PATH
				, detailDataSource: TECHNICIANS_DETAIL_PATH
				, imgPath: RESOURCES_PATH + '/css/import/img/'
			} );
			$('#map-technicians').googleMapSimpleSearch( {
				  map: map
				, markerPositions: map.markerPositions
				, searchFormContainerId: 'map-form'
				, searchFormAnimationOptions: { width: '24px', height: '24px', bottom: '6px', right: '22px', fontSize: '0em' }
			});
		}
	}


	// initialise compact forms (hide labels and assign their text as value to the corresponding input elements
	if(typeof $.fn.inputLabel == 'function') {
		$('div.compact :input').each( function() { $(this).inputLabel(); });
	}


	//handle special form-switch
	$('#form-switch-controls').find("input:radio").click(function() {
		switch (this.value) {
			case 'CUSTOMER':
				$('#installer').find('input').attr('checked','').show().end().next().hide();
			break;
			case 'INSTALLER':
				$('#installer').find('input').attr('checked','checked').hide().end().next().show();
			break;
		}
	});

	//click without browsers default action
	$('#form-switch-controls').find("input:radio:checked").triggerHandler('click');


	// initialise optional form-blocks
	$('form input.open-block').click(function() {
		//prevent action if the checkbox is hidden
		if ($(this).is(":hidden")) return;

		var id = this.id;
		var $formBlock = $("#fiels-section-wrapper-" + id);
		if ($formBlock.length) {
			if (this.checked) {
				$formBlock.fadeIn();
			} else {
				$formBlock.hide();
			}
		}
	});

	//click without browsers default action
	$('form input.open-block:checked').each(function(){
		$(this).triggerHandler('click');
	})


	//handle all multi form blocks
	if ($.isFunction($.fn.multiForm)) {
		$('form div.multi').multiForm();
	}


	// init form validation for standard forms
	if(typeof $.fn.validate == 'function') {
		$.validator.setDefaults( {
			errorClass: 'invalid'
			, errorElement: 'em'
			, errorPlacement: function(error, element) {
				return false;	// suppress output of field related error messages
			  }
			, highlight: function(element, errorClass) {
				var fieldObj = $(element);
				if(fieldObj.parents('div.compact').length > 0) {
					fieldObj.addClass(errorClass);
				} else {
					$(element.form).find("label[for=" + element.id + "]").addClass(errorClass);
				}
			  }
			, unhighlight: function(element, errorClass) {
				var fieldObj = $(element);
				if(fieldObj.parents('div.compact').length > 0) {
					fieldObj.removeClass(errorClass);
				} else {
					$(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
				}
			  }
			, invalidHandler: function(form, validator) {
				var errors = validator.numberOfInvalids();
				var formId = $(this).attr('id');
				errors > 0? $('#errors-' + formId).fadeIn(500) : $('#errors-' + formId).hide();
				errors > 0? $('#hints-' + formId).hide() : $(this).prev('#hints-' + formId).show();
			  }
			  , submitHandler: function(form) {
				//delete our self-made defaultValues
				$('div.compact :input', form).each(function() {
					var inputLabelValue = $.trim($(this.form).find("label[for=" + this.id + "]").text());
					if (inputLabelValue == this.value) {
						this.value = "";
					}
				});
				form.submit();
			  }
		} );

		// add a custom validation method to ignore default values and input labels
		$.validator.addMethod('ignoreDefault', function(value, element) {
			var defaultValue = element.defaultValue;
			var inputLabelValue = $.trim($(element.form).find("label[for=" + element.id + "]").text());
			return value != defaultValue && value != inputLabelValue;
		}, $.validator.messages.required);

		// Override the required-classRule: add the test, whether the item is visible.
		$.validator.addClassRules("required", {
			required: function(element) {
				return $(element).is(":visible");
			}
		});

		// add special submit handler to "Google Maps" search forms
		$('div#map-form form').validate({
			submitHandler: function() {}	// do nothing - the form handling is done by "jquery.google-maps.js"
		});

		// add special submit handler for mc-login
		$('form#form-mc-login').validate({
			submitHandler: function(form) {
				form.target="_blank";
				form.submit();
			}
		});

		// now initialise the forms on the page
		$('form').each( function() { $(this).validate({}); });
	}

	// initialise slideshows
	if(typeof $.fn.lightBox == 'function') {
		var imgPath = RESOURCES_PATH + '/css/import/img/';
		var slideshowCustomSettings = {
			imageLoading: imgPath + 'lightbox-ico-loading.gif'
			, imageBtnClose: imgPath + 'lightbox-btn-close.gif'
			, imageBtnPrev: imgPath + 'lightbox-btn-prev.gif'
			, imageBtnNext: imgPath + 'lightbox-btn-next.gif'
			, imageBlank: imgPath + 'lightbox-blank.gif'
			, overlayOpacity: 0.85
			, containerBorderSize: 0
		}
		$('div.slideshow').each( function() {
			var slideshowContainer = $(this);
			var slideshowLinks = $('ul a', slideshowContainer);
			var slideshowStarter = $('div.image, p.caption', slideshowContainer);
			if(slideshowLinks.length > 0) {
				slideshowStarter.children('a').click( function() { slideshowLinks.eq(0).click(); } );
				slideshowStarter.show();
				slideshowLinks.lightBox(slideshowCustomSettings);
			}
		});
	}
});