/**
*
* TRW Theme Scripts
*
**/

/* collapse menues tagged with collapsable */

$(document).ready(function()
{
	if ($("ul.collapsable"))
	{
		listClass = ".item-list";
		var activeNav = 'false';
		$("ul.collapsable span.category").addClass('pointer');	
		//$("ul.collapsable div"+class).not(":has(a.active)").hide();
		
		// loop through the objects to find the active nav item
		$("ul.collapsable div"+listClass).each(function(i)
		{
			hasActive = $(this).find("a.active").length;
			if (hasActive)
			{
				activeNav = 'true';
			}else{
				$(this).hide();
			}			
		});
		
		// if there isn't an active nav item, expand the first drop down.
		if (activeNav == 'false'){
			$("ul.collapsable div"+listClass).eq(0).show();
		}
		
		$("ul.collapsable span.category").click(function(){
			if ($(this).next().is(":hidden"))
			{
				$(this).parent().siblings().children().filter(listClass).slideUp("fast");
				$(this).next().slideDown("slow");
			}
		});	
	}
});

/**
*
* Form updates/tweak
*
**/

$(document).ready(function() {
	
	// Hide input and set listener to get URL on change
	$("#AdtlBusWJS").html("<label for='componentsDropDwn'>Additional Businesses</label><select id='AdtlBus' name='componentsDropDwn'><option value=''>Additional Businesses</option><optgroup><option value='/aftermarket'>Aftermarket</option><option value='/system/body_control_systems'>Body Control Systems</option><option value='/system/commercial_steering'>Commercial Steering</option><option value='/system/engineered_fasteners_and_components'>Eng. Fasteners & Components</option><option value='/system/engine_components'>Engine Components</option><option value='/additional_businesses/trw_conekt'>TRW Conekt</option></optgroup></select>");
	$(".adtlBusNoJsAlt").hide();
	$(".afterMktNoJsAlt").hide();
	var optValueUrl;
	$("#AdtlBus").change(function() 
	{
		optValueUrl = $("#AdtlBus").val();
		fullUrl = optValueUrl.indexOf("http://", 0);
		//alert(optValueUrl);
		if (optValueUrl != null && fullUrl == -1) 
		{
			window.location.href = "http://www.trw.com"+optValueUrl;
		}
		else if (optValueUrl != null && fullUrl != -1)
		{
			window.location.href = optValueUrl;
		}
		else
		{
			return false;
		}
	});
	
	// fix form styles for Opera and Safari and IE
	if($.browser.safari) 
	{ 
		$( 
			function() 
			{
				$("#componentsDropDwn").css({fontSize:"15px", marginTop:"0px"});
				$("#search_theme_form").height(13);
			} 
		); 
	} else if($.browser.opera){
		$( 
			function() 
			{
				$("#componentsDropDwn").css({fontSize:"14px", marginTop:"-3px"});
			} 
		); 
	} else if($.browser.msie){
		$( 
			function() 
			{
				$("#search-0").css({marginLeft:"55px"});
			} 
		); 
	}
	
	var searchFormVal = $("input#search_theme_form").val();
	$("input#search_theme_form").focus(function(){
		if ($.trim($(this).val()) == searchFormVal)
		{
			$(this).val('');
			return false;
		}
	});
	$("input#search_theme_form").blur(function(){
		if ($.trim($(this).val()) == '')
		{
			$(this).val(searchFormVal);
		}
	});
});

/**
*
* set listener for General PDF link
* Expandable/Collapsiple Unordered Lists as appropriate (for Tech Info page)
*
**/

$(document).ready(function() {
	
	// set listener
	$("a.generalPDF").click(function() {
		linkHref = $(this).attr("href");
		location.href = linkHref+"?sys="+sys;
		return false;
	});
	
	
	//converts sys into name var to be passed on
	function systemChk() {
	   var name = "sys";
	   var url = window.location.href;
	   var paramsStart = url.indexOf("?");
	
	   if(paramsStart != -1){
		  
		  var paramString = url.substr(paramsStart);
		  var tokenStart = paramString.indexOf(name);
		  
	
		  if(tokenStart != -1){
	
			 paramToEnd = paramString.substr(tokenStart + name.length + 1);
			 var delimiterPos = paramToEnd.indexOf("&");
	
			
			
	
			 if(delimiterPos == -1){
				return paramToEnd;
			 }
			 else {
				return paramToEnd.substr(0, delimiterPos);
			 }
		  }

		return paramString;
	   } else {
		  return '';
	   }
	};
	
	
	// expand / collapse as required 
	$('div#node-203 div.content ul:has(ul)').each(function() {
		$(this).addClass('parent');
		var sysChkVar = systemChk();
		if (sysChkVar && $(this).children().children().filter("h3").hasClass(sysChkVar))
		{
			$(this).children().children().filter("h3").addClass("expanded");
		}else{
			$(this).children().children().filter("h3").addClass('contracted');
			$(this).children().children().filter("ul").hide();
		}
	});
	$('div#node-203 div.content ul li h3').click(function() {
		if (!$(this).hasClass("contracted"))
		{
			$(this).addClass("contracted");
		}
		$(this).toggleClass("expanded");
		$(this).next().toggle();
	}); 
});


/**
*
* Open Hi-Res gallery links in new window
*
**/

$(document).ready(function() {
	$("#gallery_container a.image_link").click(function() {
		linkHref = $(this).attr("href");
		//open href in new window
		window.open(linkHref);
		return false;		
	});
});




/**
*
* Purchasing Contancts Commodity bu Content Handler
*
**/

$(document).ready(function () {
	var ap_form = $("#webform-component-asia_pacific_commodity");
	var nm_form = $("#webform-component-north_america");
	var e_form = $("#webform-component-europe");
							
	$("#webform-component-select_the_continent_test").html("<select id='continentSelect' class='form-select'><option selected='selected' value=''>select...</option><option value='0' id='ap'>Asia Pacific</option><option value='1' id='na'>North America</option><option value='2' id='e'>Europe</option></select>");
	
	$("#webform-component-only_one_commodity_total_should_be_selected").hide();
	ap_form.hide();
	nm_form.hide();
	e_form.hide();
		
	$("#webform-component-select_the_continent_test select").change(function() {
		var childForms = $("#webform-component-select_the_continent_test").nextAll().addClass("test");
		//alert(childForms);
		$("#webform-component-select_the_continent_test").append(childForms);		
		
		var selected = $("#webform-component-select_the_continent_test option:selected");
		var ap_selected = $("#webform-component-asia_pacific_commodity option:selected");
		var nm_selected = $("#webform-component-north_america option:selected");
		var e_selected = $("#webform-component-europe option:selected");
		
		if(selected.val() == "0") {
			ap_form.show();
			nm_form.hide();
			e_form.hide();
		} else if(selected.val() == "1") {  
			ap_form.hide();
			nm_form.show();
			e_form.hide();
		} else if(selected.val() == "2") { 
			ap_form.hide();
			nm_form.hide();
			e_form.show();
		}
		
		ap_selected.val("");
		nm_selected.val("");
		e_selected.val("");
		//alert(ap_selected.val());

	});
});
  


/*$(document).ready(function() {
	var childForms = $("#webform-component-select_the_continent_test").nextAll().clone(true);
	
	
						   
	$("#webform-component-select_the_continent_test").html("<select id='continentSelect' class='form-select'><option selected='selected' value=''>select...</option><option value='0' id='ap'>Asia Pacific</option><option value='1' id='na'>North America</option><option value='2' id='e'>Europe</option></select>");
	
});*/





/*
	if ($("ul.collapsable"))
	{
		listClass = ".item-list";
		var activeNav = 'false';
		$("ul.collapsable span.category").addClass('pointer');	
		//$("ul.collapsable div"+class).not(":has(a.active)").hide();
		
		// loop through the objects to find the active nav item
		$("ul.collapsable div"+listClass).each(function(i)
		{
			hasActive = $(this).find("a.active").length;
			if (hasActive)
			{
				activeNav = 'true';
			}else{
				$(this).hide();
			}			
		});
		
		// if there isn't an active nav item, expand the first drop down.
		if (activeNav == 'false'){
			$("ul.collapsable div"+listClass).eq(0).show();
		}
		
		$("ul.collapsable span.category").click(function(){
			if ($(this).next().is(":hidden"))
			{
				$(this).parent().siblings().children().filter(listClass).slideUp("fast");
				$(this).next().slideDown("slow");
			}
		});	
	}
*/

