var inactivityTime=0;

$(window).click(function() {
    //console.log('click');
    inactivityTime=0;
});
$(window).mousemove(function() {
    //console.log('mouse move');
    inactivityTime = 0;
});

setInterval(function(){
    if(inactivityTime<5) {
        inactivityTime++;
    }
}, 1000);

function calcHeight() {
    if(inactivityTime>1) {
        //console.log('SKIPPED height calculation');
        return;
    }
    //console.log('height calculation');
	$('.postbitlegacy').each(function() {
		$(this).find('.clickHandler').css('height', 'inherit');
		var $prH = $(this).find('.postrow').height();
		var $plusH = $prH + 15 + 'px';
		var $pbH = $(this).find('.postbody').css('height', $plusH);
		var $bcInner = $(this).find('.postfoot').height();
		$(this).find('.clickHandler').css('height', $bcInner);
		var $pbH = $(this).find('.postbody').height();
		$(this).find('.userinfo').css('min-height', $pbH);
	});

};
function generalPopup (event) {
	
		var $thisarrowtoggle = $(this).find('.postarrow');
		
			
			var $tg = $(event.target);
			if ($tg.is('a') || $tg.is('input')) {

			} else {

				var $currl = $(this).find('.postlinking.wjs');

				if ($currl.is(":hidden")) {
					$thisarrowtoggle.toggle();
					$currl.show();
				} else {
					$thisarrowtoggle.toggle();
					$currl.hide();
				}
			}
			
};
function makeHover() {
    if (inactivityTime > 1) {
        //console.log('SKIPPED make hover');
        return;
    }
    //console.log('make hover is DISABLED');
    return;
};

$(document).ready(
		function() {
            calcHeight();
			setInterval('calcHeight()', 1000);
			//setInterval('makeHover ()', 200);
			$('.userinfo').live("click", generalPopup); 
            /*var popClick = function () {
                // $thispararrow variable is not known in this scope
                 // closures do not make this variable available in function defined eariler
                 // and anyway a new function  should be created that passes the variable to the defined function
                 // so this modification does not affect the performance practically
                var $findpopup = $(this).parents('.postbitlegacy').find('.postlinking.wjs');
                if ($findpopup.is(":hidden")) {
                    $thispararrow.toggle();
                    $findpopup.show();
                } else {
                    $thispararrow.toggle();
                    $findpopup.hide();
                }
            };*/

			$('.clickHandler').each(
	            function() {
                // finding an element only once is good idea though
                var $thispararrow = $(this).parents('.postbitlegacy').find('.postarrow');
                $(this).click(function(){
                    var $findpopup = $(this).parents('.postbitlegacy').find('.postlinking.wjs');
                    if ($findpopup.is(":hidden")) {
                        $thispararrow.toggle();
                        $findpopup.show();
                    } else {
                        $thispararrow.toggle();
                        $findpopup.hide();
                    }
                });

            });
            
			var resizeTimer = null;
			$(window).bind('resize', function() {
				if (resizeTimer)
					clearTimeout(resizeTimer);
				resizeTimer = setTimeout(calcHeight, 1000);

			});

            // Only create the function once
            /*var hoverCallback = function (event) {
                var $postBit = $(this).parents('.postbitlegacy');

                if (event.type == 'mouseenter') {
                    $postBit.find('.clickHandler').css('background-color', '#ffffff');
                    $postBit.find('.userinfo').css('background-color', '#ffffff');
                } else {
                    $postBit.find('.clickHandler').css('background-color', '#F7FBF9');
                    $postBit.find('.userinfo').css('background-color', '#F7FBF9');
                }
            };

            $('.postdetails').live('hover', hoverCallback);*/

            // does the code below re-create the function mutliple times???
            $('.postdetails').live('hover', function(event){
                var $postBit = $(this).parents('.postbitlegacy');
                if (event.type == 'mouseenter') {
                    $postBit.find('.clickHandler').css('background-color', '#ffffff');
                    $postBit.find('.userinfo').css('background-color', '#ffffff');
                } else {
                    $postBit.find('.clickHandler').css('background-color', '#F7FBF9');
                    $postBit.find('.userinfo').css('background-color', '#F7FBF9');
                }
            });
		});

