

var validation = "";

$(document).ready(function() {
	// Tracking für die Teaser-Boxen auf der Startseite. Wir verlassen uns auf die vergebenen Div-Ids.
	$("div#c63").click(function() { _gaq.push(['_trackEvent','Teaserbox 1', 'Klick']); });	
	$("div#c64").click(function() { _gaq.push(['_trackEvent','Teaserbox 2', 'Klick']); });
	
	
	
	query = window.location.search;
	affilinet = query.match('m:affilinet');
	if(affilinet != null && affilinet.length > 0) {
		id = query.match("pid:{([0-9]+?)}");
		if(id != null && id.length > 0) 
			 $.cookie('affref', id[1], { path: '/', secure: false });	}
	
	$('.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));
		
		var ref = escape($.cookie('affref'));
		
		if(ref == "null")
			return;

		$('body').append('<IMG SRC="http://partners.webmasterplan.com/registersale.asp?site=8975&ref='+ref+'&mode=ppl&ltype=1&order='+orderID+'" WIDTH="1" HEIGHT="1">');
		additionalGet = '&cid=t:'+ref+'|m:affilinet|pid:'+ref+'|leadid:'+orderID+'&utm_source=Affiliate&utm_campaign=affilinet&utm_term=t:affiliate';
	});
	
	
	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 : '';
	
	$('#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 .productsHide').css('display', 'block');
	});
	
	$('#product-details #productsLess').click(function() {
		$(this).css('display', 'none');
		$('#product-details #productsMore').css('display', 'block');
		$('#product-details .productsHide').css('display', 'none');
	});
	
		$.ajax({
			  url: link,
			  dataType: 'json',
			  data: '',
			  success: function (ret) {

				validation = ret.krid;
				}
		});
	
	$('#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;
	});

	$('#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; });
	$("span.nolink").children('a').css('cursor','default');
	
	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").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: 20, //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]);
	});
	
});

//for later use
var additionalGet = "";





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;
}


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);
