jQuery.fx.prototype.originalCustom = jQuery.fx.prototype.custom; 
jQuery.fx.prototype.custom = function(from, to, unit) { 
	if (this.prop == "height") {
		to = to || 1; 
		from = from || 1; 
	}
	this.originalCustom(from, to, unit);
}

function trackPage(stepsElement) {
	if (window.trackPageview) {
		var el = stepsElement.closest("div.inlineform").find(".steps");
		var step = el.attr('class').split(' ').slice(-1);
		var url = stepsElement.closest("div.inlineform").parent().find("a.analyticsurl").attr("href").replace(/^http:\/\/[^\/]+\//, "/") + "/" + step;
		trackPageview(url);
	}
}


/* OMRON INLINE FORMS */
jQuery.voormedia = {}
jQuery.voormedia.inlineForm = {
	options: {
		overlayElement: "div.overlay"
	},

	initialize: function(options) {
		this.options = jQuery.extend({}, this.options, options);
		this.attachHandlers();
	},
	
	replaceForm: function(newHtml) {
		this.find("input").unbind("change");
		this.html(newHtml);
		this.attachHandlers();
		this.downloadAction();
	},

	downloadAction: function(downloads) {
		downloadAction = jQuery("span.action:contains('DOWNLOAD')");
		if (downloadAction.length > 0) {
			result = downloadAction.next();
			jQuery("div.content").append("<p>" + result.html() + "</p>");
			location=result.find("a").attr("href");
		}
	},

	attachHandlers: function() {
		if (this.form = this.find("form")) {
			this.attachSubmitHandler();
			this.attachConditionHandler();
			this.configureFileUploading();
		}
	},

	configureFileUploading: function ()	{
		var self = this;
		if (this.containsFileUploadField()) {
			self.form.iframePostForm ({
				post : function () {
					self.form.attr({
					  action: self.changeFormAction(self),
					  target: "iframe-post-form"
					});
				},
				complete : function (response) {
					/* The success message of a successful form submit contains an
					   HTML tag with a class named 'success'. */
					if (response.search("success") >= 0) {
						self.successResponseHandler(response, self);
					} else {
						self.errorResponseHandler(response, self);
					}
				}
			});
		}
	},

	containsFileUploadField: function() {
		return this.find('input[type=file]').length > 0;
	},

	containsFileUpload: function() {
		return this.containsFileUploadField() && this.find('input[type=file]').attr("value").length > 0;
	},
	
	changeFormAction: function(form) {
		return form.form.attr("action").replace("/page/", "/fragment/") ;
	},
	
	successResponseHandler: function(data, form) {
		form.replaceForm(data);
		trackPage(form);
	},
	
	errorResponseHandler: function(data, form) {
		form.replaceForm("<div class=\"steps failure\"><b>We are experiencing technical difficulties. " +
						"Your form submission has not been successful. " +
						"Please accept our apologies and try again later.</b></div>");
	},
	
	submitXHR: function(form) {
		jQuery.ajax({
			url: form.changeFormAction(form),
			data: form.form.serialize() + "&submit=submit",
			type: "POST",
			success: function(data) {
				form.successResponseHandler(data, form);
			},
			error: function(request, options, error) {
				form.errorResponseHandler(error, form);
			}
		});
	},

	attachSubmitHandler: function() {
		var self = this;
		self.form.submit(function() {
			if (self.preSubmitHandler) {
				if (!self.preSubmitHandler()) {
					return false;
				}
			}
			self.showOverlay();
			if (self.containsFileUpload()) {
				return true;
			} else {
				self.submitXHR(self);
				return false;
			}
		});
	},
	
	attachConditionHandler: function() {
		var self = this;
		var id = this.find("input:hidden[name='_formId']").val();
		if (window.inlineFormHandlers && inlineFormHandlers[id]) {
			inlineFormHandlers[id].initHandler(self.form);
			this.preSubmitHandler = function() {
				return inlineFormHandlers[id].submitHandler(self.form);
			};
		}
	},
	
	showOverlay: function() {
		this.children("div.steps").append("<div class=\"overlay\"><div class=\"loading\"></div></div>");
		var steps = this.children("div.steps");
		var height = steps.height();
		var overlay = steps.find("div.overlay");
		overlay.css({top: (-1 * height), left: -5});
		overlay.width(steps.width());
		overlay.height(height);
		overlay.show();
		overlay.fadeTo(0, 0);
		overlay.fadeTo(250, 0.8);
		overlay.children("div.loading").css({top: (height / 2.0 - 12), left: 185});
	}
}


jQuery(function(){

	/* PRODUCT FEATURE WINDOW TABS  - replaces FeatureTab JS code */
	jQuery("table.featurewindow div.featurecontent").removeClass("jsnodisplay");
	jQuery("table.featurewindow div.featurecontent").hide();
	jQuery("table.featurewindow div.featurecontent:first").show();
	jQuery("table.featurewindow div.buttons a:first").addClass("selected");
	jQuery("table.featurewindow div.buttons a").click(function(){
		var index = jQuery("table.featurewindow div.buttons a").index(this);
		jQuery("table.featurewindow div.featurecontent").hide();
		jQuery("table.featurewindow div.featurecontent:eq("+index+")").show();
		jQuery("table.featurewindow div.buttons a").removeClass("selected");
		jQuery("table.featurewindow div.buttons a:eq("+index+")").addClass("selected");
		return false;
	});

	/* PRODUCT FEATURE BANNER TABS */
	jQuery("div#featurebanners div.banner").removeClass("jsnodisplay");
	jQuery("div#featurebanners div.banner").hide();
	jQuery("div#featurebanners div.banner:first").show();
	jQuery("div#featurebanners div.buttons a:first").addClass("selected");
	jQuery("div#featurebanners div.banners").addClass("maxheight");
	jQuery("div#featurebanners div.buttons").addClass("maxheight");
	jQuery("div#featurebanners div.buttons a").click(function(){
		var index = jQuery("div#featurebanners div.buttons a").index(this);
		jQuery("div#featurebanners div.banner").hide();
		jQuery("div#featurebanners div.banner:eq("+index+")").show();
		jQuery("div#featurebanners div.buttons a").removeClass("selected");
		jQuery("div#featurebanners div.buttons a:eq("+index+")").addClass("selected");
		return false;
	});
	
	/* NEW CALL TO ACTION SYSTEM */
	var actionHeights = {};
	var formIsActive = false;
	jQuery("div#contactactions div.steps").hide();
	jQuery("div#contactactions h2").addClass("hand");
	jQuery("div#contactactions div.action").each(function(i) {
		id = "action" + i;
		jQuery(this).attr("id", id);
		actionHeights[id] = jQuery(this).find("div.description").height();
	});
	jQuery("div#contactactions div.description").addClass("hand").css({ overflow: "hidden", height: 1 }).hide();

	function slideDescription(element) {
		newHeight = actionHeights[jQuery(element).closest("div.action").attr("id")];
		ownDescription = jQuery(element).closest("div.action").find("div.description");
		ownDescription.stop().animate({ height: newHeight }, { duration: (newHeight - ownDescription.height()) * 10 });
		otherDescriptions = jQuery(element).closest("div.action").siblings().find("div.description");
		otherDescriptions.each(function() {
			if (jQuery(this).is(":visible")) {
				jQuery(this).stop().animate({ height: 1 }, { duration: (jQuery(this).height() - 1) * 10 });
			}
		});
	}

	jQuery("div#contactactions div.heading h2, div#contactactions div.description").click(function() {
		formIsActive = false;
		trackPage(jQuery(this).closest("div.action").find("div.steps"));
		jQuery("div#contactactions div.steps").slideUp({ duration: 500 });
		ownForm = jQuery(this).closest("div.action").find("div.steps");
		if (ownForm.is(":hidden")) {
			formIsActive = true;
			ownForm.slideDown({ duration: 500 });
			slideDescription(this);
		}
		return false;
	});

	jQuery("div#contactactions div.heading h2").hover(function() {
		if (!formIsActive) {
			slideDescription(this);
		}
		return false;
	});

	jQuery("div.inlineform").each(function() {
		jQuery(this).extend(jQuery.voormedia.inlineForm).initialize();
	});

});