function connect_cv($divski){
	//if ($('#cv-preview').length == 0) return;
	if (typeof $divski == 'undefined') $divski = $('div.cv-wrap').parents('form');
	else $('a', $divski).show();
	
	if ($divski.length == 0) return;
	
	if ($('div.hide').length && $('.error', 'div.hide').length) {
		$('div.hide').removeClass('hide');
	}
	
	$('a.add-another', $divski).unbind('click').bind('click', function(){
		var $ul = $(this).parent().parent();
		var $prev = $ul.prev();
		
		if ($prev.find('div.hide').length) {
			connect.hideit = true;
			$prev.find('div.hide').removeClass('hide');
			return false;
		}
		
		var $div = $prev.clone();
		connect_tips($div);
		
		$('*', $div).clearFields();
		$('input.ac_input').removeClass('ac_input');
		$('a', $div).each(function(){
			if (/work-([0-9]+)-status/.test(this.id)) {
				$(this).parents('div.form-wrap').css('display', 'none');
			}
		});
		$('a', $div).show();
		
		var len = $(this).parents('div.box').find('div.cv-wrap').length;
		
		connect_really_renumber($div,len)
		connect_cv($div);
		$div.insertBefore($ul);
		connect_autocompleters();
		//connect_checkboxes();
		
		if ($.browser.msie) setTimeout(connect_renumber_cv_divs, 200);
		else connect_renumber_cv_divs();
		return false;
	});
	
	$('a.remove-link', $divski).unbind('click').bind('click', function(){
		var $prev = $(this).parents('div.cv-wrap');
		var len = $(this).parents('div.box').find('div.cv-wrap').length;
		
		if (len > 1) $prev.remove();
		else if (connect.hideit) $prev.find('>div:first').addClass('hide');
		else $('*', $prev).clearFields();
		
		if ($.browser.msie) setTimeout(connect_renumber_cv_divs, 200);
		else connect_renumber_cv_divs();
		
		return false;
	});
}

function connect_renumber_cv_divs(){
	$('div.box-content').each(function(i,box) {
		$('div.cv-wrap', this).each(function(j,div){
			connect_really_renumber($(this),j);
		});
	});
}

function connect_really_renumber($div,i){
	$div.find('label').each(function() {
		var $this = $(this), e, e1, e2;
		if (e=$this.attr('for')) {
			e1=e.substr(0,6);
			e2=e.substr(6);
			$this.attr('for', e1+e2.replace(/[0-9]+/g,i));
		}
	})
	$div.find('input,textarea,select').each(function() {
		var $this = $(this), e, e1, e2;
		if (e = $this.attr('id')) {
			e1=e.substr(0,6);
			e2=e.substr(6);
			$this.attr('id', e1+e2.replace(/[0-9]+/g,i));
			e=false;
		}
		if (e = $this.attr('name')) {
			e1=e.substr(0,6);
			e2=e.substr(6);
			$this.attr('name', e1+e2.replace(/[0-9]+/g,i));
		}
	})
	return $div;
}

// stolen from jform plugin
jQuery.fn.clearFields=jQuery.fn.clearInputs=function(){
return this.each(function(){
var t=this.type,tag=this.tagName.toLowerCase();
if(t=="text"||t=="password"||tag=="textarea"){
if (!$(this).attr('readonly')) this.value="";
}else{
if(t=="checkbox"||t=="radio"){
this.checked=false;
}else{
if(tag=="select"){this.selectedIndex=-1;}}
}});};

//init
connect.load_events.push(connect_cv);
