var BD = BD || {};

BD.DraggingController = function(element, parentWidth, parentHeight)
{
	var height = element.height();
	var width = element.width();
	
	function update_dimensions()
	{
		if (!height || !width)
		{
			height = element.height();
			width = element.width();
		}
	}
	
	function on_drag(event, ui)
	{
		update_dimensions();
		if (ui.position.left + width < parentWidth)
		{
			ui.position.left = parentWidth - width;
		}
		if (ui.position.top + height < parentHeight)
		{
			ui.position.top = parentHeight - height;
		}
		
		if (ui.position.left > 0)
		{
			ui.position.left = 0;
		}
		if (ui.position.top > 0)
		{
			ui.position.top = 0;
		}
	}
	
	function on_touchstart()
	{
	}
	
	function on_touchmove()
	{
	}
	
	function on_touchend()
	{
	}
	
	// Ходжаев Тимур - отключаю меню на время перетаскивания картинки в промоблоке
	function on_stop(event, ui) {
		$('.thm_disable_menu').remove();		
	}
	function on_start(event, ui) {
		var thm_elem = $('#horizontal-multilevel-menu');
		var thm_pos = $(thm_elem).offset();
		$('body').append('<div class="thm_disable_menu" style="position:absolute;background:transparent;width:'+($(thm_elem).outerWidth()+4)+'px;height:'+($(thm_elem).outerHeight()+4)+'px;left:'+(thm_pos.left-2)+'px;top:'+(thm_pos.top-2)+'px;"></div>');
	}
	// ---------------------------------------------------------------------------
	
	element.draggable({drag: on_drag, stop: on_stop, start: on_start});
		
	element.bind('touchstart', on_touchstart);
	element.bind('touchmove', on_touchmove);
	element.bind('touchend', on_touchend);
}
