$(function(){

	//create toogles, tabs panels, twitter feed, posts widget, testimonials widget, lightboxes, etc..
	if($('.toggle').length > 0) $('.toggle').toggle();
	if($('.tabs').length > 0) $('.tabs').tabs('div.tabsContent');
	if($('.twitterList').length > 0) $('.twitterList').twitter('envato',2);
	if($('.widget.posts').length > 0) $('.widget.posts').tabs('div.postTabs');
	if($('.testimonialsWidget').length > 0) $('.testimonialsWidget').testimonials();
	 $("a[rel^='prettyPhoto']").prettyPhoto();
	 
	if($('#folioFilters').length > 0){
		
		$filters = $('#folioFilters');
		$source = $('#folioSource');
		$destination = $('#folioDestination');
		
		$filters.find('a').click(function(){
			if(!$(this).hasClass('selected')){
				$filters.find('a.selected').removeClass('selected');
				$(this).addClass('selected');
				$source.quicksand($destination.children('li' + $(this).attr('rel')), function(){
					addFolioHovers();
				});
			}
		});
		
		$source.quicksand($destination.find('li'), function(){
			addFolioHovers();
		});
		
		//portfolio hovers
		function addFolioHovers(){
			$source.children('li').hover(function(){
				$(this).find('div').stop().animate({'opacity': 0}, 0).css('display', 'block').animate({'opacity':1}, 200);
			}, function(){
				$(this).find('div').stop().fadeOut(200);
			});
		}
		
	}
	 
	 //start homepage sliders
	 if($('#parallaxSlider').length > 0 ) $('#parallaxSlider').parallaxy();
	 if($('#circlesSlider').length > 0) { 
		
		var $slider = $('#circlesSlider').find('.slidesHolder');
		$slider.cycle({
			timeout:5000,
			prev: $slider.parent().find('a.btnPrev'),
			next: $slider.parent().find('a.btnNext'),
			pager: $slider.parent().find('div.slidesControls')
		});
		
		if($.browser.opera){
			$slider.find('.slide > div > div').css('border-radius', 0);
		}
	
	 }
	
	//for all images that don't have a lightbox script attached, do simple hovers
	$('img.imgFrame').hover(function(){
		$(this).stop().animate({'opacity': .8}, 200);
	}, function(){
		$(this).stop().animate({'opacity': 1}, 200);
	});
	
	//lightbox hovers
	$('a[rel]').each(function(){
		if($(this).attr('rel').slice(0,6)=='pretty') {
			$(this)
				.append('<div class="jQueryHover"><!-- block added by the script --></div>')
				.hover(function(){
					$(this).find('div.jQueryHover').stop().animate({'opacity': 0}, 0).css('display', 'block').animate({'opacity':1}, 200);
				}, function(){
					$(this).find('div.jQueryHover').fadeOut(200);
				});
		}
	});
	
	//add input replacement and hover
	$('input, textarea').each(function(){
	
		if($(this).attr('id') != 'submitButton'){
			$(this).attr('data-value', $(this).val())
				.focus(function(){
					$(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'));
					}
				});
		}
		
	});
	
	//menu animation
	$('#top ul > li').each(function(){;
		if($(this).children('div').length > 0){
		
			var $li = $(this).find('li');
			
			$li.animate({'opacity': '0'}, 0);
			$(this).find('div').animate({'opacity': '0'}, 0);
			
			$(this).hover(function(){
				
				$(this).find('div').animate({'opacity': '1'}, 300);
				$li.each(function(){
					$(this).stop().delay($(this).index()*100).animate({'opacity': '1'}, 300);
				});
			
			}, function(){
			
				$li.animate({'opacity': '0'}, 0);
			
			});
		
		}
	});
	
	
	//function that handles a contact form
	if($('#contact').length){
		
		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){
			
				$('#contact 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>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();
			}
		}
	
	}
	
});
