﻿var $j = jQuery.noConflict();

//Runs when document is ready to be manipulated
$j(document).ready(function() {
    //code to set last link width in main nav
    //    if ($j('.main-nav').length) {
    //        var li = $j('#mainnav').children();
    //        for (var i = 0; i < li.length; i++) {
    //            var a = $j(li[i]).children().filter('a');
    //            if ($j(a).hasClass('last')) {
    //                var ulWidth = $j('#mainnav').width();
    //                var siteWidth = $j('#Wrapper').width();
    //                var difference = (siteWidth - ulWidth) - 1;
    //                var aWidth = $j(a).width();
    //                $j(a).css("width", aWidth + difference + "px");
    //                $j(a).css("text-align", "center");
    //            }
    //        }
    //    }


    //Inserts image into main level li
    var topLevel = $j('#mainnav').children().filter('li');

    $j.each(topLevel, function() {
        var liWidth = $j(this).width();
        var imgWidth = 93;
        var moveLeft = (liWidth - imgWidth) / 2;

        $j(this).append('<img src="SiteFiles/501129/Images/MainNav_RO.png" />');
        $j('img', this).css('left', moveLeft);
    });


    //Function to remove "Search" text from site search button
    if ($j('#Site-Search').length) {
        $j('.searchButton').val('');
        $j('.searchField').val('search site');
    }

    //Function to remove "Sign Up" text from newsletter sign up button
    if ($j('#Newsletter').length) {
        $j('.signupButton').val('');
        $j('.signupField').val('enter email');
    }

    //Code to grab the top nav and through it into the bottom nav section
    if ($j('#Bottom-Nav').length) {
        $j('.main-nav').clone().prependTo('#Bottom-Nav');
    }

    //IE 6 browser detection
    if ($j.browser.msie) {
        var version = $j.browser.version;

        if (version == '6.0') {
            $j('#IE6').show().stop().animate({
                top: 0
            }, 1000);

            $j('.ie6close').click(function() {
                $j('#IE6').fadeOut(500);
            });

            //Gets rid of background image flicker.
            try {
                document.execCommand('BackgroundImageCache', flase, true);
            } catch (e) { }
        }
    }

    $j('#Navigation ul').children().filter('li').hover(
        function() {
            var img = $j(this).children().filter('img');
            $j(img).fadeIn(300);
        },
        function() {
            var img = $j(this).children().filter('img');
            $j(img).fadeOut(300);
        }
    )

    //jQUery's scrollable tool initializer
    if ($j('#Home').length) {
        $j('.scrollable').scrollable({ size: 1 }).circular().autoscroll(10000);
        var api = $j('.scrollable').scrollable();

        //Handle home page banner text navigation
        $j('.pause').click(function() {
            var txt = $j(this).text();
            if (txt == 'Pause') {
                api.stop();
                $j(this).text('Play');
            } else {
                api.play();
                $j(this).text('Pause');
            }
            return false;
        });

        $j('.prev').click(function() {
            api.prev();
            return false;
        });

        $j('.next').click(function() {
            api.next();
            return false;
        });
    }

    //Changes link to registration page at bottom of memeber login screen 
    if ($j('.member-register').length) {
        $j('.member-register a').attr('href', 'member-registration-form');
    }

    //Residents dir table sorting.
    if ($j('#Resident-Dir').length) {
        $j('#Resident-Dir thead td:first').each(function(column) {
            $j(this).click(function() {
                if ($j(this).hasClass('asc')) {
                    $j(this).removeClass('asc');
                    $j(this).addClass('desc');
                    sortDir = -1;
                } else {
                    $j(this).removeClass('desc');
                    $j(this).addClass('asc');
                    sortDir = 1;
                }

                $j(this).siblings().removeClass('asc');
                $j(this).siblings().removeClass('desc');

                var rec = $j('#Resident-Dir').find('tbody tr');

                rec.sort(function(a, b) {
                    var val1 = $j(a).children('td').eq(column).text().toUpperCase();
                    var val2 = $j(b).children('td').eq(column).text().toUpperCase();
                    return (val1 < val2) ? -sortDir : (val1 > val2) ? sortDir : 0;
                });

                $j.each(rec, function(index, row) {
                    $j('#Resident-Dir tbody').append(row);
                });
            });
        });
    }
});


//Runs when document is fully loaded including images.
$j(window).load(function() {
    //Set cart items to same height
    if ($j('#Store').length) {
        var items = $j('.product-list-item');
        var maxHeight = 0;

        $j.each(items, function() {
            var itemHeight = $j(this).height();

            if (itemHeight > maxHeight) {
                maxHeight = itemHeight;
            }
        });

        $j.each(items, function() {
            $j(this).height(maxHeight);
        });
    }

    if ($j('#one').length) {
        //alert('hello');
        $j('#one a').lightBox();
    }

    if ($j('#two').length) {
        //alert('hello');
        $j('#two a').lightBox();
    }

    if ($j('#three').length) {
        //alert('hello');
        $j('#three a').lightBox();
    }

    if ($j('#four').length) {
        //alert('hello');
        $j('#four a').lightBox();
    }

    if ($j('#five').length) {
        //alert('hello');
        $j('#five a').lightBox();
    }

    if ($j('#six').length) {
        //alert('hello');
        $j('#six a').lightBox();
    }

    if ($j('#seven').length) {
        //alert('hello');
        $j('#seven a').lightBox();
    }

    if ($j('#eight').length) {
        //alert('hello');
        $j('#eight a').lightBox();
    }
});