﻿$(document).ready(function() {

/* -- SERVICESPANEL -- */
    var isVisibleServices = false;
    var wantToCloseServices = false;
    var closingServices = false;
    $('.pnlServices').mouseenter(function () {
        // Only animate if hidden
            //console.log('wantToCloseServices ' + wantToCloseServices) ;
            if (isVisibleServices && wantToCloseServices)
            {
               // Stop close if entered again and we are visible
           //console.log('wantToCloseServices stopped when entered again') ;
               wantToCloseServices = false;
            }

            if (closingServices) {
              //console.log('panel is closingServices');
              // TODO: Open panel again after fully closed
              return;
            }

        if (($('.pnlServices .pnlContent').is(':hidden') && !isVisibleServices) || closingServices) {
        //console.log('opening panel');
            isVisibleServices = true;
            var pnlHeight = $('.pnlServices .pnlContent').height()
	        pnlHeight = Math.round((pnlHeight - 32)  / 2 * -1);

            //console.log(pnlHeight) ;

            $('.pnlServices .pnlContent').animate({
		        top: pnlHeight,
		        height: 'show',
                opacity: 'toggle',
		        step: $('.pnlServices .pnlContent').slideUp()
    	      },
              500,
              function() {
                //wantToCloseServices = false;
                //console.log('wantToCloseServices stopped') ;
                closePanelSearch() ;
	          }
            );
        }
    });

     $('.pnlServices').mouseleave(function () {

        if (wantToCloseServices || !isVisibleServices) return;

        // Delay this one second
        wantToCloseServices = true;
        //console.log("requesting close");
        

        window.setTimeout(closePanelServices, 500);
        //closePanel();
    });

    function closePanelServices() {
        //console.log('wantToCloseServices in close ' + wantToCloseServices);
        if (wantToCloseServices && isVisibleServices) {
            closingServices = true;
            //console.log('closingServices panel');
             $('.pnlServices .pnlContent').animate({
		        top: 36,
		        height: 'hide',
                opacity: 'toggle',
		        step: $(this).slideDown()
	          }, 500, function() 
                { 
                    wantToCloseServices = false;
                    isVisibleServices = false;
                    closingServices = false;
                  //console.log('panel closed');
                });
      }
    }

    /* -- WHO ARE WE WORKING FOR -- */
    var isVisibleWork = false;
    var wantToCloseWork = false;
    var closingWork = false;
    $('.pnlWorking').mouseenter(function () {
        // Only animate if hidden
            //console.log('wantToCloseWork ' + wantToCloseWork) ;
            if (isVisibleWork && wantToCloseWork)
            {
               // Stop close if entered again and we are visible
           //console.log('wantToCloseWork stopped when entered again') ;
               wantToCloseWork = false;
            }

            if (closingWork) {
              //console.log('panel is closingWork');
              // TODO: Open panel again after fully closed
              return;
            }

        if (($('.pnlWorking .pnlContent').is(':hidden') && !isVisibleWork) || closingWork) {
        //console.log('opening panel');
            isVisibleWork = true;
            var pnlHeight = $('.pnlWorking .pnlContent').height()
	        pnlHeight = Math.round((pnlHeight - 32)  / 2 * -1);

            //console.log(pnlHeight) ;

            $('.pnlWorking .pnlContent').animate({
		        top: pnlHeight,
		        height: 'show',
                opacity: 'toggle',
		        step: $('.pnlWorking .pnlContent').slideUp()
    	      },
              500,
              function() {
                //wantToCloseWork = false;
                //console.log('wantToCloseWork stopped') ;
                closePanelSearch() ;
	          }
            );
        }
    });

     $('.pnlWorking').mouseleave(function () {

        if (wantToCloseWork || !isVisibleWork) return;

        // Delay this one second
        wantToCloseWork = true;
        //console.log("requesting close");
        

        window.setTimeout(closePanelWorkFor, 500);
        //closePanel();
    });

    function closePanelWorkFor() {
        //console.log('wantToCloseWork in close ' + wantToCloseWork);
        if (wantToCloseWork && isVisibleWork) {
            closingWork = true;
            //console.log('closingWork panel');
             $('.pnlWorking .pnlContent').animate({
		        top: 36,
		        height: 'hide',
                opacity: 'toggle',
		        step: $(this).slideDown()
	          }, 500, function() 
                { 
                    wantToCloseWork = false;
                    isVisibleWork = false;
                    closingWork = false;
                  //console.log('panel closed');
                });
      }
    }

    /* -- SEARCH PANEL -- */
    var isVisibleSearch = false;
    var wantToCloseSearch = false;
    var closingSearch = false;
    $('.pnlSearch').mouseenter(function () {
        // Only animate if hidden
            //console.log('wantToCloseSearch ' + wantToCloseSearch) ;
            if (isVisibleSearch && wantToCloseSearch)
            {
               // Stop close if entered again and we are visible
           //console.log('wantToCloseSearch stopped when entered again') ;
               wantToCloseSearch = false;
            }

            if (closingSearch) {
              //console.log('panel is closingSearch');
              // TODO: Open panel again after fully closed
              return;
            }

        if (($('.pnlSearch .pnlContent').is(':hidden') && !isVisibleSearch) || closingSearch) {
        //console.log('opening panel');
            isVisibleSearch = true;
            var pnlHeight = $('.pnlSearch .pnlContent').height()
	        pnlHeight = Math.round((pnlHeight - 32)  / 2 * -1);

            //console.log(pnlHeight) ;

            $('.pnlSearch .pnlContent').animate({
		        top: pnlHeight,
		        height: 'show',
                opacity: 'toggle',
		        step: $('.pnlWorking .pnlContent').slideUp()
    	      },
              500,
              function() {
                //wantToCloseSearch = false;
                //console.log('wantToCloseSearch stopped') ;
	          }
            );
        }
    });
/*
     $('.pnlSearch .pnlContent').click(function () {

        if (wantToCloseSearch || !isVisibleSearch) return;

        // Delay this one second
        wantToCloseSearch = true;
        //console.log("requesting close");
        

        window.setTimeout(closePanelSearch, 500);
        //closePanel();
    });
*/
    function closePanelSearch() {
        //console.log('wantToCloseSearch in close ' + wantToCloseSearch);
            //console.log('closingSearch panel');
             $('.pnlSearch .pnlContent').animate({
		        top: 36,
		        height: 'hide',
                opacity: 'toggle',
		        step: $(this).slideDown()
	          }, 500, function() 
                { 
                    wantToCloseSearch = false;
                    isVisibleSearch = false;
                    closingSearch = false;
                  //console.log('panel closed');
                });
    }
});
