// Google Internal Site Search script- By JavaScriptKit.com (http://www.javascriptkit.com)
// For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/
// This notice must stay intact for use

// Based on JSKit script, rewritten for jquery

$(document).ready( function() {
	var nav_search = $("form#navigation-search");
	nav_search.attr("aria-label", "Search Site");
	
	nav_search.bind( 'submit', function(e) {
		nav_search.find("[name=q]").attr("value", "site:www.teamfdi.com " +
										 nav_search.find("[name=qfront]").attr("value"));
	});
	
	if(! Modernizr.inputtypes['search'] ) {
		var search_input = nav_search.find("input[name=qfront]");
		var placeholder  = "Enter search here…";
		
		search_input.focus( function() {
			var self = $(this);
			
			if(self.attr("value") == placeholder) {
				self.attr("value", "").removeClass("placeholder");	
			}
		}).blur( function() {
			var self = $(this);
			
			if(self.attr("value") == "") {
				self.attr("value", placeholder).addClass("placeholder");	
			}
		}).attr("value", placeholder).addClass("placeholder");
	}
});

// This stuff is ours.
// Uncork-It Communications 2010

// Fake some CSS3
$(document).ready( function() {
	$("div#product-family-sidebar div.products-division div.product:last-child").addClass('last-child');
	$("ul.capabilities-list > li:nth-child(odd)").addClass('nth-child-odd');
	$("ul.capabilities-list-hardware > li:nth-child(odd)").addClass('nth-child-odd');	
});

// Enable toggling of elements on the support pages.
$(document).ready( function() {
	var ulToggles = $(".toggle-ul");
	var showULAfter = function() {
		var instant = (typeof this.instant != 'undefined') ? this.instant : false;
		var self = $(this);
		var instant = self.data('instant');
		if ( typeof instant == 'undefined' ) { instant = false; }
		var nextUL = self.next('ul');
		if(instant)
			nextUL.show();
		else
			nextUL.slideToggle('slow');
		
		self.addClass('toggled');
	};
	var hideULAfter = function() {
		var instant = (typeof this.instant != 'undefined') ? this.instant : false;
		var self = $(this);
		var instant = self.data('instant');
		if ( typeof instant == 'undefined' ) { instant = false; }
		var nextUL = self.next('ul');
		if(instant)
			nextUL.hide();
		else
			nextUL.slideToggle('slow');
		
		self.removeClass('toggled');
	};
	
	ulToggles.each( function(index, obj) {
		var self = $(this);
		self.data('instant', true);
		hideULAfter.call(this);
		self.data('instant', false);
		
		self.toggle( showULAfter, hideULAfter );
	});
	
	var dlToggles = $(".toggle-dl");
	var showDLAfter = function() {
		var instant = (typeof this.instant != 'undefined') ? this.instant : false;
		var self = $(this);
		var instant = self.data('instant');
		if ( typeof instant == 'undefined' ) { instant = false; }
		var nextDL = self.next('dl');
		if(instant)
			nextDL.show();
		else
			nextDL.slideToggle('slow');
		
		self.addClass('toggled');
	};
	var hideDLAfter = function() {
		var instant = (typeof this.instant != 'undefined') ? this.instant : false;
		var self = $(this);
		var instant = self.data('instant');
		if ( typeof instant == 'undefined' ) { instant = false; }
		var nextDL = self.next('dl');
		if(instant)
			nextDL.hide();
		else
			nextDL.slideToggle('slow');
		
		self.removeClass('toggled');
	};
	
	dlToggles.each( function(index, obj) {
		var self = $(this);
		self.data('instant', true);
		hideDLAfter.call(this);
		self.data('instant', false);
		
		self.toggle( showDLAfter, hideDLAfter );
	});
	
	var faqToggles = $(".faq-list dt");
	var showDDAfter = function() {
		var instant = (typeof this.instant != 'undefined') ? this.instant : false;
		var self = $(this);
		var instant = self.data('instant');
		if ( typeof instant == 'undefined' ) { instant = false; }
		var nextDD = self.next('dd');
		if(instant)
			nextDD.show();
		else
			nextDD.slideToggle('slow');
		
		self.addClass('toggled');
	};
	var hideDDAfter = function() {
		var instant = (typeof this.instant != 'undefined') ? this.instant : false;
		var self = $(this);
		var instant = self.data('instant');
		if ( typeof instant == 'undefined' ) { instant = false; }
		var nextDD = self.next('dd');
		if(instant)
			nextDD.hide();
		else
			nextDD.slideToggle('slow');
		
		self.removeClass('toggled');
	};
	
	faqToggles.each( function(index, obj) {
		var self = $(this);
		self.data('instant', true);
		hideDDAfter.call(this);
		self.data('instant', false);
		
		self.toggle( showDDAfter, hideDDAfter );
	});
	
	if(dlToggles.length || ulToggles.length) {
		var hash = window.location.hash;
		var	element = $(hash.toString());
		
		if(hash && element.length) {
			element.trigger('click');
		}
	}
});

var FDIAdjustHeights = function() {
	if(document.getElementById('product-view-desc-specs-list')) {
		var columnHolder = $("#product-view-desc-specs-list .product-column-specs");
		if(columnHolder.length) {
			var column1 =  $("#product-view-desc-specs-list .column-first");
			var column2 =  $("#product-view-desc-specs-list .column-notfirst");
			
			// super imbalance.
			if( column1.innerHeight() > column2.innerHeight() * 1.5) {
				var innerEls = column1.find(".element");
				if(innerEls.length > 1) {
					var lastEl = innerEls.last();
					if(lastEl.innerHeight() * 1.3 < column1.innerHeight()) {
						column2.prepend(lastEl);	
					}
				}
			}
		}
		var smallprint = $("p.smallprint");
		if(smallprint.length) {
			smallprint.each( function(index, obj) {
				var self = $(this);
				
				self.appendTo($('div.product-specifications').first());
			});
		}
	}
	
	$("h4.full-width-lines .h4-line").each(function(index, obj) {
		if(! $.browser.mozilla ) {
			$(this).stretch(); // plugin is bugged with mozilla, have to work this out later.
		}
	});
	
	var maxHeight = 0;
	$("div.equalheight-columns .column").each(function(index, obj) {
		var self = $(this);
		if(self.innerHeight() > maxHeight) {
			maxHeight = self.innerHeight();	
		}
	});
		
	$("div.equalheight-columns .column").each(function(index, obj) {
		var self = $(this);
		self.css("min-height", maxHeight + "px");
	});
	
	$("div.equalheight-columns-with-content-div .column .column-content").each(function(index, obj) {
		var self = $(this);
		var parent = self.parents(".column").first();
		
		var parentHeight = parent.innerHeight();
		self.css("min-height", (parentHeight - 27) + "px");
	});
	
	// now, we adjust the sidenav box.
	
	var sidenav = $("#sidenav");
	var sidenav_ul = $("#sidenav ul");
	var content_main = $("#content-main");
	var content_div = $("#content");
	
	if($.browser.msie && $.browser.version <= 6.1) {
		sidenav.css('height', (content_div.innerHeight() - 20) + "px");
	} else {
		sidenav.css('min-height', (content_div.innerHeight() - 20) + "px");
	}
	/*if(sidenav.length && content_main.length) {
		var content_height = content_main.innerHeight();
		var sidenav_height = sidenav.innerHeight();
		var sidenav_ul_height = sidenav_ul.innerHeight();
		var sidenav_other_height = sidenav_height - sidenav_ul_height;
		
		if(content_height > sidenav.innerHeight()) {
			var new_height = content_height - sidenav_other_height + 10;
			//sidenav_ul.data('original-height', sidenav_ul_height);
			//sidenav_ul.css("min-height", new_height + "px");
		}
	}*/
	
	// now we adjust an optional box that is set to extend to the bottom, to meet the sidenav at bottom.
	
	var boxToBottom = $(".content-box-extend-to-bottom");
	if(boxToBottom.length) {
		var boxOffset = boxToBottom.offset();
		var sidenavOffset = sidenav.offset();
		
		var yToReach = sidenavOffset.top + sidenav.innerHeight();
		
		if( boxOffset.top + boxToBottom.innerHeight() < yToReach ) {
			boxToBottom.css("min-height", (yToReach - boxOffset.top - 5) + "px");
		}
	}
};

// Fixing various heights
$(window).load( function() { window.FDI_custom_loaded = true; } );
$(document).ready( function() {
	if(typeof window.FDI_custom_loaded == 'undefined') {
		   $(window).load(FDIAdjustHeights);
		 } else {
			FDIAdjustHeights(); 
		 }
});
