// car-care.js
var carcare = ({
	init: function(){
		if($('#chalkboard')){
			carcare.chalkboard.init();
		}
	},
	chalkboard: ({
		current: 0,
		name: ['Brakes','Coolant','Exhaust','Electrical','Engine','Fuel','Power Train','Steering','Chassis'],
		desc: ['One of the key factors in car performance is the effectiveness of braking systems. Learn how to keep your brakes working their best.',
			'By nature, engines run hot. Here’s how to keep your ride cool. Learn about the different degrees of auto coolant protection.',
			'From form to function, find out the whats, wheres and hows of exhaust systems.',
			'Healthy batteries are just the start to a well running vehicle. Get a charge out of these electrical system articles.',
			'Your engine is the heart of your automobile. Learn how to increase the longevity of your vehicle by keeping your engine running at top performance.',
			'Check out our tips and tricks on how to make sure your fuel system works optimally while increasing power, performance and mileage.',
			'Find articles on how to remedy power train issues and improve performance.',
			'Learn how to adjust rack-and-pinion steering, change old steering wheels, increase steering power and more.',
			'Learn about suspension, chassis restoration, frame fixer-uppers and more.'],
		init: function(){
			// initialize navigation
			var navItems = $("#chalkboard-nav a");
			carcare.chalkboard.populate();
			carcare.chalkboard.highlight(navItems);
			carcare.searches();
			
			$("#toggler").hide();
			
			navItems.each(function(index){
				var offset = index > 6 ? -(750 * (index+1)) : -(750 * index);
				
				$(this).click(function(ev){
					ev.preventDefault();
					carcare.chalkboard.current = index;
					carcare.chalkboard.move(offset);
				});
				
			});			
			
			$("#chalkboard-recent p").click(function(){
				carcare.chalkboard.recentResize(true);
			});

			$("#toggler").click(function(ev){
				ev.preventDefault();
				
				if($(this).text() == "View Rear Wheel Drive System"){
					carcare.chalkboard.move(-(750*7));
					$(this).text("View Front Wheel Drive System");
				}else{
					carcare.chalkboard.move(-(750*6));
					$(this).text("View Rear Wheel Drive System");
				}
				
			});
			
		},
		move: function(offset){
			var chalk = $("#chalkboard-strip div");
			var catcher = $("#chalkboard-desc");

			carcare.chalkboard.highlight();
			catcher.stop();
			
			// fade out catcher
			catcher.animate({opacity: 0}, 300, 'swing', function(){
					carcare.chalkboard.populate();
					chalk.stop();
					// slide to new car care system
					chalk.animate({left: offset}, 1200,'swing');
					if(carcare.chalkboard.current == 6){
						$("#toggler").show(300);
					}else{
						$("#toggler").hide(300);
						$("#toggler").text("View Rear Wheel Drive System");
					}
				
			}).animate({opacity: 0.95}, 300, "swing");
			
		},
		
		
		highlight: function(){
			var navItems = $("#chalkboard-nav a");
			navItems.each(function(index){
				if(index == carcare.chalkboard.current){
					$(this).parent().css("backgroundImage","url(/images/car-care/chalkboard-nav-bg.gif)");
				}else{
					if(!ie6){
						$(this).parent().css("backgroundImage","url(/images/car-care/chalkboard-nav-bg-off.png)");						
					}else{
						$(this).parent().css("backgroundImage","url(/images/car-care/chalkboard-nav-bg-off.gif)");											
					}
				}
			});
		},
		populate: function(html){
			var sTitle = $("#chalkboard-desc h3");
			var sDesc = $("#chalkboard-desc p");
			var recent = $("#recent-catcher");
			var viewAll = $("#chalkboard-recent .view-all");
			
			sTitle.text(carcare.chalkboard.name[carcare.chalkboard.current]);
			sDesc.text(carcare.chalkboard.desc[carcare.chalkboard.current]);
			
			recent.load("/include/car-care/recent-articles.jsf",{'global' : 'automotive-system', 'cat': carcare.chalkboard.name[carcare.chalkboard.current].replace(" ","-").toLowerCase()},function(){
				$("#chalkboard-recent a.view-all").attr('href','/car-care/automotive-system/' + carcare.chalkboard.name[carcare.chalkboard.current].toLowerCase());
				carcare.chalkboard.recentResize(false);				
			});
			
			
		},
		recentResize: function(toggle){
			
			var recent = $("#chalkboard-recent");
			var catcher = $("#recent-catcher");
			var notClosed = recent.outerHeight() > 32;
			if(!notClosed && !toggle){return}
			
			var height = (notClosed && toggle) ? 32 : (catcher.outerHeight() + 74);
			if(ie6){
				$(recent.children().get(0)).css({
					backgroundPositionX: '169px',
					backgroundPositionY: '0'
				});
			}
			
			recent.animate({height: height});
			var bgPos = closed ? "169px -32px": "169px 0" ;
			$(recent.children().get(0)).css("backgroundPosition",bgPos);				
			
		}
	}),
	article: function(){
		// hide tips
		var tipsArr = $(".tip");
		tipsArr.css({
			display: 'none',
			opacity: 0
		});
		$(tipsArr.get(0)).css({
			display: 'block',
			opacity: 1
		});

		// set up drag bar
		var dragbar = $("#drag-bar");
		var howtobar = $("#how-to-label");
		var tips = $("#car-care-tips");
		var article = $("#car-care-article");

		var combo = $("#car-care-tips, #car-care-article");
		
		dragbar.click(function(ev){
			ev.preventDefault();
			
			if(tips.css('width') != '24px'){
				dragbar.css({
					marginLeft: '20px'
				});
				howtobar.css({
					backgroundPosition: '0 0'
				});
				article.animate({
					width: (ie6 == true ? '726px' : '686px')
				},500,'swing');
				tips.animate({
					width:'24px'
				},500,'swing');
				
			}else{
				dragbar.css({
					marginLeft: 0
				});
				howtobar.css({
					backgroundPosition: '-22px 0'
				});
				combo.animate({
					width: (ie6 == true ? '395px' : '345px')
				},500,'swing');				
			}
			
		});
		
		// set up previous / next buttons in tips
		var prev = $('.prev');
		var next = $('.next');
		var prevnext = $('.prev, .next');
		
		prevnext.click(function(ev){
			ev.preventDefault();
			
			var moveTo = $(this).attr("class").toString().split(" ")[1];
			
			$(this).parent().animate({
				opacity: 0
			},500,'swing',function(){

				tipsArr.each(function(){
					if($(this).hasClass(moveTo)){
						$(this).css('display','block').animate({
							opacity: 1
						},500,'swing');
					}else{
						$(this).hide();
					}
				});
				
			});
			
			
			
		});
		
		
		// default state for tips is open
		dragbar.trigger("click");
		
	},
		
	searches: function(){

		$("#car-care-search-term").bind("focus",function(){
			if($(this).attr("value") == "Article Keyword Search"){
				$(this).attr("value","")
			}
		});

		$("#car-care-search-term").bind("blur",function(){
			if($(this).attr("value") == ""){
				$(this).attr("value","Article Keyword Search")
			}			
		});

	}
		
});