// JavaScript Document

var hide_message = false;

function gotoPage(val) {
	window.location = val;
}

function drawSubNav(section) {
	if ((typeof section != 'string') || (section.length == 0)) {
		section = defsection;
	}
	var visibility = 0;
	$('.subnav:visible').each(function() {
		if ($(this).attr('id') != section + '_nav') {
			$(this).hide();
		}
		else {
			visibility = 1;
		}
	});
	if (! visibility) {
		$('#' + section + '_nav').show();
	}
}
var qryStr = window.location.search;
var defsection = null;
function empty(){return false;}
$(document).ready(function() {
	
	if(qryStr.indexOf("product=") != -1) {
		var prodNum = qryStr.replace(/.*product=([^\&]*)\&?.*/,"$1");

		if($("#product_data").children().length > 1)
			$("#" + prodNum).remove().insertBefore(".productbox:first").addClass("highlight");
		
	}
	defsection = $('.subnav:visible').attr('id');
	if (typeof defsection != 'string') {
		defsection = '';
	}
	defsection = defsection.replace(/_nav$/,"");
	
	drawSubNav();
	$('.subnavspace').each(function(index) {
		$(this).hover(function() {drawSubNav($(this).attr('id'));},empty);
		//var position = $(this).position();
		//alert($(this).attr('id') + '-left: ' + $(this).offset().left);
	});
	
	
	$('#civconav').hover(empty,drawSubNav);
	
	$('.solbar').hide();
	$('li.solnav').hover(
		function() {
			$(this).css('zIndex','2');
			$('.solbar',this).show();
		},
		function() {
			$(this).css('zIndex','1');
			$('.solbar',this).hide();
		}
	);
	//$('#solutionsnav').css('left','10px');
	
	if (hide_message) {
		$('#noMatch').hide();
	}

//Add random flash movie to homepage
	if (typeof movie_opts != 'undefined') {
		$('#homeMovie').flash(
			{ src: '/gallery/' + movie_opts[Math.floor(Math.random()*movie_opts.length)],
				width: 900,
				height: 200 },
			{ version: '8.0.0.0',
				update: false },
			function(htmlOptions) {
				$('#homeImage').hide();
				$(this).prepend($.fn.flash.transform(htmlOptions));
			}
		);
	}
	$("#transsearchboximage").click(launchFocusSearch);
	$("#transsearchbox").keypress(function(e){
		if(e.keyCode == 13)
			launchFocusSearch();
	});

});

function launchFocusSearch(){
		this.blur();
		var newWin;
		var trans = $("#transsearchbox")[0].value;
		if(trans == "" || trans == "Transducer Search"){
			newWin = window.open("/transducersearch/","FocusSearch","scrollbars=no,menubar=no,height=750,width=735,resizable=no,toolbar=no,location=no,status=no");
		} else {
			newWin = window.open("/transducersearch/?transducer="+trans,"FocusSearch","scrollbars=no,menubar=no,height=750,width=735,resizable=no,toolbar=no,location=no,status=no");
		}
		if(!newWin) {
			alert("Error: Transducer Search window could not be opened.\nTry disabling your pop-up blocker.");
			return false;
		}
		newWin.focus();
	}

//overridden funtion to draw news 
function showNews(xml, parent) {

	var news = xml.getElementsByTagName("news");
	var outStr = "";
	for(var i=0; i < news.length; i++) {
		var curr = news[i];
		var path = curr.getAttribute("path");
		var title = "";
		var desc = "";
		var graphic = "";
		var href = path;
		for(var j=0; j < curr.childNodes.length; j++) {
			var child = curr.childNodes[j];
			if(child.hasChildNodes()) {
				switch(child.tagName.toLowerCase()) {
					case "title":
						title = xmlUnescape(child.firstChild.data);
						break;
					case "description":
						desc = xmlUnescape(child.firstChild.data);
						break;
					case "graphic":
						graphic = child.firstChild.data;
						break;
					case "href":
						href = child.firstChild.data;
						break;
				}
			}
		}
		outStr += '<div class="newsitem">';
		//if(graphic.length)
			//outStr += '<img class="eventimg" src="' + graphic + '" align="left" />';
		outStr += '<div class="newstitle"><a href="' + href + '">' + title + '</a></div>';
		outStr += desc + '<br /><a class="newsmore" href="' + href + '">Read more...</a>';
		outStr += '<div style="height: 1px; clear: both;"></div></div>'
	}
	parent.innerHTML = outStr;
}

//Clears value from search text box
function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = ""
}


//Checks for blank entries in search box
function validate_searchbox(field, alerttxt) {
	with (field) {
		if (value==null||value=="") {
  			alert(alerttxt);return false
		}
		else if (value=="search CIVCO"){
			alert(alerttxt);return false
		}
		else {
			return true
		}
	}
}

//opens alert box and returns focus if there's a blank entry in the search box
function open_alertbox(thisform) {
	with (thisform) {
		if (validate_searchbox(productskeyword,"You must enter a term in the search box.")==false) {
  			productskeyword.focus();return false
		}
	}
}

