﻿    $(function () {
    	$.preLoadImages('/images/menu-full-bg-hover-2.png', '/images/dropdown-bg-topfade-hover.png');
        doCufon();

        setHeight();
        $(window).bind('resize', function () {
        	setHeight();
        	setRedMenuArrow();
        });
        initMenu();
        $('a').bind('click', function () {
            $(this).blur();
        })

        $('#slide').cycle({
            fx: 'fade',
            speed: 3750,
            timeout: 10000,
            randomizeEffects: false,
            easing: 'easeOutCubic',
            next: '.slide-next',
            prev: '.slide-prev',
            pager: '#slidePager',
            cleartypeNoBg: true
        });

        initNewsTicker('.ticker div', 10000);
        setRedMenuArrow();
        centerTagLine();

    });

    function setHeight() {
        fHeight = $(window).height();
        fHeight = fHeight - 12;
        $('#wrapper-main').css({
            'min-height': fHeight
        });
           setRedMenuArrow();
    }

    function setRedMenuArrow() {
    	if ($('.menu-li-' + rootpid).offset() != null) {
    		fLeft = $('.menu-li-' + rootpid).offset().left;
    		fMainMenuLeft = $('#mainmenu').offset().left;

    		if (fLeft != undefined || fLeft > 0) {
    			fWidth = $('.menu-li-' + rootpid).width();
    			$('#menu-red-arrow').css({
    				'width': fWidth,
    				'left': fLeft,
    				'display': 'block'
    			});

    		}
    	}
    }

    function initMenu() {

        /* Add an extra li so we can add the shadow at the bottom without losing the top shadow */
        $('#nav > li > ul').each(function () {
            fLen = $(this).children().length;
            if (fLen == 1) {
                $(this).append('<li style="height: 3px"></li>');
            }
        });

		/* Set the Nav hovers */
        $('#nav > li').bind('mouseenter', function () {
        	$(this).css({
        		'background-image': 'url(/images/menu-full-bg-hover-2.png)'
        	});
        });

        $('#nav > li').bind('mouseleave', function () {
        	$(this).css({
        		'background-image': 'url(/images/menu-full-bg.png)'
        	});
        });


        /* Set the Sub Menu hovers */
        $('#nav > li > ul li').bind('mouseenter', function () {
        	$(this).css({
        		'background-image': 'url(/images/dropdown-bg-topfade-hover.png)'
        	});
        });

        $('#nav > li > ul li').bind('mouseleave', function () {
        	$(this).css({
        		'background-image': 'url(/images/dropdown-bg-topfade.png)'
        	});
        });

        /* Set the fade for the first li in the drop down */
        $('#nav > li > ul > li:first-child').unbind();
        $('#nav > li > ul > li:first-child').css({
        	'background-image': 'url(/images/dropdown-bg-topfade.png)',
        	'background-position': 'center -5px'
        });

        $('#nav > li > ul > li:first-child > ul > li:first-child').css({
        	'background-image': 'url(/images/dropdown-bg-topfade.png)',
        	'background-position': 'center -5px'
        });

        $('#nav > li > ul > li:first-child').bind('mouseenter', function () {
        	$(this).css({
        		'background-image': 'url(/images/dropdown-bg-topfade-hover.png)',
				'background-position': 'center -5px'
        	});
        });

		$('#nav > li > ul > li:first-child').bind('mouseleave', function () {
			$(this).css({
				'background-image': 'url(/images/dropdown-bg-topfade.png)',
				'background-position': 'center -5px'
			});
		});

		$('#nav > li > ul > li:first-child > ul > li:first-child').bind('mouseenter', function () {
			$(this).css({
				'background-image': 'url(/images/dropdown-bg-topfade-hover.png)',
				'background-position': 'center -5px'
			});
		});

		$('#nav > li > ul > li:first-child > ul > li:first-child').bind('mouseleave', function () {
			$(this).css({
				'background-image': 'url(/images/dropdown-bg-topfade.png)',
				'background-position': 'center -5px'
			});
		});

		/* This sets the background image to display the top shadow for the first LI of each dropdown - including sub subs  
		$('#nav > li > ul > li > ul > li:first-child').css({
		'background-position': 'center top',
		'padding-top': '5px'
		});
		*/

		/* This sets the background image to display the bottom shadow for the last LI of each dropdown - including sub subs */
		$('#nav > li > ul li:last-child').css({
			'background-position': 'center bottom',
			'padding-bottom': '4px'
		});
		

		/* This removes the line from the A in the last LI in the drop down - including sub subs */
		$('#nav > li > ul li:last-child > a').css({
			'border-bottom-width': '0px'
		});

		/* Add the little right arrow if a sub sub menu exists */
		$('#nav > li > ul > li > ul').parent().find('a:first').addClass('has-sub');
		
    	jQuery("#nav a").removeAttr('title');
        jQuery("#nav ul").css({ display: "none" }); // Opera Fix

        jQuery("#nav li").hover(function () {
            jQuery(this).find('ul:first').css({ visibility: "visible", display: "none" }).slideDown(400, "easeInOutQuart");
        }
		, function () {
		    jQuery(this).find('ul:first').css({ visibility: "hidden" });
		});

		

		/* Remove the vertical line from the last item in the menu */
		
		$('#nav > li:first-child').addClass('first');
		$('#nav > li:first-child > a').addClass('first');
		$('#nav > li:last-child').addClass('last');
		$('#nav > li:last-child > a').addClass('last');

		$('#nav > li > ul > li:last-child').addClass('last');

	}

	function doCufon() {
	/*
        Cufon.replace('#nav li a', {
            hover: true
        });
	*/	
        Cufon.replace('#footer', {
        	hover: true
        });

        Cufon.replace('.ticker div a', {
            hover: true
        });

    }

    function initNewsTicker($items_to_fade, $display_time) {

        tickerchange = setInterval(k_start_ticker, $display_time);
        var $tickeritem = jQuery($items_to_fade);
        var i = 1;

        function k_start_ticker() {
            $tickeritem.filter(":eq(" + i + ")").trigger('fade');
            i + 1 < $tickeritem.length ? i++ : i = 0;
        }

        $tickeritem.each(function (i) {
            jQuery(this).bind("fade", function () {
                $new_ticker_item = $tickeritem.filter(":eq(" + i + ")");

                if ($new_ticker_item.css("display") == "none") {
                    $tickeritem.filter(":visible").fadeOut(400, function () {
                        $new_ticker_item.fadeIn(400);
                    });
                }

            });
        });
    }

    function centerTagLine() {
    	fWidth = $('#mainmenu').width();
    	$('#tag-line').css({
    		'width': fWidth
    	});
    }
