$(document).ready(function(){

	if ($('.post-ratings').length){
		votecontroller();
	}; //if post-ratings

	images_too_wide();
	if ($('body').is('.home')){
		hero_setup();
	}; //if home
	
	if ($('.advanced-recent-posts').length){
		extend_recent_posts();
	}; //for all "recent posts"-having pages
	
}); //document.ready

$(window).load(function(){
	images_too_wide();
	if ($('body').is('.home')){
		nominees_clear();
	}; //if home
}); //window.load






/*==( ^ Post image is wider than the .major column? Knock it down to size. )======================================================*/

function images_too_wide() {

	innerwidth = 420;
	outerwidth = 460;
	
	if ($('body').is('.page')){
		innerwidth = 520;
		outerwidth = 580;
	}; // if body is .page

	$('.entry-content, .entry-summary').find('img').each(function(){
		howwideisit = $(this).attr('width');
		if (howwideisit > innerwidth && howwideisit < outerwidth) {
			$(this).css({'width': innerwidth, 'height': 'auto', 'position': 'relative'}); //if wider than container, but less than container+padding, resize down to containerwidth
		} else if (howwideisit >= outerwidth) {
			$(this).css({'width': outerwidth, 'height': 'auto', 'position': 'relative', 'left': '-30px'});

		if ($(this).parent().is('.entry-summary')){
			whichelementami = $(this).parent('.entry-summary').index($(this).parent());
		} else {
			whichelementami = $(this).parent().parent('.entry-content').children().index($(this).parent());
		}

		if (whichelementami == 0 && $('body').is(':not(".page")')) {
			$(this).css({'top': '-25px', 'marginBottom': '-25px'}).addClass('movedup');
		}; //if it's the first element in .entry-content; ergo, header image.


		}; //if
		
	$('.entry-content, .entry-summary').find('img').not('.ajax-loader').css({'visibility': 'visible'});
		
	}); //each img in .entry-content

}; //images_too_wide



/*==( ^ Homepage hero/slider; rotates images over time, and on click. )======================================================*/

function hero_setup() {
	hero_initialize();
	slider();
	heroloop();
}; //hero_setup




function hero_initialize() {
	$('.panelcontainer').children('img').css({'display': 'none'});
   $('.panelcontainer').children('img').eq(0).addClass('active').css({'display': 'block'});
   $('.topstory').append('<div class="panelnav"></div><!--panelnav-->');
   $('div.panelnav').append('<a class="goleft">Previous</a>');
   $('div.panelnav').append('<a class="goright">Next</a>');
}; //hero_initialize




function slider() {
   $('.panelnav').mouseover(function() {
      clearInterval(thehomepageloop);
      //$('body').append("STOPPED<br />");
   }); //mouseover

   $('.panelnav').bind("mouseleave",function(){
      clearInterval(thehomepageloop);
      //$('body').append("STARTED AGAIN<br />");
      heroloop();
   }); //mouseleave

	assignclicks();
}; //slider


function assignclicks() {
   $('.panelnav').children('a.goright').click(rightclick);
   $('.panelnav').children('a.goleft').click(leftclick);
}; //assignclicks


function heroloop() {
   thehomepageloop = setInterval(function() {
      //$('body').append("STARTED<br />");
      $('.goright').click();
   }, 4500); //thehomepageloop
} // heroloop



function rightclick () {
   $('.panelnav').children('a.goright').unbind('click');
   $('.panelnav').children('a.goleft').unbind('click');

   if ($('img.active').is(':last-child')) {
      $('div.panelcontainer').children('img.active').removeClass('active').fadeOut(800);
      $('div.panelcontainer').children('img').eq(0).addClass('active').fadeIn(800);
   } else {
      $('div.panelcontainer').children('img.active').removeClass('active').fadeOut(800).next('img').addClass('active').fadeIn(800);
   }

	setTimeout(function () {
		assignclicks();
	}, 800 );
} // rightclick

function leftclick() {
   $('.panelnav').children('a.goleft').unbind('click');
   $('.panelnav').children('a.goright').unbind('click');

   if ($('img.active').is(':first-child')) {
      $('div.panelcontainer').children('img.active').removeClass('active').fadeOut(800);
      $('div.panelcontainer').children('img:last-child').addClass('active').fadeIn(800);
   } else {
      $('div.panelcontainer').children('img.active').removeClass('active').fadeOut(800).prev('img').addClass('active').fadeIn(800);
   }

	setTimeout(function () {
		assignclicks();
	}, 800 );
} // leftclick










/*==( ^ Javascript helper for the Recent Posts sidebar; adds links appropriately, etc. )======================================================*/


function extend_recent_posts(){
	$('ul.advanced-recent-posts').children('li').each(function(){
		the_href = $(this).children('a:first').attr('href');
		$(this).append('<a href="'+the_href+'" class="readmore">Read More <span>&raquo;</span></a>');
		if ($(this).find('span.date').is(':empty')){
		$(this).find('span.date').remove();
		}; //if span's empty
	}); //each li
}; //extend_recent_posts




/*==( ^ Clearing helper for Homepage floated nominees list. )======================================================*/


function nominees_clear(){
	$('.doublewide').children('ul.advanced-recent-posts').children('li:even').css({'clear': 'both'});
	$('.doublewide').children('ul.advanced-recent-posts').children('li:odd').each(function(){
		if (($(this).height()) < ($(this).prev('li').height())){
			$(this).css({'height': ($(this).prev('li').height())});
		}; //if
	});//each
}; //nominees_clear



/*==( ^ Vote Controller - hiding/showing function based on user's "voted/notevoted" cookies )======================================================*/


function votecontroller(){
	if ($('span.alreadyvoted').length){
		$('span.alreadyvoted').each(function(){
			$(this).parent('div.post-ratings').remove();
		}); //each .alreadyvoted
	}; //if .alreadyvoted exists
	
	if ($('#thankyouforvoting').length){
		$('#thankyouforvoting').hide();
	}; //if #thankyouforvoting exists
	
	if (document.URL.indexOf('#thankyouforvoting') >= 0) {
		$('#thankyouforvoting').show();
	}; //if







if ($('.widget_ratings-widget').length) {
	$('.widget_ratings-widget').children('ul').children('li').children('a').each(function(){
		theurlinquestion = $(this).attr('href');
		$('.widget_advancedrecentposts').children('ul').children('li').children('h4').children('a[href="'+theurlinquestion+'"]').parent('h4').parent('li').addClass('keepme').appendTo('.widget_advancedrecentposts ul');
	}); //each
	$('.widget_advancedrecentposts').children('ul').children('li').not('.keepme').remove();
	$('.widget_advancedrecentposts').addClass('orderaffected').children('h3').text('Hambone of the Year - Most Votes');
	
	$('.keepme').each(function(i){
	$(this).append('<span class="numeral">'+(i+1)+'</span>');
	}); //each .keepme
	
}; //if








	
}; //votecontroller

//The following two functions (rate_post() and rate_post_success()) have been ripped from postratings-js.js and placed here for manipulation's sake. Plugin was a bit close-ended, but still the best fit for the job.
//N.B.: Don't update the postratings WP plugin for this reason.

function rate_post(){
	if(!is_being_rated){
		set_is_being_rated(true);
		if(ratingsL10n.show_fading){
			jQuery("#post-ratings-"+post_id).fadeTo("def",0,function(){
				if(ratingsL10n.show_loading){
					jQuery("#post-ratings-"+post_id+"-loading").show()
				}
				jQuery.ajax({type:"POST",url:ratingsL10n.ajax_url,data:"pid="+post_id+"&rate="+post_rating,cache:false,success:rate_post_success})
			})
			}else{
				if(ratingsL10n.show_loading){
					jQuery("#post-ratings-"+post_id+"-loading").show()
				}
				jQuery.ajax({type:"POST",url:ratingsL10n.ajax_url,data:"pid="+post_id+"&rate="+post_rating,cache:false,success:rate_post_success})
			}
	}else{
		alert(ratingsL10n.text_wait)
	}
};


function rate_post_success(a){

	jQuery("#post-ratings-"+post_id).html(a);

	if(ratingsL10n.show_loading){
		jQuery("#post-ratings-"+post_id+"-loading").hide()
	}

	if(ratingsL10n.show_fading){
		jQuery("#post-ratings-"+post_id).fadeTo("def",1,function(){set_is_being_rated(false)})
		if ($('div.post-'+post_id).children('.upperpost').children('h4.entry-title').children('a').length){
			headingto = $('div.post-'+post_id).children('.upperpost').children('h4.entry-title').children('a').attr('href');
			headingto = headingto + '#thankyouforvoting';
		} else {
			headingto = window.location + '#thankyouforvoting';
			$('#thankyouforvoting').show();
		}; //if on archive or on nominee page
		window.location = headingto;
	}else{
		set_is_being_rated(false)
	}

}













/*==================================================================
=       ====  ===        ===      ===        ==        ==       ==
=  ====  ===  ===  ========  ====  =====  =====  ========  ====  =
=  ====  ===  ===  ========  ====  =====  =====  ========  ====  =
=  ===   ===  ===  =========  ==========  =====  ========  ===   =
=      =====  ===      =======  ========  =====      ====      ===
=  ====  ===  ===  =============  ======  =====  ========  ====  =
=  ====  ===  ===  ========  ====  =====  =====  ========  ====  =
=  ====  ===  ===  ========  ====  =====  =====  ========  ====  =
=  ====  ===  ===        ===      ======  =====        ==  ====  =
==================================================================*/


