function click_mi(element) {
	html = $(element).html()
	id = $(element).attr('id')
	dd_id = $('#' + id).parent().parent().attr('id')

	$('#' + dd_id + ' > .ddt').html(html)
	$('#' + dd_id + ' > .ddm').hide()
	form_id = dd_id.replace('dd_', 'form_')
	period = $('#' + id + ' > .period').html()
	$('#' + dd_id + ' > #' + form_id).val(period)
}

function click_ddt(element) {
	parent_id = $(element).parent().attr('id')

	if ($('#' + parent_id + ' > .ddm').hasClass('ddm_open')) {
		new_value = 'closed'
	} else {
		new_value = 'open'
	}

	$('.ddm_open').each( function(i) {
		$(this).hide()
		$(this).removeClass('ddm_open')
	});

	if (new_value == 'closed') {
		$('#' + parent_id + ' > .ddm').removeClass('ddm_open')

		$('body').unbind('mousedown')
	} else {
		$('#' + parent_id + ' > .ddm').addClass('ddm_open')
		$('#' + parent_id + ' > .ddm').show()

/*
		$('body').bind('mousedown', {element:element}, function(e) {
			if (!$(e.target).hasClass('ddm')) {
				click_ddt(e.data.element)
			}
		});
		
		$(element).parent().children('.ddm').bind('mousedown', function(e) {
			click_mi(e)
			e.stopPropagation()
		});
*/
	}
}

$(document).ready(function(){
	//Add hover to table rows
	$('.mi').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	})

	$('.mi.first').each( function(i) {
		click_mi(this)
	});
});

/**
 Combell:
 
 function initDDM(w){
	Event.observe(document.body, 'mouseup', function(e){
		if($(Event.element(e)).className != "ddt"){
			$$('.ddm').each(function(item3){
				item3.hide();
			});
		}
	});
	
	$$('.dd').each(function(item){
		new Insertion.After(item, '<span class="ddloa" style="display:none" id="'+item.id+'l"><img src="img/default/loading.gif" alt="" /></span>');
		$$('#'+item.id+' .ddt').each(function(item2){
			Event.observe(item2, 'click', function(e){
				$$('.ddm').each(function(item3){
					temp = item3.ancestors();
					if(Element.visible(item3) && temp[0].id != item.id){
						item3.hide();
					}
				})
				$$('#'+item.id+' .ddm').each(function(item4){
					Element.toggle(item4);
				})
			})
		})
	});


	$$('.mi').each(function(item){
		Event.observe(item, 'mouseover', function(e){
			item.className='mio';
		})
		Event.observe(item, 'mouseout', function(e){
			item.className='mi';
		})
		Event.observe(item, 'click', function(e){
			anc = item.ancestors();
			$(anc[1].id+'l').show();
			if(w == 'P'){
				var c = '<div class="mi" style="width:330px;">'+item.innerHTML+'</div>';
				$$('#'+anc[1].id+' .ddt').each(function(it){ it.innerHTML = c; });
				saveProduct(item.id);
			}
			if(w == 'D'){
				thei = item.id;
				if(thei.indexOf('Transfer') != -1){
					if(confirm(lang['conf']['ctransfer']+' '+$('actdom').value+' '+lang['conf']['ctransfer_b'])){
						$('pprds').hide();
						addDomain(item.id);
					}
					else{
						$('pprds').hide();
					}
				}
				else{
					if(thei.indexOf('Trade') != -1){
						if(confirm(lang['conf']['ctrade']+' '+$('actdom').value+'.')){
							$('pprds').hide();
							addDomain(item.id);
						}
						else{
							$('pprds').hide();
						}
					}
					else{
						$('pprds').hide();
						addDomain(item.id);
					}
				}
			}
		})
	});
}
*/
