jQuery(document).ready(function($){

	var url = 'http://'+document.domain+'/';

	$('.branch .widget:not(#navigation) nav button').click(function(event){
		var button				= $(event.target);
		var parentContainer 	= button.parent().parent().parent();
		var category 			= $(parentContainer).attr('id');
		var size				= $(parentContainer).attr('size');
		var offset				= $(parentContainer).attr('offset');
		var postCount			= $(parentContainer).attr('postcount');
		var posts_per_page		= $(parentContainer).attr('postsperpage');
				
		if(button.is('.nextPosts')){
			var nextPosts 	 		= button;
			var previousPosts 	= $(event.target).parent().parent().find('.previousPosts');			
			if(offset > 0){
				offset = parseInt(offset) - 1;
				$(parentContainer).attr('offset',offset);
				variableURL = url+'?page_id=395&category='+ category +'&offset='+ offset +'&size='+ size + '&posts_per_page=' + posts_per_page + '&postCount=' + postCount; 
				html = $.get(variableURL, function(data){
				    $('#' + category + ' .widgetBody').empty().append(innerShiv(data));				
				    equalizeWidgetHeights()
				})
				
			}
		}
		else if(button.is('.previousPosts')){
			var previousPosts = button;
			var nextPosts 		= $(event.target).parent().parent().find('.nextPosts');
			if(postCount > (parseInt(offset) + 1)){			
				offset = parseInt(offset) + 1;
				$(parentContainer).attr('offset',offset);
				variableURL = url+'?page_id=395&category='+ category +'&offset='+ offset +'&size='+ size + '&posts_per_page=' + posts_per_page + '&postCount=' + postCount;
				html = $.get(variableURL, function(data){
				    $('#' + category + ' .widgetBody').empty().append(innerShiv(data));				
				    equalizeWidgetHeights()
				})
			}
		}
		
		if(parseInt(parentContainer.attr('offset')) + 1 == parentContainer.attr('postCount')){
			previousPosts.addClass('disabled');
		}else{
			previousPosts.removeClass('disabled');
		}
		if(parseInt(parentContainer.attr('offset')) == 0){
			nextPosts.addClass('disabled');
		}else{
			nextPosts.removeClass('disabled');
		}	
		return false;
	});
	
	

	$('.branch button.nextPosts').each(function(){
		var $parentContainer = $(this).parent().parent().parent();
			 status = parseInt($parentContainer.attr('offset')) == 0 ? 'disabled' : '' ;
		$(this).addClass(status);
	});
	
	$('.branch button.previousPosts').each(function(){
		var $parentContainer = $(this).parent().parent().parent();
			 status	 			= $parentContainer.attr('offset') == $parentContainer.attr('posts_per_page') ? 'disabled' : '' ;			
			 
		$(this).addClass(status);
	});


	
	addGutterToLeftSingleColumnWidgets();
	function addGutterToLeftSingleColumnWidgets(){
		var gutterOnOddWidgets = false;
		$('.branch #siteContentGroup > div').each(function(index){
			oddEvenSearchSwitcherIfLargeWidgetsUsed(this);
			if(gutterOnOddWidgets){
				if(isOdd(index)){$(this).addClass('gutter');}
			}else{
				if(!isOdd(index)){$(this).addClass('gutter');}
			}
		});
		function oddEvenSearchSwitcherIfLargeWidgetsUsed(domElement){
			if( $(domElement).is('.large') ){
				gutterOnOddWidgets = !gutterOnOddWidgets;
			}
		}
	}
	
	
	

	function isOdd(number){ return number % 2; }
	
	$(window).load(function(){
		equalizeWidgetHeights();
	});
	
	function equalizeWidgetHeights(){
		$(".branch .gutter .widgetBody").each(function(){
			var sibling 	   	= $(this).parent().next().find(".widgetBody")
			var siblingHeight 	= $(sibling).outerHeight();
	        var thisHeight	   	= $(this).outerHeight();
	        var difference		= Math.abs(siblingHeight - thisHeight);
			
			targetChange = thisHeight > siblingHeight   ?		 $(sibling).find('.recent') :   $(this).find('.recent') ;
			targetChange.height( targetChange.outerHeight() + difference )
		});
	}
});
