﻿$(document).ready(function() {

	productNav();
	productGallery();
	fadeEffects();
	newsAccordion();
	blockList();
	window.makeMailLinks()?makeMailLinks():null;

	
});

/* Product & Category Navigation */

function productNav() {
	
	$('.up').click(function () {
	
		$('#product-listing>ul>li>ul:visible').animate( {scrollTop: "0px"}, 500 );
		return false;
		
	});
	
	$('.down').click(function () {
	
		$('#product-listing>ul>li>ul:visible').animate( {scrollTop: "330px"}, 500 );
		return false;
		
	});
	
	$('#product-listing>ul>li>a').click(function() {
		var scrollheight = 0;
		$(this).next('ul').children().each(function(){
			scrollheight+=parseInt($(this).css('height'));
			});
		if(scrollheight > 330)
		{
			$('#menuscroll').show();
		}
		
		$(this).next('ul').show();
		$(this).next('ul').scrollTop(0);
		$(this).parent().parent().animate({marginLeft: "-250px"}, 500 );
		
		$('#product-listing>h2').text($(this).text());
		$('#product-listing>h2').addClass('backlink');
		
		return false;

	});
	
	$('#product-listing>h2:not(.passthrough)').click(function() {
		$('#menuscroll').hide();	
		$('#product-listing>h2').text('Categorieën');
		$('#product-listing>ul').animate({marginLeft: "0px"}, 500, function() {$('#product-listing>ul>li>ul').hide();} );
		$('#product-listing>h2').removeClass('backlink');
	
	});
	/*$('#nav li a').click(function(){
	
		if($('.'+$(this).attr('id')).size()>0){
			$('.division.default').addClass('hidden');
			$('.division.'+$(this).attr('id')).removeClass('hidden');
		}else{
			$('.division').addClass('hidden');
			$('.division.default').removeClass('hidden');
		}
		var scrollheight = 0;
		$('#'+$(this).attr('id')+'-sub').children().each(function(){
			scrollheight+=parseInt($(this).css('height'));
			});
		if(scrollheight > 330) {
			$('#menuscroll').show();
		} else {
			$('#menuscroll').hide();
		}
		$('#nav a.active').removeClass('active');
		$(this).addClass('active');
		$("#product-listing>ul:visible").hide();
 		$('#'+$(this).attr('id')+'-sub').show();
 		$('#product-listing>h2').empty();
 		if($('.'+$(this).attr('id')).size()>0){
			$('#product-listing>h2').append('Categorieën');
		}else{
			$('#product-listing>h2').append('&nbsp;');
		}
 		$('#product-listing>h2').show();
 		$('#product-listing>ul').css({marginLeft: "0px"});
 		$('#product-listing>ul>li>ul').hide();
 		$('#product-listing>h2').removeClass('backlink');
 		
 		return false;
	
	});*/
	/* do stuff on load if there's .active classes */
		if($('#nav a.active').attr('id')!=undefined){
			$('#'+$('#nav a.active').attr('id')+'-sub').show();
			$('#product-listing>h2').show();
			if($('#'+$('#nav a.active').attr('id')+'-sub>li>ul>li a.active')[0]!=undefined){
				//console.log($('#'+$('#nav a.active').attr('id')+'-sub ul li a.active'));
				$('#'+$('#nav a.active').attr('id')+'-sub ul li a.active').parent().parent().show();
				$('#product-listing>h2').text($('#'+$('#nav a.active').attr('id')+'-sub ul li a.active').parent().parent().parent().children('a').text());
				$('#product-listing>h2').addClass('backlink');
				//$('#'+$('#nav a.active').attr('id')+'-sub').animate({marginLeft: "-250px"}, 500 );
				$('#'+$('#nav a.active').attr('id')+'-sub').css({marginLeft: "-250px"});

				var scrollheight = 0;
				var ul = $('#'+$('#nav a.active').attr('id')+'-sub>li>ul>li a.active').parent().parent();
				ul.children().each(function(){
					scrollheight+=parseInt($(this).css('height'));
					});
				if(scrollheight > 330)
				{
					$('#menuscroll').show();
				}
			}
		}
				
}

/* Product Gallery */

function productGallery() {

	$('#thumbs a').click(function() {
	
		var src = $(this).attr('href');
		
		$('#product-detail>img').fadeOut('def', function() {
			$('#product-detail>img').attr('src', ''+src+'').fadeIn();
		});
	
		$('#thumbs>ul>li>a').removeClass('activethumb');
		$(this).addClass('activethumb');
		return false;
	});
	
}

/* Fade effects */

function fadeEffects() {

	setTimeout(function() {$('#product-detail img, #thumbs ul').fadeIn();}, 600);
	
}

		
/* Newssystem */

function newsAccordion() {
	if ( $('ul#newslist>li>ul>li>a').is('.active') )
		{
		
			$('ul#newslist>li>ul').hide();
			$('ul#newslist>li>ul>li>a.active').parent().parent().show();
		
		}else{
		
			$('ul#newslist>li>ul').hide();
			
		}
	$('ul#newslist>li>a.active').next().slideDown();
	$('ul#newslist>li>a').click(function() {
	
		if(!$(this).next('ul').is(':visible'))	{
			
			$('ul#newslist>li>ul:visible').slideUp('slow');
			$('ul#newslist>li>a.active').toggleClass('active');
		 
		}
		
		 $(this).next('ul').slideToggle('slow');
		 
				$(this).toggleClass('active');
				
	  return false;
	});
};

function blockList() {
	$('#blocklist>li>a').click(function() {
		$('#blocklist>li>a').removeClass('active');
		$(this).addClass('active');
		$('#txt>p').hide();
		$($(this).attr('href')).show();
		
		return false;
		
	});
}
/* <span class="email">x (at) x.com</span> becomes <a href="x@x.com">x@x.com</a> 
* this is an anti-spam measure
*/
function makeMailLinks()
	{
	if (!document.getElementsByTagName && !document.createElement && !document.createTextNode)
		return;
	var spans = document.getElementsByTagName("span");
	for(var i=0;i<spans.length;i++)
		{
		if (spans[i].className=="email")
			{
			var theNode = spans[i];
			var theAddress = theNode.firstChild.nodeValue;
			
			theAddress = theAddress.replace(/ \(at\) /, "@");
			theAddressNode = document.createElement('A');
			theAddressNode.setAttribute("href", "mailto:" + theAddress);
			theAddressNode.appendChild(document.createTextNode(theAddress));
			theNode.parentNode.replaceChild(theAddressNode, theNode);
			i--; //because we implicitly removed a <span> from the spans array by making it an <a>; Otherwise it'll skip the next span
			}
		}
	}

