jQuery.fn.populate = function() {
		this.each(function(){
			this.value = jQuery(this).siblings('label').html();
		});	
		return this.focus(function() {
		
		if( this.value == jQuery(this).siblings('label').html() ) {
			this.value = "";
		}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = jQuery(this).siblings('label').html();
		}
		});
		
		
	};



jQuery(function(){
	jQuery('#addComment input[type=text], #addComment textarea, #footer input[type=text], #footer textarea').populate();
	jQuery('#addComment label, #footer label').hide();
	jQuery('#globalNav').flipNav();
	
	
	jQuery('.workExamples li a').click(function(){
		
		var project = jQuery(this).attr('href');
		
		jQuery('#projectInfo').load('templates/info_'+ project.substr(1) +'.html');
		
		return false;
	});
	
	jQuery('#globalContact a').click(function(){
		jQuery(window).scrollTo(jQuery('#contactForm'), 1000);
		return false;
	});
	
	jQuery('a#topButton').click(function(){
		jQuery(window).scrollTo(jQuery('#top'), 1000);
		return false;
	});
	
	/* LOAD MORE POSTS */
	
	jQuery('#loadPosts a').click(function(){
		
		// get current number showing
		
		var loaded = jQuery('.articleList li').length;
		
		var current = jQuery('.articleList').html();
		
		jQuery.get('loadposts/index/' + loaded, function(data){
		
			jQuery('.articleList').html(current + data);
			jQuery('.articleList li.hidden').slideDown(500, function(){
				
				jQuery(this).removeClass('hidden');
				
			});
		});
		
		return false;
	
	
	});
	
	/* FORM POSTINGS */
	
	/* Contact form */
	jQuery('#contactForm').submit(function(eve){
		eve.preventDefault();
				
		jQuery.post('contact',{
			
			security: jQuery('input[name=security]').val(),
			contactName: jQuery('#contactName').val(),
			contactEmail: jQuery('#contactEmail').val(),
			contactMessage: jQuery('#contactMessage').val(),
			postType: 'ajax'
		
		},function(data){
			
			if(data == 1){
				
				jQuery('#contactForm label.error').remove();
				
				jQuery('#contactForm').slideUp(function(){
				
					jQuery('#contact').delay('300').append('<p>Ta very much, I\'ll get back to you soon.</p><p><a href="#" class="reveal" title="">Reload the form</a></p>').fadeIn();
				
				});
		
			} else {
			
			var errors = (data.split('|'));
			
				jQuery('#contactForm label.error').remove();
				
				jQuery('#contactForm .formbox').each(function(){
				
					for( i = 0; i < errors.length; i++){
						
						if(strpos(errors[i], jQuery(this).attr('id'), 0)){
						// get the identifier
						jQuery(this).after(errors[i]);
						jQuery(this).addClass('error');
						}
						
							
					}
				});
			}
		});
		
		
		
	});
	/* comment form */
	jQuery('#addComment').submit(function(eve){
		eve.preventDefault();
		
		
		jQuery.post('blog/postcomment',{
			
			security: jQuery('input[name=security]').val(),
			name: jQuery('#name').val(),
			email: jQuery('#email').val(),
			comment: jQuery('#comment').val(),
			website: jQuery('#website').val(),
			page: jQuery('input[name=page]').val(),
			postType: 'ajax'
		
		},function(data){
			
			if(data == 1){
				
				jQuery('#commentForm label.error').remove();
				
				jQuery('#addComment').attr('');
				
				if(jQuery('.noComments').length > 0){
									
					jQuery('#comments').removeClass('noComments');
					
					jQuery('#comments li:first').fadeOut(function(){
					
						jQuery('#commentList').load('lastcomment');
					
					}).fadeIn();
					
				
				} else {
					
					var $old = jQuery('#commentList').html();
					
					
					jQuery('#commentList').load('lastcomment', function(){
						
						jQuery('#commentList').prepend($old);
						jQuery('.lastComment').fadeIn();
					
					});
					
					jQuery('input[name=security]').val('');
					jQuery('#name').val('Name');
					jQuery('#email').val('Email');
					jQuery('#comment').val('Comment');
					jQuery('#website').val('URL (optional)');
				}
				
			
			} else {
			
			var errors = (data.split('|'));
			
				jQuery('#addComment label.error').remove();
				
				jQuery('#addComment .formbox').each(function(){
				
					for( i = 0; i < errors.length; i++){
						
						if(strpos(errors[i], jQuery(this).attr('id'), 0)){
						// get the identifier
						jQuery(this).after(errors[i]);
						jQuery(this).addClass('error');
						}
						
							
					}
				});
			}
		});
		
		
		
	});
	jQuery('.reveal').live("click", function(){
	
		jQuery('#contact p').fadeOut(function(){
			
			jQuery('#contact p').remove();
			jQuery('#contactName').val('Name');
			jQuery('#contactEmail').val('Email');
			jQuery('#contactMessage').val('Message');
			jQuery('#contactForm').slideDown();
		
		});
		
		return false;
	});
	
	
});

function strpos (haystack, needle, offset) {
    var i = (haystack+'').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}