var activeCustomerPageIndex = 0;
$(document).ready( function() {
	$('.accordionHeader').click( function() {
		if ($(this).hasClass('accordionHeaderActive')) {
			$(this).attr('class', 'accordionHeader');

			$(this).next().slideUp();
		}
		else {
			$(this).attr('class', 'accordionHeaderActive');
			$(this).next().slideDown();
		}
	});

	$('#loginUsernameInput').focus(function() {
		if ($(this).val() == 'Benutzername') {
			$(this).val('');
		}
	});

	$('#loginUsernameInput').blur(function() {
		if ($(this).val() == '') {
			$(this).val('Benutzername');
		}
	});

	$('#loginPasswordInput').focus(function() {
		if ($(this).val() == 'Passwort') {
			$(this).val('');
		}
	});

	$('#loginPasswordInput').blur(function() {
		if ($(this).val() == '') {
			$(this).val('Passwort');
		}
	});

	/* -- -- accordion -- -- */
	
	$('div.accordion').accordion({
		autoHeight: false,
		header: 'h5',
		collapsible: true
	});

	/* -- -- references -- -- */
	$('div.imagePopup').click(function() {
		var popupOptions = $(this).attr('id').split('_');

		switch (popupType) {
			case 'references':
				getReferencePopup(popupOptions[0], popupOptions[1]);
			break;

			case 'news':
				getNewsPopup(popupOptions[1]);
			break;
		}
	});

	/**
	 * @param	string		key		// customer or reference
	 * @param	integer		id
 	 */
	function getReferencePopup(key, id) {
		$.ajax({
			url: '/ajax.php',
			data: {
				action: 'references',
				key: key,
				id: id
			},
			success: function(template) {
				$('#imagePopupContent').html(template);

				$("#imagePopupLogoContent").mbScrollable({
					width: 315,
					elementsInPage: 1,
					elementMargin: 0,
					height: 73,
					controls: '#imagePopupLogoControls',
					autoscroll: false,
					nextCallback: function() {
						window.setTimeout(function() {
							getReferencePopup('customer', getElementKey($('#imagePopupLogoContent div.scrollableStrip div.SECont:nth-child(' + (activeCustomerPageIndex+1) + ') img').attr('id')));
						}, 600);
					},
					prevCallback: function() {
						window.setTimeout(function() {
							getReferencePopup('customer', getElementKey($('#imagePopupLogoContent div.scrollableStrip div.SECont:nth-child(' + (activeCustomerPageIndex-1) + ') img').attr('id')));
						}, 600);
					}
				}).goToPage(activeCustomerPageIndex, true);

				$("#sliderContent").mbScrollable({
					width: 703,
					elementsInPage: 5,
					elementMargin: 4,
					height: 73,
					controls: '#sliderControls',
					autoscroll: false
				});

				$('#imagePopupSlider').stop().animate({
					bottom: '-73px'
				}, 0);

				$('#imagePopupContainer').show();

			}
		});
	}

	/**
	 * @param	integer		id
 	 */
	function getNewsPopup(id) {
		$.ajax({
			url: '/ajax.php',
			data: {
				action: 'news',
				id: id
			},
			success: function(template) {
				$('#imagePopupContent').html(template);

				$("#sliderContent").mbScrollable({
					width: 703,
					elementsInPage: 5,
					elementMargin: 4,
					height: 73,
					controls: '#sliderControls',
					autoscroll: false
				});

				$('#imagePopupSlider').stop().animate({
					bottom: '-73px'
				}, 0);

				$('#imagePopupContainer').show();
			}
		});
	}

	$('#imagePopupImageContainer').live('hover', function(event) {
		if (event.type == 'mouseover' || event.type == 'mouseenter') {
			$('#imagePopupSlider').stop().animate({
				bottom: '0'
			}, 500);
		}
		else {
			$('#imagePopupSlider').stop().animate({
				bottom: '-73px'
			}, 500);
		}
	});

	$('.thumbnailImage').live('click', function() {
		var elementID = getElementKey($(this).attr('id'));
		$('#imagePopupImage').attr('src', '/fileadmin/user_upload/' + popupType + '/' + images[elementID].pictureName);

		if ('undefined' != typeof images[elementID].subject) {
			$('#imagePopupText h3').text(images[elementID].subject);
		}

		$('#imagePopupText p').text(images[elementID].message);
	});

	$('div#imagePopup div.close, div#dimmer').click(function() {
		$('#imagePopupContainer').hide();
	});

	/**
	 * returns the id or name of the given element id
	 *
	 * @param	string		elementID
	 * @param	string		extractWhat
	 */
	function getElementKey(elementID, extractWhat) {
		var IDSplits = elementID.split('_');
		switch (extractWhat) {
			case 'name':
				return IDSplits[0];
			break;

			default:
				return IDSplits[IDSplits.length-1];
		}
	}
});
