/*** ACTIEVE PAGINA ***/

$(document).ready(function() {
  var title = document.title;
  // reset the active class for each link
  $('#menu a').removeClass('active').removeClass('menu').addClass('menu');
  // give the active page a different bg image in the menu
  if(title == "mzilverberg.nl - Home") {
    $('.home-link').removeClass('menu').addClass('active');
  } else if(title == "mzilverberg.nl - Maarten") {
    $('.maarten-link').removeClass('menu').addClass('active');
  } else if(title == "mzilverberg.nl - Diensten") {
    $('.diensten-link').removeClass('menu').addClass('active');
  } else if(title == "mzilverberg.nl - Portfolio") {
    $('.portfolio-link').removeClass('menu').addClass('active');
  } else if(title == "mzilverberg.nl - Contact") {
    $('.contact-link').removeClass('menu').addClass('active');
  }
});

/*** HEADINGS ***/
$(document).ready(function() {
  var heading = $('.heading');
  heading.appendTo('body').disableTextSelect().css({'cursor' : 'default'});

});

// disable text selection
(function($) {
    if ($.browser.mozilla) {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).css({
                    'MozUserSelect' : 'none'
                });
            });
        };
        $.fn.enableTextSelect = function() {
            return this.each(function() {
                $(this).css({
                    'MozUserSelect' : ''
                });
            });
        };
    } else if ($.browser.msie) {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).bind('selectstart.disableTextSelect', function() {
                    return false;
                });
            });
        };
        $.fn.enableTextSelect = function() {
            return this.each(function() {
                $(this).unbind('selectstart.disableTextSelect');
            });
        };
    } else {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).bind('mousedown.disableTextSelect', function() {
                    return false;
                });
            });
        };
        $.fn.enableTextSelect = function() {
            return this.each(function() {
                $(this).unbind('mousedown.disableTextSelect');
            });
        };
    }
})(jQuery);

/*** RECENT WERK ***/

$(document).ready(function() {
  var p = "";
  $('.recent-werk').hover(function() {
	$(this).addClass('recent-werk-hover');
	$(this).append('<div class="recent-werk-arrow"></div>');
  }, function() { 
    $(this).removeClass('recent-werk-hover');
	$(this).find('div.recent-werk-arrow').remove();
  });
  $('.recent-werk').click(function() {
	p = $(this).children('.hidden').text();
	// tekst opschonen voor schone url
	p = p.replace(/\s+/g, '-').toLowerCase();
	window.location = 'portfolio/' + p;
  });
});

/*** PORTFOLIO IMAGE OVERLAY ***/

$(document).ready(function() {
  $('.pf-img h5').hide();
  $('.pf-img').hover(function() {
	$(this).children('h5').show();
  }, function() { 
    $(this).children('h5').hide();
  });
});

/*** CONTACT ***/

/* autogrow textarea */
$(document).ready(function(){	
  $('textarea.text-area').autogrow();
});

// functie
(function(jQuery) {
	var self = null;
	jQuery.fn.autogrow = function(o) {	
	  return this.each(function() {
	    new jQuery.autogrow(this, o);
	  });
    };
    /**
     * The autogrow object.
     *
     * @constructor
     * @name jQuery.autogrow
     * @param Object e The textarea to create the autogrow for.
     * @param Hash o A set of key/value pairs to set as configuration properties.
     * @cat Plugins/autogrow
     */
	jQuery.autogrow = function (e, o) {
	  // this.options	   = o || { };
	  this.dummy	   = null;
	  this.interval	   = null;
	  this.line_height = /*this.options.lineHeight || */parseInt(jQuery(e).css('line-height'));
	  this.min_height  = /*this.options.minHeight || */parseInt(jQuery(e).css('min-height'));
	  this.max_height  = /*this.options.maxHeight || */parseInt(jQuery(e).css('max-height'));
	  this.textarea    = jQuery(e);
	  if(this.line_height == NaN) {
	    this.line_height = 0;
	  }
	  // Only one textarea activated at a time, the one being used
	  this.init();
	};
	jQuery.autogrow.fn = jQuery.autogrow.prototype = {
      autogrow: '1.2.2'
    };
 	jQuery.autogrow.fn.extend = jQuery.autogrow.extend = jQuery.extend;
	jQuery.autogrow.fn.extend({
	  init: function() {			
		var self = this;			
		this.textarea.css({overflow: 'hidden', display: 'block'});
		this.textarea.bind('focus', function() { self.startExpand() } ).bind('blur', function() { self.stopExpand() });
		this.checkExpand();	
	  },
	  startExpand: function() {				
		var self = this;
		this.interval = window.setInterval(function() { self.checkExpand() }, 400);
	  },
	  stopExpand: function() {
		clearInterval(this.interval);	
	  },
	  checkExpand: function() {
		if (this.dummy == null) {
		  this.dummy = jQuery('<div></div>');
		  this.dummy.css({
			'font-size'  : this.textarea.css('font-size'),
			'font-family': this.textarea.css('font-family'),
			'width'      : this.textarea.css('width'),
			'padding'    : this.textarea.css('padding'),
			'line-height': this.line_height + 'px',
			'overflow-x' : 'hidden',
			'position'   : 'absolute',
			'top'        : 0,
			'left'		 : -9999
		  }).appendTo('body');
		}
		// Strip HTML tags
		var html = this.textarea.val().replace(/(<|>)/g, '');
		// IE is different, as per usual
		if ($.browser.msie) {
		  html = html.replace(/\n/g, '<BR>new');
		} else {
		  html = html.replace(/\n/g, '<br>new');
		}
		if (this.dummy.html() != html) {
		  this.dummy.html(html);	
		  if (this.max_height > 0 && (this.dummy.height() + this.line_height > this.max_height)) {
			this.textarea.css('overflow-y', 'auto');	
		  } else {
			this.textarea.css('overflow-y', 'hidden');
			if (this.textarea.height() < this.dummy.height() + this.line_height || (this.dummy.height() < this.textarea.height())) {	
			  this.textarea.animate({height: (this.dummy.height() + this.line_height) + 'px'}, 100);	
			}
		  }
		}
	  }
	});
}) (jQuery);

/*** PORTFOLIO ***/

$(document).ready(function(){	
  $("#portfolio-img").easySlider();
});

/* base: Easy Slider 1.5 - jQuery plugin (written by Alen Grakalic ©) */	
/* slightly modified by mzilverberg.nl for better implementation © */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'',
			nextId: 		'nextBtn',	
			nextText: 		'',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'',
			lastShow:		false,				
			vertical:		false,
			speed: 			500,
			auto:			false,
			pause:			2000,
			continuous:		false
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).width(); 
			var h = $("li", obj).height(); 
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).css('width',s*w);			
			if(!options.vertical) $("li", obj).css('float','left');
			
			if(options.controlsShow){
				var html = options.controlsBefore;
				if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
				html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
				html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
				if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';
				html += options.controlsAfter;						
				//$(obj).after(html);
				$("h5 span").before(html);
			};
	
			$("a","#"+options.nextId).click(function(){		
				animate("next",true);
			});
			$("a","#"+options.prevId).click(function(){		
				animate("prev",true);				
			});	
			$("a","#"+options.firstId).click(function(){		
				animate("first",true);
			});				
			$("a","#"+options.lastId).click(function(){		
				animate("last",true);				
			});		
			
			function animate(dir,clicked){
				var ot = t;				
				switch(dir){
					case "next":
						t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1;						
						break; 
					case "prev":
						t = (t<=0) ? (options.continuous ? ts : 0) : t-1;
						break; 
					case "first":
						t = 0;
						break; 
					case "last":
						t = ts;
						break; 
					default:
						break; 
				};	
				
				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;						
				if(!options.vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						speed
					);				
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						speed
					);					
				};
				
				if(!options.continuous && options.controlsFade){					
					if(t==ts){
						$("a","#"+options.nextId).hide();
						$("a","#"+options.lastId).hide();
					} else {
						$("a","#"+options.nextId).show();
						$("a","#"+options.lastId).show();					
					};
					if(t==0){
						$("a","#"+options.prevId).hide();
						$("a","#"+options.firstId).hide();
					} else {
						$("a","#"+options.prevId).show();
						$("a","#"+options.firstId).show();
					};					
				};				
				
				if(clicked) clearTimeout(timeout);
				if(options.auto && dir=="next" && !clicked){;
					timeout = setTimeout(function(){
						animate("next",false);
					},diff*options.speed+options.pause);
				};
				
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};				
			
		});
	  
	};

})(jQuery);