(function($, undefined) {

	var TNA = {
		init : function() {
			TNA.layout.init();
			TNA.events.init();
			TNA.slider.init();
			TNA.fancybox.init();
			TNA.wibiya.init();
			TNA.form.init();
		},
		/**
		 * fix layout
		 */
		layout : {
			init : function() {
				TNA.layout.setFooter();
				TNA.layout.setPager();
			},
			/**
			 * set footer height depending on content and browser size
			 */
			setFooter : function() {
				/*
				//reset footer
				$('#footer').css({
					height : 'auto'
				});
				//check if footer has to be enlarged
				if($('body').height() > $('#pageWrapInner').height()) {
					//set footer height
					$('#footer').css({
						height : $('body').height() - $('#pageInnerWrap').height() + $('#footer').height() + 'px'
					});
				*/
			},
			/**
			 * style pager
			 */
			setPager : function() {
				//check if a pager is displayed in page
				if($('#content div.pager').length !== 0) {
					if($('#content div.pager .browseLinksWrap a:first').prev('span.activeLinkWrap').length === 0) {
						$('#content div.pager .browseLinksWrap a:first').addClass('prev').html('').css({
							display : 'block'
						});
					}
					if($('#content div.pager .browseLinksWrap a:last').next('span.activeLinkWrap').length === 0) {
						$('#content div.pager .browseLinksWrap a:last').addClass('next').html('').css({
							display : 'block'
						});
					}
					$('#content div.pager .browseLinksWrap a').not('.prev').not('.next').add('span.activeLinkWrap').wrapAll('<div class="pageNumbers" />');
					$('div.pageNumbers a').not('div.pageNumbers a:last').after('&nbsp;|&nbsp;');
					if($('div.pageNumbers a:last').next('span.activeLinkWrap').length !== 0) {
						$('div.pageNumbers a:last').after('&nbsp;|&nbsp;');
					}
					if($('span.activeLinkWrap').next('div.pageNumbers a').length !== 0) {
						$('span.activeLinkWrap').after('&nbsp;|&nbsp;');
					}
				}
			}
		},
		/**
		 * browser events
		 */
		events : {
			init : function() {
				TNA.events.window();
				TNA.events.click();
			},
			/**
			 * window events
			 */
			window : function() {
				//resize
				$(window).resize(function() {
					TNA.layout.setFooter();
					TNA.wibiya.set();
				});
			},
			/**
			 * click events
			 */
			click : function() {
				//rider submenu selection on riders page
				$('.ridertype').delegate('a', 'click', function(evt) {
					evt.preventDefault();
					evt.stopPropagation();
					//set menu item to active
					$(this).toggleClass('active');
					//sort the riders
					TNA.rider.sort();
				});
			}
		},
		rider : {
			/**
			 * sort the riders depending on the menu selection
			 */
			sort : function() {
				//build array of active menu items
				var activeArr = [];
				$('.ridertype a.active').each(function() {
					var href = $(this).attr('href');
					var posHash = href.indexOf('#');
					var hash = href.substring(posHash + 1);
					activeArr.push(parseInt(hash));
				});
				//check if all or no menu item are/is active
				if($('.ridertype a.active').length === $('.ridertype a').length || $('.ridertype a.active').length === 0) {
					//reset to default
					$('.ridertype a.active').removeClass('active');
					$('.rider').removeClass('inactive')
				} else {
					//check each rider if the menu selection fits with the skills of the rider

					$('.rider').each(function() {
						var typeArr = $(this).attr('data-type').split(',');
						var typeArrLength = typeArr.length;
						//set inactive
						$(this).addClass('inactive');
						for(var i = 0; i < typeArrLength; i++) {
							if($.inArray(parseInt(typeArr[i]), activeArr) !== -1) {
								//remove inactive if the rider fits
								$(this).removeClass('inactive');
							}
						}
					});
				}
			}
		},
		/**
		 * startpage image slider
		 */
		slider : {
			/**
			 * initialize avia slider for more information see http://aviathemes.com/aviaslider/
			 */
			init : function() {
				if($('#slider').length !== 0) {
					$('#slider').aviaSlider({
						autorotationSpeed : 6,
						animationSpeed : 800,
						transition : 'fade',
						switchMovement : true,
						backgroundOpacity : 0.8
					});
				}
			}
		},
		/**
		 * fancy box for popup images
		 */
		fancybox : {
			/**
			 * initialize fancybox for more information see http://fancybox.net/
			 */
			init : function() {
				if($('.fancybox').length !== 0) {
					$('.fancybox').fancybox({
						'padding' : 0,
						'type' : 'image',
						'titlePosition' : 'inside',
						'transitionIn' : 'elastic',
						'transitionOut' : 'elastic',
						'centerOnScroll' : 'true',
						'autoScale' : 'true',
						'onComplete' : function() {
						}
					});
				}
			}
		},
		/**
		 * wibiya social icon bar
		 */
		wibiya : {
			/**
			 * initialize wibiya social icon bar for more information see http://www.wibiya.com/
			 */
			init : function() {
				//customize social bar
				TNA.wibiya.set();

			},
			/**
			 * customize sizes and position
			 */
			set : function() {
				/*
				//check if toolbar is added to dom yet
				if($('#wibiyaToolbar').length !== 0) {
					//do css stuff
					$('.wibiya_toolbar_wrapper').css({
						width : '911px',
						marginLeft : '-454px',
						left : '50%'
					});
					$('.wibiya_toolbar_minimized_holder').css({
						right : ($('body').width() - 932) / 2 + 'px'
					});
				} else {
					//init function again till the toolbar is loaded
					var timeout = setTimeout(function() {
						TNA.wibiya.set();
					}, 50);
				}
				*/
			}
		},
		/**
		 * form setup
		 */
		form : {
			init : function() {
				//toggle help text in form field
				$('input[type=text]').each(function() {
					//check if form field has a title
					if( typeof $(this).attr('title') !== 'undefined' && $(this).attr('title') !== false) {
						$(this).unbind('focus', 'blur').bind('focus', function() {
							//on focus remove info text
							if($(this)[0].title) {
								if($(this).val() === $(this)[0].title) {
									$(this).val('');
								}
							}
							$(this).addClass('active');
							$(this).parent('form').addClass('active');
						}).bind('blur', function() {
							//on blur show info text if input field is empty
							if($(this)[0].title) {
								if($(this).val() === '') {
									$(this).val($(this)[0].title);
								}
							}
							$(this).removeClass('active');
							$(this).parent('form').removeClass('active');

						}).blur();
					}
				});
			}
		}
	};

	$(document).ready(function() {
		//initialize the whole script
		TNA.init();
		//check for firefox to set extra css class to embed the arial narrow font differently
		if(navigator.userAgent.indexOf("Firefox") != -1) {
			$('body').addClass('ff');
		}
			// initialize the pretty-Foto-Popup-Box for Images (maybe Youtube too)
		$("a[rel^='prettyPhoto']").prettyPhoto();
	});
})(jQuery);

