function bgGallery() {
	jQuery('#supersize img').each(function(){
		imageStretcher.stretch(this)
	})
	// rotation sample
	var set = jQuery('#supersize img'),
		animSpeed = 800,
		delay = 3000,
		curInd = 0,
		prevInd = 0,
		timer;
	set.hide().eq(curInd).show();
	function nextSlide(i) {
		prevInd = curInd;
		curInd = i;
		// if(curInd < set.length-1) curInd++; else curInd = 0;
		set.eq(prevInd).fadeOut(animSpeed);
		set.eq(curInd).fadeIn(animSpeed);
		// autoSlide();
	}
	nextSlide(0)
	function autoSlide() {
		if(timer) clearTimeout(timer);
		timer = setTimeout(nextSlide,delay);
	}
	
	jQuery('.navbar > li > a').bind('click', function(){
		// if(!jQuery(this).parent().hasClass('active')){
			nextSlide(jQuery('.navbar > li > a').index(jQuery(this)))
		// }
	})
}





// image stretcher module
var imageStretcher = (function(){
	var windowWidth, windowHeight, images = [];
	function addImage(img) {
		recalcSize();
		images.push(img);
		if(img.comlete) {
			getRatio(img, resizeImage);
		} else {
			img.onload = function(){
				getRatio(img, resizeImage);
				img.onload = null;
			}
			img.src = img.src; // IE Fix
		}
	}
	function removeImage(img) {
		for(var i=0; i<images.length; i++) {
			if(images[i] === img) {
				images.splice(i,1);
				return;
			}
		}
	}
	function getRatio(img, cb) {
		img.style['msInterpolationMode'] = 'bicubic'; // IE7 fix
		var newImg = new Image();
		newImg.onload = function() {
			img.ratio = newImg.width / newImg.height;
			if(typeof cb === 'function') cb.apply(img)
			newImg.onload = null;
		}
		newImg.src = img.src;
	}
	function resizeImage(img) {
		var _ratio = this.ratio;
		var _slideWidth = windowWidth;
		var _slideHeight = _slideWidth/_ratio;
		if(_slideHeight < windowHeight) {
			_slideHeight = windowHeight;
			_slideWidth = _slideHeight * _ratio;
		}
		this.style.width = _slideWidth+'px';
		this.style.height = _slideHeight+'px';
		// this.style.top = (windowHeight-_slideHeight)/2+'px';
		this.style.top = 0;
		this.style.left = (windowWidth-_slideWidth)/2+'px';
	}
	function recalcSize() {
		windowWidth = getClientWidth();
		windowHeight = getClientHeight();
	}
	function handleResize() {
		recalcSize();
		for(var i=0; i<images.length; i++) resizeImage.apply(images[i]);
	}
	addHandler(window, 'resize', handleResize);

	// util functions
	function addHandler(object, event, handler) {
		if (typeof object.addEventListener != 'undefined') object.addEventListener(event, handler, false);
		else if (typeof object.attachEvent != 'undefined') object.attachEvent('on' + event, handler);
	}
	function getClientWidth(){return window.document.compatMode === 'CSS1Compat' && window.document.documentElement['clientWidth'] || window.document.body['clientWidth']}
	function getClientHeight(){return window.document.compatMode === 'CSS1Compat' && window.document.documentElement['clientHeight'] || window.document.body['clientHeight']}

	// methods
	return {
		stretch: addImage,
		unstretch: removeImage
	}
})()

// jquery tabs plugin
jQuery.fn.jqueryTabs = function(_options){
	// default options
	var _options = jQuery.extend({
		addToParent:false,
		holdHeight:true,
		activeClass:'active',
		tabLinks:'a',
		fadeSpeed:0,
		event:'click'
	},_options);

	return this.each(function(){
		var _holder = jQuery(this);
		var _fadeSpeed = _options.fadeSpeed;
		var _activeClass = _options.activeClass;
		var _addToParent = _options.addToParent;
		var _holdHeight = _options.holdHeight;
		var _tabLinks = jQuery(_options.tabLinks, _holder);
		var _tabset = (_addToParent ? _tabLinks.parent() : _tabLinks);
		var _event = _options.event;
		var _animating = false;
		
		// tabs init
		_tabLinks.each(function(){
			var _tmpLink = jQuery(this);
			var _tmpTab = jQuery(_tmpLink.attr('href'));
			var _classItem = (_addToParent ? _tmpLink.parent() : _tmpLink);
			if(_tmpTab.length) {
				if(_classItem.hasClass(_activeClass)) _tmpTab.show();
				else _tmpTab.hide();
			}
		});

		// tab switcher
		function switchTab(_switcher) {
			if(!_animating) {
				var _link = jQuery(_switcher);
				var _newItem = (_addToParent ? _link.parent() : _link);
				var _newTab = jQuery(_link.attr('href'));
				if(_newItem.hasClass(_activeClass)) return;

				var _oldItem = jQuery(_addToParent ? _tabset : _tabLinks).filter('.'+_activeClass);
				var _oldTab = jQuery(jQuery(_addToParent ? _oldItem.children('a') : _oldItem).attr('href'));
				if(_newTab.length) {
					_animating = true;
					if(_oldItem.length) {
						_newItem.addClass(_activeClass);
						_oldItem.removeClass(_activeClass);

						var _parent = _oldTab.parent();
						if(_holdHeight) _parent.css({height:_parent.height()});

						_oldTab.fadeOut(_fadeSpeed,function(){
							_newTab.fadeIn(_fadeSpeed,function(){
								_parent.animate({height: _newTab.outerHeight(true)}, {queue: false, duration: 500, complete: function(){
									if(_holdHeight) _parent.css({height:'auto'});
								}})
								_animating = false;
							});
						});
					} else {
						_newItem.addClass(_activeClass);
						_newTab.fadeIn(_fadeSpeed,function(){
							_animating = false;
						});
					}
				}
			}
		}

		// control
		_tabLinks.each(function(){
			jQuery(this).bind(_event,function(){
				switchTab(this);
				return false;
			});
		});
	});
}

function initValidation(){
	$('#form-switcher').show();
    $('#col3 a[id*=toggle]').click(function(){
      $('#col3 form.yform').toggleClass('columnar');
      $('#col3 a[id*=toggle]').toggle();
    });  
    $('#toggle_linear').hide();
		
		/* Enable form and validation */
		////////////////////////////////
		$myForm = $("#myForm"); // instance of myform for faster dom-select
		
    $("noscript").hide(); // first hide noscript tag, if js-enabled           
    $(":input:disabled", $myForm).removeAttr("disabled"); // remove disabled attribute from input elements, if js-enabled
    $("#submit", $myForm).show(); // show submitbutton, if js-enabled

    /* 
		 * create jquery validation plugin handler 
		 */
		$myForm.validate({
      errorPlacement: function(error, element) {
        element.parent().addClass("error");
        error.prependTo( element.parent() );
      },
      errorClass: "message",
      errorElement: "strong",
      onkeyup: false, // disable it - make some problems with errorPlacement 
      onclick: false // disable it - make some problems with errorPlacement 
    });
	// Das war jetzt der Normale Aufruf und nun der Teil,
    // der für die YAML-Forms benötigt wird!
    /*
     * Funktion um Fehlermeldungen an richtiger Stelle anzuzeigen
     * und wieder entsprechend zu entfernen.
    */
    var errorHandlerContainer = function(){
      $(this).valid(); // Validiert das aktive Feld (je nach Ereignis - blur, keyup, change)
			var $cont = $(this).parent(); // Hole Übergerodnetes Div des Feldes 
      var haserror = $("strong.message:visible", $cont).size(); // Finde Fehler-Nachricht
      if (haserror<1) {
        $cont.removeClass("error");   // Kein Fehler Entferne Error CSS-Klasse. 
      } else {
        $cont.addClass("error");       // Wenn Fehler füge Error CSS-Klasse hinzu. 
      }
    } 
		$(":input", $myForm).blur(errorHandlerContainer); // check error fields on blur on all input fields
    $("input, textarea", $myForm).keyup(errorHandlerContainer); // check error fields on keyup in typing fields
    $("select", $myForm).change(errorHandlerContainer); // check error fields on change on select/radio fields
}

// accordion function
function initAccordion() {
	var _activeClass = 'current';
	var _slideSpeed = 500;

	$('ul.navbar').each(function(){
		var _accordion = $(this);
		var _items = _accordion.find('li');
		_items.each(function(){
			var _holder = $(this);
			var _opener = _holder.find('>a');
			var _slider = _holder.find('>div.menu');

			_opener.bind('click', function(){
				var _levelItems = _holder.parent().children('li').not(_holder);

				if(!_holder.hasClass(_activeClass)) {
					_holder.addClass(_activeClass);
					_slider.slideDown(_slideSpeed);

					// collapse others
					_levelItems.find('>div.menu').slideUp(_slideSpeed,function(){
						_levelItems.removeClass(_activeClass);
					})
				}
			});

			if(_holder.hasClass(_activeClass)) _slider.show();
			else _slider.hide();
		});
	});
}

// ieHover
function ieHover(_selector, _class){
	if(_class == null) _class = 'hover';
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		jQuery(_selector).each(function(){
			jQuery(this).mouseenter(function(){
				jQuery(this).addClass(_class)
			}).mouseleave(function(){
				jQuery(this).removeClass(_class)
			})
		})
	}
}

jQuery(function(){
	ieHover('.navbar', 'navbar-hover');
	initValidation();
	jQuery('ul.navbar').jqueryTabs({
		addToParent: true
	});
	initAccordion();
	bgGallery();
})
