if ($.validator) {

	$.validator.addClassRules( {
		company_type: {
			company_type: 1
		}
	} );
	
	$.validator.addMethod("company_type", function(value, element, param) {
		
		var inputs = $("input.company_type", $(element).parents(".formRow"));
		if (!inputs.length) return true;
		
		var n = false;
		for (var i = 0; i < inputs.length; i++) {
			if ($(inputs[i]).is(":checkbox") && $(inputs[i]).is(":checked")) {
				n = true;
				break;
			} else if ($(inputs[i]).is(":radio") && $(inputs[i]).is(":checked")) { 
				n = true;
				break;
			} else if ($(inputs[i]).is(":text") && $.trim(inputs[i].value) != "" ) {
				n = true;
				break;
			}
		}
		
		if (n) {
			$("label.error", $(element).parents(".formRow")).remove();
		}
		
		return n;
		
	}, "This field is required.");
	
}

function accuveinApplicationForm(frmId) {
	
	if (!$.validator) return;
	
	$().ready( function() {
		$("#" + frmId).validate({
			errorPlacement: function(error, element) {
				if ( $(element).is("input.company_type") ) {
					var e = $(element).parents(".formRow").find("label.error");
					if (!e.length) {
						$(element).parents(".formRow").append(error);
					}
				} else {
				
					if ($(element).parents(".formRow").length) {
						$(element).parents(".formRow").append(error);
					} else if ($(element).parents(".formRow_first").length) {
						$(element).parents(".formRow_first").append(error);
					}
				}
			}
		});
	});
}

function accuveinTestimonialForm(frmId) {
	
	if (!$.validator) return;
	
	$().ready( function() {
		$("#" + frmId).validate({});
	});
}

function interiorCallMeForm(frmId) {
	
	if (!$.validator) return;
	
	$().ready( function() {
		$("#" + frmId).validate({
			errorPlacement: function(error, element) {
				$(element).parent().append(error);
			}
		});
	});
}

function callMeForm(frmId) {
	
	if (!$.validator) return;
	
	$().ready( function() {
		$("#" + frmId).validate({
			errorPlacement: function(error, element) {
				$(element).parent().append(error);
			}
		});
	});
}

//This is used for validation on forms that don't have room for error text
function FormTightSpace(frmId) {
	
	if (!$.validator) return;
	
	$().ready( function() {
		$("#" + frmId).validate({
			errorPlacement: function() {
				$('input.error').css({'border-color' : '#99110b', 'border-width' : '2px'});
			}
		});
	});
}


function scheduleDemoForm(frmId) {
	
	if (!$.validator) return;
	
	$().ready( function() {
		$("#" + frmId).validate({
			errorPlacement: function(error, element) {
				$(element).parent().append(error);
			}
		});
	});
}

function fnSameHeight(args) {
	var boxes = [];
	for (i=0, n=args.length; i < n; i++) {
		$(args[i]).each(function(){
			if ($(this).length) {
				boxes.push($(this));
			}
		});
	}
	
	var maxh = 0, h=0;
	for (i=0,n=boxes.length; i<n; i++) {
		h = $(boxes[i]).outerHeight(true);		
		if (maxh < h) {
			maxh = h;
		}
	}
	
	var num = function(elem, prop) {
		return parseInt($(elem).css(prop)) || 0;
	}
	
	for (i=0,n=boxes.length; i<n; i++) {
		var h = maxh;
		$("paddingTop paddingBottom borderTopWidth borderBottomWidth marginTop marginBottom".split(/\s+/)).each(function(){
			h -= num(boxes[i],this);
		});
		
		boxes[i].css({
			height:h+"px"
		});
	}
}

function sameHeight() {
	var args = arguments || undefined;
	args && $(document).ready(function(){
		fnSameHeight(args);
	});
}

$().ready(function(){
	
	/* Initialize Dropdown */
	$(".customDropDown").dropDownMenu();
	
	/* catalog */
	$("#catalogBox .productsListing").catalogProducts({
		itemWidth: 98,
		itemWidthOpen: 198,
		visibleItem: 5
	});
	/* catalog slider */
	$("#slider").slider({
		min: 0,
		max: $("#catalogBox .productsListing>li").length - 5,
		slide: function(event,ui) {
			$("#catalogBox .productsListing").catalogProducts("move", ui.value);
		}
	});
	/* catalog arrow */
	$("#catalogBox .slider_arrow_left").click(function(){
		this.blur();
		idx = $("#slider").slider("value");
		if (idx > 0) 
			$("#slider").slider("moveTo", idx-1);
		return false;
	});
	$("#catalogBox .slider_arrow_right").click(function(){
		this.blur();
		idx = $("#slider").slider("value");
		if (idx < $("#catalogBox .productsListing>li").length - 5) 
			$("#slider").slider("moveTo", idx+1);
		return false;
	});
	
	/* Call Me Box */
	/*
	$("#callMe .trigger").click(function(){
		if ($(this).is(".trigger_open")) {
			$(this).removeClass("trigger_open");
		} else {
			$(this).addClass("trigger_open");
		}
		var ie6 = ($.browser.msie && parseInt($.browser.version) < 7) || false;
		this.blur();
		$("#callMe form").slideToggle();
		//if ($.browser.msie && parseInt($.browser.version) < 7) {
			//$("#callMe span.cbl, #callMe span.cbr").
		//}
		return false;
	});
	*/
	$(".slideBox").each(function(){
		var elem = this;
		var content = $(".slideBox_content", elem);
		var trigger = $(".slideBox_trigger", elem);
		var ie6 = ($.browser.msie && parseInt($.browser.version) < 7) || false;
		var closed = true;
		var css_cache={};
		
		if (ie6) {
			$(elem).css("zoom",1);
		}
		
		$(content).hide();
		
		var triggerClick = function() {
			if ($(this).is(".slideBox_trigger_open")) {
				$(this).removeClass("slideBox_trigger_open");
			} else {
				$(this).addClass("slideBox_trigger_open");
			}
			if (ie6) {
				$(trigger).hide();
			}
			
			$(content).slideToggle("normal", function(){
				if ($(trigger).is(".slideBox_trigger_open")) {
					$("form input", elem).not("[type='hidden']").filter(":eq(0)").focus();	
					if (ie6) {
						$(trigger).show();
					}
				} else {
					if (ie6) {
						$(trigger).show();
					}
				}
			});
			
			if ($.browser.msie) this.blur();
			return false;
		}
		
		$(trigger).bind("click.slideBox", triggerClick);
	});
	
	$(".slideBox").each(function(){
		var elem = this;
		var content = $(".slideBox_content", elem);
		var trigger = $(".slideBox_trigger_test", elem);
		var ie6 = ($.browser.msie && parseInt($.browser.version) < 7) || false;
		var closed = true;
		var css_cache={};
		
		if (ie6) {
			$(elem).css("zoom",1);
		}
		
		$(content).hide();
		
		var triggerClick = function() {
			if ($(this).is(".slideBox_trigger_open")) {
				$(this).removeClass("slideBox_trigger_open");
			} else {
				$(this).addClass("slideBox_trigger_open");
			}
			if (ie6) {
				$(trigger).hide();
			}
			
			$(content).slideToggle("normal", function(){
				if ($(trigger).is(".slideBox_trigger_open")) {
					$("form input", elem).not("[type='hidden']").filter(":eq(0)").focus();	
					if (ie6) {
						$(trigger).show();
					}
				} else {
					if (ie6) {
						$(trigger).show();
					}
				}
			});
			
			if ($.browser.msie) this.blur();
			return false;
		}
		
		$(trigger).bind("click.slideBox", triggerClick);
	});
	
	
	
	$(".slideBoxWide").each(function(){
			var elem = this;
			var content = $(".slideBoxWide_content", elem);
			var trigger = $(".slideBoxWide_trigger", elem);
			var ie6 = ($.browser.msie && parseInt($.browser.version) < 8) || false;
			var closed = true;
			var css_cache={};
			
			if (ie6) {
				$(elem).css("zoom",1);
			}
			
			$(content).hide();
			
			var triggerClick = function() {
				if ($(this).is(".slideBoxWide_trigger_open")) {
					$(this).removeClass("slideBoxWide_trigger_open");
				} else {
					$(this).addClass("slideBoxWide_trigger_open");
				}
				if (ie6) {
					$(trigger).hide();
				}
				
				$(content).slideToggle("normal", function(){
					if ($(trigger).is(".slideBoxWide_trigger_open")) {
						$("form input", elem).not("[type='hidden']").filter(":eq(0)").focus();	
						if (ie6) {
							$(trigger).show();
						}
					} else {
						if (ie6) {
							$(trigger).show();
						}
					}
				});
				
				if ($.browser.msie) this.blur();
				return false;
			}
			
			$(trigger).bind("click.slideBoxWide", triggerClick);
		});
	
	
	/* scroll pane */
	if ($.fn.jScrollPane) {
		$(".scrollPane").jScrollPane({
			scrollbarWidth : 12,
			showArrows : true,
			arrowSize : 0,
			maintainPosition: true
		});
	}
	
	/* Tabs */
	if ($.fn.tabs) {
		$(".tabs").tabs();
	}
	
	/* FAQ */
	if ( $(".faq_topics_listing").length ) {
		if (!$(".faq_topics_listing>li").filter(".current").length) {
			$($(".faq_topics_listing>li").get(0)).addClass("current");
		} else {
			$(".faq_topics_listing>li").not($(".faq_topics_listing>li").filter(".current")[0]).removeClass("current");
		}
		$("a[href]", $(".faq_topics_listing")).each(function(){
			var href = ($(this).attr("href").split("#", 2))[1] || "";
			if (href) {
				$(this).bind("click", function() {
					if ($("#"+href).length) {
						$(this)
							.parents(".faq_topics_listing").find(">li").filter(".current")
							.find("a").trigger("hide_faq")
							.end()
							.end()
							.end()
							.end()
							.trigger("show_faq");
							
						return false;
					}
				})
				.bind("hide_faq", function() {
					$(this).parent().removeClass("current");
					$("#"+href).hide();
				}).bind("show_faq", function() {
					$(this).parent().addClass("current");
					$("#"+href).show();
				});
			}
			
			if ($("#"+href).length && !$(this).parent().is(".current")) {
				$(this).trigger("hide_faq");
			}
		});
	}
	
	/* Customer Stories */
	if ( $(".story_categories").length ) {
		var hash = document.location.hash.replace("#", "");
		var hasht = null;
		
		if (hash != "" && $("#"+hash).length > 0) {
			if ($("a[href='#"+hash+"']",".story_categories_listing>").length > 0) {
				$(".story_categories_listing>li").removeClass("current");
				$("a[href='#"+hash+"']",".story_categories_listing>").parents('li:eq(0)').addClass("current");
			}
			hasht = $("#"+hash);
			$("#"+hash).hide().attr("id", "");
			
			
		} else {
			hash = "";
		}
		
		if (!$(".story_categories_listing>li").filter(".current").length) {
			$($(".story_categories_listing>li").get(0)).addClass("current");
		} else {
			$(".story_categories_listing>li").not($(".story_categories_listing>li").filter(".current")[0]).removeClass("current");
		}
		//$(".story_contents .story_content").hide();
		$("a[href]", $(".story_categories_listing")).each(function(){
			var href = ($(this).attr("href").split("#", 2))[1] || "";
			var id = "", target = null;
			if (href) {
				$(this).bind("click", function() {
					var target, id;
					if ($("#"+href).length) {
						target = $("#"+href);
						id = target.attr("id");
						
						$(this)
							.parents(".story_categories_listing").find(">li").filter(".current")
							.find("a").trigger("hide_story")
							.end()
							.end()
							.end()
							.end()
							.trigger("show_story");
							
						target.attr("id", "");
						setTimeout(function() {
							target.attr("id", id);
						},0);
						//return false;
						return true;
					}
				})
				.bind("hide_story", function() {
					$(this).parent().removeClass("current");
					$("#"+href).hide();
				}).bind("show_story", function() {
					$(this).parent().addClass("current");
					$("#"+href).show();
				});
			}
			
			if ($("#"+href).length && !$(this).parent().is(".current")) {
				$(this).trigger("hide_story");
			} else {
				$(this).trigger("show_story");
			}
		});
		
		if (hash != "") {
			setTimeout(function() {
				hasht.show().attr("id", hash)
			},0);
		}
	}
	
	/* header nav */
	$("#headerNav li.menuItem").each(function(){
		$(this).bind("mouseover",function(){
			$(this).addClass("menuItem_hover");
		}).bind("mouseout",function(){
			$(this).removeClass("menuItem_hover");
		});;
	});
	/* header nav min width */
	$("#headerNav li.menuItem>ul").each(function(){
		if ($(this).outerWidth() < 150) {
			var d = $(this).outerWidth() - $(this).innerWidth();
			d = 150 - d;
			$(this).css("width", d+"px");
		}
	});

	
	/* ie 6 menu fix */
	if ($.browser.msie && parseInt($.browser.version) < 7) {
		$("#headerNav li.menuItem ul").each(function(){
			var w = $(this).innerWidth();
			w -= parseInt($(this).css("padding-left")) || 0;
			w -= parseInt($(this).css("padding-right")) || 0;
			
			w -= parseInt($("li:eq(0)", this).css("padding-left")) || 0;
			w -= parseInt($("li:eq(0)", this).css("padding-right")) || 0;
			
			w -= parseInt($("li>a:eq(0)", this).css("padding-left")) || 0;
			w -= parseInt($("li>a:eq(0)", this).css("padding-right")) || 0;
			
			$("li>a", this).css({width:w+"px"});
		});
	}
	
	$("img[@src$=png]").pngfix(); 
});
