// JavaScript Document
(function( $ ){
	$.fn.mt_checkbox=function( options ){
		
		var settings = { 
			cbimgon	:	'checkbox_checked.png',
			cbimgoff	:	'checkbox.png',
			graphicspath: 	'../_javascript/niceform/images',
			inputheight:	10,
			hover:		false
		};
		
		return this.each(function() {        
			// If options exist, lets merge them
			// with our default settings
			if ( options ) { 
			  $.extend( settings, options );
			};
			// Checkboxen
			var elem=$(this).find(':checkbox');
			elem.each(function(id, el) {
				$(el).hide();
				if($(el).attr('checked'))
					$(el).after('<img src="'+settings.graphicspath+'/'+settings.cbimgon+'" id=checkimg_'+$(el).attr('id')+' />');
				else $(el).after('<img src="'+settings.graphicspath+'/'+settings.cbimgoff+'" id=checkimg_'+$(el).attr('id')+' />');
				//newcb[id]=new mycb('checkimg_'+$(el).attr('id'),$(el).attr('checked'), $(el).attr('value'), $(el).attr('id'));
				$('#checkimg_'+$(el).attr('id')).click(function() {
					if($(this).prev().attr('checked')) {
						$('#checkimg_'+$(el).attr('id')).attr('src',settings.graphicspath+'/'+settings.cbimgoff);	
					} else {
						$('#checkimg_'+$(el).attr('id')).attr('src',settings.graphicspath+'/'+settings.cbimgon);		
					}
					$(this).prev().attr('checked',!($(this).prev().attr('checked')));
				});
				$(el).bind('change',function() { 
					if($(this).attr('checked')) {
						$('#checkimg_'+$(this).attr('id')).attr('src',settings.graphicspath+'/'+settings.cbimgon);	
					} else {
						$('#checkimg_'+$(this).attr('id')).attr('src',settings.graphicspath+'/'+settings.cbimgoff);		
					}
				});
			});
			// Text- und Passwortfelder
			var elem=$(this).find(':text, :password');
			elem.each(function(id, el) {
				$(el).css({
					'border':'none',
					'background': 'none'
				});
				var org=$(el).wrap("<div>").parent();
				var tmp=org.html();
				$(el).replaceWith('<table border="0" cellspacing="0" cellpadding="0" style="display: inline;"><tr><td class="niceform_input_text_left"><img src="'+settings.graphicspath+'/textfield/bl.png" alt="" /></td><td class="niceform_input_text" background="'+settings.graphicspath+'/textfield/m.png" style="height:'+settings.inputheight+'px;">'+tmp+'</td><td class="niceform_input_text_right"><img src="'+settings.graphicspath+'/textfield/br.png" alt="" /></td>');
				$(this).parent().find('.niceform_input_text').append(tmp);
				// Hover
				if(settings.hover) {
					org.bind('focusin',function() {
						$(this).find('.niceform_input_text_left img').attr('src',	settings.graphicspath+'/textfield/bl_h.png');
						$(this).find('.niceform_input_text_right img').attr('src',	settings.graphicspath+'/textfield/br_h.png');
						$(this).find('.niceform_input_text').attr('background',settings.graphicspath+"/textfield/m_h.png");
					});
					org.bind('focusout',function() {
						$(this).parent().find('.niceform_input_text_left img').attr('src',	settings.graphicspath+'/textfield/bl.png');
						$(this).parent().find('.niceform_input_text_right img').attr('src',	settings.graphicspath+'/textfield/br.png');
						$(this).parent().find('.niceform_input_text').attr('background',settings.graphicspath+'/textfield/m.png');
					});
				} 
			});
			// Textarea
			var elem=$(this).find('textarea');
			elem.each(function(id, el) {
				$(el).css({
					'border':'0px solid transparent',
					'background-image': 'url('+settings.graphicspath+'/textarea/m.png)',
					'background-position': 'bottom',
					'background-repeat': 'repeat-x',
					'margin': '-1px'
				});
				var org=$(el).wrap("<div>").parent();
				var tmp=org.html();
				$(el).replaceWith('<table cellpadding="0" cellspacing="0" border="0"><tr><td><img src="'+settings.graphicspath+'/textarea/c1.png" alt="" /></td><td background="'+settings.graphicspath+'/textarea/bt.png"></td><td><img src="'+settings.graphicspath+'/textarea/c2.png"></td></tr><tr><td background="'+settings.graphicspath+'/textarea/bl.png"></td><td>'+tmp+'</td><td background="'+settings.graphicspath+'/textarea/br.png"></td></tr><tr><td><img src="'+settings.graphicspath+'/textarea/c3.png" alt=""></td><td background="'+settings.graphicspath+'/textarea/bb.png"></td><td><img src="'+settings.graphicspath+'/textarea/c4.png" alt=""></td></tr></table>');
			});
			// Select
			var elem=$(this).find('select');
			elem.each(function(id, el) {
				var selid=$(el).attr('id');
				$(el).hide();
				$(el).after('<table cellpadding="0" cellspacing="0" border="0"><tr><td><img src="'+settings.graphicspath+'/select/bl.png" alt="" id="fimg_'+selid+'" /></td><td  background="'+settings.graphicspath+'/select/m.png"><input type="text" style="border:none; background: none;" name="selreplace_"'+selid+' id="val_'+selid+'"></td><td><img src="'+settings.graphicspath+'/select/dd.png" id="dd_'+selid+'" alt="" style="cursor:pointer" /></td></tr></table>');
				var pos=$('#val_'+selid).position();
				var top=pos.bottom+$('#val_'+selid).height();
				$(el).parent().append('<div id="opt_'+selid+'" style="position: absolute; left:'+pos.left+'px; display: none; background-color: #fff; padding: 0px 5px; width:'+($('#val_'+selid).width()-10)+'px; cursor: pointer;"></div>');
				$('#dd_'+selid+', #val_'+selid).click(function() {
					$('#opt_'+selid).slideToggle(200);	
				});
				$('#val_'+selid).keydown(function(event) {
					if(event.which != 9) {
						event.preventDefault();
						$('#val_'+selid).trigger('click');
					}
				});
	
				var ct='';
				$(el).children('option').each(function(oid, oel) {
					ct=ct+'<div><a href="#">'+$(this).html()+'</a></div>';	
				});
				$('#opt_'+selid).html(ct);
				$('#opt_'+selid).children('div').click(function() {
					$('#val_'+selid).val($(this).children('a').html());
					$('#opt_'+selid).slideUp(200);	
					$(el).attr('selectedIndex',$(this).index());
				});
				$('#opt_'+selid).children('div:eq('+$(el).attr('selectedIndex')+')').click();
				//alert($('#val_'+selid).width()-$('#dd_'+selid).width());
				$('#val_'+selid).css('width',($('#val_'+selid).width()-$('#dd_'+selid).width()+$('#fimg_'+selid).width())+'px');
			});			
		});
	};
})(jQuery);
