﻿
$(document).ready(function () {
    var slideShow = $('.slideShow');
    if (slideShow != null) {
        if ($("#homeSlideShow img").length > 1) {
            $("#homeSlideShow img:last").css("cursor", "pointer");
            $("#homeSlideShow img:last").click(function (e) {
                window.open("http://www.utilimaster.com/Reach/ReachMain.aspx?id=342", "NextGen");
                e.preventDefault();
            });

                setInterval(StartHomeSlideShow, 10000);
        }
        if ($(".slideShow img").length > 1) {
                setInterval(StartSlideShow, 4000);
            
        }

    }

    ExpandNavigation(812);
    MenuDropDowns();
    SearchWatermark();
});

function ExpandNavigation(width) {
    var curwidth = 0;
    var totalelements = 0;
    var remainder = 0;
    $('.mainMenu > li > a').each(function () {
        curwidth = curwidth + $(this).outerWidth();
        totalelements = totalelements + 1;
    });
    //        var correction = 4;
    //            if (navigator.userAgent.indexOf('Mozilla') >= 0) {
    //                correction = correction + 2;
    //            }
    if ((width - curwidth) > 0) {
        var pad = Math.floor(((width - curwidth) / totalelements) / 2);
        $('.mainMenu > li > a').css('padding-left', pad + 'px').css('padding-right', pad + 'px');
        $('.mainMenu > li').css('padding-left', pad + 'px').css('padding-right', pad + 'px');
        $('.subMenu > li > a').css('margin-left', '25px').css('margin-right', '25px');
        var newwidth = curwidth + (totalelements * (pad * 2));
        if (newwidth < width) {
            remainder = width - newwidth;
            $('.mainMenu > li:last').css('padding-right', (pad + remainder / 2) + 'px');
            $('.mainMenu > li:first').css('padding-left', (pad + remainder / 2) + 'px');
            $('.mainMenu > li > a:last').css('padding-right', (pad + remainder / 2) + 'px');
            $('.mainMenu > li > a:first').css('padding-left', (pad + remainder / 2) + 'px');
        }
    }

    $('.mainMenu > li').each(function () {
        var tempWidth = $(this).outerWidth();
        $(this.self + ' .subMenu').css('width', tempWidth);
    });

    
    //$('.subMenu:last').css('width', (temp - (remainder / 2)) + 'px');
    //temp = $('.subMenu:first').outerWidth();
    //$('.subMenu:first').css('width', (temp - (remainder / 2)) + 'px');

//    if (remainder > 0) {
//        var temp = $('.subMenu:last').outerWidth();
//        $('.subMenu:last').css('width', (temp - remainder) + 'px');
//    }
}

function StartCountDown() {
    var newYear = new Date(2011, 3-1, 8, 11, 15, 0);
    //newYear = new Date();
    $('#counterContain').countdown({ until: newYear,
        layout: '<div id="days" class="timer">{dn}</div>' +
                '<div id="hours" class="timer">{hn}</div>' +
                '<div id="minutes" class="timer">{mn}</div>' +
                '<div id="seconds" class="timer">{sn}</div>' });

}

function StartSlideShow() {
    var $active = $('.slideShow img.active');

    if ($active.length == 0) $active = $('.slideShow img:last');

    var $next = $active.next().length ? $active.next()
        : $('.slideShow img:first');

    $active.addClass('lastactive');

    $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1000, function () {
            $active.removeClass('active lastactive');
        });


    }

    function StartHomeSlideShow() {
        var $active = $('#homeSlideShow img.active');

        if ($active.length == 0) $active = $('#homeSlideShow img:last');

        var $next = $active.next().length ? $active.next()
        : $('#homeSlideShow img:first');

        $active.addClass('lastactive');

        $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1000, function () {
            $active.removeClass('active lastactive');
        });


    }

function SearchWatermark() {
    $('#ctl00_SearchInput').focus(function () {
        if ($('#ctl00_SearchInput').val() == $('#ctl00_SearchInput').attr('title')) {
            $('#ctl00_SearchInput').val("");
        }
    });
    $('#ctl00_SearchInput').blur(function () {
        if ($('#ctl00_SearchInput').val() == "") {
            $('#ctl00_SearchInput').val($('#ctl00_SearchInput').attr('title'));
        }
    });
}

function MenuDropDowns() {
    $('ul.mainMenu li').hover(
			function () {
			    $('ul.subMenu', this).css('display', 'block');
			    $('ul.subMenu', this).parent().children().first('a').addClass('hover');
			},
			function () {
			    $('ul.subMenu', this).css('display', 'none');
			    $('ul.subMenu', this).parent().children().first('a').removeClass('hover');
			});
}
