// JavaScript Document
$(document).ready(function () {

    //The default dock state is open
    var dockOpened = true;

    //Transition you want :)
    var easingType = 'swing';
    // Transition duration (in ms)
    var durationVal = 800;

    //The dock's height in case it is closed (in px)
    var dockHeight = '104';

    // the margin between the opened dock and the header (in px)
    var minDockOffset = 100;

    if ($('input.dockDown') != null && $('input.dockDown').val() == "True") {
        dockOpened = false;
    }

    function setDock() {
        var dockOffset;

        if (dockOpened) {
            dockOffset = minDockOffset;
            $('.container').css({ overflow: 'visible', height: 'auto' });

            //Hide the info button and the arrows to browse through the different background images
            $('.info').fadeOut();
            $('.arrow-left').hide();
            $('.arrow-right').hide();

            //Change state the button from up to down
            $('.switch').attr('src', '/img/switchDown.png');
            $('.switch').attr('alt', 'Click here to close');

            
        } else {
            dockOffset = $(window).height() - dockHeight;
            if (dockOffset < minDockOffset)
                dockOffset = minDockOffset;
            $('.container').css({ overflow: 'hidden', height: '100%' });

            //Show the info button and the arrows to browse through the different background images
            $('.info').fadeIn('slow');
            $('.arrow-left').show('slow');
            $('.arrow-right').show('slow');

            //Change state the button from down to up
            $('.switch').attr('src', '/img/switchUp.png');
            $('.switch').attr('alt', 'Click here to open');
        }

        $('.dock').css({ top: dockOffset });
    }

    setDock();
    $(window).resize(function () {
        setDock();
    });

    //The main event for the dock bottom menu
    $('.switch').click(function () {
        if (dockOpened) {
            initSlideDown(durationVal);
            dockOpened = false;
        } else {
            initSlideUp(durationVal);
            dockOpened = true;
        }
    });

    $('.search input[name=search], .search-column input[id$=tboxSearch], input[id$=tboxMainSearch]').unbind().keypress(function (event) {
        if (event.keyCode == 13) {
            event.stopPropagation();
            window.location = searchResultsPage + "?" + searchQueryReqParam + "=" + $(this).val();
            return false;
        }
    });

    $('input[id$=SearchSubject]').unbind().keypress(function (event) {
        if (event.keyCode == 13) {
            event.stopPropagation();
            $('#tab3 ul.form li input[id$=Search]').click();
            return false;
        }
    });

    $('li.sign-in input[id$=tboxUsername], li.sign-in input[id$=tboxLoginPass]').keypress(function (event) {
        if (event.keyCode == 13) {
            event.stopPropagation();
            $('li.sign-in input[id$=btnLogin]').click();
        }
    });

    $('.search-column input.button, .search input[name=search-button]').click(function () {
        window.location = searchResultsPage + "?" + searchQueryReqParam + "=" + $(this).siblings('input:text').val();
        return false;
    });

    function initSlideUp(durationVal) {
        $('.dock').animate({ top: minDockOffset }, { queue: false, duration: durationVal, easing: easingType,
            complete: function () {
                $('.container').css({ overflow: 'visible', height: 'auto' });
            }
        });

        //Hide the info button and the arrows to browse through the different background images
        if (durationVal > 0) {
            $('.info').fadeOut('slow');
            $('.arrow-left').hide('slow');
            $('.arrow-right').hide('slow');
        }
        else {
            $('.info').fadeOut();
            $('.arrow-left').hide();
            $('.arrow-right').hide();
        }

        //Change state the button from up to down
        $('.switch').attr('src', '/img/switchDown.png');
        $('.switch').attr('alt', 'Click here to close');
    }

    function initSlideDown(durationVal) {
        var dockOffset = $(window).height() - dockHeight;

        if (dockOffset < minDockOffset)
            dockOffset = minDockOffset;

        $('.dock').animate(
            { top: dockOffset }, 
            { 
                queue: false, 
                duration: durationVal, 
                easing: easingType,
                complete: function() {
                    // fix for IE and small height browser windows; 720 is an approximate value
                    // for min height under which we have to apply the fix.
                    if(navigator.appName == 'Microsoft Internet Explorer' && $(window).height() < 720) {
                        var offsetLeft = $('.dock').offset().left;
                        $('.dock').offset( { top: dockOffset, left: offsetLeft });
                    } 
                }
            });
        $('.container').css({ overflow: 'hidden', height: '100%' });

        //Show the info button and the arrows to browse through the different background images
        $('.info').fadeIn('slow');
        $('.arrow-left').show('slow');
        $('.arrow-right').show('slow');

        //Change state the button from down to up
        $('.switch').attr('src', '/img/switchUp.png');
        $('.switch').attr('alt', 'Click here to open');
    }
});
