/* Author: Ruben Bristian http://rubenbristian.com

*/
$(document).ready(function(){

	$('a').hoverFadeColor();

	//add input replacement and hover
	$('input, textarea').each(function(){
	
		if($(this).attr('id') != 'submitButton'){
			$(this).attr('data-value', $(this).val())
				.focus(function(){
					if(!$(this).parent().hasClass('searchBox')){
						$(this).addClass('focusInput');
					}
					if($(this).val() == $(this).attr('data-value')){
						$(this).val('');
					} else {
						$(this).select();
					}
				})
				.blur(function(){
					$(this).removeClass('focusInput');
					if($(this).val() == ''){
						$(this).val($(this).attr('data-value'));
					}
				});
		}
		
	});
	
	//add animations to all submenus
	$('.mainHeader nav ul > li').each(function(){
		if($(this).children('ul').length > 0){
		
			var $li = $(this).find('li');
			var $hr = $(this).find('hr');
			
			$hr.animate({'opacity': 0}, 0);
			$li.animate({'opacity': 0, 'margin-top': -10}, 0);
			
			$(this).hover(function(){
			
				$hr.animate({'opacity': 1}, 200);
				$li.each(function(){
					$(this).stop().delay($(this).index()*50).animate({'opacity': 1, 'margin-top': 0}, 100);
				});
			
			}, function(){
			
				$hr.animate({'opacity': 0}, 0);
				$li.animate({'opacity': 0, 'margin-top': -10}, 0);
			
			});
		
		}
	});
	
	//add pretty box
	$('a[data-rel]').each(function() {
		$(this).attr('rel', $(this).data('rel'));
	});
	 $("a[rel^='prettyPhoto']").prettyPhoto();
	 
	 //add pretty hover
	 $('a.prettyHover').each(function(){
		$(this).append('<div class="gridHover"><!-- jQuery hover class --></div>');
		$(this).hover(function(){
			$(this).find('div.gridHover').stop().css('display', 'block').animate({'opacity': 0}, 0).animate({'opacity': 1}, 300);
		}, function(){
			$(this).find('div.gridHover').stop().fadeOut(400);
		});
	 });
	 
	 //add folio hover(if there are any folio items)
	 addFolioHover();
	 
	 //add posts hover(if there are any post items)
	 $('a.post').each(function(){
		$(this).append('<div class="gridHover"><p>Read article</p></div>');
		$(this).hover(function(){
			$(this).find('div.gridHover').stop().css('display', 'block').animate({'opacity': 0}, 0).animate({'opacity': 1}, 300);
		}, function(){
			$(this).find('div.gridHover').stop().fadeOut(400);
		});
	 });
	
});

var oldColor, newColor;
function addFolioHover(){
	
	var $caption = $('figcaption h2');
	oldColor = $caption.css('color');
	$caption.addClass('h2hover');
	newColor = $caption.css('color');
	$caption.removeClass('h2hover');
		
	 $('.gridItem > a, .folioGrid ul li > a').each(function(){
	 
		$(this).append('<div class="gridHover"><!-- jQuery hover class --></div>');
		
		if($(this).parent().hasClass('gridItem'))
			$(this).find('.gridHover').css('height', $(this).find('.gridHover').height()-14);
			
		$(this).parent().find('span').hover(function(){
			$(this).parent().find('div.gridHover').stop().css('display', 'block').animate({'opacity': 0}, 0).animate({'opacity': 1}, 300);
		}, function(){
			$(this).parent().find('div.gridHover').stop().fadeOut(400);
		}).click(function(){
			window.open($(this).parent().children('a').attr('href'), '_self');
		});
		
		$(this, $(this).parent().find('span')).hover(function(){
			$(this).find('div.gridHover').stop().css('display', 'block').animate({'opacity': 0}, 0).animate({'opacity': 1}, 300);
			$(this).find('h2').animate({'color': newColor}, 300);
		}, function(){
			$(this).find('div.gridHover').stop().fadeOut(400);
			$(this).find('h2').animate({'color': oldColor}, 300);
		});
		
	 });
}

$('.socialIcons li a').each(function(){
	$(this).parent().append($(this).clone());
	$(this).addClass('socialHover').css('backgroundPosition', $(this).css('backgroundPosition').slice(0, $(this).css('backgroundPosition').indexOf('px'))+'px -16px');
	$(this).parent().hover(function(){
		$(this).find('.socialHover').stop().animate({'opacity': 1}, 300, 'linear');
	}, function(){
		$(this).find('.socialHover').stop().animate({'opacity': 0}, 300, 'linear');
	});
});

//function that initalizes the flickr widget
function initFlickr(){
	$('#flickrFeed').jflickrfeed({
		limit: 4,
		qstrings: {
			id: '52617155@N08'
		},
		itemTemplate: 
		'<li>' +
			'<a href="{{link}}" target="_blank"><img src="{{image_s}}" alt="{{title}}" /></a>' +
		'</li>'
	}, function(data){
		 $('#flickrFeed a').each(function(){
			$(this).append('<div class="gridHover"><!-- jQuery hover class --></div>');
			$(this).hover(function(){
				$(this).find('div.gridHover').stop().css('display', 'block').animate({'opacity': 0}, 0).animate({'opacity': 1}, 300);
			}, function(){
				$(this).find('div.gridHover').stop().fadeOut(400);
			});
		 });
	});
}

//function that initalizes the contact form
function initContact(){
		
	var $name = $('#formName');
	var $subject = $('#formSubject');
	var $email = $('#formEmail');
	var $message = $('#formMessage');
	
	$('#submitButton').click(function(){
	
		var ok = true;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	
		if($name.val().length < 3 || $name.val() == 'Please enter your full name...'){
			$name.val('Please enter your full name...');
			ok = false;
		}
		
		if($subject.val().length < 3 || $subject.val() == 'Please enter a subject...'){
			$subject.val('Please enter a subject...');
			ok = false;
		}
		
		if($email.val() == ''){
			$email.val('Please enter your email address...');
			ok = false;
		}else if(!emailReg.test($email.val())){
			$email.val('Please enter a valid email address...');
			ok = false;
		}
		
		if($message.val().length < 5 || $message.val() == 'Please enter a message...'){
			$message.val('Please enter a message...');
			ok = false;
		}
		
		if(ok){
		
			$('#contactForm form').fadeOut();
			
			$.ajax({
				type: 'POST',
				url: 'contactForm.php',
				data: 'name=' + $name.val() + '&subject=' + $subject.val() + '&email=' + $email.val() + '&message=' + $message.val(),
				success: function(){
					$('#contactForm').html('<h4 class="sectionHeader">Contact Form Submitted</h4><hr /><p>Thanks for writing us! We will contact you as soon as possible!</p>').hide().fadeIn();
				}
			});
			
		}
		
		return false;
	
	});
	
	$name.focus(function(){resetError($name, 'Please enter your full name...')});
	$subject.focus(function(){resetError($subject, 'Please enter a subject...')});
	$email.focus(function(){resetError($email, 'Please enter your email address...')});
	$email.focus(function(){resetError($email, 'Please enter a valid email address...')});
	$message.focus(function(){resetError($message, 'Please enter a message...')});

	function resetError($input, value){
		if($input.val() == value){
			$input.val('');
		}else{
			$input.select();
		}
	}
	
};

/* This function initializes all the jQuery stuff that are on any page */
$(window).load(function(){

		$('.nivoSlider').nivoSlider({
			effect: 'fold',
			slices: 10,
			animSpeed: 1000
		});
		
		$('#gridContainer').isotope({
		  itemSelector: '.gridItem',
		  masonry: {
			columnWidth: 240
		  }
		});
		
		$('#gridContainer > a').hover(function(){
			$(this).find('div.gridHover').stop().css('display', 'block').animate({'opacity': 0}, 0).animate({'opacity': 1}, 200);
		}, function(){
			$(this).find('div.gridHover').stop().fadeOut(400);
		});
		
		$('#gridFilters a').click(function(){
			var selector = $(this).attr('data-filter');
			$('#gridContainer').isotope({filter: selector});
			$('#gridFilters a.selected').removeClass('selected');
			$(this).addClass('selected');
			return false;
		});
		
		$('#twitterFeed').twitter('envato');
		$('#tabs').tabs();

		initContact();
		initFlickr();
		
});
