$(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;
                });
            }
        }
    });

    try {
        if ($('#iconArea').html().length > 26) {
            $('#solution-features').show();
        }
        else {
            $('#solution-features').hide();
        }

        if ($('#iconArea').html().length == 30) {
            $('#solution-features').hide();
        }
    } catch (e) {

    }

    if ($('#ctl00_ViewMode').val() == 'Edit') {
        $('#solution-features').show();
    }




    if ($('#ctl00_CurrentSiteName').val() == 'mtdata-au') {
        $('.ukAddress').hide();
        $('.auAddress').show();
        $('#au-logins').show();
        $('#au-login').show();
        $('#au-offices').show();
        $('#uk-offices').hide();
        $('#UScopyRight').hide();
        $('#copyRight').hide();
        $('#AUcopyRight').show();


    }
    else if ($('#ctl00_CurrentSiteName').val() == 'mtdata-us') {
        $('#locations-area').find('.twocolFooter').removeClass('twocolFooter').addClass('threecolFooter');
        $('.ukAddress').show();
        $('#us-offices').show();
        $('.auAddress').hide();
        $('#uk-logins').show();
        $('#au-offices').hide();
        $('#uk-offices').hide();
        $('.usaddress').appendTo(".usaddressPlaceholder");
        $('#UScopyRight').show();
        $('#copyRight').hide();

        $('#uk-login').show();


    } else {
        $('#locations-area').find('.twocolFooter').removeClass('twocolFooter').addClass('threecolFooter');
        $('.ukAddress').show();
        $('.usAddress').hide();
        $('.auAddress').hide();
        $('#uk-logins').show();
        $('#au-offices').hide();
        $('#uk-offices').show();

        $('#uk-login').show();

    }
    // Open links in a new window if rel is set to external
    $('a[rel="external"]').click(function () { window.open($(this).attr('href')); return false; });

    // Initialises Make Nice Inputs
    $('.isMNice').makeNiceInput();

    // Office locations dropdown
    $('#local-office-dropdown a').click(function () {
        $('#local-office-list').slideToggle();
    });

    // Office locations dropdown
    $('#customer-login a').click(function () {
        $('#Secure-Logins').slideToggle();
    });

    // Office locations dropdown
    $('#Secure-Logins').mouseleave(function () {
        $('#Secure-Logins').slideToggle();
    });

    // The mouse has left the building, so we hide the menu again
    $('#local-office-list').mouseleave(function () {
        $('#local-office-list').slideToggle();
    });

    // Show locations if on the homepage
    if ($('#menu li a.active').text() == "Home") {
        $('#locations-area').show();
        $('#show-locations a').hide();
    }

    // Show locations if hidden
    $('#show-locations a').click(function () {
        if ($.browser.msie && parseInt($.browser.version) == 7) {
            $('#locations-area').show();
        } else {
            $('#locations-area').slideToggle();
        }
        $('#show-locations a').fadeOut();
        return false;
    });

    // Add a class of last to the last submenu item
    if ('#submenu') {
        $('#submenu li:last').addClass('last');
    }

    // Set the panel and wrapper width of the slider
    if ('.panel') {

        // Sets the width of the panel so when the user resizes the wrapper holds shape
        var cw = $('.coda-slider-wrapper').width();
        $('.coda-slider-wrapper').css("width", cw);

        // Set the panel width depending on the screen size
        var pw = $('.row').width();
        $('.panel').css("width", pw);

        // $(window).resize(function(){
        // 	$('.panel').each(function(){
        // 		var pw = $(this).width();
        // 		$(this).css("width",pw);
        // 	});
        // });

        $('.panel .last').each(function () {
            var wh = ((360 - $(this).height()) / 4) + 10;
            $(this).css("top", wh);
        });
    }

    // Caption script for images
    if ('img.hero') {
        if (jQuery.browser.msie && jQuery.browser.version <= 7) {	//IE7
            $('img.hero').each(function () {
                // alert('bang');
                $(this).wrap("<div class='caption'></div>");
                $(this).parent('div.caption').width($(this).attr('width'));
                var c = $(this).attr("alt");
                var w = ($(this).attr('width') - 20); // 20 allows for 10px of padding as specified in the css
                $(this).after("<span style='width: " + w + "px;'>" + c + "</span>");
            });
        } else if (jQuery.browser.msie && jQuery.browser.version == '8.0') {	//IE8
            $(window).load(function () {
                $('img.hero').each(function () {
                    var c = $(this).attr("alt");
                    $(this).css('width', '');
                    $(this).wrap("<div class='caption'></div>");
                    $(this).parent('div.caption').width();
                    var w = ($(this).attr('width') - 20); // 20 allows for 10px of padding as specified in the css
                    $(this).after("<span style='display: block; clear: both; width:" + w + "px;'>" + c + "</span>");
                });
            });
        } else {
            $('img.hero').each(function () {	// Everything else
                $(this).wrap("<div class='caption'></div>");
                var c = $(this).attr("alt");
                var w = $(this).width() - 20; // 20 allows for 10px of padding as specified in the css
                $(this).after("<span style='width: " + w + "px;'>" + c + "</span>");
            });
        }
    }

});
