/**
 * @author silktech
 */

 
 function addAttribute(id)
 {
 	var div_name = 'multi_' + id;
	counter = parseInt($('form_count_attr_'+id).value);
	
	my_div = new Element('tr', {'id': 'div_div_multi_'+id+'_'+counter});
	my_label = new Element('td').update('Νέα Τιμή');
	my_input = new Element('input', {'id':'form_multi_'+id+'_'+counter, 'type': 'text', 'name': 'form[multi]['+id+'][values][]' } );
	my_remove = new Element('a', {href: '#', 'onclick': 'remAttribute('+id+', '+counter+');'} ).update('Αφαίρεση');
	my_data = new Element('td').insert(my_input);
	my_rem = new Element('td').insert(my_remove);

	
	my_div.insert(my_label);
	my_div.insert(my_data);
	my_div.insert(my_rem);
	$(div_name).insert(my_div);
	$('form_count_attr_'+id).value = (counter +1).toString();
 }

 function remAttribute(attr_id, value_id)
 {
 	$('div_div_multi_'+attr_id+'_'+value_id).remove();
 }


function closeDebug()
{
	$('flash_debug').style.display = 'none';
}
  function addFoto()
 {
 	var div_name = 'fotos_div';
	counter = parseInt($('form_counter').value);
	
	my_div = new Element('tr', {'id': 'foto_div_'+counter});
	my_label = new Element('td').update('Τοποθεσία');
	my_input = new Element('td').insert(new Element('input', {'id':'form_fotos_'+counter, 'type': 'file', 'name': 'form[fotos][]' } ));
	my_remove = new Element('td').insert(new Element('a', {href: '#', 'onclick': 'remFoto('+counter+'); checkSubmit();'} ).update('Αφαίρεση'));
	my_desc = new Element('td').insert(new Element('textarea', {'id':'form_fotos_'+counter, 'name': 'form[descriptions][]' } ));
	my_label2 = new Element('td').update('Περιγραφή');


	my_div.insert(my_label);
	my_div.insert(my_input);
	my_div.insert(my_label2);
	my_div.insert(my_desc);
	my_div.insert(my_remove);
	$(div_name).insert(my_div);
	$('form_counter').value = (counter +1).toString();
 }
 
 function remFoto(counter)
 {
 	$('foto_div_'+counter).remove();
 	counter = parseInt($('form_counter').value);
 	counter = counter - 1;
 	$('form_counter').value = counter;
 }
 
 function changeFoto(foto_id)
 {
 	$('main_image').style.display = 'none';
 	$('main_loader').style.display = 'block';

  	$$('a.hidden_fotos').each(function relme(sm) {
 		sm.rel = 'lightbox[prod_foto]';
 	});

	$('main_image').observe('load', showImg);
 	$('main_image').src = fotos[foto_id];
 	$('zoom_link').href = fotos[foto_id];
 	$('hidden_foto_'+foto_id).rel = 'dummy';
 }
 
 function showImg() {
 	$('main_image').style.display = 'block';
 	$('main_loader').style.display = 'none';
 }
 
 function nextFoto()
 {
 	if (count > 1) // slideshow only if there are more than one pics
 	{
		if (current == (count-1)) {
			current = 0;
		}
		else {
			current = current + 1;
		}
		$('prod_foto').src = fotos[current];
	}
 }

 function prevFoto()
 {
 	if (count > 1) // slideshow only if there are more than one pics
 	{
		if (current == 0) {
			current = count - 1;
		}
		else {
			current = current - 1;
		}
		$('prod_foto').src = fotos[current];
	}
 }
 
 function changeAddress(chosen)
 {
 	if (chosen.value > 0) {
		aliasInput('remove');
		address = addresses[chosen.value];
		$('shipping_address').value = address["address"];
		$('shipping_phone').value = address["phone"];
		$('shipping_area').value = address["area"];
		$('shipping_city').value = address["city"];
		$('shipping_nomarchy_id').value = address["nomarchy_id"];
		$('shipping_postal').value = address["postal"];
	}
	else
	{
		aliasInput('add');
		$('shipping_address').value = '';
		$('shipping_phone').value = '';
		$('shipping_area').value = '';
		$('shipping_city').value = '';
		$('shipping_nomarchy_id').selectedIndex = 0;
		$('shipping_postal').value = '';
	}
 }
 
 function aliasInput(action)
 {
 	if ( action == 'add' )
	{
		tr = new Element('tr');
		my_label = new Element('td').update('Λεκτικό');
		td = new Element('td');
		my_input = new Element('input', {'id':'form_alias', 'type': 'text', 'name': 'form[alias]' } );

		td.insert(my_input);
		tr.insert(my_label);
		tr.insert(td);
		$('shipping_div').insert({top: tr});
	}
	if ( action == 'remove' )
	{
		if ( $('form_alias') )
		{
			$('form_alias').remove();
			$('alias_label').remove();
			$('alias_break').remove();
		}
	}

 }

 function checkSubmit()
 {
 	if ( $('submit_div').identify() == 'submit_div' )
 	{
	 	counter = parseInt($('form_counter').value);
	 	if ( counter > 0 )
	 	{
	 		$('submit_div').show();
	 	}

 		//alert('counter: '+counter);
	 	
	 	if ( counter == 0 )
	 	{
	 		$('submit_div').hide();
	 	}
 	}
 }
 
 
 function show_submenu(link_object, submenu)
 {
 	$$('div.top_submenu').each(function hideme(sm) {
 		sm.style.display = 'none';
 	});
 	clearTimeout(submenu_timer);
	off = Element.cumulativeOffset(link_object);
	popup_x = off[0]+8;
	popup_y = off[1]+link_object.offsetHeight;
//	alert(submenu);
	//alert('x: '+popup_x);
	//alert('y: '+popup_y);
	
	
	$(submenu).style.left = popup_x + 'px';
	$(submenu).style.top = popup_y + 'px';
	$(submenu).style.display = 'none';
	//document.body.appendChild($(submenu));
	$(submenu).style.display = 'none';
	$(submenu).style.visibility = 'hidden';
	if (popup_y - document.documentElement.scrollTop + $(submenu).getHeight() > document.documentElement.clientHeight) {
		popup_y = popup_y - (popup_y - document.documentElement.scrollTop + $(submenu).getHeight() - document.documentElement.clientHeight)
		$(submenu).style.top = popup_y+ 'px';
	}
	$(submenu).style.visibility = 'visible';
	$(submenu).style.display = 'block';
	//$(submenu).show();
	//alert($(submenu).style.display);
}
 
 
 function hide_submenu(submenu)
 {
	submenu_timer = setTimeout("clear_submenu(\""+submenu+"\");", 450);
 }
 
 function clear_submenu(submenu)
{
	$(submenu).style.display = 'none';
}

function alterNodeVisibility(node_id)
{
	submenu_name = 'submenu_' + node_id;
	if ( $(submenu_name) != null )
	{
		if ( $(submenu_name).style.display == 'none' )
		{
			$(submenu_name).style.display = 'block';
			$('menu_item_'+node_id).style.background = "white url(../public/image/folderopen.gif) 0 0 no-repeat";
		}
		else
		{
			$(submenu_name).style.display = 'none';
			$('menu_item_'+node_id).style.background = "white url(../public/image/folder.gif) 0 0 no-repeat";
		}
	}
	else
	{
		// do nothing
	}
}

function calcCheckbox(ctrl, var_id, price)
{
	if ( ctrl.value )
	{
		$('check_'+var_id).checked = true;
		$('price_'+var_id).value = price;
	}
	else
	{
		$('check_'+var_id).checked = false;
		$('price_'+var_id).value = '';
	}
}


function show_stoixeia()
{
	if ( $F('form_voucher_type') == 1 )
	{
		$('stoixeia_timologisis').style.display = 'none';
	}
	else
	{
		$('stoixeia_timologisis').style.display = '';
	}
}
