
/*global window,alert,jQuery,document,$ */

(function ($) {
	'use strict';
	$.fn.filterPanelAjax = function (uri, cntClass, cntOnly) {

		$.ajax({
			url: uri + '?type=555',
			data: {},
			type: 'GET',
			cache: true,
			dataType: 'html',
			success: function (data) {
				var oldHeight = $('.' + cntClass + '-items').height(),
					items = $(data).find('.' + cntClass + '-items'),
					filtercalendar = $(data).find('.' + cntClass + '-filtercalendar'),
					filterpanel = $(data).find('.' + cntClass + '-filterpanel'),
					onlyThis = $(data).find('.' + cntClass + '-' + cntOnly);

				//smooth scroll to top
				if ($('#pbrowser-anchor').length != 0) {
					$('html, body').stop().animate({
						scrollTop: $('#pbrowser-anchor').offset().top
					}, 1250);
				}
				if (cntOnly) {
					$('.' + cntClass + '-' + cntOnly).replaceWith(onlyThis);
				} else {
					$('.' + cntClass + '-filtercalendar').replaceWith(filtercalendar);
					$('.' + cntClass + '-filterpanel').replaceWith(filterpanel);
					$('.' + cntClass + '-filterlist').itFilter({
						'filterShow': '.' + cntClass + '-filter'
					});

					$('.' + cntClass + '-items').fadeOut(150, function () {

						// load new html-content
						$('.' + cntClass + '-items').replaceWith(items);
						$('.' + cntClass + '-items').css('height', oldHeight);

						var anzahl = $('.' + cntClass + '-itemOuter').size(),
							newHeight = (Math.ceil(anzahl / 4)) * 139;
						$('.' + cntClass + '-items').children().hide();

						if ((newHeight !== oldHeight) && (newHeight > 1)) {
							$('.' + cntClass + '-items').animate({
								height: newHeight
							}, 300, function () {
								$('.' + cntClass + '-items').children().fadeIn(200);
							});
						} else {
							$('.' + cntClass + '-items').children().fadeIn(200);
						}

						

						
						// reinitialize image hover
						$('.tx-itcustomers-item').itFader({
							'fadeOut': '.tx-itcustomers-item-img',
							'fadeIn': '.tx-itcustomers-item-text',
							'duration': 300
						});
					});
				}
			},
			error: function () {
				alert('Something went wrong');
			}
		});
	};
}(jQuery));



/*
	If a hash bginning with
	fpanel
	is found the
	filterPanelAjax
	function is called.
*/
$(window).bind('hashchange', function () {
	'use strict';
	var hash = window.location.hash.slice(1),
		hashArray = hash.split('|');

	if (hashArray[0] === 'fpanel') {
		$().filterPanelAjax(hashArray[3], hashArray[1], hashArray[2]);
	}
});


$(document).ready(function () {
	'use strict';

/*
	Search for all dom elements with ID like
	jqAjaxFilterXXX
	and add a hash vlue to the links.
*/
	$('[id^=jqAjaxFilter]').each(function () {
		var cntClass = $(this).attr('class');
		$(this).delegate('a', 'click', function (e) {
			var uri = $(this).attr('href'),
				cntOnly = '';

			if ($(this).parents('.jQHasAjaxLinks').length) {
				e.preventDefault();
				if ($(this).hasClass('jQAjaxOnlyThis')) {
					cntOnly = $(this).attr('rel');
				}
				window.location.hash = '#fpanel|' + cntClass + '|' + cntOnly + '|' + uri;
			}
		});
	});
/*
	If a hash begins with
	"fpanel"
	then
	"filterPanelAjax"
	function is called.
*/
	if (window.location.hash) {
		var hash = window.location.hash.slice(1),
			hashArray = hash.split("|");

		if (hashArray[0] === 'fpanel') {
			$().filterPanelAjax(hashArray[3], hashArray[1], hashArray[2]);
		}
	}


/*
	General function for "close buttons"
*/
	$('body').delegate('.closeBtn', 'click', function (e) {
		e.preventDefault();
		$(this).closest('.flyoutOutContent').fadeOut("fast");
	});

/*
	General function to load only one content item in a given dom object
*/
	$('[rel^=ajaxLoadElement]').each(function () {
		var $link = $(this),
			uri = $link.attr('href'),
			cntFullId = $link.attr('rel'),
			cntHelper = cntFullId.split('-'),
			cntUid = cntHelper[1],
			cntLang = cntHelper[2] ? cntHelper[2] : '0',
			spinner = '<div class="defaultLoading"><!-- some simple div with a bg image --></div>';

		$link.click(function (e) {
			e.preventDefault();

			$('#' + cntFullId).html(spinner).load(
				'index.php?id=52&type=556&tx_itpageoptions_pi1[uid]=' + cntUid + '&L=' + cntLang,
				function () {
	// maybe it's a form, then apply jqTransform
					$('.jqtransform').jqTransform();
	// if it is a link in the menu
					$link.parent('li').toggleClass('no').toggleClass('cur');
					$('.closeBtn').click(function () {
						$link.parent('li').toggleClass('no').toggleClass('cur');
					});
				}
			);
		});
	});

	
/*
	General function to submit a ajax form
*/
	$('[id^=ajaxLoadElement]').delegate('form', 'mouseup', function (e) {
		e.preventDefault();
		var options = {
			target: $(this).closest('[id^=ajaxLoadElement]'),
			success: whatToDo
		}; 
		$(this).ajaxForm(options);

	});	
	
	function whatToDo(responseText, statusText, xhr, $form)  { 
		$('.jqtransform').jqTransform();
// if it is a login-form and login was successful:
		if ($(responseText).find('#loginbox-redirect').length) {
			window.location = 'index.php?id=141&no_cache=1&u=1';
		}
// if it is a login-form and login was not successful:
		if ($(responseText).find('.hasLoginError').length) {
			loginHasErrorFields();
		} 
	}
	
	function loginHasErrorFields() {
		$('[id^=ajaxLoadElement]').find('.hasLoginError').closest('.loginBody').find(':input').not(':submit').css({'border':'2px solid #99042F','height':'24px','padding-right':'0'});
	}
	
	
/*
	General function for an ajax load
*/

	$('[id^=ajaxLoadElement]').delegate('.ajaxLoadElement > a', 'click', function (e) {
		e.preventDefault();

		var cnt = $(this).closest('[id^=ajaxLoadElement]'),
			spinner = '<div class="defaultLoading"><!-- some simple div with a bg image --></div>',
			uri = $(this).attr('href');

		$(cnt).empty().append(spinner);

		$(cnt).html(spinner).load(
			uri,
			function () {
				$('<a name="closeBtn" class="closeBtn">&nbsp;</a>').appendTo('#' + cnt + ' > div');
// maybe it's a form, then apply jqTransform
				$('.jqtransform').jqTransform();
			}
		);
	});

// END OF DOCUMNET READY
});

