var slideshow;

var TINY={};

function T$(i){return document.getElementById(i)}
function T$$(e,p){return p.getElementsByTagName(e)}

TINY.slider=function(){
	function slide(n,p){this.n=n; this.init(p)}
	slide.prototype.init=function(p){
		var s=this.x=T$(p.id), u=this.u=T$$('ul',s)[0], c=this.m=T$$('li',u), l=c.length, i=this.l=this.c=0; this.b=1;
		if(p.navid&&p.activeclass){this.g=T$$('li',T$(p.navid)); this.s=p.activeclass}
		this.a=p.auto||0; this.p=p.resume||0; this.r=p.rewind||0; this.e=p.elastic||false; this.v=p.vertical||0; s.style.overflow='hidden';
		for(i;i<l;i++){if(c[i].parentNode==u){this.l++}}
		if(this.v){;
			u.style.top=0; this.h=p.height||c[0].offsetHeight; u.style.height=(this.l*this.h)+'px'
		}else{
			u.style.left=0; this.w=p.width||c[0].offsetWidth; u.style.width=(this.l*this.w)+'px'
		}
		this.nav(p.position||0);
		if(p.position){this.pos(p.position||0,this.a?1:0,1)}else if(this.a){this.auto()}
		if(p.left){this.sel(p.left)}
		if(p.right){this.sel(p.right)}
	},
	slide.prototype.auto=function(){
		this.x.ai=setInterval(new Function(this.n+'.move(1,1,1)'),this.a*1000)
	},
	slide.prototype.move=function(d,a){
		var n=this.c+d;
		if(this.r){n=d==1?n==this.l?0:n:n<0?this.l-1:n}
		this.pos(n,a,1)
	},
	slide.prototype.pos=function(p,a,m){
		var v=p; clearInterval(this.x.ai); clearInterval(this.x.si);
		if(!this.r){
			if(m){
				if(p==-1||(p!=0&&Math.abs(p)%this.l==0)){
					this.b++;
					for(var i=0;i<this.l;i++){this.u.appendChild(this.m[i].cloneNode(1))}
					this.v?this.u.style.height=(this.l*this.h*this.b)+'px':this.u.style.width=(this.l*this.w*this.b)+'px';
				}
				if(p==-1||(p<0&&Math.abs(p)%this.l==0)){
					this.v?this.u.style.top=(this.l*this.h*-1)+'px':this.u.style.left=(this.l*this.w*-1)+'px'; v=this.l-1
				}
			}else if(this.c>this.l&&this.b>1){
				v=(this.l*(this.b-1))+p; p=v
			}
		}
		var t=this.v?v*this.h*-1:v*this.w*-1, d=p<this.c?-1:1; this.c=v; var n=this.c%this.l; this.nav(n);
		if(this.e){t=t-(8*d)}
		this.x.si=setInterval(new Function(this.n+'.slide('+t+','+d+',1,'+a+')'),10)
	},
	slide.prototype.nav=function(n){
		if(this.g){for(var i=0;i<this.l;i++){this.g[i].className=i==n?this.s:''}}
	},
	slide.prototype.slide=function(t,d,i,a){
		var o=this.v?parseInt(this.u.style.top):parseInt(this.u.style.left);
		if(o==t){
			clearInterval(this.x.si);
			if(this.e&&i<3){
				this.x.si=setInterval(new Function(this.n+'.slide('+(i==1?t+(12*d):t+(4*d))+','+(i==1?(-1*d):(-1*d))+','+(i==1?2:3)+','+a+')'),10)
			}else{
				if(a||(this.a&&this.p)){this.auto()}
				if(this.b>1&&this.c%this.l==0){this.clear()}
			}
		}else{
			var v=o-Math.ceil(Math.abs(t-o)*.1)*d+'px';
			this.v?this.u.style.top=v:this.u.style.left=v
		}
	},
	slide.prototype.clear=function(){
		var c=T$$('li',this.u), t=i=c.length; this.v?this.u.style.top=0:this.u.style.left=0; this.b=1; this.c=0;
		for(i;i>0;i--){
			var e=c[i-1];
			if(t>this.l&&e.parentNode==this.u){this.u.removeChild(e); t--}
		}
	},
	slide.prototype.sel=function(i){
		var e=T$(i); e.onselectstart=e.onmousedown=function(){return false}
	}
	return{slide:slide}
}();

var TINY={};

function T$(i){return document.getElementById(i)}
function T$$(e,p){return p.getElementsByTagName(e)}

TINY.fader=function(){
	function fade(n,p){this.n=n; this.init(p)}
	fade.prototype.init=function(p){
		var s=T$(p.id), u=this.u=T$$('li',s), l=u.length, i=this.l=this.c=this.z=0;
		if(p.navid&&p.activeclass){this.g=T$$('li',T$(p.navid)); this.s=p.activeclass}
		s.style.overflow='hidden'; this.a=p.auto||0; this.p=p.resume||0;
		for(i;i<l;i++){
			if(u[i].parentNode==s){
				u[i].style.position='absolute'; this.l++; u[i].o=p.visible?100:0;
				u[i].style.opacity=u[i].o/100; u[i].style.filter='alpha(opacity='+u[i].o+')'
			}
		}
		this.pos(p.position||0,this.a?1:0,p.visible)
	},
	fade.prototype.auto=function(){
		this.u.ai=setInterval(new Function(this.n+'.move(1,1)'),this.a*1000)
	},
	fade.prototype.move=function(d,a){
		var n=this.c+d, i=d==1?n==this.l?0:n:n<0?this.l-1:n; this.pos(i,a)
	},
	fade.prototype.pos=function(i,a,v){
		var p=this.u[i]; this.z++; p.style.zIndex=this.z;
		clearInterval(p.si); clearInterval(this.u.ai); this.u.ai=0; this.c=i;
		if(p.o>=100&&!v){p.o=0; p.style.opacity=0; p.style.filter='alpha(opacity=0)'}
		if(this.g){for(var x=0;x<this.l;x++){this.g[x].className=x==i?this.s:''}}
		p.si=setInterval(new Function(this.n+'.fade('+i+','+a+')'),20)
	},
	fade.prototype.fade=function(i,a){
		var p=this.u[i];
		if(p.o>=100){
			clearInterval(p.si); if((a||(this.a&&this.p))&&!this.u.ai){this.auto()}
		}else{
			p.o+=5; p.style.opacity=p.o/100; p.style.filter='alpha(opacity='+p.o+')'
		}
	};
	return{fade:fade}
}();

$(document).ready(function()
{
	$('#nav, #home-nav').find('a').each(function(){
		jQuery.data(this,'color',$(this).css('color'));
	}).mouseenter(function(){
		$(this).stop().animate({color:'#ffffff'},'fast');
	}).mouseleave(function(){
		var color = jQuery.data(this,'color');
		$(this).stop().animate({color:color},'fast');
	});
	$('#bar a').mouseenter(function(){
		$(this).stop().animate({opacity:'1'},'fast');
	}).mouseleave(function(){
		$(this).stop().animate({opacity:'.6'},'fast');
	});
	$('#product-links li').mouseenter(function(){
		$(this).stop().animate({opacity:'1', marginTop:'0'},'fast');
	}).mouseleave(function(){
		$(this).stop().animate({opacity:'.8', marginTop:'3px'},'fast');
	});
	if($('#zip-form').length > 0){
		$('#zip-submit').click(function(){
			var zip = $('#zip-code').val(),
			dist = $('#distance').val(),
			location = '/dealers/results/' + zip + '/' + dist;
			window.location.href = location;
		});
		$('#zip-code').focus(function(){
			if($(this).val()=='Zip Code'){
				$(this).val('');
			}
		}).blur(function(){
			if($(this).val()==''){
				$(this).val('Zip Code');
			}
		});
	}
	if($('#store-features').length > 0){
		slideshow = new TINY.fader.fade('slideshow',{
			id:'store-features',
			navid:false,
			activeclass:false,
			auto:6,
			resume:false,
			position:0
		});
	}
	if($('#timeline').length > 0){
		slideshow = new TINY.fader.fade('slideshow',{
			id:'features',
			navid:'slide-nav',
			activeclass:'current',
			auto:6,
			resume:false,
			position:0
		});
		$('#zip-search').submit(function(){
			var zip = $('#home-zip').val(),
			location = '/dealers/results/' + zip + '/150';
			window.location.href = location;
			return false;
		});
		$('#home-zip').focus(function(){
			if($(this).val()=='zip code'){
				$(this).val('');
			}
		}).blur(function(){
			if($(this).val()==''){
				$(this).val('zip code');
			}
		});
		var div = $('#timeline'),
		ul = $('#slide'),
		divWidth = div.width(),
		ulWidth = (ul.children('li').size() * 116) + 400,
		lastLi = ul.find('li:last-child'),
		left = (divWidth - ulWidth) / 2;
		ul.css({width:ulWidth+1, left:'-200px'});
		var v = $("#slide > li").css('visibility', 'hidden'), cur = 0;
		for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
		function fadeInNextLI() {
			v.eq(cur++).hide().css('visibility','visible').hide().fadeIn();
			if(cur != v.length) setTimeout(fadeInNextLI, 100);
		}
		fadeInNextLI();
		ul.animate({left:left}, 1000);
		ul.children().mouseenter(function(e){
			var pos = (e.pageX * (ulWidth-divWidth) / divWidth) * -1;
			ul.stop().animate({left:pos},'fast');
		});
		ul.children().click(function(){
			var id = $(this).attr('id').replace('timeline-', '');
			ul.children().removeClass('displayed');
			$('#displayed-arrow').remove();
			$(this).addClass('displayed').append('<div id="displayed-arrow"></div>');
			$.ajax({
				url: "/general/timeline/" + id,
				success: function(response){
				//	if($('#home-sections').is(':visible')){
				//		var sects = $("#home-sections").children();
				//		sects.eq(0).animate({opacity:0})
				//		setTimeout(function(){sects.eq(1).animate({opacity:0})}, 200);
				//		setTimeout(function(){sects.eq(2).animate({opacity:0})}, 400);
				//	}else{
						$('#bottom').animate({opacity:0}, 'fast', function(){
							$('#bottom').html(response);
							$('#home-sections').hide();
							var imgs = $('#timeline-left img'),
							imgcount = imgs.length;
							if(!imgcount){
								$('#bottom').animate({opacity:1});
							}else{
								imgs.load(function(){
									imgcount--;
									if (imgcount < 1){
										$('#bottom').animate({opacity:1});
									}
								});
							}
						});
				//	}
				}
			});
		});
		div.mouseleave(function(e){
			ul.stop();
		});
	}
	if($('#broadhead-list').length > 0){
		$('#broadhead-list').children().mouseenter(function(){
			$(this).children('.broadhead-info').addClass('hover-state').find('.broadhead-highlights').show();
		});
		$('#broadhead-list').children().mouseleave(function(){
			$(this).children('.broadhead-info').removeClass('hover-state').find('.broadhead-highlights').hide();
		});
	}
});
