jQuery(function() {
	window.list.allCrossRefLinks();
});



window.util =
{
	getParamByName: function(name)
	{
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");

		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );

		return (results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")));
	},

	calculateScrollTop: function(element)
	{
		var
			scroll_top      = null,
			element_top     = element.offset().top,
			element_bottom  = element_top + element.height(),
			window_top      = jQuery(window).scrollTop(),
			window_bottom   = window_top + jQuery(window).height(),
			distance_top    = element_top - window_top,
			distance_bottom = window_bottom - element_bottom;

		if (distance_top < 0)
		{
			scroll_top = element_top;
		}
		else if (distance_bottom < 0)
		{
			distance_bottom = Math.abs(distance_bottom);

			if (distance_bottom > distance_top)
			{
				scroll_top = element_top; // = window_top + distance_top
			}
			else
			{
				scroll_top = window_top + distance_bottom;
			}
		}

		return scroll_top;
	},

	calculateScrollReal: function(scroll_top)
	{
		return Math.abs(jQuery(window).scrollTop() - scroll_top);
	}
}



window.list =
{
	hideShowFactor: 500 / 250, // multiplied with element height
	scrollFactor: 1000 / 250, // multiplied with element top edge

	minHideShowTime: 500,
	minScrollTime: 500,

	basePath: jQuery('meta[name=base]').attr("content"),

	allCrossRefLinks: function()
	{
		jQuery('a').each(function() {
			jQuery(this).attr('href', jQuery(this).attr('href').replace(/&detail=[0-9]+/, '').replace(/detail=[0-9]+&/, ''));
		});

		window.list.initCrossRefLinks('body');
	},

	initCrossRefLinks: function(parent)
	{
		jQuery('a.cross_reference', jQuery(parent)).each(function() {
			jQuery(this).attr('href', jQuery(this).attr('href').replace('detailpage=', 'detail='));
		});
	},

	initDetailLinks: function(area)
	{
		jQuery('#content_list .list_element h3 a').each(function() {
			jQuery(this).click(function() {
				window.list.showElementInfo(jQuery(this).parents('.list_element'), area);
				return false;
			});
		});

		if (detail = window.util.getParamByName('detail'))
		{
			window.list.showElementInfo(jQuery('#element' + detail), area);
		}
	},

	showElementInfo: function(element, area)
	{
		var visible = jQuery('#content_list .list_element .list_info:visible').eq(0);
		var info    = element.find('.list_info');

		if (info.html().length > 0)
		{
			window.list.hideShowElements(element, visible, info);
		}
		else
		{
			var id = element.attr('id').replace('element', '');
			var load = element.find('.loading');
			load.show();

			jQuery.post(
				window.list.basePath + 'index.php',
				{
					'path':      'arbeit/detail',
					'detailpage': id,
					'area':       area
				},
				function(data) {
					load.hide();
					info.hide().html(data);

					window.list.initGalleryLinks(element);
					window.list.initCrossRefLinks(element);
					window.list.hideShowElements(element, visible, info);
				}
			);

		}
	},

	hideShowElements: function(element, visible, info)
	{
		// just to be save
		visible = visible.eq(0);

		var
			visible_height = visible.height(),
			hide_time = Math.floor(visible_height * window.list.hideShowFactor);

		if (hide_time < window.list.minHideShowTime)
		{
			hide_time = window.list.minHideShowTime;
		}

		visible.hide('blind', hide_time);

		if (! info.is(":visible"))
		{
			var
				info_height = info.height(),
				show_time = Math.floor(info_height * window.list.hideShowFactor),
				scroll_delay = 0;

			if (show_time < window.list.minHideShowTime)
			{
				show_time = window.list.minHideShowTime;
			}

			if (visible_height > info_height)
			{
				scroll_delay = Math.floor(visible_height * window.list.hideShowFactor - show_time);
			}

			info.show('blind', show_time, function() {
				setTimeout(function() {
					var scroll_top = window.util.calculateScrollTop(element);

					if (scroll_top != null)
					{
						var scroll_time = Math.floor(window.util.calculateScrollReal(scroll_top) * window.list.scrollFactor);

						if (scroll_time < window.list.minScrollTime)
						{
							scroll_time = window.list.minScrollTime;
						}

						jQuery('html, body').animate({scrollTop: scroll_top}, scroll_time);
					}
				}, scroll_delay);
			});
		}
	},

	initGalleryLinks: function(parent)
	{
		jQuery('.gallery', jQuery(parent)).each(function() {
			jQuery('a', this).lightBox({
				overlayOpacity: 0.5,
				containerResizeSpeed: 500,
				imageBlank: window.list.basePath + 'scripts/lightbox/images/blank.gif',
				imageLoading: window.list.basePath + 'scripts/lightbox/images/loading2.gif',
				imageBtnClose: window.list.basePath + 'scripts/lightbox/images/close.png',
				imageBtnPrev: window.list.basePath + 'scripts/lightbox/images/prevarrow.png',
				imageBtnNext: window.list.basePath + 'scripts/lightbox/images/nextarrow.png',
				txtImage: 'Screenshot',
				txtOf: 'von'
			});
		});
	}
}



window.slideshow =
{
	fadeSpeed: 1000,
	showLength: 5000,

	init: function(parent)
	{
		var position = 0;
		var elements = jQuery('.slideshow', jQuery(parent)).children();

		elements.eq(position).fadeIn(window.slideshow.fadeSpeed);

		setInterval(function(){
//			elements.eq(position).fadeOut(window.slideshow.fadeSpeed);
			elements.each(function() {
				if (jQuery(this).is(":visible"))
				{
					jQuery(this).fadeOut(window.slideshow.fadeSpeed);
				}
			});

			position++;

			if (position >= elements.length)
			{
				position = 0;
			}

			elements.eq(position).fadeIn(window.slideshow.fadeSpeed);

		}, window.slideshow.showLength);
	}
}



window.validate =
{
	init: function()
	{
		jQuery('form').each(function() {
			jQuery(this).submit(function() {
				return window.validate.checkForm(jQuery(this));
			});
		});
	},

	checkForm: function(form)
	{
		var valid = true;
		var required = {};

		form.find('.required').each(function() {
			var name = jQuery(this).attr('name');
			required[name] = name;
		});

		for (var name in required)
		{
			var field = jQuery("form [name='" + name + "']");
			var length = "";

			if (jQuery(field).attr('type') == "radio" || jQuery(field).attr('type') == "checkbox")
			{
				length = jQuery(field + ":checked").length;
			}
			else
			{
				length = jQuery(field).val().replace(/^\s*|\s*$/g,'').length;
			}

			if (length)
			{
				jQuery(field).parents('.form_row').removeClass('missing_value');
			}
			else
			{
				jQuery(field).parents('.form_row').addClass('missing_value');
				valid = false;
			}
		}

		return valid;
	}
}

