/**
 * @author diederik van hoorebeke
 */
jQuery.noConflict();
jQuery(document).ready(function(){

	//smooth scrolling
	jQuery('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
			var jQuerytarget = jQuery(this.hash);
			jQuerytarget = jQuerytarget.length && jQuerytarget || jQuery('[name=' + this.hash.slice(1) + ']');
			if (jQuerytarget.length) {
				var targetOffset = jQuerytarget.offset().top;
				jQuery('html,body').animate({scrollTop: targetOffset});
				return false;
			}
		}
	});
	
	//paging fix
	jQuery('.wp-pagenavi a:contains(Previous)').each(function(){
		link = jQuery(this);
		jQuery('.wp-pagenavi a:contains(Next)').each(function(){
			newLink = link.clone(true).insertAfter(this);
			link.remove();
		});
	});
	
	//search
	jQuery('input[name=s]:first').focus(function(){
		input = jQuery(this);
		if(input.attr('value') == 'Search'){
			input.attr('value','');
		}
	});
	jQuery('input[name=s]:first').blur(function(){
		input = jQuery(this);
		if(!input.attr('value')){
			input.attr('value','Search');
		}
	});
	
	//newsletter
	jQuery('input[name=email]:first').focus(function(){
		input = jQuery(this);
		if(input.attr('value') == 'E-mail address'){
			input.attr('value','');
		}
	});
	jQuery('input[name=email]:first').blur(function(){
		input = jQuery(this);
		if(!input.attr('value')){
			input.attr('value','E-mail address');
		}
	});
	
	//Image gallery
	jQuery('.post:has(img):not(.page)').each(function(){
		var post = jQuery(this);
		var title = post.find('h2:first');
		
		//relocating images
		var list = '<div class="imageGallery"><ul>';
		post.find('p:has(img)').each(function(){
			var p = jQuery(this);
			p.find('img').each(function(){
				list += '<li><img src="'+jQuery(this).attr('src')+'" title="Click to go to next image." alt="'+jQuery(this).attr('title')+'" /></li>';
			});
			p.remove();
		});
		post.find('img').each(function(){
			image = jQuery(this);
			list += '<li><img src="'+image.attr('src')+'" title="Click to go to next image." alt="'+img.attr('title')+'" /></li>';
			image.remove();
		});
		list += '</ul></div>';
		title.after(list);
		
		//making it slide using buttons
		var gallery = post.find('div:first');
		var ul = gallery.find('ul:first');
		var max = ul.find('img').length;
		if (max > 1) {
			var firstImage = ul.find('img:first');
			var current = 1;
			var i = 1;
			var buttons = '<span class="buttons">';
			var active = ' active';
			ul.find('li').each(function(){
				li = jQuery(this);
				li.addClass('image' + i);
				buttons += '<span class="image' + i + active + '">image' + i + '</span>';
				active = '';
				i++;
			});
			buttons += '</span>';
			gallery.append(buttons);
			var span = gallery.find('span:first');
			span.find('span:first').addClass('active');
			span.find('span').each(function(){
				button = jQuery(this);
				button.click(function(){
					button = jQuery(this);
					button.addClass('active');
					button.siblings('span').removeClass('active');
					var clicked = parseInt(button.attr('class').replace('image', '').replace('active', ''));
					var left = (0 - (550 * (clicked - 1))) + 'px';
					current = clicked;
					ul.animate({
						'left': left
					});
				});
			});
			
			//catch click on image event and slide
			ul.click(function(){
				ul = jQuery(this);
				if (current == max) {
					ul.animate({
						'left': '0'
					});
					current = 1;
				}
				else {
					left = (0 - (550 * current)) + 'px';
					ul.animate({
						'left': left
					});
					current++;
				}
				var button = span.find('span.image' + current + ':first');
				button.addClass('active');
				button.siblings('span').removeClass('active');
			});
			
			// set height after first image is loaded
			ul.find('img:first').load(function(){
				var height = firstImage.height();
				gallery.css({
					'height': height
				})
				ul.css({
					'height': height,
					'width': ((550 * max) + 5) + 'px'
				})
			});
		}else{
			gallery.css('paddingBottom','0');
			ul.find('li img').css('cursor','auto');
		}
	});
	
	//active States
	var curURL = document.location.href;
	var subCurURL = curURL.substring(0,(curURL.length-1));
	jQuery("a[href='"+curURL+"']").addClass('active');
	jQuery("a[href='"+curURL+"/']").addClass('active');
	jQuery("a[href='"+subCurURL+"']").addClass('active');
	
	//open sub menu's when active
	jQuery('ul#navigation li:has(a.active)').addClass('open');
	
})

jQuery(window).load(function () {
  	jQuery('input[name=email]:first').blur();
	jQuery('h3:first').click();
});