/**
 * autozone.js - These are javascript objects built for autozone.com
 *
 * @author Resource Interactive <www.resource.com>
 */
var rounded = {
	start: function(){
		rounded.Corners();
	},
	Corners: function(){
		var corners = $$('.round-all','.round-top','.round-right','.round-left','.round-bottom');
		corners.setStyle('position', 'relative'); // to make sure absolute positioning will work
		var p = '/images/common/'; // one path for rounded images
		var ext = '.png'; // default file extension
		if(window.ie6){
			var ext = '.gif'; // ie file extension
		}
		corners.each(function(el,i){
			// default is main
			var itl = p + 'bkg-rounded_tl' + ext;
			var itr = p + 'bkg-rounded_tr' + ext;
			var ibl = p + 'bkg-rounded_bl' + ext;
			var ibr = p + 'bkg-rounded_br' + ext;
			// inner corners
			if(el.hasClass('inner')){
				var itl = p + 'bkg-rounded_itl' + ext;
				var itr = p + 'bkg-rounded_itr' + ext;
				var ibl = p + 'bkg-rounded_ibl' + ext;
				var ibr = p + 'bkg-rounded_ibr' + ext;
			}
			if(el.hasClass('orange')){
				var itl = p + 'bkg-rounded_tl_o' + ext;
				var itr = p + 'bkg-rounded_tr_o' + ext;
				var ibl = p + 'bkg-rounded_bl_o' + ext;
				var ibr = p + 'bkg-rounded_br_o' + ext;
			}
			/* not done yet - image rounded corners
			if(el.hasAttribute('src')){
				var img =	el.getProperty('src');
				var ic = el.getProperty('class')
				var ii = el.getProperty('id')
				var ih =	el.getCoordinates().height;
				var iw =	el.getCoordinates().width;
				var roundSpan = new Element('span',{
						'styles':{
							'background': 'url(' + img + ') no-repeat 0 0',
							'float':'left',
							'height': ih,
							'position':'relative',
							'width': iw
						}
				});
				el.replaceWith(roundSpan);
			}
			*/
			// the new corner element
			var corner = new Element('img', {
					'class': 'corner',
					'styles':{
						'height': 8,
						'position': 'absolute',
						'width': 8,
						'z-index': 10
					}
			});
			// change the size of the corner image
			if(el.hasClass('small')){
				corner.setStyles({
					'height': 3,
					'width': 3
				});
			}
			if(el.hasClass('medium')){
				corner.setStyles({
					'height': 6,
					'width': 6
				});
			}
			if(el.hasClass('large')){
				corner.setStyles({
					'height': 9,
					'width': 9
				});
			}
			// defining the corners
			var tlc = corner.clone().set({
				'src': itl,
				'styles':{
					'left':'0',
					'top':0
				}
			});
			var trc = corner.clone().set({
				'src': itr,
				'styles':{
					'right':0,
					'top':0
				}
			});
			var blc = corner.clone().set({
				'src': ibl,
				'styles':{
					'bottom':'0',
					'left':'0'
				}
			});
			var brc = corner.clone().set({
				'src': ibr,
				'styles':{
					'bottom':'0',
					'right':0
				}
			});
			// injecting the corners based on class
			if(el.hasClass('round-all')){
				tlc.inject(el);
				trc.injectAfter(tlc);
				blc.injectAfter(trc);
				brc.injectAfter(blc);
			}
			if(el.hasClass('round-top')){
				tlc.inject(el);
				trc.injectAfter(tlc);
			}
			if(el.hasClass('round-right')){
				trc.inject(el);
				brc.injectAfter(trc);
			}
			if(el.hasClass('round-bottom')){
				blc.inject(el);
				brc.injectAfter(blc);
			}
			if(el.hasClass('round-left')){
				tlc.inject(el);
				blc.injectAfter(tlc);
			}
		});
	}
};

function addHovers() {
	$$('.hover').each(function(el, i){
		el.hoverinfo = {
			'off':		el.getAttribute('src'),
			'over':		el.getAttribute('src').replace(/\.([a-zA-Z]+)$/, '-over.$1'),
			'selected':	el.getAttribute('src').replace(/\.([a-zA-Z]+)$/, '-active.$1')
		};
		el.addEvents({
			'mouseover': function() {
				this.setAttribute('src', this.hoverinfo.over);
			},
			'mouseout': function() {
				this.setAttribute('src', this.hoverinfo.off);
			}
		});
	});
}

function transitionSlideshow(){
	slideTransitioning = true;
	for (var i = 0; i < slideMap.length; i++){
		if (i == slideActive){
			clearTimeout(slideTimer);
			slideTimer = null;
			var slideNext = null;
			if (slideOverride == null) slideNext = (i == (slideMap.length - 1)) ? 0 : (i + 1);
			else {
				slideNext = slideOverride;
				slideOverride = null;
			}
			var fx = new Fx.Elements([
				$('slide-feature-' + i),
				$('slide-feature-' + slideNext),
				$('slide-button-' + i),
				$('slide-button-' + slideNext)
			], {
				'wait': false,
				'duration': 500,
				'transition': Fx.Transitions.Back.easeOut,
				'onComplete': function(){
					slideActive = slideNext;
					slideTransitioning = false;
					enableSlideshow();
				}
			}).start({
				0: {'opacity': 0},
				1: {'opacity': 1},
				2: {'opacity': 1},
				3: {'opacity': 0}
			});
		}
	}
}

function activateSlideshow(){
	slideActive = 0;
	slideOverride = null;
	slideTimer = null;
	slideTransitioning = false;
	slideMap = [];
	var slides = $ES('.slide');
	slides.each(function(slide, i){
		slideMap[slideMap.length] = i;
		slide.setAttribute('id', 'slide-' + i);
		var link = slide.getElements('.slide-link')[0];
		var button = slide.getElements('.slide-button')[0];
		var feature = slide.getElements('.slide-feature')[0];
		if (slide.className.contains('slide-active')) slideActive = i;
		button.setAttribute('id', 'slide-button-' + i);
		feature.setAttribute('id', 'slide-feature-' + i);
		feature.setStyle('opacity', (slideActive == i) ? 1 : 0);
		link.setAttribute('id', 'slide-link-' + i);
		link.setStyles({
			'display': 'block',
			'width': button.width,
			'height': button.height,
			'background-image': 'url(' + button.hoverinfo.selected + ')',
			'background-repeat': 'no-repeat'
		});
		feature.addEvent('mouseover', disableSlideshow);
		feature.addEvent('mouseout', enableSlideshow);
		button.setStyle('opacity', (slideActive == i) ? 0 : 1);
		button.addEvent('mouseover', disableSlideshow);
		button.addEvent('mouseout', enableSlideshow);
		button.idx = i;
		button.addEvent('click', function(){
			if (!slideTransitioning){
				slideOverride = this.idx;
				transitionSlideshow();
			}
		});
	});
	var playButton = $('slide-button-play');
	var playLink = $('slide-link-play');
	playLink.setStyles({
		'display': 'block',
		'width': playButton.width,
		'height': playButton.height,
		'background-image': 'url(' + playButton.hoverinfo.selected + ')',
		'background-repeat': 'no-repeat'
	});
	enableSlideshow();
}

function enableSlideshow(){
	if (slideTimer == null){
		slideTimer = setTimeout(transitionSlideshow, 5000);
		$('slide-button-play').setStyle('opacity', 0);
	}
}

function disableSlideshow(){
	clearTimeout(slideTimer);
	slideTimer = null;
	$('slide-button-play').setStyle('opacity', 1);
}

function toggleSlideshow(){
	if (slideTimer != null) disableSlideshow();
	else enableSlideshow();
}

function setMaxheight(){
	var bdContainerHeight = 0;
	$$('.maxheight').each(function(el){
		if ($('bdcontainer')) {
			bdContainerHeight = $('bdcontainer').getCoordinates().height;
			el.setStyle('height', bdContainerHeight + 'px');
		}
		else {
			el.setStyle('height', el.getParent().getCoordinates().height + 'px');
		}
	});
	var bottomMarginOffset = 0;
	$$('.content-maxheight').each(function(el){
		if ($('bdcontainer')) {
			if (bdContainerHeight == 0) {
				bdContainerHeight = $('bdcontainer').getCoordinates().height;
			}
			var marginOffset = 0;
			var bottomMarginOffsetSet = false;
			if (el.hasClass('tm')) {
				marginOffset = 9;
			}
			if (el.hasClass('bm') && !bottomMarginOffsetSet) {
				bottomMarginOffsetSet = true
				bottomMarginOffset = $('bdcontainer').getCoordinates().bottom - el.getCoordinates().bottom;
			}
			var containerHeight = bdContainerHeight;
			var contentHeight = containerHeight - (el.getCoordinates().top - $('bdcontainer').getCoordinates().top) - marginOffset - bottomMarginOffset;
			el.setStyle('height', contentHeight + 'px');
		}
	});
}

//window.addEvent('domready', rounded.start);
//window.addEvent('domready', addHovers);
//if ($('home-slideshow')) window.addEvent('domready', activateSlideshow);
//window.addEvent('domready', function(){var ap = new AlphaPng({'clearImage':'/autozone/images/common/spacer.gif'});});
//window.addEvent('domready', setMaxheight);

window.addEvent('load', function(){
 if($(document.body).getElement('.hover')) addHovers();
 if ($('home-slideshow')) activateSlideshow();
 // This AlphaPng code is causing massive slowdowns in IE6.  Disable for now.
 // var ap = new AlphaPng({'clearImage':'/autozone/images/common/spacer.gif'});
 if($(document.body).getElement('.maxheight')) setMaxheight();
});

