jQuery.connectual = [];

//jQuery(function(){ jQuery.panther.debug=true; });

jQuery.connectual.blogSearch = function() {
	jQuery('#cse-search-box2').submit(function() {
		jQuery('#cse-search-box2 [name=q]').val('site:connectual.com/blog '+jQuery('#cse-search-box2 [name=val]').val());
		jQuery('#cse-search-box2 [name=val]').removeAttr('name');
	});
}


// CONTACT
jQuery.connectual.prepContact = function() {
	jQuery(function() {
		var settings={
			jcallOpts:jQuery(jQuery.panther.apLoader('.x_submit',{offset:{offsetX:10,offsetY:8}})).cloneObj({scall:false}),
			errLoc:'after',
			callbacks : "contactForm"
		}
		$("#contact").formulate(settings);
		$("#contact").find('[name^="phone"]')
			.keyup(function(event){getFormat(this,event,'ph')})
			.bind('blur change',function(){FormatPhone(this);});
	});


	jQuery('#inquiry').val(0);
	jQuery('.input_other').hide();
	jQuery('#other').attr("value","");
	jQuery("#inquiry").change(function() {
		if(jQuery(this).val() == 'Other') {
			jQuery('.input_other').fadeIn("slow");
		} else {
			jQuery('.input_other').fadeOut("slow", function(){jQuery('#other').attr("value","");});
		}
	});
};

//TESTIMONIAL TAGS
jQuery.connectual.tagSystem = function() {
	function bindSwap() {
			$("input[name='curItems']").lnkListSwap("input[name='itemList']",{displ:".tags_cur ul",swapDispl:".tagList"});
	}
	$('.autoComplete').keydown(function(e) {
			if(e.keyCode == 13) {
					addTag();
					return false;
			}
	});
	$('.x_newTag').click(addTag);
	function addTag() {
			var tag = $('.autoComplete'), dupTag = false;
			if ($(tag).val()) {
					$(tag).val($.trim($(tag).val()));
					for(i = 0; i < tagArray.length; i++){
							if ($(tag).val().toLowerCase() == tagArray[i].split(':')[0].toLowerCase()) {
									dupTag = true;
									break
							}
				};
					if (dupTag) {
							$('[name="'+$(tag).val().toLowerCase()+'"]').click();
					} else {
							$('input[name="curItems"]').val(($('input[name="curItems"]').val()+','+$(tag).val()+':(0):0').replace(/^,+|^\s+|,+$|\s+$/g,""));
							bindSwap();
							tagArray.push($(tag).val());
					}
					$(tag).val('').end().focus();
			}
			return false;
	}
	bindSwap();
}

// TESTIMONIALS
jQuery.connectual.testDelete = function() {
	jQuery('.x_del').click(function(){
		if (confirm("Delete "+jQuery(this).attr('title')+"?")) {
			if(jQuery.panther.wait) return false;
			$.panther.jcallOpts.scall=true;
			$.panther.jcallOpts.jsonly=true;
			jQuery.panther.jcall('testDelete',[jQuery(this).attr('href')],jQuery.panther.apLoader(this));
		}
		return false;
	});
}
jQuery.connectual.testEdit = function(goBack) {
	jQuery(function() {
		var settings={
			jcallOpts:jQuery(jQuery.panther.apLoader('.x_submit',{offset:{offsetX:10,offsetY:8}})).cloneObj({scall:true}),
			autoLabels:false,
			callbacks:"testEdit"
		};
		jQuery("#edit_form").formulate(settings);
		jQuery('.x_cancel').click(function(){
			location.href = goBack;
			return false;
		});
		jQuery.connectual.tagSystem();
	});
}
jQuery.connectual.testNew = function(goBack) {
	jQuery(function() {
		var settings={
			jcallOpts:jQuery(jQuery.panther.apLoader('.x_submit',{offset:{offsetX:10,offsetY:8}})).cloneObj({scall:true}),
			autoLabels:false,
			callbacks:"testNew"
		};
		jQuery("#edit_form").formulate(settings);
		jQuery('.x_cancel').click(function(){
			location.href = goBack;
			return false;
		});
		jQuery.connectual.tagSystem();
	});
}

// CONNECTIONS
jQuery.connectual.conDelete = function() {
	jQuery('.x_del').click(function(){
		if (confirm("Delete "+jQuery(this).attr('title')+"?")) {
			if(jQuery.panther.wait) return false;
			$.panther.jcallOpts.scall=true;
			$.panther.jcallOpts.jsonly=true;
			jQuery.panther.jcall('conDelete',[jQuery(this).attr('href')],jQuery.panther.apLoader(this));
		}
		return false;
	});
}
jQuery.connectual.conEdit = function(goBack) {
	jQuery(function() {
		var settings={
			jcallOpts:jQuery(jQuery.panther.apLoader('.x_submit',{offset:{offsetX:10,offsetY:8}})).cloneObj({scall:true}),
			autoLabels:false,
			callbacks:"conEdit"
		};
		jQuery("#edit_form").formulate(settings);
		jQuery('.x_cancel').click(function(){
			location.href = goBack;
			return false;
		});
	});
}
jQuery.connectual.conNew = function(goBack) {
	jQuery(function() {
		var settings={
			jcallOpts:jQuery(jQuery.panther.apLoader('.x_submit',{offset:{offsetX:10,offsetY:8}})).cloneObj({scall:true}),
			autoLabels:false,
			callbacks:"conNew"
		};
		jQuery("#edit_form").formulate(settings);
		jQuery('.x_cancel').click(function(){
			location.href = goBack;
			return false;
		});
	});
}