jQuery(document).ready(function() {

	var itemWidth = 140; //for example
	var number = jQuery(window).width() / itemWidth;
	number = Math.floor( number );

function pauseVimeo(){
	jQuery('.vimeoLoaded').each(function(){
		var myId = jQuery(this).attr('ID');
		Froogaloop(myId).api('pause');
        Froogaloop(myId).api('unload');
	});	
}

//check to see if all images have loaded using imagesLoaded plugin
jQuery('#wrapper').imagesLoaded( function( $images ) {
	jQuery("#content").fadeIn("fast",function(){
		jQuery(".bottomNav").fadeIn("fast",function(){
			var iframe = jQuery('#portfolio article:first-child').find('iframe').attr('id');
			loadIframe(iframe);
		});
	});
  // callback provides one argument, the jQuery object of child images
  console.log( $images.length + ' images have been loaded in ' + this )
});

//initiate carousel
jQuery(".thumbnails").carouFredSel({
	prev: "#prev",
	next: "#next",
	width:1400,
	auto: false,
	items: {
		minimum: 10,
		width: 140,
		visible:10,
		height: 96
	},
	scroll: {
		items: 4,
		easing: "easeOutQuad",
		duration: 1000
	}
});
	

//reveal or hide post descriptions on CREDITS click
jQuery('.credits').click(function(){
	jQuery(this).toggleClass('creditsActive').parent().next().stop(true,true).toggle('slow', function(){
		//finished
	});
});

//hide post description on CLOSE click
jQuery('.close').click(function(){
	jQuery(this).parent().hide('slow').prev().find('.credits').addClass('creditsActive');
});

//show posts when thumb is clicked
jQuery('.thumbnail').stop().click(function(){
	var myIndex = jQuery(this).attr('rel');
	pauseVimeo();
	jQuery('#content').find('.visible').stop().fadeTo(250,0,function(){
		jQuery(this).addClass('hidden');
		jQuery('#content').find('.mainPost').removeClass('visible');
		jQuery('#portfolio').find('.mainPost').eq(myIndex).removeClass('hidden').addClass('visible').fadeTo(250, 1, function() {
			var iframe = jQuery(this).find('iframe').attr('id');
			loadIframe(iframe);
			jQuery(this).find('.video-js').each(function(){
				if (Modernizr.video) {
					this.player.play();
				} else {
				  return false;
				}
				});
		});		
	});
});


//show main content when header link is clicked
jQuery('#mainHeader').stop().click(function(){
	jQuery('.video-js').each(function(){
		pauseVimeo();
		if (Modernizr.video) {
			this.player.pause();
		} else {
		  return false;
		}
    });
	jQuery('#content').find('.visible').stop().fadeTo(250,0,function(){
		jQuery(this).addClass('hidden');
		jQuery('#content').find('.mainPost').removeClass('visible');
		jQuery('#portfolio').find('.mainPost').eq(0).removeClass('hidden').addClass('visible').fadeTo(250, 1, function() {});
	});
});

//show about when link is clicked
jQuery('#aboutLink').stop().click(function(){
	jQuery('.video-js').each(function(){
		pauseVimeo();
		if (Modernizr.video) {
			this.player.pause();
		} else {
		  return false;
		}
    });
	jQuery('#content').find('.visible').stop().fadeTo(250,0,function(){
		jQuery(this).addClass('hidden');
		jQuery('#content').find('.mainPost').removeClass('visible');
		jQuery('#about').removeClass('hidden').addClass('visible').fadeTo(250, 1, function() {
		});		
	});
});

//show about when link is clicked
jQuery('#privateLink').stop().click(function(){
	jQuery('.video-js').each(function(){
		pauseVimeo();
		if (Modernizr.video) {
			this.player.pause();
		} else {
		  return false;
		}
    });
	jQuery('#content').find('.visible').stop().fadeTo(250,0,function(){
		jQuery(this).addClass('hidden');
		jQuery('#content').find('.mainPost').removeClass('visible');
		jQuery('#login').removeClass('hidden').addClass('visible').fadeTo(250, 1, function() {
		});		
	});
});

//show contact when link is clicked
jQuery('#contactLink').stop().click(function(){
	jQuery('.video-js').each(function(){
		pauseVimeo();
		if (Modernizr.video) {
			this.player.pause();
		} else {
		  return false;
		}
    });
	jQuery('#content').find('.visible').stop().fadeTo(250,0,function(){
		jQuery(this).addClass('hidden');
		jQuery('#content').find('.mainPost').removeClass('visible');
		jQuery('#contact').removeClass('hidden').addClass('visible').fadeTo(250, 1, function() {
		});		
	});
});

function loadIframe(iframeID){
	var iframeID2 = "#" + iframeID;
	var mySrc = jQuery(iframeID2).attr('name');
	Froogaloop(iframeID2).addEvent('ready');
	jQuery(iframeID2).addClass('vimeoLoaded').attr('src',mySrc)
}


function pauseAllVideos(){
	jQuery('iframe').each(function(){
		jQuery(this).api('api_pause');
	});
}


});
