$(document).ready(function() {
	/* Sidepicture-Zoom */
	$('div.sidepicture.zoompic').each(function() {
		var _curZP=$(this); // store current zoompic-container
		var _maxWidth=$('#content').width();
		var _prevWidth=$(this).children('div.sidepicture-moreimages').children('img').width();
		var _imgId=1;

		$(this).children('a.zoompic').width($(this).children('a.zoompic').children('img').width()); // fix the a-container width to the size of the image
		$(this).children('img.hugepic').attr('zid',0).appendTo(_curZP); // clone the zoom-picture and use as hugepic
		$(this).children('a.zoompic').children('img').attr('zid',0); // set unique id for this element
		
		$(this).on('click','img.hugepic',function(){$(this).css('left','-9999px');}); // bind hugepic-click to hide it
		$(this).on('click','a.zoompic',function(){_curZP.children('img.hugepic[zid="'+$(this).children('img').attr('zid')+'"]').css('left','0');return false;}); // bind zoompic-click to show the hugepic
		
		$(this).children('div.sidepicture-moreimages').children('img').each(function(){
			$(this).attr('zid',_imgId); // set uid
			$(this).clone().attr('width',null).attr('height',null).addClass('hugepic').appendTo(_curZP); // clone to hugepic
			if (_curZP.children('img.hugepic[zid="'+_imgId+'"]').width()>_maxWidth) _curZP.children('img.hugepic[zid="'+_imgId+'"]').attr('width',_maxWidth); // fix hugepic-width
			_imgId++;
		});
		
		// handle more images
		$(this).children('div.sidepicture-moreimages').on('click','img',function() {
			var pw=_curZP.children('a.zoompic').children('img').attr('width'); //store image width
			_curZP.children('a.zoompic').children('img').appendTo($(this).parent()).attr('width',_prevWidth).attr('height',null); // set current zoompic as more-pic and resize to the correct width
			$(this).appendTo(_curZP.children('a.zoompic')).attr('width',pw); // use current more-pic as new zoompic
			return false;
		});
	});

	/*Foldered-Text*/
	if ($('div.foldered').length > 0) {
		$('div.foldered').each(function(){
			$(this).children('div.box2').parent('div.foldered').append('<a href="javascript:void(0)" class="foldered-action box-closed">mehr lesen</a><div class="clearer"></div>');
			$(this).children('a.foldered-action').bind('click',function(){
				$(this).parent('div.foldered').children('div.box2').slideToggle('fast',function(){
					var b=$(this).parent('div.foldered').children('a.foldered-action')
					if ($(this).is(':hidden')) {
						$(b).removeClass('box-open').addClass('box-closed').text('mehr lesen');
					} else {
						$(b).removeClass('box-closed').addClass('box-open').text('weniger lesen');
					}
				});
				return false;
			});
		});
	}
	
	/*Tabs*/
	if($('div.tabContainer').length>0) {
		var ttc='div.tabContainer';
		var thb='div.tabHeaderBox';
		var tcb='div.tabContentBox';
		$(ttc).each(function(){
			var _tc=0;
			var _tmh=0;
			$(this).append('<div class="tabHeaderBox"></div>');
			$(this).append('<div class="tabContentBox"></div>');
			$(this).children('div.tabbox').each(function(){
				if ($(this).height()>_tmh) _tmh=$(this).height();
			});
			$(this).children('div.tabbox').each(function() {
				_tc++;
				var tt=$(this).find('h2:first').text();$(this).find('h2:first').remove();
				$(this).parent(ttc).children(thb).append('<a href="#" class="tab'+_tc+'">'+tt+'</a>');
				$(this).removeClass('tabbox').addClass('tab tab'+_tc).height(_tmh-50).appendTo($(this).parent(ttc).children(tcb));
			});
			var _ccb=$(this).children(tcb);
			var _chb=$(this).children(thb);
			_chb.children('a:eq(0)').addClass('active');
			_ccb.children('div.tab:eq(0)').addClass('active');
			_chb.children('a').click(function(){
				var idx=_chb.children('a').index($(this));
				if (_chb.children('a:eq('+idx+')').hasClass('active')) return false;
				_chb.children('a').removeClass('active');
				_ccb.children('div.tab').hide();
				_chb.children('a:eq('+idx+')').addClass('active');
				_ccb.children('div.tab:eq('+idx+')').fadeIn();
				return false;
			});
		});
	}
	
	/*Productrating*/
	if ($('#ekomi-productrating').length>0) {
		function loadProductRating() {
			$('#ekomi-productrating .feedbackbox').hide();
			$.ajax({
				url: "/assets/getProductFeedback.php",
				type: "GET",
				dataType: 'json',
				data: {'product_id':$.trim($('span#productid').text().replace('Art-Nr:',''))},
				success: function(data) {
					$('#ekomi-productrating .feedbackbox').html(''); // feedbackbox leeren..
					if (data.state=="success") {
						$.each(data.data,function(i,l){
							$('#ekomi-productrating .feedbackbox').prepend('<div class="ecfb"><div class="rstars cnt-'+l.rating+'"><div>&nbsp;</div></div><div class="rdate">'+l.rdate+'</div><div class="rtext">'+l.feedback+'</div></div>');
						});
						if (data.data.length>0) {
							$('#ekomi-productrating .infotext').clone().show().appendTo($('#ekomi-productrating .feedbackbox'));
							$('#ekomi-productrating .feedbackbox').fadeIn('fast');
						}
					} else {
						$('#ekomi-productrating .feedbackbox').append('<p>'+data.msg+'</p>').hide().fadeIn('fast');
					}
					
				}
			});
		}
		if ($('span#productid')) {
			$(document).on('ready',loadProductRating);
			$('table.variationTable td.varSelect').on('change','select',loadProductRating);
			
		}
	}
});
