jQuery(document).ready(function($){
	
	/* --------------------------------------------------
		Portfolio - Lightbox - Genereate after change
	-------------------------------------------------- */
	function generate_lightbox(){
		
		/* Initialize */
		$("a[rel^='prettyPhoto']").prettyPhoto({
			theme:'facebook'
		});
		
		/* Hover */
		$("a[rel^='prettyPhoto']:not(.portfolio-popup-info-zoom)").append('<span class="lightbox-overlay"><span class="lightbox-overlay-inner"></span></span>');
		
		if($('.portfolio-popup-info').length){
			$('.portfolio-popup-info').css({ 'opacity' : 0 });
		}
		
	}
	
	/* --------------------------------------------------
		Top Slider & Slider Posts - Hide all but first
	-------------------------------------------------- */
	$('#subheader-slider-slides .slide:not(:first)').hide();
	$('.slides li:not(:first)').hide();
	
	/* --------------------------------------------------
		Toggle
	-------------------------------------------------- */
	
	/* Fix the issue with jumping when sliding up and down */
	$('.box .box-content').each(function(){ $(this).height($(this).height()); });
	
	/* Hide all the collapsed toggles */
	$('.box.collapsed .box-content').hide();
	
	/* Open toggle */
	$('.box.collapsed .box-toggle').live('click', function(e){
		e.preventDefault();
		var box = $(this).parents('.box');
		if(box.hasClass('instant-toggle')){
			box.removeClass('collapsed').find('.box-content').show();
		}else{
			box.removeClass('collapsed').find('.box-content').slideDown(300);
		}
	});
	
	/* Collapse toggle */
	$('.box:not(.collapsed) .box-toggle').live('click', function(e){
		e.preventDefault();
		var box = $(this).parents('.box');
		if(box.hasClass('instant-toggle')){
			box.addClass('collapsed').find('.box-content').hide();
		}else{
			box.find('.box-content').slideUp(300, function(){
				box.addClass('collapsed');
			});
		}
	});
	
	/* --------------------------------------------------
		Tabs
	-------------------------------------------------- */
	
	/* Hide all tabs but first */
	$('.tabs-container').each(function(){
		$(this).find('.tab-content:not(:first)').hide();
		$(this).find('.tab-content:first').show();
		$(this).find('.tabs-nav li:first').addClass('active');
	});
	
	/* Change tab (click) */
	$('.tabs-nav li a').click(function(e){
		e.preventDefault();
		$(this).parents('li').addClass('active').siblings('li.active').removeClass('active');
		var tab_id = $(this).parents('li').index();
		$(this).parents('.tabs-container').find('.tab-content').eq(tab_id).show().siblings('.tab-content').hide();
	});
	
	/* --------------------------------------------------
		Portfolio - Popup
	-------------------------------------------------- */
	if($('.portfolio-popup-info').length){
		$('.portfolio-popup-info').css({ 'opacity' : 0 });
	}
	
	$('.portfolio-popup li').live('mouseenter', function(){
		$(this).find('.portfolio-popup-info').stop().animate({ opacity : 1 }, 300).parents('li').siblings('li').stop().animate({ opacity : 0.7 }, 300);
	}).live('mouseleave', function(){
		$(this).find('.portfolio-popup-info').stop().animate({ opacity : 0 }, 300).parents('li').siblings('li').stop().animate({ opacity : 1 }, 300);;
	});
	
	/* --------------------------------------------------
		PrettyPhoto
	-------------------------------------------------- */
	
	/* Initialize */
	/*$("a[rel^='prettyPhoto']").prettyPhoto({
		theme:'facebook'
	});*/
	
	/* Hover */
	$("a[rel^='prettyPhoto']:not(.portfolio-popup-info-zoom)").append('<span class="lightbox-overlay"><span class="lightbox-overlay-inner"></span></span>');
	$("a[rel^='prettyPhoto']:not(.portfolio-popup-info-zoom)").live('mouseenter', function(){
		$(this).find('.lightbox-overlay').fadeIn(300);
	}).live('mouseleave', function(){
		$(this).find('.lightbox-overlay').fadeOut(300);
	});
	
	/* --------------------------------------------------
		Portfolio - Filter - Quicksand
	-------------------------------------------------- */
	$('#quicksand-nav a.button').click(function(e) {
		e.preventDefault();  
		$.get( $(this).attr('href'), function(data) {
			$('.portfolio-ul').quicksand( $(data).find('li'), { adjustHeight: 'dynamic' }, function(){
				generate_lightbox();
			});
		});  
	});
	
	/* --------------------------------------------------
		Twitter
	-------------------------------------------------- */
	var twitter_total = 0;
	var twitter_id;
	$('.twitterfeed, .twitterfeed_2').each(function(){
		
		twitter_total = twitter_total + 1;
		twitter_id  = 'twitter_' + twitter_total;
		$(this).find('.message').attr('id', twitter_id);
		
		if($(this).find('input.amount').length){
			var amount = $(this).find('input.amount').val();
		}else{
			amount = 1;
		}
		
		var profile = $(this).find('input.twitter_profile').val();
		
		getTwitters(twitter_id, {
			id: profile, 
			count: amount, 
			enableLinks: true, 
			ignoreReplies: false,
			template: '<span class="twitterPrefix"><p class="twitterStatus">%text% <em class="twitterTime"><a href="http://twitter.com/%user_screen_name%/statuses/%id%">- %time%</a></em><span class="username"> - @ <a href="http://twitter.com/%user_screen_name%/statuses/%id%">%user_screen_name%</a></span></p>',
			newwindow: true
		});
		
	});
	
	/* --------------------------------------------------
		Testimonials - Scroller
	-------------------------------------------------- */
	//$('ul.testimonials').cycle();
	$('ul.testimonials li').css({ 'background-color' : 'transparent' });
	
	/* --------------------------------------------------
		Wrapped image with anchor - Hover
	-------------------------------------------------- */
	
	$('a:has(img.wrapped)').live('mouseenter', function(){
		$(this).find('img.wrapped').css({ 'background-color' : '#4d4d49', 'border-color' : '#4d4d49' });
	}).live('mouseleave', function(){		
		$(this).find('img.wrapped').css({ 'background-color' : '#fff', 'border-color' : '#e6e6da' });
	});
	
	$('a:has(img.wrapped-small)').live('mouseenter', function(){
		$(this).find('img.wrapped-small').css({ 'border-color' : '#4d4d49' });
	}).live('mouseleave', function(){		
		$(this).find('img.wrapped-small').css({ 'border-color' : '#e6e6da' });
	});
	
	
	/* --------------------------------------------------
		Contact Form
	-------------------------------------------------- */
	$('.contactForm').submit(function(e){

		//prevent the normal processing
		e.preventDefault();

		//delete the errors (so we don't get duplicates)
		$(this).find('.LV_invalid_field').removeClass('LV_invalid_field');

		//declaring and setting vars
		var value, theID, error, emailReg;
		var submit_path = $(this).attr('action');
		error = false;
		emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;				

		//validating
		$(this).find('textarea, input[type=text]').each(function(){
			value = $(this).val();
			theID = $(this).attr('id');
			if(value == ''){
				$(this).addClass('LV_invalid_field');
				error = true;
			}
			if(theID == 'cemail' && (value == '' || value == 'Email' || !emailReg.test(value))){
				$(this).addClass('LV_invalid_field');
				error = true;
			}
		});

		//send email and loaded message
		if(error == false){
			$(this).load(submit_path, $(this).serialize());
		}

	});
	
	/* --------------------------------------------------
		Navigation Submenu - First and last item padding
	-------------------------------------------------- */
	$('#navigation ul ul li:last-child').css({ 'padding-bottom' : '10px' });
	
});

jQuery(window).load(function(){
	
	/* --------------------------------------------------
		Widget Slider Posts - Init
	-------------------------------------------------- */
	if(jQuery('.slides').length){
		jQuery('.slides').cycle({
			timeout: 0
		});
		jQuery('.slides li').css({ 'background-color' : 'transparent' });
	}
	
	jQuery('.jw-slider-posts .next').click(function(e){
		e.preventDefault();
		jQuery(this).parents('.jw-slider-posts').find('.slides').cycle('next');
	});
	
	jQuery('.jw-slider-posts .previous').click(function(e){
		e.preventDefault();
		jQuery(this).parents('.jw-slider-posts').find('.slides').cycle('prev');
	});

});
