(function($){

    $('a.new-window, a[rel=external]').live("click", function(){
        window.open(this.href);
        return false;
    });

    $(document).ready(function () {
        $("li:last-child").addClass("last-child");
        $("li:first-child,.sidebar:first-child").addClass("first-child");
        $("#pande a").addClass("new-window");

        $("li:not(:last):after","#footer").append("<span>&gt;</span>");
        $("li:not(:last):after","#breadcrumbs").append("<span>&gt;</span>");

        $("li.current").parents("li").addClass("on");
        

        // Set this is JS since we know we can unset it on focus
        $("#p").val("Search");

        //$('body.home div.row').equalHeights();
        if ($('div.sidebar', 'div.row').length > 1) {
            $('div.row').equalHeights();
        }

        $('body.home div.col1 .sidebars').equalHeights();
        if (location.pathname == "/") {
            $('div.col1 .sidebars').equalHeights();
        }
        $('body.blog div.row div.col2').equalHeights();

        $("img[align='right']","#content").addClass("align-right");
        $("img[align='left']","#content").addClass("align-left");
        $("img[align='center']","#content").addClass("align-center");
        $("img").removeAttr("vspace");
        $("img").removeAttr("hspace");


        //$(".yellow").corner("3px");
        //DD_roundies.addRule('.yellow', '4px');
        /*
        jQuery('#mycarousel').jcarousel({
            auto: 2,
            wrap: 'last'
        });
        */

        if ( !$('.col3 .sidebar', '#container').length ) {
            $('.col3', '#container').hide(); 
            $('.col2', '#container').addClass("col2-wide"); 
        }

        // News pager
        var pageAt = 3;
        postCount = $("div.newsitem","#news_all").length;
        pageCount = Math.ceil(postCount/pageAt);
        if (postCount > pageAt){
            $("div.newsitem:gt("+(pageAt-1) +")", "#news_all").css("display", "none");
            // Add the pager
            $("#news_all").append("<div id='pager' class='clear clearfix'></div>");
            PageClick = function(pageclickednumber) {
                $("div.newsitem", "#news_all").css("display", "block");
                $("div.newsitem:gt("+((pageclickednumber*(pageAt))-1)+")", "#news_all").css("display", "none");
                $("div.newsitem:lt("+(pageclickednumber*(pageAt) - pageAt)+")", "#news_all").css("display", "none");
                $("#pager").pager({ pagenumber: pageclickednumber, pagecount: pageCount, buttonClickCallback: PageClick });
            }
            $("#pager").pager({ pagenumber: 1, pagecount: pageCount, buttonClickCallback: PageClick });
        }

        swapValues = [];
        $(".swapValue").each(function(i){
            swapValues[i] = $(this).val();
            $(this).focus(function(){
                if ($(this).val() == swapValues[i]) {
                    $(this).val("");
                }
            }).blur(function(){
                if ($.trim($(this).val()) == "") {
                    $(this).val(swapValues[i]);
                }
            });
        });

        if($.browser.msie){
            $("#topNav li").hover(
                function () {
                    $(this).addClass('sfhover');
                }, 
                function () {
                    $(this).removeClass('sfhover');
                }
            );
        }
        
    });
})(jQuery);

/*-------------------------------------------------------------------- 
 * javascript method: "pxToEm"
 * by:
   Scott Jehl (scott@filamentgroup.com) 
   Maggie Wachs (maggie@filamentgroup.com)
   http://www.filamentgroup.com
 *
 * Copyright (c) 2008 Filament Group
 * Dual licensed under the MIT (filamentgroup.com/examples/mit-license.txt) and GPL (filamentgroup.com/examples/gpl-license.txt) licenses.
 *
 * Description: Extends the native Number and String objects with pxToEm method. pxToEm converts a pixel value to ems depending on inherited font size.  
 * Article: http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/
 * Demo: http://www.filamentgroup.com/examples/pxToEm/	 	
 *							
 * Options:  	 								
 		scope: string or jQuery selector for font-size scoping
 		reverse: Boolean, true reverses the conversion to em-px
 * Dependencies: jQuery library						  
 * Usage Example: myPixelValue.pxToEm(); or myPixelValue.pxToEm({'scope':'#navigation', reverse: true});
 *
 * Version: 2.0, 08.01.2008 
 * Changelog:
 *		08.02.2007 initial Version 1.0
 *		08.01.2008 - fixed font-size calculation for IE
--------------------------------------------------------------------*/

Number.prototype.pxToEm = String.prototype.pxToEm = function(settings){
	//set defaults
	settings = jQuery.extend({
		scope: 'body',
		reverse: false
	}, settings);
	
	var pxVal = (this == '') ? 0 : parseFloat(this);
	var scopeVal;
	var getWindowWidth = function(){
		var de = document.documentElement;
		return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
	};	
	
	/* When a percentage-based font-size is set on the body, IE returns that percent of the window width as the font-size. 
		For example, if the body font-size is 62.5% and the window width is 1000px, IE will return 625px as the font-size. 	
		When this happens, we calculate the correct body font-size (%) and multiply it by 16 (the standard browser font size) 
		to get an accurate em value. */
				
	if (settings.scope == 'body' && $.browser.msie && (parseFloat($('body').css('font-size')) / getWindowWidth()).toFixed(1) > 0.0) {
		var calcFontSize = function(){		
			return (parseFloat($('body').css('font-size'))/getWindowWidth()).toFixed(3) * 16;
		};
		scopeVal = calcFontSize();
	}
	else { scopeVal = parseFloat(jQuery(settings.scope).css("font-size")); };
			
	var result = (settings.reverse == true) ? (pxVal * scopeVal).toFixed(2) + 'px' : (pxVal / scopeVal).toFixed(2) + 'em';
	return result;
};

/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element 
 		and sets their min-height to the tallest height (or width to widest width). Sets in em units 
 		by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method	(article: 
		http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)							  
 * Usage Example: $(element).equalHeights();
  		Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 2.0, 08.01.2008
--------------------------------------------------------------------*/
$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest && $(this).hasClass("na") != true) { currentTallest = $(this).height(); }
		});
		if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};

