//
//	Wrapper function.
//
(function(){

	//
	// Intra-global variables
	//
	$ = jQuery;

	//
	//	Re-size
	//
	resizeMe();

	//
	//	Wait for window load
	//
	$(window).load(function(){

		//
		//	Remove the border-bottom property, margin & padding of the last post of class .hentry in any WordPress post output list
		//
		var lastPost = $('.hentry');
		var lastPostIndex = (lastPost.length - 1);
		
		//
		//	Only if there is no navigation below
		//
		if (($('#nav-below .nav-previous a').length === 0) && ($('#nav-below .nav-next a').length === 0)) {

			$(lastPost[lastPostIndex]).css('border-bottom', 'none');
			$(lastPost[lastPostIndex]).css('margin', 0);

			//
			//	Don't remove the padding on pages
			//
			if ($('.entry-utility').length === 0) {
				$(lastPost[lastPostIndex]).css('padding', 0);
			}
			
		};
	
		//
		//	Invoke image captions
		//
		$(".ic_container").capslide({
			caption_color	: 'white',
			caption_bgcolor	: 'black',
			overlay_bgcolor : '',
			border			: '',
			showcaption	    : false
		});
	
		//
		//	Invoke the accordian
		//
		$('.menu').initMenu();
		
		//
		//	Enhance the breadcrumb trail
		//
		var currentPageItem = $('#primary li.page_item.current_page_item').parent().closest('li');
		if ((currentPageItem).length == 0) {
			var currentPageParent = $('#primary li.page_item.current_page_parent').parent().closest('li');
		}
		var currentCatItem = $('#primary li.cat-item.current-cat').parent().closest('li');
		var currentArchItem = $('#primary li.page_item.current_page_item').parent().closest('li');
		$(currentPageItem).addClass('youarehere');
		$(currentPageParent).addClass('youarehere');
		$(currentCatItem).addClass('youarehere');
		$(currentArchItem).addClass('youarehere');

		alignToGrid();
		resizeMe();
		
	});

	//
	//	On window re-size
	//
	$(window).bind('resize', function() {
        resizeMe();
        });

	//
	//	Re-size me
	//
	function resizeMe() {

		//
		// Gather browser & available space dimensions
		//
		var fontSize = $("html").css("font-size");
		if (fontSize) {
			fontSize = parseFloat(fontSize.replace('px',''));
		}
		else {
			fontSize = 16;
		}		

		var preferredWidth = 1000;
		var displayWidth = window.innerWidth || document.documentElement.clientWidth;
		var contentWidth = fontSize * 60.0625;
		
		var preferredHeight = 600;  
		var displayHeight = window.innerHeight || document.documentElement.clientHeight;
		var contentHeight = ((fontSize * 11) * 3) + ((fontSize * 1.25) * 2) + (fontSize * 1.25);
		
		var displayRatio = displayHeight / displayWidth;
		var contentRatio = contentHeight / contentWidth;

		var percentage = displayWidth / preferredWidth;
		if( displayRatio < 1) {
			if( displayRatio < contentRatio) {
				var percentage = displayHeight / preferredHeight;
			}
		}
		
		var newFontSize = (16 * percentage);
		if( newFontSize < 12) { newFontSize = 12; }
		$("html").css("font-size", newFontSize);

		alignToGrid();

	}

	//
	//	Align everything to the grid concept
	//
	function alignToGrid() {

		//
		//	Gallery
		//
		if (false) {

			var galleries = $('.gallery-item');
	
			//
			//	Add a class to the rightmost gallery item on each row
			//		This will give the layout some necessary breathing space by removing the right margin
			//		on this rightmost gallery item.
			//
			var i = 0;
			galleries.each( function( index, element) {
				i++;
				if (i == 4) {
					$(galleries[index]).addClass( 'lastcol');
					i = 0;
				}
			});
		}

		//
		//	What's the current base font size?
		//
		agFontSize = $("#primary").css("font-size");
		
		//
		//	Wait for #primary to load
		//
		if( agFontSize) {
		
			//
			//	Base font size
			//
			// console.log('');
			agFontSize = parseFloat(agFontSize.replace('px',''));
			// console.log('agFontSize = [' + agFontSize + ']');
			
			//
			//	Blogrolls
			//
			var blogrolls = $('.thumblink.fright');

			//
			//	Box Width
			//
			agBoxWidth = $(blogrolls[0]).css('width');
			// console.log('agBoxWidth = [' + agBoxWidth + ']');
			// console.log('agBoxWidth.indexOf = [' + agBoxWidth.indexOf("em") + ']');
			if (agBoxWidth.indexOf("em", 0) > 0) {
				agBoxWidth = parseFloat(agBoxWidth.replace('em',''));
				agBoxWidth = agBoxWidth * agFontSize;
			}
			else {
				agBoxWidth = parseFloat(agMarginLeft.replace('px',''));
			}
			// console.log('agBoxWidth = [' + agBoxWidth + ']');

			//
			//	Left Margin
			//
			agMarginLeft = $(blogrolls[0]).css('margin-left');
			// console.log('agMarginLeft = [' + agMarginLeft + ']');
			// console.log('agMarginLeft.indexOf = [' + agMarginLeft.indexOf("em") + ']');
			if (agMarginLeft.indexOf("em", 0) > 0) {
				agMarginLeft = parseFloat(agMarginLeft.replace('em',''));
				agMarginLeft = agMarginLeft * agFontSize;
			}
			else {
				agMarginLeft = parseFloat(agMarginLeft.replace('px',''));
			}
			// console.log('agMarginLeft = [' + agMarginLeft + ']');
			// console.log('');
			
			// console.log('start');
			// blogrolls.each( function( index, element) {
			// 	console.log(index + " - " + $(blogrolls[index]).css('margin-left') + " - " + $(blogrolls[index]).position().left); 
			// });
			
			//
			//	Put the left margin back on all blogroll elements
			//
			blogrolls.each( function( index, element) {
				$(blogrolls[index]).css('margin-left', '1.25em'); 
			});
	
			// console.log('clean');
			// blogrolls.each( function( index, element) {
			// 	console.log(index + " - " + $(blogrolls[index]).css('margin-left') + " - " + $(blogrolls[index]).position().left); 
			// });

			//
			//	Look for first column blogroll elements and remove the left margin
			//
			blogrolls.each( function( index, element) {

				if( ($(blogrolls[index]).position().left > ((agMarginLeft + agBoxWidth))) && ($(blogrolls[index]).position().left < ((agMarginLeft + agBoxWidth) * 2))) {
					$(blogrolls[index+1]).css('margin-left', '0'); 
				}
			});

			// console.log('fixed');
			// blogrolls.each( function( index, element) {
			// 	console.log(index + " - " + $(blogrolls[index]).css('margin-left') + " - " + $(blogrolls[index]).position().left); 
			// });

		}
	}

	//
	//	Accordian
	//
	jQuery.fn.initMenu = function() {  

		return this.each(function(){
			
			var theMenu = jQuery(this).get(0);
			
			//
			//	What's the current base font size?
			//
			acFontSize = $("#primary").css("font-size");
			acFontSize = parseFloat(acFontSize.replace('px',''));
			
			//
			//	Heightening
			//
			var menuheight = {};
			var primaryheight = 13.50 * acFontSize;
			var innerpadding = 1.076923076923077 * acFontSize;
			var innermargin = 1.692307692307692 * acFontSize;
			var bottommargin = 1.538461538461538 * acFontSize;
			
			//
			//	Make an array of the heights of all the expandible
			//		<li class="widgetcontainer.. selectors so that we can use these heights in later
			//		calculations at #whichli
			//
			jQuery('.acitem', this).each(function(index){
				menuheight[index] = $('.acitem:eq('+index+')').height();
			});
			
			jQuery('.acitem', this).hide();
			jQuery('li.expand > .acitem', this).show();
			jQuery('li.expand > .acitem', this).prev().addClass('active');
			
			//
			//	Clicking has occured
			//
			jQuery('li a', this).click(function(e) {
				e.stopImmediatePropagation();
				var theElement = jQuery(this).next();
				
				var parent = this.parentNode.parentNode;
				if(jQuery(parent).hasClass('noaccordion')) {
					if(theElement[0] === undefined) {
						window.location.href = this.href;
					}
					jQuery(theElement).slideToggle('normal', function() {
						if (jQuery(this).is(':visible')) {
							jQuery(this).prev().addClass('active');
						}
						else {
							jQuery(this).prev().removeClass('active');
						}    
						alignToGrid();
					});

					return false;

				}

				else {

					if(theElement.hasClass('acitem') && theElement.is(':visible')) {
						if(jQuery(parent).hasClass('collapsible')) {
							jQuery('.acitem:visible', parent).first().slideUp('normal', function() {
								jQuery(this).prev().removeClass('active');
								$('#primary').animate({ height: ((primaryheight - innerpadding) / acFontSize) + 'em'}, 400, function() {
									alignToGrid();
								});
							});

							return false;  
						}

						return false;
					}

					if(theElement.hasClass('acitem') && !theElement.is(':visible')) {         
	
						jQuery('.acitem:visible', parent).first().slideUp('normal', function() {
							jQuery(this).prev().removeClass('active');
						});
						
						//
						//	Finding out which <li class="widgetcontainer.. <a href="#".. anchor
						//	was clicked and then messing with the animation to smoothly autosize and
						//	respect the devlingrid concept
						//
						jQuery(this).addClass('thisone');
						jQuery('li.widgetcontainer a h3').parent().each(function(index){
							
							//
							//	Is it this one?
							//
							if($(this).hasClass('thisone')) {
								
								//	#whichli
								//
								//	Ah ha! it's this one.
								//	Now we can use [index] to get the right size from the previously
								//	stored array of <li class="widgetcontainer.. selectors
								//
								$(this).removeClass('thisone');
								$(this).next('ul.acitem').slideDown('normal').addClass('active');
								
								//
								//	How many box heights does <div id="primary..
								//	(+ bottom margins) occupy?	
								//
								var boxes = Math.ceil((( primaryheight + menuheight[index] + innermargin + bottommargin ) / (primaryheight + bottommargin)));
								
								//
								//	Animate to the grid
								//
								$('#primary').animate({ height: (( (boxes * (primaryheight + bottommargin)) - bottommargin - innerpadding) / acFontSize) + 'em'}, 400, function() {
									alignToGrid();
								
								});							
	
							};
						
						});
						
						return false;
					}

				}

			});

		});
	
	};

})();

