var announceWidth = 400;
var hideTimeout = 200;

var hHideTimeout = null;
$(document).ready( 
	function() {
		$(".catD_row").hover(
			function() {
                if($(this).next("div").attr('initialized') != 'yes'){
                    $(this).next("div").attr('initialized', 'yes');
                    $(this).next("div").hover(
                        function(){clearTimeout(hHideTimeout)},
                        function(){
                            if ($(this).css("display") == "block") {
                                hHideTimeout = setTimeout(function(_this){
                                    return function(){
                                        $(_this).animate({opacity: "hide"}, "fast");
                                    }
                                }(this), hideTimeout);
                            }
                        }
                    );
                }
                            
                clearTimeout(hHideTimeout);
				if($(this).next("div").css("display") != "block") {
                    $(".catD_announce").css("display", "none");
					
					/*reset accessible ways*/
					var can = {"right" : 0, "left" : 0, "bottom" : 0, "top" : 0};
					
					/*checking DIV position depends on free space around link*/
					var scrolTop = $(window).scrollTop();
					var scrolLeft = $(window).scrollLeft();

					var pos = $(this).position();
					var posX = parseInt(pos.left); var posY = parseInt(pos.top);
									
					var sizeW = $(this).width(); var sizeH = $(this).height();
					
					var winX = $(window).width() + scrolLeft; var winY = $(window).height() + scrolTop;

					var announceHeight = $(this).next("div").height();
					
					if (posY - announceHeight - 20 > scrolTop) { can["top"] = 1; }
					if (posX + sizeW + announceWidth + 20 < winX) { can["right"] = 1; }
					if (posY + sizeH + announceHeight + 20 < winY) { can["bottom"] = 1; }
					if (posX - announceWidth - 20 > scrolLeft) { can["left"] = 1; }

					/*positioning announce div*/
					for (var el in can) { 
						if (can[el] == 1) {
							if (el == 'top') { 
								$(this).next("div").css({top: posY - announceHeight - 20 + 'px' });	
								
								if (can['right']) { $(this).next("div").css({left: posX + sizeW + 'px' }); }
								else { 
									/*calcltate Ox-position of block ? to window top or to window bottom if block higer than window*/
									if (announceHeight > winX - scrolLeft) { $(this).next("div").css({top: scrolLeft + 'px'}); } 
									else { $(this).next("div").css({top: winX - announceWidth + 'px'}); }
								}
							}
							if (el == 'right') { 
								$(this).next("div").css({left: posX + sizeW + 20 + 'px'}); 
								
								if (can['bottom']) { $(this).next("div").css({top: posY + 'px'}); }
								else { 
									/*calcltate Oy-position of block ? to window top or to window bottom if block higer than window*/
									if (announceHeight > winY - scrolTop) {	$(this).next("div").css({top: scrolTop + 'px'}); } 
									else { $(this).next("div").css({top: winY - announceHeight + 'px'}); }
								}
							}
							if (el == 'bottom') { 
								$(this).next("div").css({top: posY + sizeH + 20 + 'px'}); 
								
								if (can['right']) { $(this).next("div").css({left: posX + sizeW + 'px' }); }
								else { 
									/*calcltate Ox-position of block ? to window top or to window bottom if block higer than window*/
									if (announceHeight > winX - scrolLeft) { $(this).next("div").css({top: scrolLeft + 'px'}); } 
									else { $(this).next("div").css({top: winX - announceWidth + 'px'}); }
								}
							}
							if (el == 'left') { 
								$(this).next("div").css({ left: posX - announceWidth - 20 + 'px'}); 

								if (can['bottom']) { $(this).next("div").css({top: posY + 'px'}); }
								else { 
									/*calcltate Oy-position of block ? to window top or to window bottom if block higer than window*/
									if (announceHeight > winY - scrolTop) {	$(this).next("div").css({top: scrolTop + 'px'}); } 
									else { $(this).next("div").css({top: winY - announceHeight + 'px'}); }
								}
							}
							
							break;
						}
					}

					//show announce DIV
					$(this).next("div").animate({opacity: "show"}, "fast");
				}
			},
            
            function() { 
                if ($(this).next("div").css("display") == "block") {
                    hHideTimeout = setTimeout(function(_this){
                        return function(){
                            $(_this).next("div").animate({opacity: "hide"}, "fast");
                        }
                    }(this), hideTimeout);
                }
            }
		);
		
		$(".catD_announce_text").click(
			function(){ $(this).parent("div").animate({opacity: "hide"}, "fast"); }
		);
	}
);

