/*
** use jQuery to traverse and manipulate the DOM
*/

$(document).ready(function(){
	// Transparent png's wont work in IE 6 and below use pngFix plugin
	$(document).pngFix();
	
	// Apply superfish plugin to nav-main
	$('ul#nav_main').superfish({
		autoArrows:  false,                           // disable generation of arrow mark-up 
		dropShadows: false                           // disable drop shadows
	});// end superfish
	
	// apply vertical alignment to an element
	$('.v-align').each(function(i){
  		var ah = $('.v-align').eq(i).height();
		var ph = $('.v-align').eq(i).parent().height();
		var mh = (ph - ah) / 2;
		$('.v-align').eq(i).css('margin-top', mh);
	});

        // css was originally hiding the v-align content - lets now show it
        $('.v-align').css("visibility","visible");

	$("#tabs ul li").each(function(i){
		
		// Lets loop through the UL LI's produced by the
		// Multipage TOC UL and add some 'sliding doors' wrappers
		
		// If child node length is greater than 0 then this LI has an AHREF element
		// and that means they are standard - not current - and only require SPAN
		// element wrappers around text node
		if ($(this).children().length > 0) {
			$(this).children().wrapInner("<span class='left_bg'><span class='right_bg'></span></span>");	
		// Else the LI normal and requires SPAN and AHREF element wrappers around text node
		} else {
			// make this LI current
			$(this).addClass('current');
			$(this).wrapInner("<a><span class='left_bg'><span class='right_bg'></span></span></a>");
		}

	}); // End each() function
	
	// IE6 doesnt like adjacent selector styles so add dynamically
	$("#featured_products .info_box h3 + img + p").addClass("pad_top_ie6");
	$("#featured_products .info_box h3 + a + p").addClass("pad_top_ie6");
	
	// Remove bottom margin from last three items in hover tile listings on landing pages
	var liLength = $("#hover_tile_listing li").length;
	var liRemainder = remainder(liLength, 3);
	// if the there are three tiles ending the list remove bottom margin from all
	if(liRemainder == 0){
		$("#hover_tile_listing li").eq(liLength-1).addClass("no_bottom_margin");
		$("#hover_tile_listing li").eq(liLength-2).addClass("no_bottom_margin");
		$("#hover_tile_listing li").eq(liLength-3).addClass("no_bottom_margin");
	}
	// if the there are two tiles ending the list remove bottom margin
	if(liRemainder == 2){
		$("#hover_tile_listing li").eq(liLength-1).addClass("no_bottom_margin");
		$("#hover_tile_listing li").eq(liLength-2).addClass("no_bottom_margin");
	}
	// if the there is one tile ending the list remove bottom margin
	if(liRemainder == 1){
		$("#hover_tile_listing li").eq(liLength-1).addClass("no_bottom_margin");
	}

	
	// remove top border and padding from first info box div in featured products div
	$("#featured_products .info_box:first").addClass("no_top");
        // now make them visible to avoid page load rendering jolt
        $("#featured_products .info_box").css("visibility", "visible");
	
	
});// end .ready function


function emailPage() {
  var messageText = 'Hello, I found a great web page at ' + window.location;
  var subjectText = 'Check out this Website';
  var email = prompt("Enter your friend's email address:", '');
  window.location = 'mailto:' + email +
      '?subject=' + encodeURIComponent(subjectText) +
      '&body=' + encodeURIComponent(messageText);
}

var smallFont = true;

function toggleFontSize(){
	var fontSizeToggle = getElementsByClass(document.getElementById("content_container"), "font-size-toggle");	
	if (!fontSizeToggle){
		return;
	}	
	if(smallFont){
		for (var i = 0; i < fontSizeToggle.length; i++){
			fontSizeToggle[i].style.fontSize = "115%";
			smallFont = false;
		}
	}else{
		for (var i = 0; i < fontSizeToggle.length; i++){
			fontSizeToggle[i].style.fontSize = "100%";
			smallFont = true;
		}
	}    
}

function getElementsByClass(theNode, theClass){
	var cArray = [];
	var cArrayLoc = 0;
	
	function doTree(theNode){
		if (theNode.className && theNode.className == theClass){
			cArray[cArrayLoc] = theNode;
			cArrayLoc++;
		}
		for (var i=0, len=theNode.childNodes.length; i<len; i++){
			doTree(theNode.childNodes[i]);
		}
	}
	
	doTree(theNode);
	return cArray;
}

function remainder(varOne, varTwo) {
	return(varOne % varTwo);
}


// Form validation for comment system

$(document).ready(function() { 

// form validation
$(".submit_comment").click(function() {
if($("#comment_0_name").attr("value")==""){
window.alert("Please enter a heading");
return false;
}
if($("#comment_0_comment").attr("value")==""){
window.alert("Please enter a comment");
return false;
}
if($("input[name=comment_0_rating]").attr("value")=="-1"){
window.alert("Please enter a rating");
return false;
}
if($("#metadata_field_text_35207_value").attr("value")==""||$("#metadata_field_text_35207_value").attr("value").indexOf("@")<0||$("#metadata_field_text_35207_value").attr("value").indexOf(".")<0){
window.alert("Please enter a valid email address.");
return false;
}
if($("#SQ_SYSTEM_SECURITY_KEY_VALUE").attr("value")==""){
window.alert("Please prove you are a human by entering the characters from the image above the submit button.");
return false;
}

});
});
