var _IE = (navigator.appName == "Microsoft Internet Explorer");

function printThisPage() {
	window.print();
}

//function refreshCaptcha(uid) {
//	var contents = getAjaxContent('/refreshcaptcha/' + uid + '/');
//	$('#captcha_container').children('img').replaceWith(contents);
//}
function openEmailDialogue() {
	var body = document.getElementsByTagName('body')[0];
	var container = document.createElement('div');
	body.appendChild(container);
	container.innerHTML = getAjaxContent('http://www.tenneco.com/emailpagelink/index.html');
	jQuery(container).dialog({autoOpen:true, position: 'center', draggable:true, height: 600, width: 500, modal:true, resizeable:true, overlay:{opacity: 0.5,background: "black"} });
}
var _IE = (navigator.appName == "Microsoft Internet Explorer");
function getAjaxContent(myUrl) {
    var _XMLHTTP;
	if (_IE) {
		_XMLHTTP = new ActiveXObject('Microsoft.XMLHTTP');
	}
	else {
		_XMLHTTP = new XMLHttpRequest();	
	}
	_XMLHTTP.open('GET',myUrl, false);
	_XMLHTTP.send(null);
    return _XMLHTTP.responseText;
}
function getAjaxPostContent(myUrl, PostDict) {
    var _XMLHTTP;
	if (_IE) {
		_XMLHTTP = new ActiveXObject('Microsoft.XMLHTTP');
	}
	else {
		_XMLHTTP = new XMLHttpRequest();	
	}

	_XMLHTTP.open('POST',myUrl, false);
    _XMLHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    _XMLHTTP.setRequestHeader("Content-length", PostDict.length);
    _XMLHTTP.setRequestHeader("Connection", "close");  
	_XMLHTTP.send(PostDict);
	return _XMLHTTP.responseText;
}
function getAjaxPostContentReturnRequest(myUrl, PostDict) {
    var _XMLHTTP;
	if (_IE) {
		_XMLHTTP = new ActiveXObject('Microsoft.XMLHTTP');
	}
	else {
		_XMLHTTP = new XMLHttpRequest();	
	}

	_XMLHTTP.open('POST',myUrl, false);
    _XMLHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    _XMLHTTP.setRequestHeader("Content-length", PostDict.length);
    _XMLHTTP.setRequestHeader("Connection", "close");  
	_XMLHTTP.send(PostDict);
	return _XMLHTTP;
}

function getAjaxContentBooleanClosure(myUrl) {
    var _XMLHTTP;
	if (_IE) {
		_XMLHTTP = new ActiveXObject('Microsoft.XMLHTTP');
	}
	else {
		_XMLHTTP = new XMLHttpRequest();	
	}

	_XMLHTTP.open('GET',myUrl, false);
    _XMLHTTP.setRequestHeader("Connection", "close");  
	_XMLHTTP.send(null);
	return false;
}
function contactFormHandler() {
	alert(this.status);
	alert(this.readyState);
	if(this.readyState == 4 && this.status == 200) {
		alert(this.responseText);
		$('#contact_form_container').text(this.responseText);		
	}
}

function openTermsAndConditions() {
	var body = document.getElementsByTagName('body')[0];
	var node = document.createElement('div');


	jQuery(node).addClass('popupdialog');
	jQuery(node).css('background', 'white');
	
	jQuery(node).hide();

	jQuery(node).css('margin-top', '20px');
	jQuery(node).css('margin-left', '20px');
	jQuery(node).css('margin-bottom', '20px');
	jQuery(node).css('padding', '0px');

	node.innerHTML = getAjaxContent('/gettermsandconditions/');
	
	var adcontainer = document.createElement('div');
	jQuery(adcontainer).css('margin-left', 'auto');
	jQuery(adcontainer).css('margin-right', 'auto');
	jQuery(adcontainer).css('margin-top', '100px');
	jQuery(adcontainer).css('width', '300px');
	jQuery(adcontainer).appendTo(node);
	
	var accept = document.createElement('a');
	accept.href = 'javascript:acceptTermsAndConditions();';
	jQuery(accept).html('accept');
	jQuery(accept).css('width', '74px');
	jQuery(accept).css('height', '24px');
	jQuery(accept).css('font-size', '0px');
	jQuery(accept).css('background', 'url("/media/uploads/buttons/button_accept.gif") no-repeat 0 0');
	jQuery(accept).css('float', 'right');
	jQuery(accept).appendTo(adcontainer);
	
	var decline = document.createElement('a');
	decline.href = 'javascript:closeTermsAndConditions();';
	jQuery(decline).html('decline');
	jQuery(decline).css('float', 'left');
	jQuery(decline).css('width', '74px');
	jQuery(decline).css('height', '24px');
	jQuery(decline).css('font-size', '0px');
	jQuery(decline).css('background', 'url("/media/uploads/buttons/button_decline.gif") no-repeat 0 0');
	jQuery(decline).appendTo(adcontainer);
	
	jQuery(node).appendTo(body);
	jQuery(node).dialog({autoOpen:true, position: 'center', draggable:true, height: 600, width: 660, modal:true, resizeable:true, overlay:{opacity: 0.5,background: "black"} });
	jQuery(node).fadeIn('slow');

}
	

function closeTermsAndConditions() {
	$('.popupdialog').dialog('close');
	$('.popupdialog').remove();
}

function acceptTermsAndConditions() {
	var jSend = $('#submitformdisclaimer');
	var send = document.createElement('span');
	jQuery(send).attr('id', 'submitlink');
	jQuery(send).addClass('page-link');
	jQuery(send).html('Send');
	jQuery(send).click(function() {processEmail();});
	jSend.replaceWith(send);
	closeTermsAndConditions();
}
function emailThisPage() {
	var postData = "";
	postData += encodeURIComponent($('#id_sender_name').attr('name')) + '=' + encodeURIComponent($('#id_sender_name').val());
	postData += '&';
	postData += encodeURIComponent($('#id_sender_email').attr('name')) + '=' + encodeURIComponent($('#id_sender_email').val());
	postData += '&';
	postData += encodeURIComponent($('#id_recipient_name').attr('name')) + '=' + encodeURIComponent($('#id_recipient_name').val());
	postData += '&';
	postData += encodeURIComponent($('#id_recipient_email').attr('name')) + '=' + encodeURIComponent($('#id_recipient_email').val());
	postData += '&';
//	postData += encodeURIComponent($('#id_captcha').attr('name')) + '=' + encodeURIComponent($('#id_captcha').val());
//	postData += '&';
	postData += encodeURIComponent($('#id_page_url').attr('name')) + '=' + encodeURIComponent(document.location.href);

	var temp = getAjaxPostContent('/emailpagelink/', postData);
	$('.emaillinkcontainer').html(temp);

}

function getAjaxPostContentContact(myUrl, PostDict) {
    var _XMLHTTP;
	if (_IE) {
		_XMLHTTP = new ActiveXObject('Microsoft.XMLHTTP');
	}
	else {
		_XMLHTTP = new XMLHttpRequest();	
	}
	_XMLHTTP.onreadystatechange == contactFormHandler;
	_XMLHTTP.open('POST',myUrl);
    _XMLHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    _XMLHTTP.setRequestHeader("Content-length", PostDict.length);
    _XMLHTTP.setRequestHeader("Connection", "close");  
	_XMLHTTP.send(PostDict);

}

function expandDiv(hObj) {
		var parentHObj = hObj;
		var parentHValue = parentHObj.getAttribute('value');
		var childDivId = parentHValue.replace(/first_/, 'second_level_');	
		var childDiv = document.getElementById(childDivId);
		if (jQuery(parentHObj).hasClass('leftnav-highlight')) jQuery(parentHObj).removeClass('leftnav-highlight');
		else jQuery(parentHObj).addClass('leftnav-highlight');
		var aObj = parentHObj.lastChild;


		jChildDiv = $('#'+childDivId);
		jChildDiv.slideDown('slow');	
}
function expandDivSub(hObj) {
		var parentHObj = hObj;
		
		if (jQuery(parentHObj).hasClass('sub-leftnav-highlight')) jQuery(parentHObj).removeClass('sub-leftnav-highlight');
		else jQuery(parentHObj).addClass('sub-leftnav-highlight');
		var aObj = parentHObj.lastChild;

		var parentDivObj = parentHObj.parentNode;
		var tempDivId = parentDivObj.getAttribute('id').replace(/second/, 'third');
		
		var parentHValue = parentHObj.getAttribute('value');
		var childDivId = tempDivId + "_" + parentHValue.replace(/second_/, '');
		
		jChildDiv = $('#'+childDivId);
		jChildDiv.slideDown('slow');	
}
function displayH(hObjValue) {
	var leftNavList = $('.leftnav-link')
	for (var i = 0; i < leftNavList.length; i++) {
		var leftNavValue = leftNavList[i].getAttribute('value');
		if (leftNavValue == hObjValue) {
			var aObj = leftNavList[i].lastChild;
			if (!jQuery(aObj).hasClass('link-highlight')) jQuery(aObj).addClass('link-highlight');
			expandDiv(leftNavList[i]);
			break;
		}
	}
}
function displayHSub(childDivId) {
	var childDiv = document.getElementById(childDivId);
	var parentHObj = childDiv.previousSibling;
	var aObj = parentHObj.lastChild;
	if (!jQuery(aObj).hasClass('link-highlight')) jQuery(aObj).addClass('link-highlight');
	if (jQuery(parentHObj).hasClass('sub-leftnav-highlight')) jQuery(parentHObj).removeClass('sub-leftnav-highlight');
	else jQuery(parentHObj).addClass('sub-leftnav-highlight');
	jChildDiv = $('#'+childDivId);
	jChildDiv.slideDown('slow');	
}
function processEmail() {
	var comments = $('#id_comments').val();
	var name = $('#id_name').val();
	var email = $('#id_email').val();
	var telephone = $('#id_telephone').val();
//	var captcha = $('#id_captcha').val();
//	var captcha_uid = $('#id_captcha_uid').val();
	var reason = $('input[checked]').val();
	var reason_id;
	try {
		reason_id = $('input[checked]').attr('id');
	}
	catch(ex) {reason_id = "dummy";}
	
	var postData = "reason=" + encodeURIComponent(reason) + "&comments=" + encodeURIComponent(comments) + "&name=" + encodeURIComponent(name) + "&email=" + encodeURIComponent(email) + "&telephone=" + encodeURIComponent(telephone);
	if (reason_id != "dummy") {
		postData += "&reason_id=" + encodeURIComponent(reason_id);
	}
	var temp = getAjaxPostContent('/processemail/', postData);
	temp = temp.replace(/&gt;/, '>');
	temp = temp.replace(/&lt;/, '<');
	document.getElementById('contact_form_container').innerHTML = temp;
	$('#submitlink').hide();
	$('#clearlink').hide();
	
}

var timeFrame =  function(startMonth, startDay, startYear, endMonth, endDay, endYear, doIt) {
  /* clean up input, tries to fix to MM,DD,YYYY,MM,DD,YYYY */
  if (startDay.length == 1) { startDay = "0"+startDay; }
  if (startMonth.length == 1) { startMonth = "0"+startMonth; }
  if (startYear == 2) { startYear = "20"+startYear; }
  if (endDay.length == 1) { endDay = "0"+endDay; }
  if (endMonth.length == 1) { endMonth = "0"+endMonth; }
  if (endYear.length == 2) { endYear = "20"+endYear; }

  var startDate = startYear +""+ startMonth +""+ startDay;
  var endDate = endYear +""+ endMonth +""+ endDay;
  
  var nowDate = new Date();
  var day = nowDate.getUTCDate();
  var month = nowDate.getUTCMonth();
  var correctedMonth = month + 1;  //month - JavaScript starts at "0" for January, so we add "1"

  if (correctedMonth < 10) {  /* if less than "10", put a "0" in front of the number. */
    correctedMonth = "0" + correctedMonth;
  }
   
  if (day < 10) {  /* if less than "10", put a "0" in front of the number. */
    day = "0" + day;
  }

  var year = nowDate.getYear();  /* Get the year. Firefox and Netscape might use century bit, and two-digit year. */
  if (year < 1900) {
    year = year + 1900;  /*This is to make sure Netscape AND FireFox doesn't show the year as "107" for "2007." */
  }

  var GMTdate = year + "" + correctedMonth + "" + day;  //corrected month GMT date.

  if ((GMTdate <= endDate) && (GMTdate >= startDate)) {
    if (doIt){
        doIt();
    } else {
        /* no function set, do nothing */
    }
  }
}



	var showDiv = function(evt) {
		if (!evt) evt = window.event;
		var parentHObj;
		if (_IE) parentHObj = window.event.srcElement.parentNode;
		else parentHObj = evt.currentTarget.parentNode;
		flList = $('.first_level');
		for ( var i = 0; i < flList.length; i++) {
			if (parentHObj.getAttribute('id') != flList[i].getAttribute('id')) {
				$('#'+flList[i].getAttribute('id')).removeClass('leftnav-highlight');
				$('#'+flList[i].getAttribute('id')).children('a').removeClass('link-highlight');
			}
		}

		if (jQuery(parentHObj).hasClass('leftnav-highlight')) jQuery(parentHObj).removeClass('leftnav-highlight');
		else jQuery(parentHObj).addClass('leftnav-highlight');
		var aObj = parentHObj.lastChild;
		var parentHValue = parentHObj.getAttribute('value');

		var childDivId = parentHValue.replace(/first_/, 'second_level_');
		jDivList = $('.second_level');
		for (var i = 0; i < jDivList.length; i++){
			var divObj = jDivList[i];
			var divId = divObj.getAttribute('id');
			
			$('#'+divId+':visible').slideUp('slow');
		}
		jChildDiv = $('#'+childDivId+":hidden");
		jChildDiv.slideDown('slow');

	};
	var showSubDiv = function(evt) {
		if (!evt) evt = window.event;
		var parentHObj;
		if (_IE) parentHObj = window.event.srcElement.parentNode;
		else parentHObj = evt.currentTarget.parentNode;
		var divId = parentHObj.parentNode.getAttribute('id');
		var slList = $('#'+divId).children('h1');
		var fl = parentHObj.parentNode.previousSibling;
		var flId = fl.getAttribute('id').replace(/first_/, '');
		for ( var i = 0; i < slList.length; i++) {
			slList[i].setAttribute('id', slList[i].getAttribute('value') + '_' + flId);
			if (parentHObj.getAttribute('id') != slList[i].getAttribute('id')) {
				
				$('#'+slList[i].getAttribute('id')).removeClass('sub-leftnav-highlight');
				$('#'+slList[i].getAttribute('id')).children('a').removeClass('link-highlight');
			}
		}

		var parentDivObj = parentHObj.parentNode;
		if (jQuery(parentHObj).hasClass('sub-leftnav-highlight')) jQuery(parentHObj).removeClass('sub-leftnav-highlight');
		else jQuery(parentHObj).addClass('sub-leftnav-highlight');
		var aObj = parentHObj.lastChild;
		var parentDivId = parentDivObj.getAttribute('id');
		var tempChildDivId = parentDivId.replace(/second_level_/, 'third_level_');
		var parentHValue = parentHObj.getAttribute('value').replace(/second_/, '');
		var childDivId = tempChildDivId + '_' + parentHValue;
		jDivList = $('.third_level');
		for (var i = 0; i < jDivList.length; i++){
			var divObj = jDivList[i];
			var divId = divObj.getAttribute('id');
			
			$('#'+divId+':visible').slideUp('slow');
		}
		jChildDiv = $('#'+childDivId+":hidden");
		jChildDiv.slideDown('slow');
		
		
	};
	
	



$(document).ready(function() {

	var container = document.getElementById('vertical_container');
	var jHList = $('.leftnav-link');
	loop_i:
	for (var i = 0; i < jHList.length; i++) {
		var hObj = jHList[i];
		var value = hObj.getAttribute('value');
		if (value.indexOf('first') != -1) { //first-level
			jQuery(hObj).attr('id', value);
			jQuery(hObj).addClass('first_level');
			var node = document.createElement('div');
			container.appendChild(node);
			var jNode = jQuery(node);
			jNode.hide();
			var value_ = value.replace(/first_/, '');
			jNode.attr('id', 'second_level_'+value_);
			jNode.addClass('second_level');
			loop_j:
			for (var j=i+1; j < jHList.length; j++) {
				var hObj_sub = jHList[j];
				var jHObj_sub = jQuery(hObj_sub);
				var value_sub = hObj_sub.getAttribute('value');
				if (value_sub.indexOf('second') != -1) { //second_level
					container.removeChild(hObj_sub); //remove
					node.appendChild(hObj_sub); //and replace
					var subnode = document.createElement('div');
					node.appendChild(subnode);
					var jSubnode = jQuery(subnode);
					jSubnode.hide();
					var value__ = value_sub.replace(/second_/, '');
					jSubnode.attr('id', 'third_level_'+value_+'_'+value__);
					jSubnode.addClass('third_level');
					jHObj_sub.addClass('leftnav-nobkg');
					loop_k:
					for (var k=j+1; k < jHList.length; k++) {
						var hObj_sub_sub = jHList[k];
						var jHObj_sub_sub = jQuery(hObj_sub_sub);
						var value_sub_sub = hObj_sub_sub.getAttribute('value');
						if (value_sub_sub.indexOf('third') != -1) { //third_level
							jHObj_sub_sub.addClass("leftnav-nobkg");
							jHObj_sub_sub.addClass("sub-no-arrow");
							container.removeChild(hObj_sub_sub); //remove
							subnode.appendChild(hObj_sub_sub); //and replace
						}
						else break loop_k; // break because the next second level element is reached.
					}
					if (subnode.innerHTML != '') {
						obj = hObj_sub.firstChild;
						type = 'click'
						if (obj.addEventListener) obj.addEventListener(type, function(evt) {showSubDiv(evt);}, false);
						else obj.attachEvent('on'+type, function(evt) {showSubDiv(evt);});
					}
					else {
						jHObj_sub.addClass("sub-no-arrow");
						node.removeChild(subnode);
					}
				}
				else if (value_sub.indexOf('first') != -1) break loop_j; // break if reached the next first level
				else continue loop_j; // continue if third level
				
			}
			if (node.innerHTML != '') {
				obj = hObj.firstChild;
				type = 'click'
				if (obj.addEventListener) obj.addEventListener(type, function(evt) {showDiv(evt);}, false);
				else obj.attachEvent('on'+type, function(evt) {showDiv(evt);});
			}
			
		}
	}
	//now reorder the children....
	secondLevelList = $('.second_level');
	for (var i =0; i < secondLevelList.length; i++) {
		sl = secondLevelList[i];
		if (sl.innerHTML == '') {
			sli = parseInt(sl.getAttribute('id').replace(/second_level_/, ''));
			objId = 'first_' + (sli).toString();
			hsl = document.getElementById(objId);
			//alert(hsl);
			jQuery(hsl).addClass('no-arrow');
			container.removeChild(sl);
			
		}
		else {
			sli = parseInt(sl.getAttribute('id').replace(/second_level_/, ''));
			objId = 'first_' + (sli).toString();
			try {
				obj = document.getElementById(objId).nextSibling;
				container.removeChild(sl);
				container.insertBefore(sl, obj);
			}
			catch(ex) {
			alert(ex);
			}
		}
	}
	
	
	//overlay setup:
		var pcss = document.createElement('link');	
		jQuery(pcss).attr('href', '/media/stylesheets/overlayprint.css');
		jQuery(pcss).attr('rel', 'stylesheet');
		jQuery(pcss).attr('type', 'text/css');
		jQuery(pcss).attr('media', 'print');
		
		$('.pageoverlay').click(function() {
		$('link[media=print]').hide()
		var body = document.getElementsByTagName('body')[0];
		var container = document.createElement('div');
		body.appendChild(container);
		container.innerHTML = getAjaxContent(this.href);

		var head = document.getElementsByTagName('head')[0];

		jQuery(pcss).appendTo(head);
		
		var switchStyles = function() {
			jQuery(pcss).remove();
			jQuery(container).dialog('destroy');
			jQuery(container).remove();
			$('link[media=print]').show()
		};
		
		jQuery(container).dialog({autoOpen:true, close: switchStyles, position: 'center', draggable:true, height: 800, width: 660, modal:true, resizeable:true, overlay:{opacity: 0.5,background: "black"} });
		
		$('.imagedownloadlink').click(function() {
			var imageHref = this.href;
			var postContent = encodeURIComponent('picture_src') + '=' + encodeURIComponent(imageHref);
			document.location.href = '/downloadpicture/?' + postContent;
			return false
		});
		
		
		
		return false
	});
	$('.imagedownloadlink').click(function() {
		var imageHref = this.href;
		var postContent = encodeURIComponent('picture_src') + '=' + encodeURIComponent(imageHref);
		document.location.href = '/downloadpicture/?' + postContent;

		return false
	});
	$('.videooverlay').click(function() {
		$('.ui-dialog').empty();
		$('#cacheplayer').empty();
		
		linkHref = this.href;
		var body = document.getElementsByTagName('body')[0];
		var container = document.createElement('div');
		body.appendChild(container);
		container.innerHTML = getAjaxContent(linkHref);
		
		var fileHref = $(".embed_container").children('object').children('param[name="src"]').attr('value');
		var so = new SWFObject('media/swf/player.swf','mpl','640','480','9', '#000000');
		so.addParam('allowscriptaccess','always');
		so.addParam('allowfullscreen','true');
		so.addParam('flashvars','file='+fileHref+'&autostart=true');
		so.write('player');		
		$(".embed_container").empty();
		var clearVideo = function(event, ui) {
			$('.cache_container').empty();

		};

		jQuery(container).dialog({autoOpen:true, position: 'center', draggable:true, height: 600, width: 700, modal:true, resizeable:true, overlay:{opacity: 0.5,background: "black"}, close: clearVideo });
		return false;
	});







	$(".infoBox").each(function(){

			var moreLink = $(this).find("span.more:first");
			var moreList = $(this).find("ul.hiddenlist:first");




			moreLink.click(function() {

					moreList.toggle();
					$(this).toggleClass("plus");

					return false;                       

			});

	});



	$('.LaunchTenAt10').click(function() {
		$('.ui-dialog').empty();
		$('#cacheplayer').empty();
		
		linkHref = this.href;
		var body = document.getElementsByTagName('body')[0];
		var container = document.createElement('div');
		body.appendChild(container);
		container.innerHTML = getAjaxContent(linkHref);
		
		var fileHref = $(".embed_container").children('object').children('param[name="src"]').attr('value');
		var so = new SWFObject('media/swf/player.swf','mpl','480','360','9', '#000000');
		so.addParam('allowscriptaccess','always');
		so.addParam('allowfullscreen','true');
		so.addParam('flashvars','file='+fileHref+'&autostart=true');
		so.write('player');		
		$(".embed_container").empty();
		var clearVideo = function(event, ui) {
			$('.cache_container').empty();

		};

		jQuery(container).dialog({autoOpen:true, position: 'center', draggable:true, height: 430, width: 540, modal:true, resizeable:true, overlay:{opacity: 0.5,background: "black"}, close: clearVideo });
		return false;
		
	});
	

});		

