jQuery(document).ready(function($) {
	
		//set min-width of site
	$('.item').css('min-width', ($('#wrap').width()-825));
	
	//set the position of the header
	$('.a-content-wrap').css('top', ($('.a-content').height()/2)-($('.a-content-wrap').height()/2));
			
	//get all of the images for the post and put them where they belong
	 $('.b-body .item .i-content-slide').each(function() {
		var wrap = $(this);
		var h = wrap.closest('.item').find('.post-images-holder');
		wrap.find('img').each(function() {
			var img = $(this);
			img.remove().appendTo(h);
		});
		wrap.find('p:empty').remove();
	 });
	
	//drop down menu
	$('.a-head .a-nav li').hover(function() {
		//on over
		$(this).find('.hover').show();
	}, function() {
		//on out
		$(this).find('.hover').hide();
	});
	
	function setSize() {
	  
	  //main slider
	  /*$('.b-scrollPane').jScrollHorizontalPane();
	  $('.b-scrollPane').jScrollHorizontalPane();*/
	  $('.i-content-slide').jScrollPane();
	  
	  //main slider
	  var conveyor = $('.b-body .b-content');
	  var item = $('.b-body .b-content-wrap');
	  var cfooter = $('.c-footer');
	  //var bodyWidth = $('.b-content-wrap').width()-$('.a-head').width()-$('.c-footer').width()-425;
	  var bodyWidth = (item.width()+cfooter.width())-($('.b-body').width()-$('.a-head').width()-63);
	  
	  //set the width of the slider
	  $('.b-body .slider').css('width', ($('.b-body').width()-760)-150); //minus 760 for width of header and footer. 150 for slider
		  
	   //create slider  
	   $(".b-body .slider").slider({
		  animate:true,
		  orientation: 'horizontal',
		  max: (item.width()+cfooter.width())-($('.b-body').width()-$('.a-head').width()-63),
		  min: 0,
		  step: 7,
		  slide: function(e, ui) {
			  var pos = conveyor.position();
			  conveyor.css("left", "-" + ui.value + "px");
		  },
		  change:function() {
			  var s = $('#slide-helper')
			  if(s.length > 0)
			  	s.stopTime('slidehelper').fadeOut().remove();
		  }
	   });
	   
	   
	   $('.b-body').bind('mousewheel', function(event, delta) {
			var bbody = $(".b-body .slider");			  
			var pos = conveyor.position();
			
			// remove helper
			var s = $('#slide-helper')
			  if(s.length > 0)
			  	s.stopTime('slidehelper').fadeOut().remove();
			// end remove helper
			
			//var bodyPercent = .2;
			if(delta < 0) {
				//scrolling right
				if(pos.left > -bodyWidth) {
				  //conveyor.animate({left:pos.left-140}, 500);
				  if((-bodyWidth-pos.left) > -140) {
					  conveyor.css("left", -bodyWidth);
					  bbody.slider('option', 'value', bbody.slider('option', 'value')+140);
				  }else {
					  conveyor.css("left", pos.left-140 + "px");
					  bbody.slider('option', 'value', bbody.slider('option', 'value')+140);
				  }
				}
			}else {
				//scrolling left
				if(pos.left < 0) {
				  //conveyor.animate({left:pos.left+140}, 500);
				  conveyor.css("left", pos.left+140 + "px");
				  bbody.slider('option', 'value', bbody.slider('option', 'value')-140);
				}
			}
		  });
	 
	}
	//init
	setSize();
	 	 
	 //position the archives in the footer
	 $('.c-footer .c-foot-center').css('top', ($('.c-footer').height()/2)-($('.c-foot-center').height()/2));
	 
	 //add comment functionality
	 $('.link-add-thoughts').click(function(event) {
	 	event.preventDefault();
	 	var c = $(this).closest('.item').find('.i-actions');
		c.children().hide();
		c.find('.i-content-comment').fadeIn(500);
	 });
	 
	 //send to friend functionality
	 $('.link-send-friend').click(function(event) {
	 	event.preventDefault();
		var c = $(this).closest('.item').find('.i-actions');
		c.children().hide();
		c.find('.i-content-friend').fadeIn(500);
	 });
	 
	 //add cancel button functionality
	 $('.c-button').click(function(event) {
	 	event.preventDefault();
	 	var c = $(this).closest('.item').find('.i-actions');
		c.children().hide();
		c.find('.jScrollPaneContainer').fadeIn(500);
	 });
	 
	 
	 //submit contact form
	 $('#contact-submit').click(function(event) {
		event.preventDefault();
		
		//validate
		var errors = false;
		var form = $(this).closest('form');
		var n = form.find('input[name=name]');
		var e = form.find('input[name=email]');
		var m = form.find('textarea');
		
		//clear errors
		if(n.val() == 'required') { n.val(''); }
		if(m.val() == 'required') { m.val(''); }
		if(e.val() == 'required') { e.val(''); }
		
		if(n.val() == '') { errors = true; n.val('required'); }
		if(m.val() == '') { errors = true; m.val('required'); }
		if(e.val() == '' || e.val().indexOf("@") == -1 || e.val().indexOf(".") == -1) { errors = true; e.val('required'); }
		
		if(!errors) { form.find('input[type=submit]').click(); }
		
	 });
	 
	 Cufon.replace('.a-nav ul li a', { hover: true });
	 Cufon.replace('.blurb-title', {});
	 Cufon.replace('.a-search input', {});
	 
	 //replace the text in the search field
	 $('.a-search input').focus(function() {		
	 	if($(this).val() == "SEARCH THIS BLOG") {
			$(this).val('');
		}
	 });
	 $('.a-search input').blur(function() { if($(this).val() == '') { $(this).val('SEARCH THIS BLOG'); }});
	 
	 //vertically center images
	 $('.post-images-holder img').each(function() {
		var i = $(this);
		i.css('margin-top', (500/2) - (i.height()/2));
	 });
	 
	 
	 //slider helper
	 $(function() {
		
		var s = $('#slide-helper');
		if(s.length < 0) return;
		
		s.oneTime(3500, 'slidehelper', function() {			
			s.animate({opacity:1, marginTop:'140'}, {duration:600})
			 .animate({opacity:.4}, {duration:1000})
			 .animate({opacity:1}, {duration:1000})
			 .animate({opacity:.4}, {duration:1000})
			 .animate({opacity:1}, {duration:1000})
			 .animate({opacity:.4}, {duration:1000})
			 .animate({opacity:1}, {duration:1000})
			 .animate({opacity:0}, {duration:1000});
		});
		
		s.everyTime(17000, 'slidehelper', function() {
			
			s.css({'margin-top':0});
			
			s.animate({opacity:1, marginTop:'140'}, {duration:600})
			 .animate({opacity:.4}, {duration:1000})
			 .animate({opacity:1}, {duration:1000})
			 .animate({opacity:.4}, {duration:1000})
			 .animate({opacity:1}, {duration:1000})
			 .animate({opacity:.4}, {duration:1000})
			 .animate({opacity:1}, {duration:1000})
			 .animate({opacity:0}, {duration:1000});
		});
				
	});
	
});