$(document).ready(function() {
  	
	$('.preownedmainrotator1').cycle({ 
    fx:     'scrollHorz', 
    speed:  'slow', 
    prev:   '.arrowright1', 
    next:   '.arrowleft1',
    timeout: 0, 
	speedIn: 700,
    speedOut:700   
  
	});
	$('.preownedmainrotator2').cycle({ 
    fx:     'scrollHorz', 
    speed:  'slow', 
    prev:   '.arrowright2', 
    next:   '.arrowleft2',
    timeout: 0, 
	speedIn: 700,
    speedOut:700   
  
	});

	$('.preownedmainrotator3').cycle({ 
    fx:     'scrollHorz', 
    speed:  'slow', 
    prev:   '.arrowright3', 
    next:   '.arrowleft3',
    timeout: 0, 
	speedIn: 700,
    speedOut:700   
  
	});
	$('.preownedmainrotator4').cycle({ 
    fx:     'scrollHorz', 
    speed:  'slow', 
    prev:   '.arrowright4', 
    next:   '.arrowleft4',
	speedIn: 700,
	timeout: 0, 
    speedOut:700   
  
	});
		$('.bannerholder').cycle({ 
    fx:     'fade', 
    speed:  'slow', 
    timeout: 5000 
	});

	$('.historymainnavprev').css({ opacity: 0});
	$('.historymainnav ul li').click(function(){
	        $('div.historyitem').removeClass('selected');
	        $('.historymainnav ul li').removeClass('selected');
	     	var myIndex = $(this).prevAll().length;
	        $('div.historyscrollermaincontainer').stop().scrollTo( 'div.historyitem:eq('+ myIndex +')', 1500 );
	        $('div.historyitem:eq('+ myIndex +')').addClass('selected');
	        $(this).addClass('selected');
	        var countall = $('.historymainnav ul li').length - 1;
	        if ( myIndex == 0 ) {
	        	$('.historymainnavprev').animate({"opacity": "hide"},500 );	
	        }else { $('.historymainnavprev').fadeTo("slow", 1);};
	        if (myIndex == countall ) {
	        	$('.historymainnavnext').animate({"opacity": "hide"},500 );
	        } else {$('.historymainnavnext').fadeTo("slow", 1);}
	});
	$('.historymainnavprev').click(function(){
		var myIndex = $('.historymainnav ul li.selected').prevAll().length;
		var newindex = myIndex - 1;
		if (newindex ==0) {
		$('div.historyitem').removeClass('selected');
		$('.historymainnav ul li').removeClass('selected');
		$(this).animate({"opacity": "hide"},500 );
		$('div.historyitem').removeClass('selected');
		$('.historymainnav ul li').removeClass('selected');
		$('.historymainnav ul li:eq('+ newindex +')').addClass('selected');
		$('div.historyitem:eq('+ newindex +')').addClass('selected');
		$('div.historyscrollermaincontainer').stop().scrollTo( 'div.historyitem:eq('+ newindex +')', 1500 );
		$('.historymainnavnext').fadeTo("slow", 1);
		} else if (newindex > 0) {
		$('div.historyitem').removeClass('selected');
		$('.historymainnav ul li').removeClass('selected');
		$('.historymainnav ul li:eq('+ newindex +')').addClass('selected');
		$('div.historyitem:eq('+ newindex +')').addClass('selected');
		$('div.historyscrollermaincontainer').stop().scrollTo( 'div.historyitem:eq('+ newindex +')', 1500 );
		$('.historymainnavnext').fadeTo("slow", 1);
		}
		else  {
		return false;
		}
	});
	$('.historymainnavnext').click(function(){
		var myIndex = $('.historymainnav ul li.selected').prevAll().length;
		var newindex = myIndex + 1;
		var countall = $('.historymainnav ul li').length - 1;
		if (newindex == countall ) {
		$('div.historyitem').removeClass('selected');
		$('.historymainnav ul li').removeClass('selected');
		$('.historymainnavprev').fadeTo("slow", 1);
		$(this).animate({"opacity": "hide"},500 );
		$('div.historyitem').removeClass('selected');
		$('.historymainnav ul li').removeClass('selected');
		$('.historymainnav ul li:eq('+ newindex +')').addClass('selected');
		$('div.historyitem:eq('+ newindex +')').addClass('selected');
		$('div.historyscrollermaincontainer').stop().scrollTo( 'div.historyitem:eq('+ newindex +')', 1500 );
		} else if (newindex < countall) {
		$('div.historyitem').removeClass('selected');
		$('.historymainnav ul li').removeClass('selected');
		$('.historymainnav ul li:eq('+ newindex +')').addClass('selected');
		$('div.historyitem:eq('+ newindex +')').addClass('selected');
		$('div.historyscrollermaincontainer').stop().scrollTo( 'div.historyitem:eq('+ newindex +')', 1500 );
		$('.historymainnavprev').fadeTo("slow", 1);
		}
		else  {
		return false;
		}
	});


	////////////////////
	//Menu Animations and fixes//
	$('.mainnav ul').each(function() {
     var menulfix = $(this).width()/2; 
  $(this).css({'marginLeft': -menulfix}) ;
 });
	
	$(".mainnav ul ").css({display: "none"}); // Opera Fix
	
	$(".mainnav > li").hoverIntent(
		function(){
			$(this).find('ul').css({visibility: "visible",display: "none"}).slideToggle(400);
			$(this).find('> a').css({color :'#00b6ff'})
		},
		function(){
		$(this).find('ul').slideUp();$(this).find('> a').css({color :'#999999'})
		}
	);
	
	//End Menu Animations and fixes//
	$("img.rollover").height('75%');
	$("img.rollover").css({"vertical-align":"middle"});
	$("img.rollover").hoverIntent(
	function() { $(this).animate({'height' : '100%'});
	},
	function() { $(this).animate({'height' : '75%'});
	});

	//
	//Dealers Listing//
	$(".dealerslidting div.dealerslistitem").filter(function(idx) {
	    return (idx % 3) == 1
	}).addClass('dealerslistitemalt');
	//End Dealers Listing//
	$(".accordioncontent").accordion({collapsible:true,active: false});
	var countermain = $('.historymainnav ul li').length;
	var countermainulwidth = countermain * 55;
	$(".historymainnav ul").width(countermainulwidth);
	//preowned dropdown
	$('.brand').selectbox();
	$('.year').selectbox();
	$('.price').selectbox();
	$('.formcareer').selectbox();
});

$(window).load(function () {
    var Sum = 0;
    $('.historyitemlisting').each(function () {
        $(this).children('div.historyitem').each(function (i, e) {
            Sum += $(e).outerWidth(true);
        });
        if ($.browser.msie && $.browser.version < 8) {
            var widthadd = ('div.historyitem').length * 40;
            $(this).width(Sum + widthadd);
        } else {
            $(this).width(Sum);
        }
    });


})
