$(document).ready(function() {	
	$("#slider").easySlider({
	});
	showSubSubMenu();
	product();
	productoverzicht();
	radiobuttons();
	checkbox();
	radiobuttons_betalen();
	afleveradres();
	toggleImg();
});

function showImage() {
	$('.thumb').click(function() {
		var sSource = $(this).children('img').attr('src');
		var sNewSource = sSource.replace('_thumb','_large');
		$('.thumb').removeClass('actief');
		$(this).addClass('actief');
		$('.center').children('img').attr('src',sNewSource);
	});
}

function sifrShoppincart() {
	/* winkelwagen */
	sIFR.replace(trinite, {
		selector: 'div.item .titel h3',
		css: [
			'.sIFR-root { color: #242f42; font-size: 20px; height: 20px; }'
		],
		wmode: 'transparent'
	});
	
	sIFR.replace(trinite, {
		selector: 'div.item .prijs h4',
		css: [
			'.sIFR-root { color: #b20000; font-size: 30px; height: 30px; }'
		],
		wmode: 'transparent'
	});
	
	/* overzichtspagina */
	sIFR.replace(trinite, {
		selector: 'div.cart .stappentitel h1',
		css: [
			'.sIFR-root { color: #b20000; font-size: 32px; height: 32px; }',
			'em { color: #242f42; }'
		],
		wmode: 'transparent'
	});	
}

function sifrPrice() {
	sIFR.replace(trinite, {
		selector: 'div.specificaties .prijs h3',
		css: [
			'.sIFR-root { color: #b20000; font-size: 30px; height: 30px; }'
		],
		wmode: 'transparent'
	});
}

function showSubSubMenu() {
	$('ul.submenu li').hover(function () {
		$(this).children('.subsubmenu').show();
	},function() {
		$(this).children('.subsubmenu').hide();
	});
}

function product() {
	$('li.categorie').hover(function () {
	  $(this).addClass('hover');
	},function() {
		$(this).removeClass('hover');	
	});
}

function productoverzicht() {
	$('.product').hover(function () {
	  $(this).addClass('hover');
	},function() {
		$(this).removeClass('hover');	
	});
}

// Function for custom radiobutton
function radiobuttons() {
	$('.radio_button').click(function(){
		$(this).parent('.radios').find('.radio_button').removeClass('active');
		$(this).children('.radio_input').attr('checked','checked');
		$(this).addClass('active');
	});
}

// Function for custom radiobutton betalen
function radiobuttons_betalen() {
	$('.links').click(function(){
		$(this).parent().parent().find('.radio').removeClass('active');
		$(this).children('.radio').children('.radio').attr('checked','checked');
		$(this).children('.radio').addClass('active');
	});
}

// Function for custom checkbox
function checkbox() {
	$('.3 .formulier .checkbox').click(function(){
		if($(this).children('#address').attr('checked') == true){
			$(this).children('#address').attr('checked',false);
			$(this).removeClass('active');
		} else {
			$(this).children('#address').attr('checked',true);
			$(this).addClass('active');
		}
	});
}

// Function for activate form aflever adres
function afleveradres(){
	$('#item_afleveradres').click(function(){
		$(this).next('.form').slideToggle();
		$(this).children().children('.checkbox').toggleClass('active');
		$('#aflever_form input:not(.not)').toggleClass('required');
		//alert($(this).find('#address').attr('checked'));
		if($(this).find('#address').attr('checked') == true){
			$(this).find('#address').attr('checked', false);
		} else {
			$(this).find('#address').attr('checked', true);
		}
	});
}

//Function voor afbeelding op detailpagina
function toggleImg() {
	$('div.thumb a').click(function(){
		$(this).parents().find('.thumb').removeClass('actief');
		$(this).parent().addClass('actief')
		var largePath = $(this).attr('name');
		$('img#largeImg').attr({src:largePath});
		return false;
	});
}

// Function for sorting the products
function sortProducts(){
	$('div.select div.selectbox-wrapper ul li').click(function(){
		var sValue = $(this).attr('id');
		loadProducts('sSort=' + sValue);
	});	
}

// Function for loading the products
function loadProducts(p_sData) {
	//alert(p_sData);
	$.ajax({
		type: 'POST',
		url: sDomain+'inc/products.inc.php',
		data: p_sData,
		success: function(result){
			$('#products').html(result);	
			productoverzicht();
			$('#select-sorteren').selectbox();
			sortProducts();
		}
	});
}

function shoppingcart() {
	$('.aantal .input input').change(function(){
		var sArticle = $(this).parents('.item').attr('id'); 
		var aProduct = sArticle.split('_');
		if(aProduct[0] == 's') {
			var nProductID = aProduct[1];
			var sSize = '';
			var sColour = aProduct[2];
		} else if(aProduct[0] == 'c') {
			var nProductID = aProduct[1];
			var sSize = aProduct[2];
			var sColour = '';
		} else if(aProduct[0] == 'sc') {
			var nProductID = aProduct[1];
			var sSize = '';
			var sColour = '';
		} else {
			var nProductID = aProduct[1];
			var sSize = aProduct[2];
			var sColour = aProduct[3];
		}
		var nQuantity = $(this).val();
		nQuantity = parseInt(nQuantity);
		//alert('nArtikelID=' + nArtikelID + '&nQuantity=' + nQuantity + '&sSize=' + sSize + '&nUpdateQuantity=' + nQuantity);
		updateShoppingcart('nProductID=' + nProductID + '&sSize=' + sSize + '&sColour='+ sColour +'&nUpdateQuantity=' + nQuantity);
	});
	$('.selectbox-wrapper ul li').click(function(){
		var sArticle = $(this).parents('.item').attr('id'); 
		var aProduct = sArticle.split('_');
		//alert(aProduct[0]);
		if(aProduct[0] == 's') {
			var nProductID = aProduct[1];
			var sSize = '';
			var sColour = aProduct[2];
		} else if(aProduct[0] == 'c') {
			var nProductID = aProduct[1];
			var sSize = aProduct[2];
			var sColour = '';
		} else if(aProduct[0] == 'sc') {
			var nProductID = aProduct[1];
			var sSize = '';
			var sColour = '';
		} else {
			var nProductID = aProduct[1];
			var sSize = aProduct[2];
			var sColour = aProduct[3];
		}
		var nQuantity = $(this).parents('.item').find('.input').children('input').val();	
		var aType = $(this).parents('.selectbox-wrapper').attr('id').split('_');
		var sType = aType[0];
		
		switch(sType) {
			case 'sColour':
				var sUpdateColour = $(this).attr('id');
				//alert('nProductID=' + nProductID + '&sSize=' + sSize + '&sColour='+ sColour +'&sUpdateColour=' + sUpdateColour + '&nQuantity=' + nQuantity);
				updateShoppingcart('nProductID=' + nProductID + '&sSize=' + sSize + '&sColour='+ sColour +'&sUpdateColour=' + sUpdateColour + '&nQuantity=' + nQuantity);
			break;
			case 'sSize':
				var sUpdateSize = $(this).attr('id');
				//alert('nProductID=' + nProductID + '&sSize=' + sSize + '&sColour='+ sColour +'&sUpdateSize=' + sUpdateSize + '&nQuantity=' + nQuantity);
				updateShoppingcart('nProductID=' + nProductID + '&sSize=' + sSize + '&sColour='+ sColour +'&sUpdateSize=' + sUpdateSize + '&nQuantity=' + nQuantity);
			break;
		}
	});
	$('.vernieuwen').click(function(){
		var sArticle = $(this).parents('.item').attr('id');
		var aProduct = sArticle.split('_');
		if(aProduct[0] == 's') {
			var nProductID = aProduct[1];
			var sSize = '';
			var sColour = aProduct[2];
		} else if(aProduct[0] == 'c') {
			var nProductID = aProduct[1];
			var sSize = aProduct[2];
			var sColour = '';
		} else if(aProduct[0] == 'sc') {
			var nProductID = aProduct[1];
			var sSize = '';
			var sColour = '';
		} else {
			var nProductID = aProduct[1];
			var sSize = aProduct[2];
			var sColour = aProduct[3];
		}
		var nQuantity = $(this).prev().find('input').val();	
		nQuantity = parseInt(nQuantity);
		//alert('nProductID=' + nProductID + '&sSize=' + sSize + '&sColour='+ sColour +'&nUpdateQuantity=' + nQuantity);
		updateShoppingcart('nProductID=' + nProductID + '&sSize=' + sSize + '&sColour='+ sColour +'&nUpdateQuantity=' + nQuantity);
	});	
	$('.verwijderen img').click(function(){
		var sArticle = $(this).parents('.item').attr('id');
		var aProduct = sArticle.split('_');
		if(aProduct[0] == 's') {
			var nProductID = aProduct[1];
			var sSize = '';
			var sColour = aProduct[2];
		} else if(aProduct[0] == 'c') {
			var nProductID = aProduct[1];
			var sSize = aProduct[2];
			var sColour = '';
		} else if(aProduct[0] == 'sc') {
			var nProductID = aProduct[1];
			var sSize = '';
			var sColour = '';
		} else {
			var nProductID = aProduct[1];
			var sSize = aProduct[2];
			var sColour = aProduct[3];
		}
		//alert('nProductID=' + nProductID + '&sSize=' + sSize + '&sColour='+ sColour +'&nQuantity=0');
		updateShoppingcart('nProductID=' + nProductID + '&sSize=' + sSize + '&sColour='+ sColour +'&nUpdateQuantity=0');
	});
}

function updateCart() {
	$.ajax({
		type: 'POST',
		url: sDomain + 'inc/cart.inc.php',
		success: function(result){
			//alert(result);
			$('#cart').html(result);
		}
	});
}

// Function for updating shoppingcart
function updateShoppingcart(p_sData) {
	//alert(p_sData);
	$.ajax({
		type: 'POST',
		url: sDomain + 'inc/shoppingcart.inc.php',
		data: p_sData,
		success: function(result){
			//alert(result);
			$('#shoppingcart').html(result);
			sifrShoppincart();
			$('.colours').selectbox();
			$('.sizes').selectbox();
			shoppingcart();
			updateCart();
		}
	});
}

function updatePrice() {
	
	$('#select-size_container ul li').click(function() {
		var sValue = $(this).attr('id');
		
		for(nI = 0; nI < aSizes.length; nI++) {
			if(sValue == aSizes[nI]) {
				nSizeKey = nI;
			}
		}
		var sPrice = aDisplayPrices[nSizeKey];
		$('#price').html(sPrice);
		$('#price').attr('class','');
		sifrPrice();
		
	});
}

