var $j = jQuery.noConflict();

$j(document).ready(function(){
	
/* ==== Header
================================================================================================== */
	// text replacement for quotess
	$j("#quotes span").before(" ");
	
	// position quote's middle point to match logo's middle point.
	var $quoteHieght = $j("#quotes > ul > li").height();
	var quoteMiddle = 91 - $quoteHieght/2;
	$j("#quotes").css("top", quoteMiddle);
	
	/* ---- mainNav ---- */
    // add margin to 4th and 8th nav item
    $j("ul.topNav > li:eq(3)").css("margin", "0 0 0 170px");
	$j("ul.topNav > li:last").css("float", "right").css("margin-right", "50px");

    //first group subNav's bg
    $j("ul.topNav > li:first ul, ul.topNav > li:nth-child(2) ul, ul.topNav > li:nth-child(3) ul").addClass("firstGroup");

    //second group subNav's bg
    $j("ul.topNav > li:nth-child(4) ul, ul.topNav > li:nth-child(5) ul, ul.topNav > li:nth-child(6) ul, ul.topNav > li:nth-child(7) ul, ul.topNav > li:nth-child(8) ul").addClass("secondGroup");

	// safari hover hack
	if ($j.browser.safari) {
		$j("#mainNav ul a").removeAttr("title");
		$j("form table tr td select").css("text-indent", "10px");
	};
	
/* ==== Main 
================================================================================================== */
	// add class to widget
	$j("ul#widget-col > li:not(:first)").addClass("topPad");
	
	// make #cub-nav follow scroll
	$j('#cube-nav').scrollFollow({offset: 10});
	if ( $j.browser.opera ) {
		$j('#cube-nav').css("margin-top", "-244")
	}

	
	// change hexagon bg picure url depending on current page
	var siteURL = "http://" + top.location.host.toString() + "/wp/wp-content/themes/capital-wp/assets/images/flash-nav/"; 
	var $cPage = $j(".current_page_item > a").text().replace(/\s+/g, '-').toLowerCase() + ".jpg";
	$j("#flash-nav-bg > img").attr("src", siteURL + $cPage)
	
	// if hexagon bg picture is missing use default image
	 $j("#flash-nav-bg > img").error(function() { 
	 	$j(this).attr({ 
			src: siteURL + "home.jpg"
	    }); 
	 });
	 
	// download #arrow animation
	$j('#bottom-boxes a').hover(onArrowOver, onArrowOut);
	function onArrowOver() {
		$j(this).animate({marginLeft: "5px"}, 200)
	};
	function onArrowOut() {
		$j(this).animate({marginLeft: "0"}, 100)
	};
	
	// Change ul#widget-col height to match #content height + #bottom-boxes
	var contentH = $j("#content").height() // :Number
	var boxesH = $j("#bottom-boxes").height() // :Number
	$j("ul#widget-col").height(contentH + boxesH + 20)
	
	// change #cube-nav height based on #content's height
	// only if #content height is less then 600px
	if (contentH <= 600) {
		$j('#cube-nav').height(contentH)
	} 

	
	

	
	
/* ==== Footer
================================================================================================== */
	
/* ==== Misc 
================================================================================================== */
	// add a png class to all png images (ie6 fix)
	$j("img[src$=.png]").addClass("png")
	
	// cufon
	Cufon.set('fontFamily', 'JaneAusten').replace('.jane-austen', {
	    
	});
	
	
/* ==== Admin Functions
================================================================================================== */
	$j("div#mainNav > ul.adminNav > li > a:contains([A])").parent().addClass("adminLi");
	$j("div#mainNav > ul.adminNav > li.adminLi").wrapAll("<li><ul></ul></li>");
	$j("div#mainNav > ul.adminNav > li > ul > li.adminLi").parent().parent().addClass("adminMenu");
	
	$j("div#mainNav > ul.adminNav > li.adminMenu").prepend('<a href="wp-admin" title="Admin Dashboard">Admin Dashboard</a>');
	
	/* ---- Client Page Functions (old) ---- */
	$j("div#mainNav > ul.clientTopNav > li:last").css("float", "right");
	$j("div#mainNav > ul.clientTopNav > li:last").css("margin-right", "50px");
	
	/* ---- Client w/Menu Functions ---- */
	// remove entire menu except last item
	$j('ul#menu-list > li:not(:last)').remove();
	
	$j('ul#menu-list').css("margin", "11px");
	
	// add dynamic css classes
	$j('ul#menu-list > li > ul').addClass('coMenu');
	$j('ul#menu-list > li > a').addClass('coTitle');

	/* ---- Accordion Menu ---- */
	// close all sub-sub's trees
	
	$j('ul.coMenu > li ul').hide();

	// style icon
	$j('ul.coMenu li:has(ul)').addClass('expand');
	$j('li.expand > a').before('<span></span>');
	
	// slide each menu
	$j('ul.coMenu > li span').click(function() {
		$j(this).parent().find('> ul').slideToggle(300)
	});
	
	// open menu's tree
	$j('ul.coMenu > li span').toggle(expand, collapse);
	function expand() {
		$j(this).css("background-position", "0px -10px");
	};
	function collapse() {
		$j(this).css("background-position", "0px 0px");
	};
				
	// open current page's tree
	$j('ul.coMenu li.current_page_item').find('> ul').show().parent().find('> span').css("background-position", "0px -10px").toggle(collapse, expand);
	$j('ul.coMenu li li.current_page_item').parent().parent().find('> ul').show().parent().find('> span').css("background-position", "0px -10px").toggle(collapse, expand);
	$j('ul.coMenu li li.current_page_item').parent().parent().parent().parent().find('> ul').show().parent().find('> span').css("background-position", "0px -10px").toggle(collapse, expand);
	
	// add dynamic background colors
	$j('ul.coMenu > li:even').addClass('even');
	$j('ul.coMenu > li:odd, ul.coMenu > li:odd li').addClass('odd');
	
	
}); // end document ready 


	





