	// JavaScript Document


$(document).ready(function() {
	
	cufonIt();
	initLoginForm();
	
	initVoteBtn();
	
	$('.datepicker').datepicker({dateFormat: 'dd-mm-yy',
								  changeYear: true});
	$('.birthdatepicker').datepicker({dateFormat: 'dd-mm-yy',
									   yearRange: '1945:2010',
									   changeYear: true});
	
	$('#btn-mijn-seh').bind('click', function() {
		if($('#mijn-seh-form').css('display') == 'block') {
			// slide up
			$('#mijn-seh-form').slideUp('fast');
			$(this).removeClass('active');
		}
		else {
			if($('#mijn-seh-zoek').css('display') == 'block') {
				$('#mijn-seh-zoek').hide();
				$('#btn-zoeken').removeClass('active');
			}
			$('#mijn-seh-form').fadeIn('fast');
			$(this).addClass('active');
		}
		$('#mijn-seh-wachtwoord').hide();
		return false;
	});
	$('#btn-zoeken').bind('click', function() {
		if($('#mijn-seh-zoek').css('display') == 'block') {
			// slide up
			$('#mijn-seh-zoek').slideUp('fast');
			$(this).removeClass('active');
		}
		else {
			if($('#mijn-seh-wachtwoord').css('display') == 'block' || $('#mijn-seh-form').css('display') == 'block') {
				$('#mijn-seh-form').hide();
				$('#btn-mijn-seh').removeClass('active');
			}
			$('#mijn-seh-zoek').fadeIn('fast');
			$(this).addClass('active');
		}
		$('#mijn-seh-wachtwoord').hide();
		return false;
	});
	
	$('#plaatsnaamWerkgever').change(function() {
		var plaatsnaam = $('#plaatsnaamWerkgever option:selected').text();
		
		var data = 'ajax=ajax&plaatsnaam=' + plaatsnaam + '&action=getWerkgevers';
		var url = '/';
		$.ajax({
			type: "POST",
			url:  url,
			data: data,
			success: function(returnValue) {
				$('#werkgever').html(returnValue);
			},
			error: function(returnValue) {
				alert(returnValue);
			}
		});
		return false;
	});
	
	$('#forgot-password').click(function() {
		$('#mijn-seh-form').hide();
		$('#mijn-seh-wachtwoord').show();
		return false;
	});
	$('#password-forgot-btn').click(function() {
		return forgottenPassword();
	});
	$('#mijn-seh-wachtwoord form').submit(function() {
		return forgottenPassword();
	});
	
	$('.backbtn').bind('click', function() {
		history.go(-1);
		return false;
	});
/*	$('div.error1').click(function() {
		$(this).remove();
	});
	$('div.error1').focus(function() {
		$(this).remove();
	});
	$('div.error1').mouseover(function() {
		$(this).remove();
	});*/
	$('#location').bind('change',function(){
										  //alert('change');
		var dateID = $(this).val();
		//alert(dateID);
		$('.dates select.date').attr('name','date');
		$('.dates').css({'display':'none'});
		$('#dates_'+dateID+' select.date').attr('name','date_active');
		$('#dates_'+dateID).css({'display':'block'});
	});
	$('.anders').bind('keyup',function(){
		id = $(this).parent().attr('id');
		if($(this).parent().find('input[name="'+id+'[]"]').length > 0){
			$(this).parent().find('input[name="'+id+'[]"]').val($(this).parent().attr('rel')+$(this).val()).attr('checked','checked');;
		}
		else if($(this).parent().find('input[name="'+id+'"]').length > 0) {
			$(this).parent().find('input[name="'+id+'"]').val($(this).parent().attr('rel')+$(this).val()).attr('checked','checked');;
		}
	});
	$('.winkelwagen div.payment div.amount input').bind('keyup',function(e){
		if(e.keyCode == 38){
			curval = $(this).val();
			if(curval<0)curval = 0;
			$(this).val(parseInt(curval)+1);
		}
		if(e.keyCode == 40){
			curval = $(this).val();
			if(curval<0)curval = 0;
			$(this).val(parseInt(curval)-1);
		}
		
		if($(this).val()!=''){	
			val = $(this).val();
			var input = $(this);
			data = $('#shopform').serialize();
			data += '&ajaxshop=1';
			url = $(location).attr('href');
			$.ajax({
				type: "POST",
				url: url,
				data: data,
				success: function(msg){
					regex = new RegExp(/DATA::(.*?)::DATA/);
					dats = msg.match(regex);
					var dats = dats[1];
					each = dats.split('||');
					for(i=0; i<each.length; i++){
						var varia = each[i].replace('[','');
						varia = varia.replace(']','');
						varia = varia.split('=');
						//eval(varia[0] = varia[1]);
						window[varia[0]] = varia[1];
					}
					$('#subtotal').html('&euro; '+SUB);
					$('#btw').html('&euro; '+BTW);
					$('#total').html('&euro; '+TOTAL);
					$('.product_price').each(function(){
						id = $(this).parent().parent().find('div.amount input').attr('name');
						id = id.replace('amount_','');
						var price = window['PRICE'+id];
						price = (price/100).toFixed(2);
						price = addCommas(price);
						price = price.replace(',',';');
						price = price.replace('.',',');
						price = price.replace(';','.');
						//number_format(($input / 100), 2, ',', '.');
						$(this).html('&euro;'+price);
					});
					if(val < 1){
						if($(input).parent().parent().parent().parent().parent().parent().parent().hasClass('first')){
							$(input).parent().parent().parent().parent().parent().parent().parent().remove();
							$(input).parent().parent().parent().parent().parent().parent().parent().parent().find('div:first').addClass('first');
						}
						else {
							$(input).parent().parent().parent().parent().parent().parent().parent().remove();	
						}
						
					}
				}
			});
		}
	});
	
	$('#huisnummer').bind('blur', function() {
		//checkPostcode();
	});
	$('#postcode').bind('blur', function() {
		//checkPostcode();
	});
	
	$('a.zoom').fancyZoom();
	//$(document).pngFix();
	$('#popup').hide();
        $('.info_img').bind('click', function() {          
            var posistion
            if(document.documentElement.offsetWidth > 680){
                posistion = document.documentElement.offsetWidth - (document.documentElement.offsetWidth-540)/2;
            }else{
                posistion = 680;
            }
            $('#popup').css("top","45px");
            $('#popup').css("left",posistion+"px");
            $('#popup').fadeIn(500);
            return false;
            
        });

           $('body').bind('click', function() {
               if($('#popup').css('display') != 'none'){
                $('#popup').fadeOut(500);
                }
            });     
            
            if(location.hash != ''){
                var hash = location.hash
                hash = hash.substr(1,hash.length);
                if($("#"+hash).children('h2').length == 0){
                    toggleNextDiv($("#"+hash).parent('div').prev('h2'));
                    toggleNextSubDiv($("#"+hash));
                }else{
                    toggleNextDiv($("#"+hash).children('h2'));
                }
                
                
            }
});


function cufonIt() {
	Cufon.replace('#page-title h1', {fontFamily : 'Aller'});
	Cufon.replace('#mainnav li a', {fontFamily : 'Aller', hover: true});
	Cufon.replace('.lees-meer', {fontFamily : 'Aller', hover: true});	
	Cufon.replace('#mijn-seh .btn', {fontFamily : 'Aller', hover: true});
        Cufon.replace('.menu-items h1', {fontFamily : 'Aller', hover: true});
        Cufon.replace('.menu-items ul li', {fontFamily : 'Aller', hover: true});
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function initLoginForm() {
	$('#mijn-seh-form input').bind('focus', function() {
		clearInput($(this));
	});
	$('#mijn-seh-form input').not('.password').bind('blur', function() {
		resetInput($(this));
	});
	$('#mijn-seh-form .password').live('blur', function() {
		resetInput($(this));
	});
}
function forgottenPassword() {
	var username = $('#forgot-pass-user').val();
	
	var data = 'ajax=ajax&username=' + username + '&action=forgottenPassword';
	var url = '/';
	$.ajax({
		type: "POST",
		url:  url,
		data: data,
		success: function(returnValue) {
			$('#mijn-seh-wachtwoord').html(returnValue);
			//console.log(returnValue);
		},
		error: function(returnValue) {
//			console.log(returnValue.responseText);
			alert(returnValue.responseText);
		}
	});
	return false;
}

function clearInput(input) {
	
	if(input.attr('id') == 'username') {
		if(input.val() == 'Gebruikersnaam') {
			input.val('');
		}
	}
	else if(input.attr('id') == 'passwordtext') {
		if(input.val() == 'Wachtwoord') {
			//input.attr('type', 'password');
			//input.val('');
			$('#passwordtext').hide();
			$('#passwordpassword').show().focus();
		}
	}
}
function resetInput(input) {
	if(input.attr('id') == 'username') {
		if(input.val() == '') {
			input.val('Gebruikersnaam');
		}
	}
	else if(input.attr('id') == 'passwordpassword') {
		if(input.val() == '') {
			$('#passwordpassword').hide();
			$('#passwordtext').show();
		}
	}
	//else if(input.attr('id') == 'passwordtext') 
}
function initVoteBtn() {
	$('#btn-vote').bind('click', function() {
		var checked = $('.pollAnwser:checked');
		var pollId = parseInt(checked.attr('name').replace(/\D/g, ''));
		var voteId = parseInt(checked.val());
		
		var data = 'ajax=ajax&pollId=' + pollId + '&voteId=' + voteId + '&action=pollVote';
		var url = '/';
		$.ajax({
			type: "POST",
			url:  url,
			data: data,
			success: function(returnValue) {
				$('#poll p').remove();
				
				$('#poll table').replaceWith(returnValue);
			},
			error: function(returnValue) {
				alert(returnValue);
			}
		});
		return false;
	});
}

function renewCaptcha() {
	$('#captcha').replaceWith('<img src="/extra/captcha.php?r='+Math.random()+'" alt="Verificatiecode" id="captcha" />');
}

function toggleNextDiv(elem) {
        $('.faq_cat').children('h2').each(function(){
            if(this != elem){
                    $(this).next('div').slideUp('blind');
                    $(this).children('.faq-arrow').each(function(count){
                        if(count == 0){
                            $(this).css('display', 'block');
                        }else{
                            $(this).css('display', 'none');
                        }
                    });
            }
        });
	$(elem).next('div').toggle('blind');
	$(elem).children('.faq-arrow').toggle();
	return false;
}

function toggleNextSubDiv(elem) {
        $('.sub-cat').children('p').each(function(){
            if(this != elem){
                    $(this).next('div').slideUp('blind');
                    $(this).children('.faq-sub-arrow').each(function(count){
                        if(count == 0){
                            $(this).css('display', 'block');
                        }else{
                            $(this).css('display', 'none');
                        }
                    });
            }
        });
	$(elem).next('div').toggle('blind');
	$(elem).children('.faq-sub-arrow').toggle();
	return false;
}

function checkPostcode() {
	var postcode   = $('#postcode').val();
	var huisnummer = $('#huisnummer').val();
	if(postcode != '' && huisnummer != '') {
		//$('#postcode-error').hide();
		$('#postcode-spinner').fadeIn('fast');
		var data = 'ajax=ajax&postcode=' + postcode+ '&huisnummer=' + huisnummer + '&action=getAddress';
		$.ajax({
			type: "POST",
			url: "/",
			data: data,
			success: function(msg){
				var arr = msg.split('|');
				if(arr[0] == 1) {
					$('#postcode-error').slideUp('slow');
					$('#straat').val(arr[1]);
					$('#woonplaats').val(arr[2]);
				}
				else {
					
					//$('#postcode-error').text(arr[1]);
					/*if($('#postcode-error').css('display') == 'none') {
						$('#postcode-error').slideDown('slow');
					}*/
				}
				$('#postcode-spinner').hide();
			},
			error: function(msg) {
				alert(msg.responseText);
				$('#postcode-spinner').hide();
			}
		});
	}
}
