jQuery(document).ready(function(){
	// initilize drop down navigation using superfish plugin
	$(".nav").superfish();
	
	//initialize login form toggle link
	$(".nav_login").click(function () {
		$("#form_login").toggle();
		$(this).toggleClass("active");
		return false;
	});
	
	//initialize_header image slider if present on page
	try {
		$('#header_images').cycle({ 
			fx:    'fade', 
			speed:  5000 
		});
	} catch(e) {}
	
	//load wymeditor on textarea in CMS
	try {
		if($('#submit_button') && $(".richtext")) {
			$(".richtext").wymeditor();
			$('#submit_button').click(function(){
				// the update command gets the 1st wymeditor on the page using (0)
				$.wymeditors(0).update();
				$('#form_admin').submit();
			});
		}
	} catch(e) {}
	
	// form helpers for vendor forum registration form
	formVendorRegistrationInit();
	
	// fix IE CSS hover flicker
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
	
	//extend date input to format dates for database
	try {
		$.extend(DateInput.DEFAULT_OPTS, {
		stringToDate: function(string) {
			var matches;
			if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2})$/)) {
				return new Date(matches[1], matches[2] - 1, matches[3]);
			} else {
				return null;
			};
		},
		
		dateToString: function(date) {
		var month = (date.getMonth() + 1).toString();
			var dom = date.getDate().toString();
			if (month.length == 1) month = "0" + month;
			if (dom.length == 1) dom = "0" + dom;
			return date.getFullYear() + "-" + month + "-" + dom;
			}
		});
		// initialize date input
		var date_input = $("#start_date");
		if(date_input) {
			$(function() {
				$("#start_date").date_input();
				$("#end_date").date_input();
			});
		}
	} catch(e) {}
});

function formVendorRegistrationInit() {
	var community_member = $("#community_member");
	if (community_member) {
		$("#community_member").change(toggleCommunityMember).change();
	}
	var community_member_company = $("#community_member_company");
	if (community_member_company) {
		$("#community_member_company").change(updateCompany).change();
	}
}

function toggleCommunityMember() {
	if($("#community_member").val() == "I am a PLM Community Member") {
		$("#community_member_details").show();
		$("#payment_info").hide();
	} else {
		$("#community_member_details").hide();
		$("#payment_info").show();
	}
	updateCompany();
}

function updateCompany() {
	if($("#community_member").val() == "I am a PLM Community Member") {
		$("#company").val($("#community_member_company").val());
	} 
}
