/* =======================================================

	Site scripting and behaviors
	
	Northern Metal Fab

	
*/

	// delay in seconds to wait between switching headlines
	var newsdelay = 8.5;

	
/* ======================================================= */

//  Edit below this section at your own risk

/* ======================================================= */
// Small extension to mootools' Element object methods
/*
Element.extend({
hide: function() { return this.setStyle('visibility', 'hidden'); },
show: function() { return this.setStyle('visibility', 'visible'); }
});
*/
/* ======================================================= */
//  css-based popup nav menu

var navNames=['OurCompany','ManufEng','Showcase'];

function showSub(menuNum){
	for(i=1;i<=3;i++){
		$('navSub'+i).setStyle('left',(i==menuNum)?'0px':'-9999px');
		$('navImg'+i).src="assets/nav"+navNames[i-1]+((i==menuNum)?'Hot':'Cold')+".gif"
	}
}

function hideSubs(){
	for(i=1;i<=3;i++){
		$('navSub'+i).setStyle('left','-9999px');
		$('navImg'+i).src="assets/nav"+navNames[i-1]+"Cold.gif"
	}
}

/* ======================================================= */
//  show / hide a div - slider

var infosliders = {
	makeSlider: function(){
		this.sliders = $$('div.infoslide').map(function(theDiv){
			return new Fx.Slide(theDiv, {
				mode: 'vertical', duration: 500
			}).hide();
		});
		$$('a.infoslideOpener').each(function(lnk, index){
			lnk.addEvent('click', function(){
				this.sliders.each(function(slider, sliderIndex){
					if (sliderIndex == index) this.sliders[sliderIndex].toggle();
					else this.sliders[sliderIndex].slideOut();
				}, this);
			}.bind(this));
		}, this);
		$$('input.formslideOpener').each(function(input, index){
			input.addEvent('onchange', function(){
				this.sliders.each(function(slider, sliderIndex){
					if (sliderIndex == index) this.sliders[sliderIndex].toggle();
					else this.sliders[sliderIndex].slideOut();
				}, this);
			}.bind(this));
		}, this);
	},
	init: function(){
		this.makeSlider();
	}
};
window.onDomReady(infosliders.init.bind(infosliders));

var formsliders = {
	makeSlider: function(){
		this.sliders = $$('div.formslide').map(function(theDiv){
			return new Fx.Slide(theDiv, {
				mode: 'vertical', duration: 333
			});
		});
		/*
		$$('input.formslideOpener').each(function(el, index){
			el.addEvent('onchange', function(){
				this.sliders.each(function(slider, sliderIndex){
					if (sliderIndex == index) this.sliders[sliderIndex].toggle();
					else this.sliders[sliderIndex].slideOut();
				}, this);
			}.bind(this));
		}, this);
		*/
	},
	init: function(){
		this.makeSlider();
	}
};
window.onDomReady( function(){ if($('jobForm')) {
formsliders.init.bind(formsliders); } } );

/* ======================================================= */
//	hot news ticker

var newsID = 'latest_headlines';
var nAct = 0, nEls, newsTimer;

function startNews(){
	if(!$(newsID)){ return; }
	nEls = $(newsID).getElementsBySelector('a');
	$(nEls[nAct].parentNode).effect('opacity',{
		duration: 1000, onComplete: function (){ newsTimer = newsFadeNext.delay(newsdelay*1000) }
	}).start(0,1);
}

function newsFadeNext(){
	newsTimer = $clear(newsTimer);
	$(nEls[nAct].parentNode).effect('opacity',{ duration: 500, onComplete: nextNews }).start(0);
}
function nextNews(){
	newsTimer = $clear(newsTimer);
	for(i=0;i<nEls.length;i++){ $(nEls[i].parentNode).setStyle('opacity',0) }
	(nAct == nEls.length-1)? nAct=0 : nAct++ ;
	$(nEls[nAct].parentNode).effect('opacity',{
		duration: 333, onComplete: function (){ newsTimer = newsFadeNext.delay(newsdelay*1000) }
	}).start(1);
}

function newsFadePrev(){
	newsTimer = $clear(newsTimer);
	$(nEls[nAct].parentNode).effect('opacity',{ duration: 500, onComplete: prevNews }).start(0);
}
function prevNews(){
	newsTimer = $clear(newsTimer);
	for(i=0;i<nEls.length;i++){ $(nEls[i].parentNode).setStyle('opacity',0) }
	(nAct == 0)? nAct=nEls.length-1 : nAct-- ;
	$(nEls[nAct].parentNode).effect('opacity',{
		duration: 333, onComplete: function (){ newsTimer = newsFadeNext.delay(newsdelay*1000) }
	}).start(1);
}

/* ======================================================= */
//  fix transparency in .png files for MSIE 6 and earlier

if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
	document.writeln('<style type="text/css">img { visibility:hidden; } </style>');
	window.attachEvent("onload", fnLoadPngs);
}

function fnLoadPngs() {
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

	for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
		if (itsAllGood && img.src.match(/\.png$/i) != null) {
			var src = img.src;
			img.style.width = img.width + "px";
			img.style.height = img.height + "px";
			img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
			img.src = "assets/x.gif";
		}
		img.style.visibility = "visible";
	}
}


/* ======================================================= */
//  page load tasks:

function initPage (){
	if(window.gecko) new SmoothScroll();
	startNews();
}

window.addEvent('domready', initPage);