$j(document).ready(function () {


    /* rotating the news on the home page */
    if ($j('#newsrotator')[0]) { $j('#newsrotator').cycle({ fx: 'fade' }); }

    /* aligning callout headers to the gray area box where applicable*/
    if ($j('.contentgrayarea').length > 0) {

        $j('#calloutArea').css("margin-top", function () {
            var offset1 = $j('.contentgrayarea').offset();
            var offset2 = $j('.gencalloutheader').first().offset();
            var myMargin = parseInt(offset1.top) - parseInt(offset2.top);
            return myMargin;
        });
    }
    


    /*styling the checkboxes on the 'contact us' form */

    $j('.ContactUsCheckBox').addClass('unchecked');
    $j('.ContactUsCheckBox').click(function () {

        if ($j(this).children("input").is(':checked')) {
            // uncheck
            $j(this).children("input").removeAttr('checked');
            $j(this).removeClass("checked");
            $j(this).addClass("unchecked");
        } else {
            // check
            $j(this).children("input").attr('checked', 'checked');
            $j(this).removeClass("unchecked");
            $j(this).addClass("checked");
        }
    });
    /*end styling the checkboxes on the 'contact us' form */
    /*default text in the 'contact us' textboxes   */
    $j('input[type="text"], textarea').each(function () {

        this.value = $j(this).attr('title');
        $j(this).addClass('text-label');

        $j(this).focus(function () {
            if (this.value == $j(this).attr('title')) {
                this.value = '';
                $j(this).removeClass('text-label');
            }
        });

        $j(this).blur(function () {
            if (this.value == '') {
                this.value = $j(this).attr('title');
                $j(this).addClass('text-label');
            }
        });
    });
    /* end default text in the 'contact us' textboxes */


    /* bug fix for fading semi-transparent PNG  in IE8 */
    var i;
    for (i in document.images) {
        if (document.images[i].src) {
            var imgSrc = document.images[i].src;
            if (imgSrc.substr(imgSrc.length - 4) === '.png' || imgSrc.substr(imgSrc.length - 4) === '.PNG') {
                document.images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + imgSrc + "')";
            }
        }
    }
    /*end bug fix for fading semi-transparent PNG  in IE8 */


    setTopSearchText();

    


    onResize();

    $j(window).resize(function () {
        onResize();
    });

    function paginate(page, notUsed) {
        var theString = '<span class="position-' + page + '"><a href="#"></a></span>';
        return theString;
    }

    function afterCall() {
        onResize();
        var spotlightItems = '.spotlight, .featured-spotlight';
        $j(spotlightItems).css('display', 'none');
        $j(this).children(spotlightItems).fadeIn(1000);

    }

    function startRotator() {
        $j('#footer').prepend('<div id="nav">');
        $j('#home-page-rotator').cycle({
            speedIn: 1500,
            speedOut: 1500,
            pager: '#nav',
            timeout: 8000,
            requeueTimeout: 2000,
            pagerAnchorBuilder: paginate,
            after: afterCall
        });

        $j('#nav, .spotlight').bind('mouseover',
		    function () {
		        $j('#home-page-rotator').cycle('pause');
		    }
	    ).bind('mouseout',
		    function () {
		        $j('#home-page-rotator').cycle('resume');
		    }
	    );

        $j('#nav').bind('click',
		    function () {
		        onResize();
		    }
	    );

    }



    //let first slide fade in
    function startIt() {
        $j('#home-page-rotator .position-1').fadeIn(1000, startRotator);
    }

    if ($j.browser.msie) {
        if ($j.browser.version < 9) {
            $j(window).load(
                 function () {
                     startIt();
                 }
            );
        } else {
            //wait to start slideshow until first image is loaded
            $j('<img />').attr('src', '/assets/ava/images/biker.jpg').load(function () {
                startIt();
            });
        }
    } else {
        //wait to start slideshow until first image is loaded
        $j('<img />').attr('src', '/assets/ava/images/biker.jpg').load(function () {
            startIt();
        });
    }

    $j('.topnavli', '#topmainnav').hover(
        function () {
            $j(this).children('span').children('a').addClass('topnavli-active');
            $j(this).children('.hovermenuouter').css({
                display: 'inline'
            });
        },
        function () {
            $j(this).children('span').children('a').removeClass('topnavli-active');
            $j(this).children('.hovermenuouter').css({
                display: 'none'
            });
        }
    );
});

function setTopSearchText() {
    $j(".searchboxtext").focus(
        function () {
            if ($j(this).val() == this.defaultValue)
                $j(this).val("");
        }
    ).blur(
        function () {
            if ($j(this).val() == "")
                $j(this).val(this.defaultValue);
        }
    );
}



function onResize() {
   
    var windowHeight = $j(window).height();
	var maxHeight = 730;
	var minHeight = 600;
	var footerheight = 70;
	
    if (windowHeight <= maxHeight && windowHeight >= minHeight) {
        var newHeight = $j(window).height() - footerheight;
        $j('.slide').css('height', newHeight);
		$j('#home-page-rotator').css('height', newHeight);
    }
    else if(windowHeight > maxHeight){
        $j('.slide').css('height', '');
        $j('#home-page-rotator').css('height', '');
    } 
}



$j(window).bind("load", function () {
    /* aligning callout headers to the gray area box where applicable*/
    if ($j('.contentgrayarea').length > 0) {
        $j('div.gencalloutheader').first().css("margin-top", function () {
            var offset1 = $j('.contentgrayarea').offset();
            var offset2 = $j('.gencalloutheader').first().offset();
            var myMargin = parseInt(offset1.top) - parseInt(offset2.top);
            return myMargin;
        });

    }
});
















