$(document).ready(function() {
  $("a.portfolio").fancybox();
	$('#Zoekterm').focus(function() {
      $('#lblZoekterm').css('display', 'none');
	});

	$('#Zoekterm').blur(function() {
      if( trim($('#Zoekterm').val()).length == 0 ) {
          $('#lblZoekterm').css('display', 'block');
      }
	});

	$('#frm_zoek').submit(function(e) {
      e.preventDefault();

      if( $('#Zoekterm').val().length > 0 && $('#Zoekterm').val() != 'Vul hier uw zoekterm in...' ) {
          this.submit();
      }
      else {
          alert('Geen zoekterm opgegeven');
          $('#Zoekterm').focus();
      }
    });

    if($('#Hoe')) {
      $('#Hoe').change(function() {
        $('#hoe_klant').css('display','none');
        $('#hoe_anders').css('display','none');

        if($(this).val()=='portfolio') {
          $('#hoe_klant').css('display','block');
        }
        else if($(this).val()=='anders') {
          $('#hoe_anders').css('display','block');
        }
      });
    }
});

function valideer() {
  var rw = true;


  $('.required').each( function(index,el) {
    $(el).removeClass('error');
    //alert(el.id + ':' + $(el).val());
    
    if( ($(el).val() == null || $(el).val().length == 0) ) {
      
      // alert('Fout: Het veld "' + el.id + '" is verplicht.'); //alerts "This field is required."
      $(el).addClass('error');
      rw = false;
    }
    
    if( $(el).hasClass('email') && (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($(el).val())))) {
      // alert('Fout: Het veld "' + el.id + '" is geen geldig e-mail adres.'); //alerts "This field is required."
      $(el).addClass('error');
      rw = false;
    }
  });
  
  return rw;
}
function add_click(tagId) {
  $.ajax({
    type: 'post',
    url: '/includes/ajax_functions.php',
    dataType: 'json',
    data: {
      'tagId':tagId,
      'action':'add_click'
    },
    success: function(result) {
      if(result.error.length>0) {
        alert(result.error);
      }
      else {
        document.location = result.redirect;
      }
    }
  });
}

function move_item( dir ) {
	var str_curpos	= $('#pf_item_container').css('left');
	var str_curpos2	= $('#item_container').css('left');
	var str_width	= $('#pf_item_container').css('width');
	var str_width2	= $('#item_container').css('width');

	var curpos		= str_curpos.substring( 0, str_curpos.indexOf('px'));
	var curpos2		= str_curpos2.substring( 0, str_curpos2.indexOf('px'));
	var width		= str_width.substring( 0, str_width.indexOf('px'));
	var width2		= str_width2.substring( 0, str_width2.indexOf('px'));

	var aantal_items	= $('#item_container').children('.pf_item').length;

	switch( dir ) {
		case 'left':
			newpos			= parseInt( curpos - 440 );
			newpos2			= parseInt( curpos2 - 261 );

			if( ( newpos * -1 ) >= width ) {
				newpos 	= 0;
				newpos2	= 0;
			}
			break;

		case 'right':
			newpos			= parseInt( curpos ) + 440;
			newpos2			= parseInt( curpos2 ) + 261;

			if( newpos > 0 ) {
				newpos = ( parseInt( width ) - 440 ) * -1;
				newpos2 = ( parseInt( aantal_items - 1 ) ) * -261;
			}
			break;
	}
    
    $('#pf_item_container').animate({
      left: newpos
    },1000,'easeInOutBack');

    $('#item_container').animate({
      left: newpos2
    },1000,'easeInOutBack');
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


$('select.multiple').each( function(index, el) {
	el.geselecteerd = new Array();
});

$('select.multiple').mousedown (function() {
	var waarde = this.selectedIndex;

	if( !in_array( waarde, this.geselecteerd ) ) {
		this.geselecteerd.push( waarde );
	}
	else {
		var tmparr = new Array();

		for(i = 0; i < this.geselecteerd.length; i++) {
			if( this.geselecteerd[i] != waarde ) {
				tmparr.push(this.geselecteerd[i]);
			}
		}
		this.geselecteerd = tmparr;
	}

	for(i=0;i<this.options.length;i++) {
		if( in_array( i, this.geselecteerd ) ) {
			this.options[i].selected = true;
		}
		else {
			this.options[i].selected = false;
		}
	}
});

function trackEvent(categorie, action) {
  _gaq.push(['_trackEvent',categorie,action]);
}
