racing = ({
	initialize: function(){
		// start news ticker
		$("#news").load("/include/headlines.jsf",function(){
			$("#news").newsticker(8500);
		});
		
		$("#loading").css({
			opacity: 0
		});		
	
		if(ie6){
			racing.rosterIE6();
		}
	
		//init accordion
		jQuery("#gallery-accordion").accordion({
		    event: 'click', 
		    active: 0, 
		    header: "dt"			
		});
		
		$("#sign-up-email").focus(function(){
			if($(this).attr("value") == "Enter Your Email Address"){
				$(this).attr("value","");
			}
		}).blur(function(){
			if($(this).attr("value") == ""){
				$(this).attr("value","Enter Your Email Address");
			}
		});

		$("#selector").bind("change",function(){
		   var link = $("option:selected", this).val();
		   if(isPreview){
            link += previewString;
         }
		   
			window.location.href = link;
		});

	},
	roster: function(){
	// init carousel
	if(!ie6){$("#roster-carousel").css('opacity',0);}
	
    jQuery('#roster-carousel').jcarousel({
    	start: 1,
		scroll: 5,
		animation: 1100,
		easing: 'swing',
		initCallback: function(){
			if(!ie6){$("#roster-carousel").css('opacity',1);}
		}
		
    });	
	
	// init rollovers
	$("#roster-carousel .jcarousel-item").bind("mouseenter",function(){
		$(this).find("ul").animate({
			height: '35px'
		});
	});	

	$("#roster-carousel .jcarousel-item").bind("mouseleave",function(){
		$(this).find("ul").animate({
			height: '15px'
		});	
		
	});
	
	// shorten carousel names where necessary (Blickensderfer!)
	var names = $(".name");
	
	names.each(function(){
		var length = $(this).text().length;
		if(length > 17){
			var newLast = $(this).text().split(" ")[1];
			var newStart = $(this).text().substring(0,1);
			
			var newText = newStart + ". " + newLast;
			
			$(this).text(newText);
			
		}
	});
	
	
	},
	rosterIE6: function(){
		var imgs = $("#roster-carousel li img, .racing-driver-image img, .racing-chief-image img");
		
		imgs.each(function(){
			var newId = $(this).attr("src").substring($(this).attr("src").lastIndexOf("/")+1);
			newId = newId.split(".png")[0];
			var newEl = "<div id=\"" + newId + "\"></div>";
			
			$(this).replaceWith(newEl);
			
		});
		
		$("#roster a").css({right: "8px"});
		
	},
	news: function(){
		var raceFilter = $("#race-filter");
		
		raceFilter.change(function(ev){
			ev.preventDefault();
			window.location.href = this.options[this.selectedIndex].value;
		});
	},
	signup: function(){
	    var options = { 
	        beforeSubmit:  racing.showRequest,  // pre-submit callback 
	        success:       racing.showResponse  // post-submit callback 

	        // other available options: 
	        //target:    element	 // target element(s) to be updated with server response 
	        //url:       url         // override for form's 'action' attribute 
	        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
	        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
	        //clearForm: true        // clear all form fields after successful submit 
	        //resetForm: true        // reset the form after successful submit 

	        // $.ajax options can be used here too, for example: 
	        //timeout:   3000 
	    };

		$("#subscription-form").ajaxForm(options);

		$("#signup-submit").click(function(ev){
			ev.preventDefault();
			var email = $("#sign-up-email");

			var error = $("#error");
			error.css('opacity',0);
			var msg = "";

			if(email.attr("value").length == 0 || email.attr("value") == "Enter Your Email Address"){
			    msg = "<strong>Oops!</strong> Please enter your email address.";
			}

			var isEmail_re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
			if(email.attr("value").search(isEmail_re) == -1){
				msg = "<strong>Oops!</strong> That doesn't look like an email address."; 
			}

		    if(msg.length > 0){

		    	$('#error span').html(msg);
				error.animate({
					opacity: 1
				});
				msg = "";
				return false;

			}else{

				$("#subscription-form").submit();

			}

		});

	},
	showRequest: function(formData, jqForm, options){
		var queryString = $.param(formData);
		$("#loading").animate({
			opacity: 1
		});		
	},
	showResponse: function(responseText, statusText){
		$("#loading").animate({
			opacity: 0
		});		
		var msg = "<h2>Thank You for signing up for Track Talk</h2><p>Thank you for selecting to receive e-newsletters from Valvoline. You can update your subscriptions at any time by visiting <a href=\"http://newsletters.valvoline.com\" class=\"ext\">My Subscriptions</a>.</p>"

		var responseActions = responseText.replace(/^\s+|\s+$/g,"").split(":");
		var dmOptIn = responseActions[0] == "dm1" ? true : false;
		var ttOptIn = responseActions[1] == "tt1" ? true : false;
		var invalidEmail = responseActions[0] == "invalidEmail" ? true : false;
		
		var optIns = [dmOptIn,ttOptIn]
		
		if(invalidEmail){
			var msg = "<h2>You've already signed up for Track Talk</h2><p>We're sorry, our records show the email address you are entering is already signed up for Valvoline Newsletters.  If you are not receiving emails from Valvoline, <a href=\"our-business/contact-us\">click here</a> for Valvoline assistance. If you need to update your email address, simply manage your account by going to <a href\"http://newsletters.valvoline.com\" class=\"ext\">My Subscriptions</a>.</p>"
		}
		
		$("#track-talk").animate({
					opacity: 0
				}).html(msg).animate({
					opacity: 1
		});
		
		$("#track-talk .ext").each(function(){
			$(this).attr("target","_blank");
		});
	}
});