/*
 * jQuery Rebrush Fileupload plugin 0.1 Beta
 *
 * Copyright (c) 2009 Marc Bonsels
 *
 */

(function(jQuery){
	jQuery.rebrushfileupload = {
		defaults: {
			button_text: '...',
			class_container: 'fileupload-form-file',
			class_field: 'fileupload-form-file-field',
			class_button: 'fileupload-form-file-button'
		}
	};
	
	jQuery.fn.extend({
		rebrushfileupload: function(settings) {
			// Check if user gave us some settings
			settings = jQuery.extend({}, jQuery.rebrushfileupload.defaults, settings);
			
			// Which attributes should be added to the width of the
			// created elements?
			var attributes = ["padding-left", "padding-right", "margin-left", "margin-right", "border-left-width", "border-right-width"];
	
			// Wrap the fileuploadfield with a div for positioning
			jQuery(this).wrap('<div class="form-file-container ' + settings['class_container'] + '" />');
			var obj_container_div = jQuery(this).parent();
			
			// Create the new, stylable elements within the new div		
			obj_container_div.prepend('<input type="text" value="" readonly="readonly" class="rebrush-field ' + settings['class_field'] + '" /><input type="button" value="' + settings['button_text'] + '" class="rebrush-button ' + settings['class_button'] + '" />');
			var obj_rebrush_field = obj_container_div.find("input.rebrush-field");
			var obj_rebrush_button = obj_container_div.find("input.rebrush-button");
			
			// Calculate the real needed width of the container div for positioning
			var wrapperwidth = 0;
			for (var attr in attributes) {
				var val_field = Math.round(parseFloat(obj_rebrush_field.css(attributes[attr]) + 0)) + 0;
				var val_button = Math.round(parseFloat(obj_rebrush_button.css(attributes[attr]) + 0)) + 0;
				wrapperwidth += (isNaN(val_field) ? 0 : val_field) + (isNaN(val_button) ? 0 : val_button);
			}

			wrapperwidth += Math.round(parseFloat(obj_rebrush_field.width())) + Math.round(parseFloat(obj_rebrush_button.width()));
	
			// Give the containerdiv the calculated width and hide the original fileupload-field
			obj_container_div.css('width', wrapperwidth);
			
			jQuery(this).css({
				'position': 'absolute',
				'z-index': 2,
				'font-size': '120px',
				'opacity': '0',
				'right': '0px',
				'top': '0px'
			});

			// Bind the changeevent on the original field and copy its value
			// to the new textfield (which is readonly of course ;-)) everytime
			// the original field is changed
			jQuery(this).change(function() {
				jQuery(this).parent().find("input[type=text].rebrush-field").val(jQuery(this).val());

				if(jQuery(this).attr("id")=='edit-zeugnisse2'){
					jQuery('#webform-component-zeugnisse').delay(1000).fadeIn(1000);
					jQuery('#webform-component-zeugnisse2').addClass('fileremove');
					window.parent.jQuery.colorbox.resize({width:"630px",height:jQuery(document).height() + 13});
					window.parent.jQuery("#colorbox").colorbox.resize({width:"630px",height:jQuery(document).height() + 13});					
				}

				if(jQuery(this).attr("id")=='edit-zeugnisse3'){
					jQuery('#webform-component-zeugnisse2').delay(1000).fadeIn(1000);
					jQuery('#webform-component-zeugnisse3').addClass('fileremove');
					window.parent.jQuery.colorbox.resize({width:"630px",height:jQuery(document).height() + 13});
					window.parent.jQuery("#colorbox").colorbox.resize({width:"630px",height:jQuery(document).height() + 13});					
				}
				
				if(jQuery(this).attr("id")=='edit-zeugnisse4'){
					jQuery('#webform-component-zeugnisse3').delay(1000).fadeIn(1000);
					jQuery('#webform-component-zeugnisse4').addClass('fileremove');
					window.parent.jQuery.colorbox.resize({width:"630px",height:jQuery(document).height() + 13});
					window.parent.jQuery("#colorbox").colorbox.resize({width:"630px",height:jQuery(document).height() + 13});					
				}
				
				if(jQuery(this).attr("id")=='edit-zeugnisse5'){
					jQuery('#webform-component-zeugnisse4').delay(1000).fadeIn(1000);
					jQuery('#webform-component-zeugnisse5').addClass('fileremove');
					window.parent.jQuery.colorbox.resize({width:"630px",height:jQuery(document).height() + 13});
					window.parent.jQuery("#colorbox").colorbox.resize({width:"630px",height:jQuery(document).height() + 13});					
				}
				
				if(jQuery(this).attr("id")=='edit-zeugnisse6'){
					jQuery('#webform-component-zeugnisse5').delay(1000).fadeIn(1000);
					jQuery('#webform-component-zeugnisse6').addClass('fileremove');
					window.parent.jQuery.colorbox.resize({width:"630px",height:jQuery(document).height() + 13});
					window.parent.jQuery("#colorbox").colorbox.resize({width:"630px",height:jQuery(document).height() + 13});					
				}
				
				if(jQuery(this).attr("id")=='edit-zeugnisse7'){
					jQuery('#webform-component-zeugnisse6').delay(1000).fadeIn(1000);
					jQuery('#webform-component-zeugnisse7').addClass('fileremove');
					window.parent.jQuery.colorbox.resize({width:"630px",height:jQuery(document).height() + 13});
					window.parent.jQuery("#colorbox").colorbox.resize({width:"630px",height:jQuery(document).height() + 13});					
				}																

				if(jQuery(this).attr("id")=='edit-zeugnisse8'){
					jQuery('#webform-component-zeugnisse7').delay(1000).fadeIn(1000);
					jQuery('#webform-component-zeugnisse8').addClass('fileremove');
					window.parent.jQuery.colorbox.resize({width:"630px",height:jQuery(document).height() + 13});
					window.parent.jQuery("#colorbox").colorbox.resize({width:"630px",height:jQuery(document).height() + 13});					
				}				
				
				if(jQuery(this).attr("id")=='edit-zeugnisse9'){
					jQuery('#webform-component-zeugnisse8').delay(1000).fadeIn(1000);
					jQuery('#webform-component-zeugnisse9').addClass('fileremove');
					window.parent.jQuery.colorbox.resize({width:"630px",height:jQuery(document).height() + 13});
					window.parent.jQuery("#colorbox").colorbox.resize({width:"630px",height:jQuery(document).height() + 13});					
//					jQuery('#webform-component-zeugnisse9').before('<div id="webform-component-zeugnisse11" class="form-item webform-component webform-component-file"><label for="edit-submitted-zeugnisse11">zeugnisse11 </label><div class="form-file-container fileupload-form-file" style="width: 178px;"><input type="text" class="rebrush-field fileupload-form-file-field" readonly="readonly" value=""><input type="button" class="rebrush-button fileupload-form-file-button" value="..."><input type="file" class="form-file" size="60" name="files[zeugnisse11]" id="edit-zeugnisse11" style="position: absolute; z-index: 2; font-size: 120px; opacity: 0; right: 0px; top: 0px;"></div><input type="hidden" value="" name="submitted[zeugnisse11][_fid]"><input type="hidden" name="submitted[zeugnisse11][_old]"></div>');
				}
				
				if(jQuery(this).attr("id")=='edit-zeugnisse'){
					jQuery('#webform-component-zeugnisse').addClass('fileremove');
				}			
			});
		}
	});
})(jQuery);
