jQuery(document).ready(function() {
	
	// Attach button code to month nav
	jQuery('#header_home .month_nav li a').each( function(i) {
		jQuery(this).click( function () {
			
			var month_id = jQuery(this).attr('class');

			var targetOffset = jQuery('#calendar .'+month_id).offset().top;

			jQuery('html,body').animate({scrollTop: targetOffset}, 800, "swing", function () {
				
				jQuery('#calendar .'+month_id+' .flash').css({'opacity':'0.3'});
				jQuery('#calendar .'+month_id+' .flash').animate({
					opacity : '0'
				},900);
			});
		});
	});
	
	jQuery('#calendar li.published a').each( function(i) {
		jQuery(this).hover(
			ps_published_onhover
			,ps_published_onrollout
		);
	});
	
	if ( jQuery('#calendar li.is_first_day').length > 0 ) {
		// THIS IS THE VERY FIRST DAY. 
		ps_published_onhover( jQuery('#calendar li.veryfirstday a') );
	} else {
		ps_published_onhover( jQuery('#calendar li.today a') );
	}
	
	// Attach rollover code to sneak peek
	jQuery('#calendar li.sneakpeek').each( function(i) {
		jQuery(this).hover(
			function () {
				jQuery(this).addClass('hover');
			}
			,function () {
				jQuery(this).removeClass('hover');
			}
		);
	});
	
	if (jQuery('#page_corner').length > 0) {
		jQuery('#page_corner a').hover(
			function ()  {
				jQuery(this).addClass('hover');
			}
			,function () {
				jQuery(this).removeClass('hover');
			}
		);
	}
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	
	// If page is a photo gallery, instantiate Scrollable
	if ( jQuery('.scrollable').length > 0 ) {
		jQuery('.scrollable').scrollable({
			size:1
			,clickable: false
			,vertical: false
			,loop: false
			,easing: "swing"
			,prevPage: '#prev-button-wrap'
			,nextPage: '#next-button-wrap'
			,activeClass: "active"
			,onSeek: ps_gallery_seek
		});
		
		/*
		jQuery('.navi a').each( function(i) {
			jQuery(this).html('<img src="'+template_dir_uri+'/images/spacer.gif" height="19" width="19" alt="" />');
		});
		*/
		
		// Position 'Previous' and 'Next' buttons
		var info_top = parseInt( jQuery('#info').css('top') );
		var info_height = parseInt( jQuery('#info').height() );
		
		var button_top = parseInt( jQuery('#prev-button-wrap').css('top') );
		var button_offset = parseInt( jQuery('#prev-button-wrap').offset().top );
		
		var new_top = (info_top + info_height) - (button_offset - button_top) + 94;
		
		// Move previous
		jQuery('#prev-button-wrap, #next-button-wrap').css({'top' : new_top });
		
		//alert('button top: ' + button_top);
		//alert('button offset: ' + button_offset);
		
		ps_gallery_seek(0);
	}
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	
	// If we're on a poll and the "Vote Now" jump button exists, hook up function
	if ( jQuery('#vote-now-jump').length > 0 ) {
		jQuery('#vote-now-jump').click(function () {
			// Check for existence of either wp-poll or wp-poll-form div
			
			if ( jQuery('.wp-polls-form').length > 0 ) {
				var targetObj = jQuery('.wp-polls-form');
			} else if ( jQuery('.wp-polls').length > 0 ) {
				var targetObj = jQuery('.wp-polls');
			}
			
			var targetOffset = targetObj.offset().top;
			
			jQuery('html,body').animate({scrollTop: targetOffset}, 600, "swing", function () {
				targetObj.css({backgroundColor:'#ffffff'});
				targetObj.stop();
				targetObj.animate({
					backgroundColor : jQuery('body').css("background-color")
				},600,"swing");
			});
		});
	}
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	
	// If this is the look book page, add in some code fixing widths on some divs
	if ( jQuery('#content').hasClass('look_book') ) {
		
		jQuery('#content .right, #content .left').each( function(i) {
			
			var img_width = jQuery(this).find("img").attr('width');
			//var img_height = jQuery(this).find("img").attr('height');
			
			jQuery(this).find('.caption-wrapper').css({
				'width' : img_width
			});
			
			jQuery(this).find('.details').css({
				'width' : img_width
				/*,'top' : (img_height - 25) + 'px'*/
			});
		});
	}
	
	// Also, we want to attach the callout div if it exists
	if ( jQuery('#look_book_callout').length > 0 ) {
		// It exists
		
		var top_pos = 40 + jQuery('div.attach-callout').find('img').offset().top;
		var left_pos = parseInt(jQuery('div.attach-callout').css('margin-left')) + parseInt(jQuery('div.attach-callout').find('img').attr('width'));
		
		jQuery('#look_book_callout').css({
			'top' : top_pos
			,'left' : left_pos
			,'z-index' : '123'
		});
	}
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	
	if ( jQuery('#content').hasClass('wallpaper') ) {
		
		// Position the download floater
		if ( jQuery('#download_floater_top').length > 0 && jQuery('#download_floater_left').length > 0 ) {
			
			var new_left = parseInt(jQuery('#download_floater_left').text());
			var new_top1 = parseInt(jQuery('#download_floater_top').text());
			
			jQuery('#content .download-floater').show();
			
			jQuery('#content .download-floater').css({
				'top':new_top1 + 'px',
				'left':new_left + 'px'
			});
		}
		
		// Position the shop floater
		if ( jQuery('#shop_floater_top').length > 0 && jQuery('#shop_floater_left').length > 0 ) {
			
			var new_left = parseInt(jQuery('#shop_floater_left').text());
			var new_top2 = parseInt(jQuery('#shop_floater_top').text());
			
			jQuery('#content .shop-floater').show();
			
			jQuery('#content .shop-floater').css({
				'top':new_top2 + 'px',
				'left':new_left + 'px'
			});
		}
		
		// Position body copy, if any
		if ( jQuery('#copy_area_top').length > 0 && jQuery('#copy_area_left').length > 0 ) {
			
			var new_left = parseInt(jQuery('#copy_area_left').text());
			var new_top3 = parseInt(jQuery('#copy_area_top').text());
			
			jQuery('#content .copy-area').show();
			
			jQuery('#content .copy-area').css({
				'top':new_top3 + 'px',
				'left':new_left + 'px'
			});
		}
		
		// Set the correct height of the .post div
		
		var rows = 0;
		
		if ( jQuery('#content div.row1').length > 0 ) rows = 1;
		if ( jQuery('#content div.row2').length > 0 ) rows = 2;
		if ( jQuery('#content div.row3').length > 0 ) rows = 3;
		if ( jQuery('#content div.row4').length > 0 ) rows = 4;
		if ( jQuery('#content div.row5').length > 0 ) rows = 5;
		if ( jQuery('#content div.row6').length > 0 ) rows = 6;
		if ( jQuery('#content div.row7').length > 0 ) rows = 7;
		if ( jQuery('#content div.row8').length > 0 ) rows = 8;
		if ( jQuery('#content div.row9').length > 0 ) rows = 9;
		if ( jQuery('#content div.row10').length > 0 ) rows = 10;
		if ( jQuery('#content div.row11').length > 0 ) rows = 11;
		if ( jQuery('#content div.row12').length > 0 ) rows = 12;
		
		var new_height = rows * 240;
		
		var dl_floater_end_y = ( new_top1 + jQuery('#content .download-floater').height() );
		var shop_floater_end_y = ( new_top2 + jQuery('#content .shop-floater').height() );
		var copy_area_end_y = ( new_top3 + jQuery('#content .copy-area').height() );
		
		var floater_end_y = dl_floater_end_y;
		if (shop_floater_end_y > dl_floater_end_y) floater_end_y = shop_floater_end_y;
		if (copy_area_end_y > floater_end_y) floater_end_y = copy_area_end_y;
		
		if ( new_height < floater_end_y ) new_height = floater_end_y;
		
		jQuery('#content .post').css({'height': new_height + 'px'});
	}
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	
	if ( jQuery('#content').hasClass('denim_flowers') ) {
		
		// Position body copy, if any
		if ( jQuery('#copy_area_top').length > 0 && jQuery('#copy_area_left').length > 0 ) {
			
			var new_left = parseInt(jQuery('#copy_area_left').text());
			var new_top = parseInt(jQuery('#copy_area_top').text());
			
			jQuery('#content .copy-area').show();
			
			jQuery('#content .copy-area').css({
				'top':new_top + 'px',
				'left':new_left + 'px'
			});
		}
		
		// Set the correct height of the .post div
		
		var new_height = ( new_top + jQuery('#content .copy-area').height() );
		
		jQuery('#content .post').css({'height': new_height + 'px'});
		
	}
		
});

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ps_gallery_seek( i ) {
	
	var elem = jQuery('.scrollable li:eq('+ i +') div.caption');
	
	caption = '';
	if ( elem.length > 0 ) {
		var caption = elem.html();
	}
	
	$('.scrollable_caption').html( caption );
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ps_published_onhover( jq ) {
	
	// Loop through existing items and close any if they are open
	jQuery('#calendar li.published.hovering a').each( function(i) {
		ps_published_onrollout( jQuery(this) );
	});
	
	if ( jq.length > 0 ) {
		var this_jq = jq;
	} else {
		var this_jq = jQuery(this);
	}
	
	var this_id = this_jq.attr('class');
	
	// Add Hover class
	jQuery('#' + this_id ).addClass('hovering');
	
	jQuery('#' + this_id + ' .large-overlay').stop(true);
	
	// Is this the last column?
	var leftpos = "0px"
	if ( jQuery('#' + this_id).hasClass('lastcol') ) {
		leftpos = "-110px"
	}
	
	// Is this the very first day?
	var dest_w = '220px';
	var dest_h = '220px';
	if ( jQuery('#' + this_id).hasClass('veryfirstday') ) {
		dest_w = '550px';
		dest_h = '550px';
		
		// Adjust size of a tag
		jQuery('#' + this_id + ' a').css({
			width: dest_w
			,height: dest_h
		});
		jQuery('#' + this_id + ' a img').attr("width","550");
		jQuery('#' + this_id + ' a img').attr("height","550");
	}
	
	jQuery('#' + this_id).css({
		'z-index': get_next_zindex()
	});
	
	if ( jQuery('#' + this_id + ' .large-overlay').height() < 220 ) {
		jQuery('#' + this_id + ' .large-overlay').css({
			opacity: '.8'
			,width: '110px'
			,height: '110px'
			,'z-index': get_next_zindex()
		});
	}
	
	jQuery('#' + this_id + ' .large-overlay').animate(
		{
			width: dest_w
			,height: dest_h
			,left: leftpos
			,opacity: '1'
		}
		,200
	);
}

function ps_published_onrollout( jq ) {
	
	if ( jq.length > 0 ) {
		var this_jq = jq;
	} else {
		var this_jq = jQuery(this);
	}
	
	var this_id = this_jq.attr('class');
	
	// If the hover class is already gone, quit
	// Remove Hover class
	if (!jQuery('#' + this_id ).hasClass('hovering')) return false;
	
	// Remove Hover class
	jQuery('#' + this_id ).removeClass('hovering');
	
	jQuery('#' + this_id + ' .large-overlay').stop(true);
	
	// Adjust size of a tag
	jQuery('#' + this_id + ' a').css({
		width: '110px'
		,height: '100px'
	});
	jQuery('#' + this_id + ' a img').attr("width","110");
	jQuery('#' + this_id + ' a img').attr("height","110");
	
	jQuery('#' + this_id + ' .large-overlay').animate({
		width:'110px'
		,height:'110px'
		,left: '0px'
		,opacity:'0'
	}, 200);
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ps_share_footer_callback () {
	var targetOffset = jQuery('#footer-share').offset().top - 400;
	jQuery('html,body').animate({scrollTop: targetOffset}, 900);
}

function get_next_zindex() {
	if (!window.last_zindex) window.last_zindex = 500;
	window.last_zindex++;
	return window.last_zindex;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */