String.prototype.trim = function(){ return this.replace(/^\s*/,'').replace(/\s*$/,''); }
var text2xml = function(s) { var x, ie = /msie/i.test(navigator.userAgent); try{ var p = ie? new ActiveXObject("Microsoft.XMLDOM") : new DOMParser(); p.async = false; }catch(e){ throw new Error("XML Parser could not be instantiated") }; try{  if(ie) x = p.loadXML(s)? p : false; else x = p.parseFromString(s, "text/xml"); }catch(e){ throw new Error("Error parsing XML string") }; return x; }; 
function empty_recursive(el){ $(el).attr('busy', 'y'); $(el).val($(el).val().substring(0, $(el).val().length-1)); if($(el).val() != ''){ setTimeout(function(){ empty_recursive($(el)); }, 2); } else{ $(el).change(); $(el).attr('busy', 'n'); } }
function refill_recursive(el){ $(el).attr('busy', 'y'); $(el).val($(el).attr('defVal').substring(0, $(el).val().length+1)); if($(el).val() != $(el).attr('defVal')){ setTimeout(function(){ refill_recursive($(el)); }, 2); } else{ $(el).change(); $(el).attr('busy', 'n'); } }		

$(document).ready(function(){
	$('input[type=text]').not('.excl').each(function(){ if($(this).val().length > 0){ if(!$(this).attr('defVal') || $(this).attr('defVal').length <= 0) $(this).attr('defVal', $(this).val()); $(this).focus(function(){ if($(this).val() == $(this).attr('defVal')){ empty_recursive($(this)); } }); $(this).blur(function(){ if($(this).val() == ''){ refill_recursive($(this)); } }); } });
	$('input.password').each(function(){ $(this).change(function(){ if($(this).attr('id').match(/-t$/) && $(this).val() == ''){ $(this).hide(); $('#'+$(this).attr('id').replace(/-t$/, '')).show().focus(); } }); $(this).blur(function(){ if(!$(this).attr('id').match(/-t$/) && $(this).val() == ''){ $(this).hide(); $('#'+$(this).attr('id')+'-t').val('').show(); el = $(this); setTimeout(function(){ refill_recursive($('#'+$(el).attr('id')+'-t')) }, 200); } }); $(this).keyup(function(){ $(this).change(); }); });
	$('div.button:not(.noclick)').click(function(){ if($(this).parent().find('a').attr('target') == '_blank') window.open((!$(this).parent().find('a').attr('href').match(/^http:\/\//)? base : '')+$(this).parent().find('a').attr('href'));else window.location = (!$(this).parent().find('a').attr('href').match(/^http:\/\//)?base:'')+$(this).parent().find('a').attr('href'); }); //custom button click
	
	// menu
	var menutimeout = 400;
	var menu_init = function(){ $('div.top ul.menu > li').each(function(){ if($(this).find('div.sub').length > 0){ $(this).find('div.sub ul li *').each(function(){ $(this).html($(this).text().replace(/ /g, '&nbsp;')); });$(this).find('div.sub').show().css({ top: ($(this).offset().top+39)+'px',left: $(this).offset().left+'px', height:($(this).find('ul:first').height()+20)+'px'}).hide(); } }); }
	$(window).bind('resize', function(){ menu_init(); }); menu_init();

	$('div.top ul.menu > li').each(function(){
		var t;
		var el = $(this); 
		if($(this).find('div.sub').length > 0){
			$(this).mouseenter(function(){
				clearTimeout(t);
				var el = $(this);
				if(!$(this).hasClass('active') && $(this).find('div.sub').is(':hidden') > 0){ 
					$(el).addClass('lock'); setTimeout(function(){ $(el).removeClass('lock') }, menutimeout);
					//$(this).find('div.sub').show(menutimeout, function(){ $(el).addClass('active'); });
					$(this).find('div.sub').fadeIn(menutimeout, function(){ $(el).addClass('active'); });
				}
				$('div.top ul.menu > li:has(div.sub)').not($(this)).each(function(){ $(this).mouseout(); });
			});
			$(this).mouseleave(function(){
				var el = $(this);
				if(!$(this).hasClass('lock')){ 
					//t = setTimeout(function(){ $(el).find('div.sub').hide(menutimeout, function(){ $(el).removeClass('active'); }) }, 200);
					t = setTimeout(function(){ $(el).find('div.sub').fadeOut(menutimeout, function(){ $(el).removeClass('active'); }) }, 200);
				}
				else{ t = setTimeout(function(){ $(el).mouseout(); }, menutimeout+200); }
			});
			$(this).find('div').mouseenter(function(){ clearTimeout(t); });
			$(this).find('div').mouseleave(function(){ t = setTimeout(function(){ $(el).find('div.sub').fadeOut(menutimeout, function(){ $(el).removeClass('active'); }) }, 200);  });
			$(this).find('>a').mouseenter(function(){ clearTimeout(t); });
		}
		$(this).find('>a').not('.excl').click(function(){ return false;  });
		$(this).find('ul > li').click(function(e){ 
			e.stopPropagation();
			window.location = base+$(this).find('>a').attr('href'); 
		});
	});
	
	// anim-txtup
	$('.anim-txtup .animcnt').hide();
	setTimeout(
		function(){
			$('.anim-txtup').each(function(){
				$(this).find('.animcnt').show();
				var txtheight = $(this).find('.animcnt .anim-blackout-content').height();
				var aheight = $(this).find('h1').height()+$(this).find('h2').height();
				var xleft = 1;
				var xtop = $(this).height()-1;
				var ntop = ($(this).height()-21)-(txtheight+aheight);
				$(this).find('.animcnt').css({ top: xtop+'px', left: xleft+'px', height: 0+'px' });
				$(this).find('.animcnt .anim-blackout-content').css('height', txtheight+'px').css('top', '-'+(txtheight+10)+'px');
				$(this).find('.animcnt .anim-blackout').css('height', (txtheight+20)+'px');
				$(this).find('.animcnt').animate({ height: (txtheight+20)+'px', top: ntop+'px' }, 3000);
			});
		}, 1000);
	
	//txtup-slide
	$('div.entry-cnt').width($('div.anim-txtup-slide div.entry:first').width()*$('div.anim-txtup-slide div.entry').length+'px');
	var slide_time = 1000, slideup_time = 1000, display_time = 8000, rotateafteranim_time = 200;
	$('div.anim-txtup-slide div.entry:first').show();
	var loadvisible = function(el){
			$(el).find('.animcnt').show();
			var txtheight = $(el).find('.animcnt .anim-blackout-content').height();
			var aheight = $(el).find('h1').height()+$(el).find('h2').height()+14;
			var xleft = 1;
			var xtop = $(el).height()-1;
			var ntop = ($(el).height()-21)-(txtheight+aheight);
			$(el).find('.animcnt').css({ top: xtop+'px', left: xleft+'px', height: 0+'px' });
			$(el).find('.animcnt .anim-blackout-content').css('height', (txtheight)+'px').css('top', '-'+(txtheight+10)+'px');
			$(el).find('.animcnt .anim-blackout').css('height', (txtheight+20)+'px');
			$(el).find('.animcnt').animate({ height: (txtheight+20)+'px', top: ntop+'px' }, slideup_time);
	}
	var unloadentry = function(el){ var xtop = ($(el).height()-1); $(el).find('.animcnt').css({ top: xtop+'px', height: 0+'px' }).hide(); }
	var slideout = function(el){
		var tw = $(el).width();
		$('div.entry-cnt').animate({left:'-'+tw+'px'}, slide_time, function(){
			$(el).appendTo($(el).parent());
			$('div.entry-cnt').css({left:'0px'});
		});
	}
	function rotate(){ var c_el = $('div.anim-txtup-slide div.entry:first'); var n_el = $(c_el).next(); setTimeout(function(){ loadvisible(c_el); }, slide_time); setTimeout(function(){ slideout(c_el); }, display_time); setTimeout(function(){ unloadentry(c_el); }, slide_time+display_time); setTimeout(function(){ rotate(); }, slide_time+rotateafteranim_time+display_time); }
	setTimeout(function(){ rotate(); }, rotateafteranim_time+2000);
	
	// search pre results
	var toolt = $('<div class="toolt"></div>');
	var curreq = false;
	$('#main-zoek-input-term').keyup(function(){
		if($(this).val().length >= 3){
			var el = $(this);
			if(curreq) curreq.abort();
			curreq = $.get(base+'home/index/search/'+$(this).val(), function(data){
				var right = $(document).width()-($(el).offset().left+$(el).outerWidth());
				if($('#'+$(el).attr('id')+'_toolt').length == 0){ $(toolt).clone().appendTo('body').attr('id', $(el).attr('id')+'_toolt').css({top: $(el).offset().top+$(el).outerHeight()-1+'px', right:right+'px'}).hide(); }
				if(data != '<!&NULL&!>'){
					data = text2xml(data);
					$('#'+$(el).attr('id')+'_toolt').not(':visible').show(100);
					$('#'+$(el).attr('id')+'_toolt').html('<ul></ul>');
					$(data).find('artikel').each(function(){
						$('<li><a href="./assortiment/'+$(this).find('groep>groep').text()+'/'+$(this).find('groep>naam').text().replace('/', '_')+'/1/'+$(this).find('artikelnummer').text()+'/'+$(this).children('naam').text().replace('/', '_')+'.html">'+$(this).children('naam').text()+' (<span style="font-weight:bold;">'+$(this).find('artikelnummer').text()+'</span>)</a></li>').appendTo($('#'+$(el).attr('id')+'_toolt ul'));
					});
				}
				else{ $(document).click(); }
			}, 'xml');
		}
		else{
			$('#'+$(this).attr('id')+'_toolt').html('<ul></ul>');
			$(document).click();
		}
	});
	$('.toolt ul li').live('click', function(e){
		$('#'+$(this).parents('div.toolt').attr('id').replace('_toolt', '')).focus();
		$('#'+$(this).parents('div.toolt').attr('id').replace('_toolt', '')).val($(this).text());
		window.location = base+$(this).find('a').attr('href');
		e.stopPropagation();
	});
	
	$('#main-zoek-input-term').focus(function(e){ $(this).keyup(); });
	$('#main-zoek-input-term').click(function(e){ e.stopPropagation(); });
	$(document).click(function(){ $('.toolt').hide(100); });
	
});

$(document).ready(function(){
	Cufon.replace('h1');
	Cufon.replace('h2:not(.cufonexcl)');
	Cufon.replace('.productgroepen li a');
	//Cufon.replace('.menu li a');


	$('div.assortiment-part > div.productgroepeniconen').click(function(){
		window.location = base+$(this).parent().find('a').attr('href');
	});

	$('ul.ww li').mouseenter(function(){
		$(this).find('div a').css({color:'#ED1C2E'});
		$(this).find('div.button').show(100);
	});
	$('ul.ww li').mouseleave(function(){
		$(this).find('div a').css({color:'#000000'});
		$(this).find('div.button').hide(100);
	});
	$('ul.ww li div.button.cross').click(function(){
		window.location = base+$(this).attr('href');
	});
	
	$('.nieuws1 div').not('.nieuws1 div:eq(0)').hide();
	$('.nieuws2 a').click(function(){
		var href = $(this).attr('href').split('#');
		$('.nieuws1 div').hide();
		$('.nieuws1 #'+href[href.length-1]).show();
	});
	
	
	$('form').submit(
		function(){
			$(this).find(':input[type=text]').each(function(){
				if($(this).val() == $(this).attr('defVal')) $(this).val('');
			});
		}
	);
	
});