// JQUERY CUSTOM COMMANDS
$(document).ready( function() {  // start javascript when document is loaded
	
	// HEADER VISUAL CYCLE (uses Cycle plugin)
	$('#headerVisual').cycle({ 
	    fx:    'fade', 
	    speed:  2500,
		pause:  3,
		random:  1 
	 });
	 $.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
	    $(pager).find('li').removeClass('activeSlide') 
	        .filter('li:eq('+currSlideIndex+')').addClass('activeSlide'); 
	 };
	 $('.columnTweets ul.slides').before('<ul class="pager clearfix">').cycle({ 
	    fx:    'scrollHorz', 
	    speed:  1500,
		timeout: 5000,
		pause:  1,
		prev: '.columnTweets .prev',
		next: '.columnTweets .next',
		pager: '.columnTweets .pager',
		pagerAnchorBuilder: paginate
	 });
	 function paginate(ind, el) {
		return '<li title="Toon Tweet ' + (ind +1) + '">' + (ind +1) + '<\/li>';
	 }
	 
	
	// FORMS - focus and blur
	$('.input input, .input textarea').focus(function() {
		$(this).select().parent().addClass('focus');
	});
	$('.input input, .input textarea').blur(function() {
		$(this).parent().removeClass('focus');
	});
	// FORMS - clicking on hint
	$('.input .hint').click(function() {
		$(this).parent().addClass('focus').find('input').select();
	});
	// FORMS - Empty filled fields on blur
	$('.input input, .input textarea').each(function() {
		$(this).blur(function() {
			$(this).parent().removeClass('focus');
			if ($(this).val() != "") {
				$(this).parent().addClass('filled');
			} else {
				$(this).parent().removeClass('filled');
			};
		});
	});
	// FORMS - Empty filled fields on load
	$('.input input, .input textarea').each(function() {
		if ($(this).val() != "") {
			$(this).parent().addClass('filled');
		} else {
			$(this).parent().removeClass('filled');
		};
	});

/*
	// LOGIN FIELDS (1/2)
	$("#frm_login input.textfield").each(function() {
		if ($(this).val() == "") {
			$(this).parent().removeClass("filled");
		} else {
			$(this).parent().addClass("filled");
		}
	});
	$("#frm_login input.textfield").keyup(function() {
		if ($(this).val() == "") {
			$(this).parent().removeClass("filled");
		} else {
			$(this).parent().addClass("filled");
		}
	});
	$("#frm_login input.textfield").blur(function() {
		if ($(this).val() == "") {
			$(this).parent().removeClass("filled");
		} else {
			$(this).parent().addClass("filled");
		}
	});
*/
	
	// LISTER
	$("tbody tr:odd").addClass('odd');
	$("tbody tr:even").addClass('even');
	$("tbody tr:first").addClass('first');
	$("tbody tr:last").addClass('last');
	$('tbody tr').hover(function() {
	    $(this).addClass('hover');
	}, function() {
	    $(this).removeClass('hover');
	});
	// remove onclick
	$('tbody tr a').click(function(){
		$(this).parents('tr').removeAttr('onclick');
	})
	// make all of TD clickable
	$('.productList td').click(function(event) {
		if ($(event.target).is(":not('a, input')") ) {
			window.location = $(this).parents('tr').find('td.description a').attr("href");
		}
	});
	
	
	// DEALER MAP
	$('#dealerMap li').hover(function() {
		$('#dealerMap .dealer').hide('fast');
		$(this).find('.dealer').show('fast');
		$(this).css('z-index', '2');
	}, function() {
		$(this).find('.dealer').hide('fast');
		$(this).css('z-index', '1');
	});

	
	// ROLLOVER IMAGES (1/2)
	IMAGE.rollover.init();
   
}); // end ready function


// ROLLOVER IMAGES (2/2)
IMAGE = {};
IMAGE.rollover = {
   init: function() {
      this.preload();
      $(".rollover").hover(
         function () { $(this).attr( 'src', IMAGE.rollover.newimage($(this).attr('src')) ); },
         function () { $(this).attr( 'src', IMAGE.rollover.oldimage($(this).attr('src')) ); }
      );
   },
   preload: function() {
      $(window).bind('load', function() {
         $('.rollover').each( function( key, elm ) { $('<img>').attr( 'src', IMAGE.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   newimage: function( src ) {
      return src.substring( 0, src.search(/(\.[a-z]+)/) ) + '_on' + src.match(/(\.[a-z]+)/)[0];
   },
   oldimage: function( src ) {
      return src.replace(/_on/, '');
   }
};


