// Define variable for infinity scroll
// Gets overwritten in tpl, depending on dynamic back end settting/definition
var useInfinityScroll = false;

// Get height of items background image and realign info text
function makeImageSubtexts() {
	var maxYPosOfInfoText = 170;
	$('.list_item_box .imagebox').not('.imageTextRealigned').each(function(idx, elm) {
		var $this = this;
		
		$(this).addClass('imageTextRealigned');

		var imgsrc = $(elm).css('backgroundImage');
		imgsrc = /^url\((['"]?)(.*)\1\)$/.exec(imgsrc);
		imgsrc = imgsrc ? imgsrc[2] : false;

		if (imgsrc) {
			var img = new Image();
			$(img).bind('load', function() {
				var pos = $(this).height();
				$(this).remove();

				if (pos > maxYPosOfInfoText) {
					pos = maxYPosOfInfoText;
				}

				$('span', $this).css({
					'top': pos + 'px'
				});
			});
			$(img).attr('src', imgsrc).css('display', 'none').appendTo('body');
		}
	});
}

$(document).ready(function() {
	makeImageSubtexts();
	
	
	// items per page
	$('.pagingButton').click(function() {
		$('#pagingSelector').val($(this).attr('rel'));
		$('#pagingSelectorForm').submit();
	});

	// item list style
	$('.list_style_button').click(function() {
		$('#listStyleSelector').val($(this).attr('rel'));
		$('#listStyleForm').submit();
	});

	// item list pagination
	$('.pagination_button').click(function() {
		$('#paginationSeite').val($(this).attr('rel'));
		$('#paginationForm').submit();
	});


	// sort item list
	$('#sort_by_selector').change(function() {
		$('#sort_by').submit();
	});

	$('.item_box_image').unbind('mouseenter').bind('mouseenter', function() {
		$(this).stop().animate({
			'opacity': '0.75'
		}, 150);
	});

	$('.item_box_image').unbind('mouseleave').bind('mouseleave', function() {
		$(this).stop().animate({
			'opacity': '1'
		}, 1500);
	});

	$('.jump_to_site_wrapper a').unbind('click').bind('click', function(e) {
		e.preventDefault();

		var site = $(this).siblings('.new_site').val();
		var link = $(this).attr('href');
		if (site != 'undefined' && site != '') {
			window.location.href = link + site + '/';
		}
	});

	$('.site_form').unbind('submit').bind('submit', function(e) {
		e.preventDefault();
		$('a', this).trigger('click');
	});

	$('#pagerInput').keyup(function() {
		var tv = $(this).val();

		tv = tv.replace(/[^0-9]/, '');

		$(this).val(tv);
	});

	$('.menu_field').click(function() {
		var object = $(this).parent();
		var category_menu_height = $('.category_menu ul', object).outerHeight();

		if ($('.menu_arrow', object).hasClass('menu_arrow_active')) {
			$('.category_menu', object).stop().animate({
				'height': '0px'
			}, 250, function() {
				$(this).css({
					'border': '0px solid #000000'
				});
				$('.menu_arrow', object).removeClass('menu_arrow_active');
			});
		}
		else {

			$('.menu_arrow', object).addClass('menu_arrow_active');

			$('.category_menu', object).stop().animate({
				'height': category_menu_height + 'px'
			}, 100).css({
				'border': '1px solid #000000'
			});

		}
	});

	$('.sort_item').click(function(e) {
		e.preventDefault();
		var sort_by = $(this).attr('rel');

		if ($('.content_box_item_list .sort_wrapper .sort_direction_up').hasClass('sort_up_active')) {
			var sort_direction = 'up';
		}
		else {
			var sort_direction = 'down';
		}
		var sort_val = sort_by + '_' + sort_direction;

		$('#sort_by_selector').val(sort_val);
		$('#sort_by').submit();
	});

	$('.sort_direction_up').click(function(e) {
		e.preventDefault();

		if (!$(this).hasClass('sort_up_active')) {
			var sort_by = $('#sort_selection_item').val();
			var sort_direction = 'up';
			var sort_val = sort_by + '_' + sort_direction;
			$('#sort_by_selector').val(sort_val);
			$('#sort_by').submit();
		}

	});

	$('.sort_direction_down').click(function(e) {
		e.preventDefault();

		if (!$(this).hasClass('sort_down_active')) {
			var sort_by = $('#sort_selection_item').val();
			var sort_direction = 'down';
			var sort_val = sort_by + '_' + sort_direction;
			$('#sort_by_selector').val(sort_val);
			$('#sort_by').submit();
		}

	});

        $('#category_search_button').click(function(e) {
		e.preventDefault();
		var uri = $(this).attr('href');
                uri = uri + $('#category_search_item').val();
                window.location.href = uri;
	});

	$('.item_variant_box').live('click', function(e) {
		e.preventDefault();

		$(this).next('.variant_selection_box').slideToggle({
			'duration': 100,
			'easing': 'linear'
		});
		
		if ($(this).next('.item_variant_box').hasClass('item_variant_box_active')) {
			$(this).next('.item_variant_box').removeClass('item_variant_box_active');
		} else {
			$(this).next('.item_variant_box').addClass('item_variant_box_active');
		}
	});

	
	// Load item images and show modal gallery
	$('.list_item_box .imagebox').live('click', function(e) {
		e.preventDefault();

		var itemId = $(this).attr('rel');

		$.post('/index.php?website_modul=website_item&action=get_images', {
			'param_1': itemId
		}, function(data) {
			var images = [];

			$.each(data, function(p, img) {
				images.push([shop_dir + 'images/article/' + img]);

				console.log(shop_dir + 'images/article/' + img);
			});

			$.slimbox(images, 0, {
				loop: true
			});
		}, 'json');
	});


	// Infinite scrolling
	useInfinityScroll = useInfinityScroll || false;
	
	if (useInfinityScroll) {
		var queryReady = true;
		var noMoreItems = false;
		var page = 1;

		$(window).scroll(function() {

			var outOfView = $('.listcontent').offset().top + $('.listcontent').height() - $(window).height() - $(window).scrollTop();

			if (outOfView < 500 && !noMoreItems) {
				if (queryReady) {
					++page;
					queryReady = false;

					$.get(window.location.href, {
						'loadstep': page
					}, function(ajaxData) {
						if (typeof ajaxData === 'object' && typeof ajaxData !== 'undefined') {
							if (typeof ajaxData.counter !== 'undefined' && ajaxData.counter > 0 && typeof ajaxData.template !== 'undefined') {
								$('.listcontent').append(ajaxData.template);

								makeImageSubtexts();

								queryReady = true;
							} else {
								queryReady = false;
							}
						} else {
							queryReady = false;
						}
					}, 'json');
				}
			}
		});
	}
});