var activeTab;
$(function()
{
    $(function(){
        $('a.vergroot').fancybox({
			'titleShow'		: false,
			'autoScale'		: true,
			'hideOnContentClick' : true,
			'centerOnScroll': true,
			'overlayOpacity': 0.3,
			'overlayColor'	: '#000',
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic'
		});
    });
	
	if ($('.artikel_details.bundelTab').offset() != undefined)
	{
		$('.artikel_details.bundelTab').css('display', 'block');
		activeTabContent = $('.artikel_details.bundelTab');
		activeTab = $('#tab0');
	}
	else if ($('#tabContent_TS').offset() != undefined)
	{
		$('#tabContent_TS').css('display', 'block');
		activeTabContent = $('#tabContent_TS');
		activeTab = $('#tab1');
	}
	else if ($('#tabContent_UO').offset() != undefined)
	{
		$('#tabContent_UO').css('display', 'block');
		activeTabContent = $('#tabContent_UO');
		activeTab = $('#tab2');
	}
	else if ($('#tabContent_A').offset() != undefined)
	{
		$('#tabContent_A').css('display', 'block');
		activeTabContent = $('#tabContent_A');
		activeTab = $('#tab3');
	}
	
	var subcatCache = new Array();
	
	var actieveHoofdcat;
	var actieveSubcat;

	if ($('#hoofdcat ul li.actief a').length)
	{
		actieveHoofdcat = $('#hoofdcat ul li.actief a').attr('href').match(/\.\/shop\/([a-z0-9\-]+)\.html/)[1];
	
		if ($('#subcat ul li.actief a').length)
		{
			actieveSubcats = $('#subcat ul li.actief a').attr('href').match(/\.\/shop\/([a-z0-9\-]+)\/([a-z0-9\-\+]+)(.*)\.html/)[2].split(',');
		}
	}
	
	$('#hoofdcat ul').children().each(function(i, li) {
		$('a', $(li)).click(function() {
		
			if ($(li).hasClass('actief'))
			{
				$('#subcat ul').animate({height: 0}, 300, function() {
					$(this).parent().removeClass('actief');
					$('ul', this).html('');
				});
			
				$('#merken ul').animate({height: 0}, 300, function() { $(this).parent().removeClass('actief'); } );
			
				$('#types ul').animate({height: 0}, 300, function() { $(this).parent().removeClass('actief'); } );
			
				$('#prijzen ul').animate({height: 0}, 300, function() { $(this).parent().removeClass('actief'); } );
			
				var height = $('#hoofdcat ul').children().length;
				$('#hoofdcat ul').css('height', $('#hoofdcat ul').height()).children().removeClass('actief').removeClass('hide').each(function(a, obj) {
					height += $(obj).height();
				});
				$('#hoofdcat ul').animate({height: height}, 300);
			}
			else
			{
				var url = $(this).attr('href').match(/\.\/shop\/([a-z0-9\-]+)\.html/)[1];
				
				if (url == actieveHoofdcat)
				{
					var height = $('#merken ul').children().length;
					$('#merken ul').children().each(function(i, obj) {
						height += $(obj).height();
					});
					$('#merken ul').animate({height: height}, 300);
					$('#merken').addClass('actief');
					
					var height = $('#types ul').children().length;
					$('#types ul').children().each(function(i, obj) {
						height += $(obj).height();
					});
					$('#types ul').animate({height: height}, 300);
					$('#types').addClass('actief');
					
					var height = $('#prijzen ul').children().length;
					$('#prijzen ul').children().each(function(i, obj) {
						height += $(obj).height();
					});
					$('#prijzen ul').animate({height: height}, 300);
					$('#prijzen').addClass('actief');
				}
				
				var showSubcat = function(data) {
					$('#subcat ul').html('').css('height', 0);
					$('#subcat').addClass('actief');
					
					var height = data.length;
					$(data).each(function(i, subcat) {

						var subcatObj = $('<li><a href="./shop/'+url+'/'+subcat.urltext+'.html">'+subcat.name+'</a></li>').appendTo($('#subcat ul'));
						
						if (subcat.urltext == actieveSubcat)
						{
							subcatObj.addClass('actief');
						}
						
						height += subcatObj.height();
					});
					
					$('#subcat ul').animate({height: height}, 300);
				}
				
				if (subcatCache[url] != null)
				{
					showSubcat(subcatCache[url]);
				}
				else
				{
					$.getJSON(base_url + 'pages/ajax/subcat.php', {urltext: url}, function(data) {
						subcatCache[url] = data;
						showSubcat(data);
					});
				}
			
				$('#hoofdcat ul').animate({height: ($(this).height() + 2)}, 300, function() {
					$(this).css('height', 'auto');
					$(this).children().removeClass('actief').addClass('hide');
					$(li).removeClass('hide').addClass('actief');
				});
			}
			return false;
		});
	});
});

function switchPhoto(groteFoto, origineleFoto)
{
	document.getElementById('vergrootA').href = './uploads/' + origineleFoto;
	document.getElementById('grootImg').src = './uploads/' + groteFoto;
}

function Link(url)
{
	document.location = base_url + url;
}

function gotoMerk(urltxt)
{
	if (urltxt != '')
	{
		Link('./merk/' + urltxt + '.html');
	}
}

function checkLeeg(obj, start_text)
{
	if (obj.value == start_text)
	{
		obj.value = "";
	}
}

function ConfirmGo()
{
	var agree = confirm("Weet je het zeker?");
	
	if (agree)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function DoSubmit(form_id)
{
	document.getElementById(form_id).submit();
}

function DoClear(field_id, new_name)
{
	var field = document.getElementById(field_id);
	var field_label = document.getElementById(field_id + '-label');
	
	field.name = new_name;
	field_label.style.visibility = 'hidden';
	field.focus();
}

function ArtikelSetFoto(foto_url)
{
	document.getElementById('grote_foto').src = './uploads/' + foto_url;
}

function pause(ms)
{
	var now = new Date();
	var exitTime = now.getTime() + ms;
	while (true)
	{
		now = new Date();
		if (now.getTime() > exitTime)
		{
			return;
		}
	}
}

function Refresh(ms)
{
	pause(ms);
	document.location = document.location;
}

function CheckSubmit(form_id, event)
{
	var form = document.getElementById(form_id);
	var keycode;
	
	if (window.event)
	{
		keycode = window.event.keyCode;
	}
	else if (event)
	{
		keycode = event.which;
	}
	
	if (keycode == 13)
	{
		form.submit();
	}
}

function BestelAkkoord(betaalwijze)
{
	var voorwaarden = document.getElementById('voorwaardenbox');
	if (voorwaarden.checked)
	{
		var inpakken = 0;
		/*if (document.getElementById('kado').checked)
		{
			inpakken = 1;
		}*/
		
		// var opmerkingen =
		// escape(document.getElementById("opmerkingen").value);
		
		//setKortingsbon(document.getElementById('kortingsbon').value,
				//document.getElementById('kortingsbon_pin').value,
				//betaalwijze, inpakken, '');
		document.location = base_url + 'bestel/bevestig/' + betaalwijze + '/akkoord/';
}
	else
	{
		alert('U dient akkoord te gaan met de voorwaarden om een bestelling te kunnen plaatsen.');
	}
}

function OpenIdeal(order_id, betaalwijze)
{
	if (window
			.open(base_url + 'ideal/index.php?order_id=' + order_id
					+ '&betaalwijze=' + betaalwijze, 'iDEAL',
					'width=800,height=600,scrollbars=yes,toolbar=no,location=no,resizable=no'))
	{
	}
	else
	{
		document.write('<p><strong>Uw pop-up blokker heeft de popup van '
				+ betaalwijze
				+ ' geblokkeerd. Klik <span class="link" onclick="OpenIdeal('
				+ order_id + ', \'' + betaalwijze
				+ '\');">hier</span> om het venster te openen.</strong></p>');
	}
}

function addEvent(obj, eventType, fn, useCapture)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(eventType, fn, useCapture);
		return true;
	}
	else
	{
		if (obj.attachEvent)
		{
			var r = obj.attachEvent("on" + eventType, fn);
			return r;
		}
	}
}

function NieuwsbriefAanmelden()
{
	var textbox = document.getElementById('nieuwsbrief_email');
	
	addNieuwsbrief(textbox.value);
	textbox.value = '';
}

function Link(url)
{
	document.location = base_url + url;
}

var optie_id = new Array();
var optie_i = 0;

function addOptieField(id)
{
	optie_id[optie_i] = id;
	optie_i++;
}

function getOptieString()
{
	var tmp = '';
	
	for (i = 0; i < optie_id.length; ++i)
	{
		tmp += 'optie_id[' + optie_id[i] + ']='
				+ document.form["optie[" + optie_id[i] + "]"].value;
		
		if (i < optie_id.length - 1)
		{
			tmp += '&';
		}
	}
	
	return tmp;
}

function VoorwaardenPopup()
{
	window
			.open(base_url + 'pages/bestel/voorwaarden.php',
					'Leveringsvoorwaarden',
					'width=800,height=600,scrollbars=yes,toolbar=no,location=no,resizable=no');
}

function popUpGroot(URL)
{
	URL = base_url + URL;
	day = new Date();
	id = day.getTime();
	popup = eval("page"
			+ id
			+ " = window.open(URL, '"
			+ id
			+ "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=600,left = 300,top = 50');");
}

function groteFoto(id, foto_id)
{
	popUpGroot('pages/categorie/foto.php?id=' + id + '&foto_id=' + foto_id);
}

function setGroteFoto(id, foto_id)
{
	var grote_foto = document.getElementById('grote_foto');
	grote_foto.onclick = function()
	{
		groteFoto(id, foto_id)
	};
	grote_foto.style.cursor = "pointer";
}

function unsetGroteFoto()
{
	var grote_foto = document.getElementById('grote_foto');
	grote_foto.onclick = function()
	{
	};
	grote_foto.style.cursor = "default";
}

function addBookmark(title, url)
{
	if (window.sidebar)
	{
		window.sidebar.addPanel(title, url, "");
	}
	else if (document.all)
	{
		window.external.AddFavorite(url, title);
	}
	else
	{
		alert("Het toevoegen aan favorieten wordt niet ondersteund door uw browser.");
	}
}

function Bookmark()
{
	addBookmark("Digimaxx.nl", "http://www.digimaxx.nl/");
}

var activeEN = null;
var activeEN_oldHTML = null;

function EN_Field(id, bundelId)
{
	if (activeEN != null)
	{
		activeEN.innerHTML = activeEN_oldHTML;
	}
	
	if (bundelId)
		id = id + '-' + bundelId;
	
	var EN = document.getElementById('EN'+id);
	activeEN = EN;
	
	activeEN_oldHTML = activeEN.innerHTML;
	activeEN.innerHTML = '<div id="EN_Field"><input type="hidden" id="artikelId" value="' + id + '" /><input type="text" id="email" /><input type="submit" onclick="saveEN();" value="Opslaan" /></div>';
}

function EN_Status(response)
{
	if (response == 'OK')
	{
		activeEN.innerHTML = '<strong>U ontvangt een mail zodra dit artikel voorradig is.</strong>';
	}
	else
	{
		activeEN.innerHTML = '<strong>Uw email-adres is onjuist / al aangemeld voor dit artikel.</strong> ' + activeEN_oldHTML;
	}
}

function selectTab(tab)
{
	switch (tab)
	{
		case 0:
			if ($('#tab0').hasClass('actief'))
				break;
			
			$('#tab0').addClass('actief');
			activeTab.removeClass('actief');
			activeTab = $('#tab0');
			
			activeTabContent.fadeOut(50, function(){
				$('.artikel_details.bundelTab').fadeIn(500);
				activeTabContent = $('.artikel_details.bundelTab');
			});
			break;
		case 1:
			if ($('#tab1').hasClass('actief'))
				break;
			
			if ($('#tab1').hasClass('actief'))
				break;
			
			activeTab.removeClass('actief');
			$('#tab1').addClass('actief');
			activeTab = $('#tab1');
			
			activeTabContent.fadeOut(100, function(){
				$('#tabContent_TS').fadeIn(500);
				activeTabContent = $('#tabContent_TS');
			});
			break;
		case 2:
			if ($('#tab2').hasClass('actief'))
				break;
			
			activeTab.removeClass('actief');
			$('#tab2').addClass('actief');
			activeTab = $('#tab2');
			
			activeTabContent.fadeOut(100, function(){
				$('#tabContent_UO').fadeIn(500);
				activeTabContent = $('#tabContent_UO');
			});
			break;
		case 3:
			if ($('#tab2').hasClass('actief'))
				break;
			
			activeTab.removeClass('actief');
			$('#tab3').addClass('actief');
			activeTab = $('#tab3');
			
			activeTabContent.fadeOut(100, function(){
				$('#tabContent_A').fadeIn(500);
				activeTabContent = $('#tabContent_A');
			});
			break;
	}
	
}