/*
 */
var closeTimer = null;
var isOpen = false;
var heightOfHeader = null;

function closeMenu() {
	if ($.browser.msie) {
		$("#header").animate({'height':heightOfHeader}, 'fast');
		isOpen = false;	
	}

    $('#navcontainer>ul>li>ul').each(function (e) {
        if ($.browser.msie)
            $(this).hide();
        else
            $(this).fadeOut('fast');
    });

}
$(document).ready(function () {
	heightOfHeader  = parseInt($("#header").height());
	// menu
	$('#navcontainer>ul>li').each(function(e){

        $(this).hover(
            function() {
                if (closeTimer) {
					clearTimeout(closeTimer);
					closeTimer = null;
				}
            },
            function() {
                closeTimer = setTimeout('closeMenu()', 800);
            }
        );

        $(this).click(
            function() {
                $('#navcontainer>ul>li>ul').each(function (e) {
                    $(this).hide();
                    $('a').removeClass('semiActive');
                });
                
                $('a', this).addClass('semiActive');

            	if ($.browser.msie) {
					try {
						$("ul", this).show();
						var h = Math.max(heightOfHeader, parseInt($("ul", this).height()) + heightOfHeader - 50 + 5);
						$("ul", this).hide();
						if (isNaN(h))
							return;
			
						$("#header").stop().animate({'height':h}, 'fast');
					
						isOpen = true;
					} catch(error) {
					}
            	}
            
                if ($.browser.msie)
					$("ul", this).show();
				else
					$("ul", this).fadeIn('fast');
            }
        )
	});

	$('#navcontainer>ul>li>ul>li>a').each(function(){
		$(this).click(function() {
			if ($(this).attr('href') == '#')
				return false;
			$('#navcontainer a').each(function(){
			    $(this).removeClass('semiActive');
				$(this).removeClass('active');
			});
			
			$(this).addClass('active');
			var anchors = $(this).parent().parent().parent().find('a', this);
			
			$(anchors[0]).addClass('active');
			
			return true;
		});
	});
	
	// initial hide of menu
	$('#navcontainer>ul>li>ul').each(function (e) {
		$(this).hide();
	});

	// png fix plugin uses transp.gif, change its default location
//	$.ifixpng('/resources/images/ifixpng-pixel.gif');

	// mouseover for result tags
	$('div.resultTag').each(function(e){
		$(this).hover(
			function() {
				$(this).addClass('hovering');
				
			}, 
			function() {
				$(this).removeClass('hovering');
               
			}
		);
	});


	// fix click border on anchors
	$('a').each(function (e) {
		$(this).click(function(e){
			$(this).blur();
		});	
	});


	$('#submitsearch').click(function() {
		$('#searchform').trigger('submit');
		return false;
	});
	
	$('#searchform').submit(function() {
		if (($('#searchtext').val() == '')) {
			$('#searchtext').focus();
			return false;
		} else {
			return true;
		}
});
	

	// wait a bit
	setTimeout('randomTaggy()', 800);
});

function randomTaggy() {
	var i = 0;
	$('#tagcontainer a').each(function (e) {
		i++;							
		$('body').append("<div id=\"randomTaggy"+i+"\" class=\"randomTaggy\"><div class=\"tumbleWeedContainer\"><img width=\"62\" height=\"63\" src=\"resources/images/tumbleweed.gif\"/></div><div class=\"tagContainer\">"+$(this).parent().html()+"</div></div>");
		var el = $('#randomTaggy'+i);
		var h = Math.round(Math.random() * ($(window).height() - el.height() - 150 + 75));
		var w = Math.round(Math.random() * ($(window).width() -  el.width()  - 150 + 75)) ;
		el.css({ top:h, left:w});
		el.fadeTo(Math.round(Math.random() * 2200), .20);
		
		
		el.hover(
		function(e) {
			$(this).fadeTo(100, 1);
			$(this).css({zIndex:"11"});
		},
		function(e) {
			$(this).fadeTo(100, 0.2);
			$(this).css({zIndex:"1"});
		});
		
		$('.tumbleWeedContainer', el).mousedown(function(e) {
			var h = Math.round(Math.random() * ($(window).height() - el.height() - 150 + 75));
			var w = Math.round(Math.random() * ($(window).width() -  el.width()  - 150 + 75)) ;
			el.animate({ top:h, left:w}, 50 );
			/* double animation */
			var h = Math.round(Math.random() * ($(window).height() - el.height() - 150 + 75));
			var w = Math.round(Math.random() * ($(window).width() -  el.width()  - 150 + 75)) ;
			el.animate({ top:h, left:w}, 150 );
		});

		// fix click border on anchors
		$('a', el).each(function (e) {
			$(this).click(function(e){
				$(this).blur();
			});	
		});

    		// fix pngs for ie6
	   // 	$('img[@src$=.png]', el).ifixpng();
		
	});
	
	if ($.browser.msie) {
		// move tagContainer, it cannot be over the tublebee image id ie6
		//$('div.randomTaggy div.tagContainer').css({'left':'64px'});
		$('div.randomTaggy').css({'width':'250px'});
	}
}
