$(document).ready(function() {

    // Local Scroll
    function filterPath(string) {
        return string
			.replace(/^\//, '')
			.replace(/(index|default).[a-zA-Z]{3,4}$/, '')
			.replace(/\/$/, '');
    }
    $('a[href*=#]').each(function() {
        if (filterPath(location.pathname) == filterPath(this.pathname)
			&& location.hostname == this.hostname
			&& this.hash.replace(/#/, '')) {
            var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) + ']');
            var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
            if ($target) {
                var targetOffset = $target.offset().top;
                $(this).click(function() {
                    $('html, body').animate({ scrollTop: targetOffset }, 400);
                    return false;
                });
            }
        }
    });

    // IE6 error message
    if ($.browser.msie) {
        if ($.browser.version == "6.0") {
            $("body:first").before('<div class="container_16 ie6error"><p>It appears that you are using an out of date browser. Please upgrade your browser to get the most of this website. We recommend <a href="http://www.getfirefox.com/" title="Firefox web browser | Faster, more secure, &amp; customizable" rel="external">Firefox</a>, <a href="http://www.apple.com/au/safari/" title="Apple - Safari - Introducing Safari 4 - See the web in a whole new way" rel="external">Safari</a>, or <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx" title="Internet Explorer 8: Home page" rel="external">Internet Explorer 8</a>.</p></div>');
        }
    }

    // Open links in a new window if rel is set to external
    $('a[rel="external"]').click(function() { window.open($(this).attr('href')); return false; });

    // Activate mNI
    $('input.mni').makeNiceInput();

    // Add class to last list item in sub menus
    $(".submenu li:last, .submenu li ul li:last").addClass("last");

    // Make entire POI areas clickable
    if ($('.poi').length > 0) {
        $('.poi').each(function() {
            $(this).click(function() { window.location = $(this).find('a').attr('href'); return false; });
        });
    }

    // Add spacing to footer banners
    if ($('#footer .campaign img').length > 1) { $('#footer .campaign img:first').css('margin-right', '20px'); }


    // Initiate scrollers
    if ($(".scroller").length > 0) {
        $(".scroller").jCarouselLite({
            auto: 7000,
            speed: 900,
            easing: "easeInOutBack",
            visible: 1,
            vertical: true,
            afterEnd: function(a) {
            }
        });
        // Adds the shadows to the rotating banner, unless IE6.
        if ($.browser.version != "6.0") {
            $($('#banner-shadows').html()).appendTo($('.scroller'));
        }
    }

    if ($(".facts img").length > 0) {
        $(".facts").jCarouselLite({
            circular: true,
            visible: 1,
            scroll: 1,
            start: 0,
            auto: 8500,
            speed: 0,
            beforeStart: function(a) {
                $(a).parent().fadeTo(1000, 0);
                var bg = $(a).find('img').attr('src');
                $('.facts').css({
                    backgroundImage: "url(" + bg + ")"
                }, 1000);
            },
            afterEnd: function(a) {
                $(a).parent().fadeTo(1000, 1);
            }
        });
    }

    $('ul.submenu li a[href=javascript:#]').addClass('header');
    if ($('ul.submenu li a.header').length) {
        $('ul.submenu').accordion({ collapsible: true, active: false });
        $('ul.submenu li a.header').each(function(index) {
            var s = '\/' + $(this).text() + '\/';
            if (window.location.pathname.toString().search(s) != -1) {
                $('ul.submenu').accordion('activate', index);
            }
        });
    }

    // Stripe tables
    if ($('table.data').length > 0) {
        $('table.data tr:odd').addClass('odd');
    }

    //nopaste on forms
    $('input.nopaste').nopaste();

    // Product Offers links
    $('.resoffer a.viewprods').click(function() {
        var whichstate = $(this).attr('href') + $(this).parent().parent().find('select').val();
        $(this).attr('href', whichstate);
        return true;
    });
    $('.busoffer a.viewprods').click(function() {
        var whichstate = $(this).attr('href') + $(this).parent().parent().find('select').val();
        $(this).attr('href', whichstate);
        return true;
    });
    //maintenance page
    if (document.location.toString().toLowerCase().indexOf("maintenance.aspx") != -1) {
        if (!$("body").hasClass("EditMode")) {
            $("input").attr("disabled", "disabled");
            $("a").click(function() {
                return false;
            });
        }
    }
});

function Ajax(url, data, onOkay) {
    $.ajax({
        cache: false,
        async: true,
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: $.toJSON(data),
        url: url,
        success: onOkay,
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        }
    });
}

