var DEFAULT_SEARCH_QUERY_MESSAGE = 'What can we do for you?';

$(document).ready(function() {
    // on resize
    $(window).resize(function() {
        dockContact_Resize();
        setDockPosition();
    });
    
    $('.print_this').click(function () {
		//window.print();
		
		try {
            window.print();
        } catch(e) {
            alert('Cross-domain printing initialisation not allowed by this browser\n\nTry Firefox, Safari or Chrome');
        }

        return false;
        
    });
    
    $('#Dock_Contact').click(function() {
        $('#DockContact').fadeIn(190);
    });
    
    $('#DockContact').click(function() {
        $('#DockContact').fadeOut(190);
    });
    
    $('#LoginBackground').click(function() {
        hideLogin();
    });
    
    $('#LoginButton').click(function() {
        showLogin();
    });
    
    $('#FeedbackBackground').click(function() {
        hideFeedback();
    });
    
    $('#FeedbackButton').click(function() {
        showFeedback();
    });
    
    $('#FlashMessageWrapper').click(function() {
        $(this).fadeOut(190);
    });
    
    $('#SearchQuery').focus(function() {
        if ($(this).val() == DEFAULT_SEARCH_QUERY_MESSAGE) {
            $(this).animate({color: '#FFF'}, 150, function() {
                $(this).val('');
                $(this).css('color', '#888');
            });
        }
    });
    
    $('#SearchQuery').blur(function() {
        if ($(this).val() == '') {
            $(this).val(DEFAULT_SEARCH_QUERY_MESSAGE);
        }
    });
    
    // move the blackblock to to show the current nav-item on the left
    highlightCurrentNavLeft();
    
    // set the position of the doc
    setDockPosition();
    
    // resize the DockContact background
    dockContact_Resize();
    
    // show flash-message
    showFlashMessage();
    
    // position login form
    setLoginPosition();
    setFeedbackPosition();
});

function hideFeedback() {
    $('#FeedbackBackground').fadeOut(190);
    $('#FeedbackForeground').fadeOut(190);
    $('#FeedbackForeground').hide();
}

function hideLogin() {
    $('#LoginBackground').fadeOut(190);
    $('#LoginForeground').fadeOut(190);
    $('#LoginForeground').hide();
}

function highlightCurrentNavLeft() {
    // check if a sidebar heading is available
    if ($('.sidebar_text_heading.current').length) {
        var padding_of_group = 13;
        
        // find the top of the current div that has the current class
        var top_of_sidebar = $('.sidebar').position().top;
                
        var top_of_group = $('.sidebar_text_heading.current').offset().top + padding_of_group;
        var height_of_group = $('.sidebar_text_heading.current').height();
    
        // find the bottom of the subsequent ul element
        // NOTE: the public and private site have slightly different architectures
        // so try to figure out which architecture is being used, and calculate accordingly
        var height_of_div_items = $('.sidebar_text_heading.current').next('div.sidebar_text').height();
        var height_of_ul_items = $('.sidebar_text_heading.current').next('ul').height();
        
        var height_of_sub_items = 0;
        if (height_of_div_items != null) {
            height_of_sub_items = height_of_div_items;
        } else if (height_of_ul_items != null) {
            height_of_sub_items = height_of_ul_items;
        } else {
            padding_of_group = 0;
        }
    
        var top_of_blackblock = top_of_group - top_of_sidebar;
        var height_of_blackblock = height_of_group + height_of_sub_items + padding_of_group;
    
        $('.sidebar .colorBlock').css('margin-top', top_of_blackblock + 'px');
        $('.sidebar .colorBlock').height(height_of_blackblock);
    }
}

function setDockPosition() {
    var height_of_window = $(window).height();
    var height_of_dock = $('.dock').height();

    var top_of_dock = (height_of_window - height_of_dock) / 2;
    $('.dock').css('margin-top', top_of_dock + 'px');
}

function setFeedbackPosition() {
    $('#FeedbackBackground').height($(document).height());
    
    // horizontally center the popup
    var width_of_window = $(window).width();
    var width_of_popup = $('#Feedback').width();
    var left_of_popup = ((width_of_window / 2) - (width_of_popup / 2) - 30);
    $('#Feedback').css('margin-left', left_of_popup + 'px');
}

function setLoginPosition() {
    $('#LoginBackground').height($(document).height());
    
    // vertically centre the popup
    var height_of_window = $(window).height();
    var height_of_popup = $('#Login').height();
    var top_of_popup = ((height_of_window / 2) - (height_of_popup / 2) - 30);
    $('#Login').css('margin-top', top_of_popup + 'px');
    
    // horizontally center the popup
    var width_of_window = $(window).width();
    var width_of_popup = $('#Login').width();
    var left_of_popup = ((width_of_window / 2) - (width_of_popup / 2) - 30);
    $('#Login').css('margin-left', left_of_popup + 'px');
}

function showFeedback() {
    $('#FeedbackBackground').fadeIn(500);
    $('#FeedbackForeground').fadeIn(190);
}

function showLogin() {
    $('#LoginBackground').fadeIn(500);
    $('#LoginForeground').fadeIn(190);
}

function showFlashMessage() {
    var height_of_document = $(document).height();
    $('#FlashMessageWrapper').height(height_of_document);
    
    // vertically centre the flash message
    var height_of_window = $(window).height();
    var height_of_popup = $('#flashMessage').height();
    var top_of_popup = ((height_of_window / 2) - (height_of_popup / 2) - (25 * 2));
    $('#flashMessage').css('margin-top', top_of_popup + 'px');
    
    // horizontally centre the flash message
    var width_of_window = $('.content').width();
    var width_of_popup = $('#flashMessage').width();
    var left_of_popup = ((width_of_window / 2) - (width_of_popup / 2) - (100 * 2));
    $('#flashMessage').css('margin-left', left_of_popup + 'px');
    
    $('#FlashMessageWrapper').fadeIn(500);
}

function dockContact_Resize() {
    $('#DockContact').height($(document).height());
}
