function modUsdaBox() {

$("#usda-box span.sm").each(function() {
	var $thisText = $(this).text();
	var $isCopy =$thisText.toString().search(new RegExp("\©", "i"));
	if ( $isCopy >= 0 ) {
		$(this).parent().html("");
	}	
});

var $thisPath = $("#usda-path").attr("value");
$("#usda-box a").each(function() {
	$(this).attr("href", $thisPath).attr("target", "new");
});

}


function USDAlinks() {

$("div#content a").each(function() {
	var $thisLink = $(this).attr("href");
    var $isFound = $(this).attr("href").toString().search(new RegExp("plants.usda.gov/java/profile", "i"));
	if ( $isFound >= 0 ) {
	$(this).addClass("usda-link");
		$(this).click(function() {
			$.fancybox.showActivity();
			$.ajax({
				type: "POST",
				url: "usda2.dna",
				data: "",
				cache: true,
				success: function(html){
					$.fancybox.hideActivity();
					$.fancybox({
						'padding'		: 20,
						'autoScale'		: false,
						'transitionIn'	: 'fade',
						'transitionOut'	: 'fade',
						'width'		: 750,
						'height'		: 600,
						'href'			: "usda2.dna?plant_string=" + $thisLink
					});
				
  				}
			});
			return false;
		});
	}    
});



}



function setToggleList() {
$("h2.toggleList").click(function() {
	var thisList = $(this).attr("rel");
	$("ul[id=" + thisList + "]").toggle();
});
}



function goToPost(whichPage, whichVar, whichKey) {
		$.fancybox.showActivity();
	var $str = "page=" + whichPage + "&thisvar=" + whichVar + "&key=" + whichKey;
	$.ajax({
		type	: "POST",
		cache	: false,
		url		: "comments.dna",
		data	: $str,
		success: function(html) {
    		$("#comments-box").html(html);
    		$.fancybox.hideActivity();
  			}
	});
} 

function ContactForm() {
$("#commentForm").validate({
   submitHandler: function(form) {
   	$.ajax({
		type	: "POST",
		cache	: false,
		url		: "ajax/secureFormPost.dna",
		data	: $(form).serializeArray(),
		success: function(html) {
		window.location.reload();
  			}
	});
   }
})

}



function getToken() {

$.ajax({
	type: "GET",
	url: "ajax/secureFormToken.dna",
	cache: false,
	success: function(txt){
	$("#commentForm").css("display", "block");
    $(".secureForm").append('<input type="hidden" name="_token" value="'+txt+'" />');   
    ContactForm();
  }
});

}

function setSecureComment() {
$("#postComment").click(function() {	
	getToken();
	return false;

});
}





$(document).ready(function() { 



	$("a[rel=plantGallery]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
		}
	});



	$(".openCosm").fancybox({	
		'scrolling' : 'no',
		'autoDimensions' : true,
		'titleShow'	: false,
		ajax : {
		    type	: "GET"
		},
		'onComplete'	: function() {
	    openCosmUpdate();
		}
	});
	
setToggleList();
setSecureComment();
USDAlinks();

});
