function showSizes(ref) {
	id = $(ref).attr('rel');
	
	if (!$('#sizes_'+id).is(":visible")) {
		
		// alle ausblenden
		$(".sizes").hide();
		$("#ItemSizes_"+id).hide();
		$("#sizes_"+id).show();
		
		//Standards setzen
		$(".ListingPriceWrap").css('background-color','#ffffff');
		$(".show_sizes").css('background-color','#ffffff');
		$(".detailtext").text("Details");
		
		//MouseIn Effekte
		$("#ItemSizes_"+id).fadeIn('fast');
	}
}

function hideSizes(ref) {
	
	id = $(ref).attr('rel');
	$('#ItemSizes_'+id).fadeOut();
	$("#sizes_"+id).hide();
	
}

function start_hover_listing_item(ref) {
//	alert('test');
//	console.log('hover',$(ref).attr('id'));
//	alert('test');
	
	$(ref).stop(true,false);
	$(ref).find('.product_listing_item_more_images').stop(true,true);
	$(ref).find('.product_listing_item_sizes').stop(true,true);
	$(ref).find('.product_listing_item_buttons').stop(true,true);
	$(ref).find('#product_listing_item_price').stop(true,true);
	$(ref).find('#product_listing_item_price_hover').stop(true,true);

	delay = 400; //Verzögerung

	$('.product_listing_item_hover').css('z-index','0');

	//Listenbild ersetzen
	$(ref)
	.find('img.product_listing_item_image')
	.attr(
			'src',
			$(ref).find('img.product_listing_item_image').attr('src').replace(/-suche/,'-1-m'))
	.css({
		'z-index' :'1000', 'height':'', 'width':'140' 
		//'MozBoxShadow': '0 0 12px #666','WebkitBoxShadow' : '0 0 12px #666', 'boxShadow ': '0 0 12px #666'
	});
	
	$(ref).find('div.product_listing_item_more_images>img').each(function(index){
		$(this).attr('src',$(this).attr('src_load'));
//		console.log($(this).attr('src_load'));
	});
	
	

	$(ref).delay(delay)
		  .css({
			zIndex :'1200',
			WebkitBorderRadius: '0.5em', MozBorderRadius: '0.5em', borderRadius: '0.5em',
			MozBoxShadow: '0 0 12px #666',
			WebkitBoxShadow: '0 0 12px #666',
			boxShadow: '0 0 12px #666',
			backgroundColor: '#ffffff'
//			width: '220px'
	      });
	
	//Rahmen aufziehen	
	$(ref).delay(delay)
	      .animate({
				marginTop : '-95px', //Verschiebung nach oben
				marginBottom : '-95px', //Verschiebung nach unten
				marginLeft : '-20px',//Verschiebung nach links
				marginRight : '-50px',//Verschiebung nach rechts
				width : '320px', //Zielbreite
				height : '510px' //Zielhöhe
			},
			{
				step: function(now, fx) {
					width = 252; //Zielbreite vom Bild
					height = 330; //Zielhöhe vom Bild
					if (fx.prop=='width') {
						bild_start = 140; //Breite vom Original-Listenbild
						progress = ((now-fx.start)/(fx.end-fx.start));
						width_add = (width-bild_start)*progress; 
						width_current = bild_start + width_add;
						$(ref).find('.debug').text(fx.start);
						$(ref).find('img.product_listing_item_image').css('width',width_current);
					}
					if (fx.prop=='height') {
						bild_start= 170; //Höhe vom Original-Listenbild
						progress = ((now-fx.start)/(fx.end-fx.start));
						height_add = (height-bild_start)*progress; 
						height_current = bild_start + height_add;
//						margin_current = 95*progress*-1+0.1;
//						$(ref).find('.debug').text(progress);
						$(ref).find('.product_image_con').css('height',height_current);
//						$(ref).css('margin-bottom',margin_current);
						
					}
//					console.log($(ref).css('height'));
//					$(ref).unbind('mouseenter');
//					$(ref).unbind('mouseleave').mouseleave(function(){start_unhover_listing_item(ref);});
					
				},
				duration: 300,
				complete: function() {
					$(ref).find('img.product_listing_item_image').css({
						'border':'0px solid grey'
					});
					$(ref).find('.product_listing_item_more_images').fadeIn();
//					$(ref).find('.product_listing_item_sizes').fadeIn();
//					$(ref).find('.product_listing_item_buttons').fadeIn();
					$(ref).find('#product_listing_item_price').slideUp();
					$(ref).find('#product_listing_item_price_hover').slideDown();
					$(ref).find('.product_listing_item_sizes').slideDown();
					$(ref).find('.product_listing_item_buttons').slideDown();
				},
			});
}

function stop_hover_listing_item(ref) {
//	console.debug('hover stopped',$(ref).attr('id'));
	$(ref).stop(true,true);
}

function start_unhover_listing_item(ref) {
//	console.debug('unhover',$(ref).attr('id'));
	$(ref).stop(true,false);
	$(ref).find('.product_listing_item_more_images').stop(true,true);
	$(ref).find('.product_listing_item_sizes').stop(true,true);
	$(ref).find('.product_listing_item_buttons').stop(true,true);
	$(ref).find('#product_listing_item_price').stop(true,true);
	$(ref).find('#product_listing_item_price_hover').stop(true,true);
	
	delay = 20;

	//Rahmen schließen	
	$(ref).find('.product_listing_item_more_images').fadeOut();
	$(ref).find('.product_listing_item_sizes').fadeOut();
	$(ref).find('.product_listing_item_buttons').fadeOut();
	$(ref).find('#product_listing_item_price').fadeIn();
	$(ref).find('#product_listing_item_price_hover').fadeOut();
	
	width = $(ref).find('img.product_listing_item_image').css('width').replace('px','');
	height = $(ref).find('.product_image_con').css('height').replace('px','');
					
	$(ref).delay(delay)
	.animate({
				marginTop : '0px',
				marginBottom : '0px',
				marginLeft : '20px',
				marginRight : '0px',
				width : '230px',
				height : '320px',
				position : 'static'
			},
			{
				step: function(now, fx) {
					if (fx.prop=='width') {
						bild_start = 140;
						progress = ((now-fx.end)/(fx.start-fx.end));
						width_add = (width-bild_start)*progress; 
						width_current = bild_start + width_add;
						$(ref).find('.debug').text(fx.start);
						$(ref).find('img.product_listing_item_image').css('width',width_current);
					}
					if (fx.prop=='height') {
						bild_start= 170;
//						progress = ((now-fx.end)/(fx.start-fx.end));
						progress = 1-((now-fx.start)/(fx.end-fx.start));
						height_add = (height-bild_start)*progress; 
						height_current = bild_start + height_add;
//						$(ref).find('.debug').text(now);
						$(ref).find('.product_image_con').css('height',height_current);
					}
//					$(ref).unbind('mouseleave').unbind('mouseenter');
				},
				duration: 100,
				complete: function() {
					$(ref).css({
						zIndex :'0',
						MozBoxShadow: '0 0 0px #fff',
						WebkitBoxShadow: '0 0 0px #fff',
						boxShadow: '0 0 0px #fff',
						width: '230px'
//						border: '0px',
					});
//					$(ref).unbind('mouseenter').mouseenter(function(){start_hover_listing_item(ref);});
//					$(ref).unbind('mouseleave').mouseleave(function(){stop_hover_listing_item(ref);});

				}
			});


}


function set_info_image(model,src) {
	
	$('#product_listing_item_image_'+model).attr('src',src);
	
}

function add_to_cart(ident) {
	alert($('#size_'+ident).val());
}

function load_next_article() {

//	return;
	if ($('#infinite_scroll_waiter:visible').length>0) {
//		alert($('#infinite_scroll_waiter'));
		return;
	}
	
	$('#infinite_scroll_waiter').show();
//	return;
	$.get('ajax/reload_itembox.php',
			{
				brand : $('#filter_brand').val(),
				gender : $('#filter_gender').val(),
//				class : $('#filter_class').val(),
				type : $('#filter_type').val(),
				color : $('#filter_color').val(),
				size : $('#filter_size').val(),
				konzept : $('#filter_konzept').val(),
				keywords : $('#filter_kw').val(),
		//		new: current_filters.new,
//				count : 19,
				modus : 'product_listing',
				exclude: $('#filter_artnrs_exclude').val()
			},
			function(result) {
				if (result=='EMPTY') {
					$('#infinite_scroll_anchor').remove();
				} else {
					if (result.match("^LAST")=='LAST') {
						$('#infinite_scroll_anchor').remove();
						result = result.substr(4);
					}
					$('#product_listing').append($(result));
					$('#infinite_scroll_waiter').hide();
					$('.product_listing_item_hover').mouseenter(function(){start_hover_listing_item($(this));});
					$('.product_listing_item_hover').mouseleave(function(){start_unhover_listing_item($(this))});

				}
			}
	);
	
}

$(document).ready(function(){

	//	alert('test');
	$('.product_listing_item_hover').mouseenter(function(){
//		alert('test');
		start_hover_listing_item($(this));
	});
	$('.product_listing_item_hover').mouseleave(function(){start_unhover_listing_item($(this))});

	$('.product_listing_item_show_sizes').mouseenter(function(){
		showSizes($(this));
	});
	$('.product_listing_item_show_sizes').mouseleave(function(){
		hideSizes($(this));
	});
	
	if ($('#infinite_scroll_anchor').length!=0) {
		$(window).scroll(function() {
	        if ($('#infinite_scroll_anchor').length!=0 && $(window).scrollTop()+$(window).height()>($('#infinite_scroll_anchor').offset().top-50)) {
//	        	console.log(($(window).scrollTop()+$(window).height())+ ' ' + ($('#infinite_scroll_anchor').offset().top-50));
	        	load_next_article();
	        }
	    });
	}
	console.log($('.product_listing_item').length);
});

