var to_hide_interval = 300;
var to_hide_timer;
var isIE;
var isIE6;

$(document).ready(function() {
	
	isIE = ($.browser.msie && ($.browser.version == '6.0' || $.browser.version == '7.0'));
    isIE6 = ($.browser.msie && ($.browser.version == '6.0'));

	
    menu_item = $('#mainMenu .top-menu-item');
    child_menu = $('#mainMenu .popup');

    menu_item.hover(function(e) {mi_over(e, this)}, function(e) {mi_out(e, this)});
    child_menu.hover(function(e) {cm_over(e, this)}, function(e) {cm_out(e, this)});
    
    $('#mainMenu .top-menu-item > a, #mainMenu .top-menu-item > span').hover(function(e){
    		if ($(this).next('.popup:visible').length>0) {
    				clearTimeout(to_hide_timer);
    			}
    		},function(e){});
    
    
});


function mi_over(ev, el) {
	if ($('.popup',$(el)).length>0) {
		clearTimeout(to_hide_timer);
	    child_menu.hide();
	    //$('.popup',$(el)).show();
	    
	   if (isIE) {
	   		$('#boostBox').remove();
	    	$('body').append('<div id="boostBox"></div>');
	    	var offset = $(el).offset();
	    	//var offset2 =  $(el).parent().offset();
	    	$('#boostBox').css('left',offset.left+'px');
	    	$('#boostBox').css('top',(offset.top+$(el).height()-5)+'px');
	    	
	    	
	    	if (isIE6) {
	    		$('#gizmo').remove();
	    		//alert('<iframe id="gizmo" width="'+$('#boostBox div').width()+'" height="'+$('#boostBox div').height()+'" />');
	    		$('body').prepend('<iframe id="gizmo" width="10" height="10" />');
	    		$('#gizmo').css('left',(offset.left-10)+'px');
	    		$('#gizmo').css('top',(offset.top+$(el).height())+'px');
	    	
	    	}
	    	
	    	$('.popup',$(el)).clone(true).appendTo('#boostBox').show();
	    	
	    		$('#gizmo').attr('width',$('#boostBox ul').width()+'px');
	    		$('#gizmo').attr('height',$('#boostBox ul').height()+'px');
	    	
	    	
	    	
	   } else {
	   		$('.popup',$(el)).show();
	   }
	    
	}
}


function mi_out(ev, el) {
    
    to_hide_timer = setTimeout(function() {
        
        if (isIE) {
	    	$('#boostBox').remove();
	    	if (isIE6) {
	    		$('#gizmo').remove();
	    	}
	    } else {
	    	$('.popup',$(el)).hide();
	    }
    }, to_hide_interval
    );
}


function cm_over(ev, el) {
    clearTimeout(to_hide_timer);
}


function cm_out(ev, el) {
    to_hide_timer = setTimeout(function() {
        $(el).hide();
    	if (isIE6) {
	    	$('#gizmo').remove();
	    }
    }, to_hide_interval
    );
}


