jQuery(document).ready(function() 
{
	//~ if ( !$.browser.msie || ( $.browser.msie && parseFloat($.browser.version) >= 8.0 ) )
		//~ $("input[maxlength][size]").css('width', 'auto');
		
	// handle tab activationk
	$('a[ref]').click( function(evt) { 
		evt.preventDefault();
		selectAppsTab( $(this).attr('ref') );
	});
	
	// select the first tab having an error (if any)
	if ( $('div.error-message').length > 0 && $('div.section').length > 0 )
	{
		selectAppsTab( $('div.error-message').parents('div.section').get(0).id );
		
		$('div.error-message').parents('div.section').each( function(i) {
			$('a[ref="'+$(this).attr('id')+'"]').append('*').css({'color': "darkred", 'font-weight': "bolder"});
		});
	}
	
	$('input[type="checkbox"]').click(checkReadonly);
	
	// handle fieldlists, draggable and droppable
	
	$('.fieldlist').each( function (index) {
		if ( $(this).children('dl').length > 0 )
		{
   			var ident = $(this).attr('id');
   			if ( !ident)
   			{
   				ident = 'fieldlist_' + index;
   				$(this).attr('id', ident);
   			}
   			
   			var title = $(this).attr('title');
   			var toolbar = '<a hideall="'+ident+'" class="actions small">Hide all</a>'+
   						  '<a showall="'+ident+'" class="actions small">Show all</a>';
   						  
   			// add button to show only selected inputs (whene there are some input)
   			// check for jquery version (using has() which is introduced in 1.4)
   			if ( parseFloat($.fn.jquery) >= 1.4 )
   			{
	   			if ( $(this).has('input[type="checkbox"]').length || $(this).has('input[type="text"]').length )
	   				toolbar += '<a showsel="'+ident+'" function="hide" class="actions small">Show selected</a>';
	   		}
			
			// check if mandatory
			if ( $('input[type="checkbox"][mandat]', $(this)).length  )
			{
				$('input[type="checkbox"][mandat]', $(this)).click( function(evt) {
					var mandat = $(this).attr('mandat');
					var show = $(this).is(':checked');
					$('#'+mandat).toggle(show);
					if ( !$(this).is(':checked') )
						$('input[type="checkbox"]', '#'+mandat).removeAttr('checked');
				}).each( function(i) {
					var mandat = $(this).attr('mandat');
					var show = $(this).is(':checked');
					$('#'+mandat).toggle(show);						
				});;
			}
   				
   			$(this).before('<div class="toolbar"><b>'+title+'</b> '+toolbar+'</div>');
		
			// hide all but first model fields
			$(this).children('dl').children('dd').hide();
			$(this).children('dl').children('dd:first').show();
		}
		else
		{
   			var title = $(this).attr('title');
   			$(this).before('<div class="toolbar">'+title+'</div>');
		}
	})
	
	$('a[hideall]').click(function(evt) { evt.preventDefault(); $('dl dd', '#'+$(this).attr('hideall')).hide(); return false; });
	$('a[showall]').click(function(evt) { evt.preventDefault(); $('dl dd', '#'+$(this).attr('showall')).show(); return false; });
	if ( parseFloat($.fn.jquery) >= 1.4 )
	{
		$('a[showsel]').click(function(evt) { evt.preventDefault(); 
			var ident = '#'+$(this).attr('showsel');
			$('dl dd', ident).hide();
			if ( $(ident).has('input[type="checkbox"]').length )
				$('dl dd:has(input:checked)', ident).show();
			else
				$('dl dd:has(input[value])', ident).show();
			return false; 
		});
	}
	
	// toggle fields when clicking model
	$('.fieldlist dl dt').click(function(evt) { $(this).next().toggle('slow'); });
	
	// if we have a drag source in page, set its fields as dragggable items
	if ( $('#dragsource').length > 0 )
	{
	   	$('#dragsource .field').draggable({helper: 'clone'});
	}
	
	// if we have a drop zone, set its inputs and related labels as drop able items
	if ( $('#dropzone').length > 0 )
	{
		$('#dropzone input').droppable({ accept: function(draggable) { return $(draggable).is('.field'); }, 
									 	 drop:   function(ev, ui) {$(this).val( $(ui.draggable).attr('id') ).removeClass('candrop').attr('title', $(ui.draggable).attr('id'));}, 
										 hoverClass:'candrop'})
							.attr('readonly', 'readonly');
							
		$('#dropzone label').droppable({ accept: function (draggable) { return $(draggable).is('.field'); }, 
									 	 drop: function(ev, ui) {var in_field = $(this).attr('for'); $('#'+in_field).val( $(ui.draggable).attr('id') ).removeClass('candrop');},
										 over: function(ev, ui) {var in_field = $(this).attr('for'); $('#'+in_field).addClass('candrop');},
										 out:  function(ev, ui) {var in_field = $(this).attr('for'); $('#'+in_field).removeClass('candrop');} });
	
		// handle the reset links..
		$('#dropzone a[reset]').click(function (evt) {
			evt.preventDefault(); 
			var ident = $(this).attr('reset');
			$('#'+ident).val('');
			return false;
		});
	}
	
	if ( $('.dropfield').length > 0 )
	{
		$('.dropfield').droppable({ accept: function(draggable) 
											{ 
												var allowed = $(this).attr('drop_allow');
												if ( allowed == null )
													allowed = 'field';
												return $(draggable).is('.'+allowed); 
											}, 
									drop:   function(ev, ui) 
											{ 
												var text = $(this).val();
												if ( text=='' )
													text = $(this).attr('drop_default');
												if ( text == null )
													text = '';
												
												var before= $(this).attr('drop_prepend');
												if ( before == null )
													before = '';
												
												var after = $(this).attr('drop_append');
												if ( after == null )
														after = '';
														
												var drop_value = $(this).attr('drop_value');
												if (!drop_value )
														drop_value = 'id';
												
												var droptext = ( drop_value == 'text' ? $(ui.draggable).text() : $(ui.draggable).attr(drop_value) );
												
												$(this).val(text + before+droptext+after ).removeClass('candrop');
											}, 
									hoverClass:'candrop'});
	}
	
	$('a[sel]').click( function(evt) {
		evt.preventDefault();
		$('input[type=checkbox][value*="'+$(this).attr('sel')+'"]').attr('checked', true);
		return false;
	});
	
	$('a[unsel]').click( function(evt) {
		evt.preventDefault();
		$('input[type=checkbox][value*="'+$(this).attr('unsel')+'"]').attr('checked', false);
		return false;
	});
});

function checkReadonly( evt )
{
	if ( $(this).attr('readonly') != 'undefined' && $(this).attr('readonly')  )
	{
		evt.preventDefault(); 
		return false;
	}
	return true;
}

function safeAddFloat( f1, f2 )
{
	return Math.round((f1 + f2) * 100) / 100	
}

function safeSubFloat( f1, f2 )
{
	return Math.round((f1 - f2) * 100) / 100	
}

selectAppsTab = function ( ref )
{
	$('ul.apps_steps a.active').removeClass('active');
	$('div.section').hide();
	$('a[ref="'+ref+'"]').addClass('active');
	$('#'+ref).show();
};

