$.fn.initval = function(){
	$(this).focus(function(){
		var currentVal = $(this).val();										// Read initial field value
		$(this).val('');														// Clear initial value

		$(this).blur(function(){
			var getNewVal = $(this).val();										// Read new input value
			if ( getNewVal == '' || getNewVal == ' ' ) {
				$(this).val(currentVal);										// Switch to initial value

			}
		});
	});
	$()
};
// Define indexOf for IE

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};

(function($){
	$.fn.editinplace = function(cmd){
		this.each(function(){
			var fieldset = this;
			if ( typeof this.mode == 'undefined' )
			{

				this.fields = $(this).children('label,div.label');
				this.mode = 'display';
				this.redraw = function(){
					if ( 'edit' == this.mode ){
						$(this).children('dl').remove();
						$(this).children('a.edit').hide();
						$(this).children('div.submit').show();
						this.fields.show();
						this.fields.filter('.noedit').hide();
					}else{
						this.fields.hide();
						$(this).children('a.edit').show();
						$(this).children('div.submit').hide();
						$(this).children('dl').remove();
						var dl = $("<dl />").appendTo(this);
						this.fields.each(function(){
							if(!$(this).hasClass('noshow')){
								var field = $(this).getFieldValue();
								dl.append('<dt class="'+$(this).attr("class")+'">'+field.name+'</dt><dd class="'+$(this).attr("class")+'">'+field.value+'</dd>');
							}
						});
					}
					$("div.error, div.info", this).remove();
				};
				this.error = function(error){
					var err = $(this).data("error").join("<br />");
					var div = $('<div>').html(err).addClass('error');
					$(this).find('h3').after(div);
				}
				this.success = function(){
					var div = $('<div>').css({backgroundColor: ''}).html('Your changes were saved').addClass('info');
					$(this).find('h3').after(div);
					setTimeout(function(){
						div.fadeOut(500, function(){div.show().css({visibility: 'hidden'})});
					}, 3000);
				}

				$(this).children("a.edit").click(function(){
					$(fieldset).editinplace('edit');
					return false;
				});
				$(this).find("a.btn-cancel").click(function(){
					fieldset.reset();
					$(fieldset).editinplace('display');
					return false;
				});
				$(this).submit(function(){
					if(!$(this).data('realSubmit')){
						$.fn.editinplace.submitCallback(this);
						return false;
					}else{
						$(this).data('realSubmit',0)
						return true;
					}
				});
			}

			switch(cmd){
				case 'edit':{
					this.mode = 'edit';
					this.redraw();
					break;
				}

				case 'display':{
					this.mode = 'display';
					this.redraw();
					break;
				}

				case 'redraw': {
					this.redraw();
					break;
				}
				
				case 'error': {
					this.error();
					break;
				}
				
				case 'success': {
					this.success();
					break;
				}
			}
		});


		return this;
	};

	$.fn.getFieldValue = function(){
		var i = this.eq(0);
		var n = i.children('span:not(.input)').text().replace(/^\*\s*/,'');
		var v = '';
		if ( i.is('.eip-checkboxes') ){
			v = '';
			i.find('input:checked').each(function(){
				if ( v != '' ) v += '/';
				v+= $(this).val();
			});
		}else if ( i.is('.eip-multi') ){
			i.find('input[type=text],textarea').each(function(){
				if ( v != '' ) v += '<br />';
				v+= $(this).val();
			});
			i.find('select').each(function(){
				if ( v != '' ) v += '<br />';
				v+= this.options[this.selectedIndex].text;
			});
			if(v == 0) v = '-';
		}else if ( i.is('.date') ) {
			v = '';
			i.find('input').each(function(){
				if ( v != '' ) v += '/';
				v+= $(this).val();
			});
		}else if ( i.is('.eip-checkbox') ){
			v = "<input type='checkbox' disabled='disabled'";
			if ( i.find('input:checked').length ) v+= ' checked="checked"'; 
			v += ' />';

		}else if(i.find('select').length){
			v = i.find('select')[0].options[i.find('select')[0].selectedIndex].text;
		}else if(i.find('input') && i.find('input').is(':password')){
			v = '*******';
		}else if(i.is('.noedit')){
			v = i.html().replace(/<span(.*)\/span>/g, '');
		}else if(i.is('.eip-salary')){
			v = '&pound;'+i.find('input').val() + ' P/A';
		}else if (i.is('.eip-daily')){
			v = '&pound;'+i.find('input').val();
		}else{
			var plus = i.find('input,textarea').attr("rel");
			if(plus && plus.indexOf('#before#') == 0){
				plus = plus.substring(8);
				before = true;
			}else{
				before = false;
			}
			v = i.find('input[type=text],textarea').val();
			if(v){
				v = (plus && before ? ' '+plus : '') + v.replace(/\n/g,"<br />") + (plus && !before ? ' '+plus : '');
			}else if(plus){
				v = plus;
			}
		}
		if(!v) v = '-';
		return { name: n, value: v};
	}
})(jQuery);
