/* Hier findest du alle selbst erstellten Funktionen und Klassen, für die sich keine eigene Datei lohnt.
 */
var validation = "";


/* lazy loading */
function ll_img() {
	var items = $(".clickout img."+ll_tempClass);
	if(items.length ==0 )
		clearInterval(ll_imgInterval);
	$(items).each(function() {
		var imgOffset = $(this).offset();
		var scrollTop = $(document).scrollTop();
		var wh = $(window).height();
		if(imgOffset.top-350 < wh+scrollTop && imgOffset.top+350 > scrollTop) {
			$(this).attr('src', $(this).attr('longDesc'));
			$(this).removeClass(ll_tempClass);	
		}
	});
		
}
if(typeof ll_ll_maxItems == "undefined")
	var ll_maxItems = 15;
if(typeof ll_imgInterval == "undefined")
	var ll_imgInterval = null;
if(typeof ll_imgLoadGif == "undefined")
	var ll_imgLoadGif =  '/typo3conf/ext/living24templates/res/templates/fe/images/spinner.gif';
if(typeof ll_tempClass == "undefined")
	var ll_tempClass = 'imgLoad';
/*lazy loading ende*/

$(document).ready(function() {



	$(".clickout img[longdesc!='']").each(function(index){
		if(typeof $(this).attr('longdesc') == 'undefined')
			return;

		$(this).attr('alt', $(this).attr('title'));
		if(index < ll_maxItems) {
			$(this).attr('src', $(this).attr('longDesc'));
		} else {
			$(this).addClass(ll_tempClass);
			$(this).attr('src',ll_imgLoadGif);
		}
	});	
	ll_imgInterval = setInterval('ll_img()', 100);



	$('.clickout, .zoom_image').click(function () {
		var data = $(this).attr('name').split('|');
		data[3] = escape(data[3]);
		data[2] = escape(data[2]);
		orderID = escape(new Date().getTime()+'-'+data[3]+'-'+Math.floor(Math.random()*999999));
		/* Criteo clickout Tracking */
		$('body').append('<div style="display:none"><img src="https://sslwidget.criteo.com/psu/display.js?p1='+escape('v=2&wi=7715269&t='+orderID+'&s=1&i1='+data[3]+'&p1='+data[2]+'&q1=1')+'&t1=transaction&resptype=gif&cb='+orderID+'"/></div>');		
		/* Criteo clickout Tracking */
	});
	
	$('#product-details #detailMore').click(function() {
		$(this).css('display', 'none');
		$('#product-details #detailLess').css('display', 'block');
		$('#product-details #descrlong').css('display', 'block');
		$('#product-details #descrshort').css('display', 'none');
	});
	
	$('#product-details #detailLess').click(function() {
		$(this).css('display', 'none');
		$('#product-details #detailMore').css('display', 'block');
		$('#product-details #descrlong').css('display', 'none');
		$('#product-details #descrshort').css('display', 'block');
	});
	
	$('#product-details #productsMore').click(function() {
		$(this).css('display', 'none');
		$('#product-details #productsLess').css('display', 'block');
		$('#product-details .productHide').css('display', 'block');
	});
	
	$('#product-details #productsLess').click(function() {
		$(this).css('display', 'none');
		$('#product-details #productsMore').css('display', 'block');
		$('#product-details .productHide').css('display', 'none');
	});
	
	var cid = null;
	var i, param;
	var getParams = document.location.search.substr(1).split('&');
	for(i=0; i<getParams.length; i++) {
		param=getParams[i].split('=');
		if(param[0]=='cid') {
			cid = param[1];
			break;
		}
	}
	
	if(dom.indexOf('?')>0)
		var link = dom+'&type=25&tx_smatchmaster_pi1[ajax]=krid';
	else 
		var link = dom+'?type=25&tx_smatchmaster_pi1[ajax]=krid';
	
	link += cid ? '&cid='+cid : '';
	
	$('#search').submit(function (event) {
		
		var search = $(this).find('#textfield').val();
		if(search == 'Ihr Suchbegriff' || search == '' || search == '*')
		{	
			event.stopPropagation();
			event.preventDefault();
			return false;
		
		}

		$.cookie('searching', search, {path:'/', domain:document.location.host, expires:1});
		return true;
	});
	
	$.ajax({
			  url: link,
			  dataType: 'json',
			  data: '',
			  success: function (ret) {

				validation = ret.krid;
				}
			});

		$('#priceFromS, #priceToS').bind('keyup', function(ev) {
			
			$('#filterNowBtn span').css('display', 'inline');
			if(ev.keyCode == 13)
		    	 $('#filterForm').submit();
		});
		
		$("#filterNowBtn span").click(function (event) {
			$('#filterForm').submit();
			return false;
			
		
		});
		
		$('.allCategories').mouseenter(function(item) {
			$(this).find('ul').css('display', "block");
			return false;
		});

		$('.allCategories').mouseleave(function(item) {
			$(this).find('ul').css('display', "none");
			return false;
		});
		
		
		$('div.filters ul.topfilter li.active').mouseenter(function(item) {
			$(this).find('ul.innerUl').css('display', "block");


			return false;

		});

		$('div.filters ul.topfilter li.active').mouseleave(function(item) {
			$(this).find('ul.innerUl').css('display', "none");


			return false;

		});
		$('div.filters ul.topfilter li.active ul.innerUl').mouseleave(function(item) {
			$(this).css('display', "none");


			return false;

		});
		
		
	window.onmouseup = function(ev) {
		if(!isAutocomplete(ev.target)) {
			closeAutocomplete();
		}
	};
	

	
	//einzelne menupunkte deaktivieren mit ts: NO.linkWrap = <span class="nolink">|</span>
	$("span.nolink").click(function() { return false; });
	
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo(0.1, 1).show();
		$(this).find(".sub").css('z-index', '999999');
		$(this).find(".sub").parent().find("a").addClass("selected");
		$("#nav li .sub li").find("a").removeClass("selected");
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
	//		$(this).calcSubWidth();
			//Set Width
//			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
		$(this).find(".sub").css('z-index', '99999');  
		$(this).find(".sub").stop().fadeTo(0.1, 0, function() {
		  $(this).hide();
		
		  $("#nav li").find("a").removeClass("selected");
	  });
	}
	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 70, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 70, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
 
	$("#nav li .sub").css({'opacity':'0'});
	$("#nav li").hoverIntent(config);

//	$("ul.items li").hover(function(event){ 
	$("ul.items li .middleOptions").hover(function(event){
		$(this).addClass("");
		if($(this).find('.nclickout').length != 0)
			$(this).parent('li').find('.product_detail').addClass('pdt_hov');
		$(this).find(".zum-shop").show();
	},function(event){
		if($(this).find('.nclickout').length != 0)
			$(this).parent('li').find('.product_detail').removeClass('pdt_hov');
		$(this).find(".zum-shop").hide();
	});
	
	$("#content .filter li a").not('#resetFilters a').click(function(event){
		$(this).parent().toggleClass("active");	
		if($(this).parent().parent()[0] == $("#content ul.filter")[0]  )
			return false;			
	});
 
	

	

	$('.zoom_image').each(function(index) {
    	var img = new Image();
        var elem = $(this);
        $(img).load(function () {
        	if (this.width<300 || this.height<300) {
            	elem.removeClass("zoom_image");
            } else {
            	maxWidth = this.width;
                maxHeight = this.height;
                if (maxWidth > 420) { maxWidth = 420 };
                if (maxHeight > 400) { maxHeight = 400 };
					   var options = {
						   zoomType: 'reverse', //standard/reverse/innerzoom
						   xOffset: 90, //zoomed div default offset
						   yOffset: 0,
						   imageOpacity: 0.3,
						   title : false,
						   zoomWidth: maxWidth,
						   zoomHeight: maxHeight
					   };
					   $(".zoom_image").jqzoom(options);
				   }
                })
                img.src = this.href;

	});
	// ENDE ZOOM CODE

	 $("input#textfield").attr("autocomplete", "off").autocomplete("?eID=suggester", {
     	minChars: 3,
        timeout: 90,
        validSelection: false,
        parameters: {}
    });

	$('#headline').html('');

		
	$('.filterMore a, .filterMore').unbind();
	$('.filterMore a, .filterMore').click(function(ev) {

			var parent = $(this).parents("ul")[0];
	                $(parent).children('.allItems').show();

	                $(parent).children('.filterLess').show();
	                $(this).parent().hide();
	                $(parent).children('.firstFive').hide();
			return false;
		});

	$('.filterLess a, .filterLess').unbind();
	$('.filterLess a, .filterLess').click(function(ev) {

			var parent = $(this).parents("ul")[0];
	                $(parent).children('.allItems').hide();
	                $(parent).children('.filterMore').show();
	                $(parent).children('.firstFive').show();
	                $(this).parent().hide();
			return false;
		});
	
	
	
	$.expr[':'].icontains = function(obj, index, meta, stack){
		return (obj.textContent || obj.innerText || jQuery(obj).text() || '').toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
	};
	
	$('.jsFilter').unbind();
	$('.jsFilter').focus(function (ev) {
		$(this).attr('value', '');
		return false;
	});
	$('.jsFilter').keyup(function(ev) {

		items= $(this).parents("li")[0];
		if($(this).attr('value') =="") {
		     liItems = $(items).children('ul').children("li").css('display','block');
		     return false;
		}
		liItems = $(items).children('ul').children("li").css('display','none');
		liItems = $(items).children('ul').children("li:icontains('"+$(this).attr('value')+"')").css('display','block');
		


		    return false;

	});         
	
	// Social Tracking auf Share Button legen
	$('a[name="fb_share"] span span span').click(function () {
		_gaq.push(['_trackSocial', 'facebook', 'share', document.location]);
	});
	
});

function isAutocomplete(elm) {
	if(elm.className=='autocomplete') {
		return true;
	} else if(elm.parentNode!=document) {
		return isAutocomplete(elm.parentNode);
	}
	return false;
}

function closeAutocomplete() {
	var i, ul;
	var uls = document.getElementsByTagName('ul');
	for(i=0; ul=uls[i]; i++) {
		if(ul.className=='autocomplete') {
			ul.innerHTML = '';
		}
	}
}

(function($) {
    $.fn.extend({
        isChildOf: function( filter_string ) {
          
          var parents = $(this).parents().get();
         
          for ( j = 0; j < parents.length; j++ ) {
           if ( $(parents[j]).is(filter_string) ) {
      return true;
           }
          }
          
          return false;
        }
    });
})(jQuery);


function trim(stringa)
{
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}

//for later use
var additionalGet = "";	


function urlencode (str) {
    // URL-encodes string  
    // 
    // version: 1107.2516
    // discuss at: http://phpjs.org/functions/urlencode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Joris
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: This reflects PHP 5.3/6.0+ behavior
    // %        note 2: Please be aware that this function expects to encode into UTF-8 encoded strings, as found on
    // %        note 2: pages served as UTF-8
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
    str = (str + '').toString();
 
    // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
    // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

