var top_menu_obj;

function TopMenuAdditional(params){
	var params = params || {};
	params.elements = params.elements || null;
	params.container = params.container || null;
	params.numOfColumns = params.numOfColumns || 5;
	this.elements = params.elements;
	this.$elements = $(params.elements);
	this.params = params;
	var count = this.$elements.length;
	var exceed = count % params.numOfColumns;
	var numInColumn = ( exceed != 0) ? (count - exceed)/params.numOfColumns + 1 : (count - exceed)/params.numOfColumns;
	var html = '';
	for(var j = 0; j < params.numOfColumns; j++){
		html += '<td width="20%"><ul class="b-menuAdditional">';
		for(var i = 0; i < numInColumn; i++) {
            if (this.$elements.eq( j*numInColumn + i ).html()) html += '<li>' + this.$elements.eq( j*numInColumn + i ).html() + '</li>';
            
		}
		
		html += '</ul></td>';
	}
	this.$menu = $(
		'<div class="b-topMenuAdditional">' +
			'<table>' +
				'<tr>' +
					html +
				'</tr>' +
			'</table>' +
			'<div class="gradient"></div>' +
		'</div>'
	).hide();
	$(params.container).append(this.$menu);
	this.bind();
	function set_cookie(name, value, expires) {
		if (!expires) {
			expires = new Date();
		}
		
		document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/";
	}

	function save_cookie() {
		var expires = new Date(); // получаем текущую дату

		expires.setTime(expires.getTime() + (1000 * 86400)); // вычисляем срок хранения cookie
		set_cookie('mobile', 1, expires); // устанавливаем cookie с помощью функции set_cookie
	}

	$('.js-trigger-site').click(function() {
			save_cookie();
	});
}
TopMenuAdditional.prototype.bind = function(){
    top_menu_obj = this;

	$(this.params.link).mouseenter(function(){
		top_menu_obj.opened = true;
        top_menu_obj.is_inside = true;
        top_menu_obj.is_inside_menu = false;
	    top_menu_obj.show();
	});
    $(this.$menu).mouseenter(function(){
        top_menu_obj.is_inside_menu = true;
    });

    $(this.$menu).mouseleave(function(){
        top_menu_obj.is_inside_menu = false;
        setTimeout('func_hide_menu()', 1000);
    });
    $(this.params.link).mouseleave(function(){
        top_menu_obj.is_inside = false;
        setTimeout('func_hide_menu()', 1000);
    });
}

function func_hide_menu() {
    if (!top_menu_obj.is_inside && !top_menu_obj.is_inside_menu) { 
        top_menu_obj.opened = false;
        top_menu_obj.hide();
    }
}

TopMenuAdditional.prototype.show = function(){
	this.$menu.slideDown();
}
TopMenuAdditional.prototype.hide = function(){
	this.$menu.slideUp();
}
