$(function() {
	
	var form = $('form.generated');
	
	form.each(function() {
		return false;
		var table = form.attr('id');
			
		var fields = form.find('input').add(form.find('select')).add(form.find('textarea'));
		
		fields.change(function() {
		
			$.ajax({					
				type: 'POST',
				url: BASE_DIR+'plugins/form/store.php',
				data: {
					field: $(this).attr('name'),
					value: $(this).attr('value')
				}
			});
		});
	});
	
	$('a.separator_toggle').click(function() {

		var separator = $(this).parent().next('div.separator');
		
		if(separator.is(':visible')) {
			separator.slideUp(300);
		} else {
			separator.slideDown(300);
		}
		
		return false;
	});
	
	$('form.generated').submit(function() {
		var form = $(this);
		$('div.multiple').each(function() {
			var hiddenValue = new Array();
			var div = $(this).attr('id').replace(':', '.').replace(',', '.');
			$(this).find('ul.multiple_tabs li a').each(function() {
				if(!$(this).parent().hasClass('multiple_add_element')) {
					var id = $(this).attr('id').replace(/^tab_multiple_/, '').replace(':', '.').replace(',', '.');
					var html = $(this).html().replace(':', '.').replace(',', '.');
					hiddenValue.push(id+'|'+html);
				}
			});
			form.append('<input type="hidden" name="multiples[]" value="'+(div+':'+hiddenValue.join(','))+'" />');
		});
	});
	
	$('li.multiple_add_element a').click(function() {
		
		var div = $(this).closest('div.multiple');
		var tabs = div.find('ul.multiple_tabs li.tab a');
		var a = $(this);
		
		var name = _prompt("Anna uuden välilehden nimi:", { success: function(name) {
				
			if(!name) {
				return false;
			} else {
				
				name = name.replace(/[\.<>\/\\]/g, '');
							
				var id = 'multiple_'+name.replace(/ /g, '_');
				var tabID = 'tab_'+id;
				
				var exists = false;
				tabs.each(function() {
					if($(this).html().toLowerCase() == name.toLowerCase()) exists = true;
				});
				
				if(exists || div.find('a#'+tabID).size() > 0) {
					_alert('Antamasi nimi on jo käytössä!');
					return false;
				}
				
				$tab = $('<li class="tab"><a href="#" id="'+tabID+'">'+name+'</a></li>');
				
				a.parent().before($tab);
				
				var visible_element = div.find('ul.multiple_element:visible');
				
				var new_element = visible_element.clone().attr('id', id);
				
				new_element.find('input').val('');
				visible_element.hide();
				div.find('ul.multiple_element:last').after(new_element);
				new_element.find('input.date').removeClass('hasDatepicker').datepicker($.datepicker.regional['fi']);
				
				$tab.find('a').click();
			}
				
		}});
		
		return false;
	});
	
	$('ul.multiple_tabs li a').live('click', function() {
			
		if($(this).parent().hasClass('multiple_add_element')) return; 
			
		$(this).closest('ul').find('.active').removeClass('active');
		$(this).addClass('active');
		
		var id = $(this).attr('id').replace(/^tab_/, '');
		
		$(this).closest('div.multiple').find('ul.multiple_element').hide();
		$("ul#"+id).show();
		
		return false;		
	});
	
	$('li.multiple_tools a').live('click', function() {
			
		var tool = $(this).attr('class');
		var div = $(this).closest('div.multiple');
		var ul = $(this).closest('ul.multiple_element');
		var tab = $('a#tab_'+ul.attr('id'));
		
		var tabs = div.find('ul.multiple_tabs li.tab a');
		
		switch(tool) {
			case 'multiple_rename':
				var new_name = _prompt('Anna välilehden uusi nimi:', {
					success: function(value) {
						
						if(value == false) {
							return false;
						} else {
							
							value = value.replace(/[\.<>\/\\]/g, '');
							
							var tabID = 'tab_multiple_'+value.replace(/ /g, '_');
							
							var exists = false;
							tabs.each(function() {
								if($(this).html().toLowerCase() == value.toLowerCase()) exists = true;
							});
							
							if($('a#'+tabID).size() > 0 || exists) {
								_alert('Antamasi nimi on jo käytössä!');
								return false;
							}
							tab.html(value).attr('id', tabID);
							ul.attr('id', 'multiple_'+value.replace(/ /g, '_'));
						}
					}
				});
				break;
			case 'multiple_delete':
				
				if(tabs.size() > 1) {
					if(confirm('Haluatko varmasti poistaa tämän välilehden?')) {
						tab.parent().remove();
						ul.remove();
						div.find('li.tab a:first').click();
					}
				}
				else if(tabs.size() == 1) {
					_alert('Ainoaa välilehteä ei voi poistaa');
				}
				break;
		}
		return false;
			
	});

	$('div.multiple').each(function() {
		$(this).find('ul.multiple_tabs li:first a').click();
	});
	
});

