(function ($) {
$(function () {
    if (typeof PIE === 'undefined')
        PIE = {attach: function () {}};
    
    var oldie = $('html').hasClass('oldie');
    var ie6 = $('html').hasClass('ie6');
    var ie7 = $('html').hasClass('ie7');
    var ie8 = $('html').hasClass('ie8');

    // ie png fix
    if (ie6)
    {
        var splash = $('nav#splash').length > 0;
        $('header#main_head img, div#navtab img, footer img, nav#splash img, div#sidebar img, img.icon')
        //.filter('img[src$=".png"]')
        .add('.pie')
        .css('-pie-png-fix','true')
        .each(function () {
            if (!splash && $(this).css('position') != 'absolute')
                $(this).css('position','relative').css('z-index','50');
            PIE.attach(this);
        });
    }
        
    // footer navigation transistions
    if (!ie6)
    {
        $('footer nav').hover(function() {
            $(this).find('a label').stop(true).animate({opacity: 1},250);
            $(this).find('a').stop(true).animate({backgroundPositionY: '15px'},500);
            $(this).find('a:hover').stop(true).animate({backgroundPositionY: '0'},500);
        },function() {
            $(this).find('a').stop(true).animate({backgroundPositionY: '45px'},500);
            $(this).find('a label').stop(true).animate({opacity: 0},500);
        });
        
        $(this).find('footer nav a label').delay(3000).animate({opacity: 0},1250);
        $(this).find('footer nav a').delay(3000).animate({backgroundPositionY: '45px'},1250);

        $('footer nav a').hover(function(){
            $(this).stop(true).animate({backgroundPositionY: '0'},300);
        },function(){
            $(this).stop(true).animate({backgroundPositionY: '15px'},300);
        });
    }
        
    // css transition replacements
    if (!$('html').hasClass('csstransitions') && !ie6)
    {
        // splash page
        if ($('nav#splash').length > 0)
        {
            $('nav#splash a img.glow').css({opacity: 0}).show();
            $('nav#splash a img.label').css({opacity: 0.5});
            $('nav#splash a').hover(function () {
                $(this).find('img.glow').stop(true).animate({opacity: 1},250);
                $(this).find('img.label').stop(true).animate({left: '-38px', opacity: 1},250,'linear');
                $(this).find('div.description').stop(true).animate({paddingTop: '15px', opacity: 1},250,'linear');
            }, function () {
                $(this).find('img.glow').stop(true).animate({opacity: 0},250);
                $(this).find('img.label').stop(true).animate({left: '-34px', opacity: 0.5},250,'linear');
                $(this).find('div.description').stop(true).animate({paddingTop: '10px', opacity: 0},250,'linear');
            });
        }
    }
    
    // sidebar navigation
    if ($('div#sidebar div.navexlist').length > 0)
    {
        function showNav (li) {
            var sublist = $(li).children('ul');
            var expander = $(li).children('img.navexpander');

            // show
            sublist.fadeIn();//.slideDown();
            
            // change expander
            expander.attr('alt','-');
            expander.attr('src','/v2/image/nav_open.png');
        }

        function hideNav (li) {
            var sublist = $(li).children('ul');
            var expander = $(li).children('img.navexpander');

            // hide
            sublist.hide();//.slideUp();
            
            // change expander
            expander.attr('alt','+');
            expander.attr('src','/v2/image/nav_closed.png');
        }

        function setNavCookie (listContainer) {
            var exDate = new Date();
            var shownIDs = [];
            $('li.navexpand ul:visible').each(function () {
                shownIDs.push($(this).attr('id'));
            });
            exDate.setTime(exDate.getTime() + 8 *24*60*60*1000);
            document.cookie = listContainer.attr('id') + '=' + shownIDs.join() + '; expires=' + exDate.toGMTString() + '; path=/';
        }

	    // sidebar navigation
	    $('div#sidebar li.navexpand').each(function () {
	        $(this).children('img.navexpander').css('cursor','pointer').click(function () {
	            if ($(this).attr('alt') == '+')
	            {
	                var parentLI = $(this).parent('li.navexpand');
	                // fpnav is special case: only one sibling open at a time
	                if ($(this).parents('div#fpnav').length > 0)
    	                hideNav(parentLI.siblings('li.navexpand'));
	                showNav(parentLI);
	            }
	            else
	            {
	                hideNav($(this).parent('li.navexpand'));
	            }
	            setNavCookie($(this).parents('div.navexlist'));
	        });
	    });
	    
	    hideNav('div#sidebar li.autoclose');
    }
    
    // supplement section
    var supplement = $('div#supplement');
    if (supplement.length > 0 && !ie6)
    {
        function resizeSupplement (sup, fast, exp) {
            var oldShow = sup.find('div.supbox:visible');
            var oldHide = sup.find('div.supbox:hidden');
            // max height
            var maxHeight = sup.height() * 3 / 5;
            sup.find('div.supbox').css('max-height', maxHeight);
            if (!!exp) exp.show();
            var lastTitle = sup.find('div.suptitle, div.supbox:visible').filter(':last');
            // collapse sections to make stuff fit
            if (lastTitle.length > 0)
            {
                var position = lastTitle.position();
                var titleTop = position.top;
                var nexthide = null;
                while (titleTop + lastTitle.outerHeight() > supplement.height() - 10)
                {
                    nexthide = sup.find('div.supbox:visible').not(nexthide).not(exp).filter(':last');
                    if (nexthide.length > 0)
                    {
                        nexthide.hide();
                    }
                    else
                    {
                        maxHeight = maxHeight - 5;
                        $(exp).css('max-height',maxHeight);
                    }
                    lastTitle = sup.find('div.suptitle, div.supbox:visible').filter(':last');
                    position = lastTitle.position();
                    titleTop = position.top;
                }
            }
            
            if (!fast)
            {
                var newShow = sup.find('div.supbox:visible');
                var newHide = sup.find('div.supbox:hidden');
                
                oldShow.show();
                oldHide.hide();
                oldHide.filter(newShow).each(function() {
                    $(this).slideDown(2 * $(this).height(), 'linear');
                });
                oldShow.filter(newHide).each(function() {
                    $(this).slideUp(2 * $(this).height(), 'linear');
                });
            }
        }
    
        resizeSupplement(supplement, true, null);
        
        //bind events
        $(window).resize(function () {
            resizeSupplement(supplement, false, null);
        });
        
        supplement.find('div.suptitle').css('cursor','pointer').click(function () {
            var toggle = $(this).next().find('div.supbox').andSelf().filter('div.supbox');
            if (toggle.is(':visible'))
            {
                toggle.slideUp(2 * toggle.height(), 'linear');
            }
            else
            {
                resizeSupplement(supplement, false, toggle);
            }
        });
    }
    
    // store pages
	$('div.shop_prod_contain').hover(
	    function() {$(this).addClass("shop_prod_hover")},
	    function()  {$(this).removeClass("shop_prod_hover")}
	);
		
	$('input.toggle').css('color','gray').focus(
	    function()
	    {
	        if ($(this).val() == this.defaultValue)
	            $(this).val('');
	        $(this).css('color','black');
	    }
	).blur(
	    function()
	    {
	        if($(this).val() == '')
	        {
	            $(this).val(this.defaultValue);
	            $(this).css('color','Gray');
	        }
	    }
	).css('color','Gray');	
});
})(jQuery);
