window.$ = jQuery;

jQuery(document).ready(function($) {

	pd_main_menu_hide($);

	pd_site_title_hover($);

	pd_dealers_list($);
	
	//$('a:has(img)').addClass('img');
	
	$('#secondary').openCloseTab(350);

});

// Stepcarousel can't run inside of .ready(), because it calls it on setup
jQuery('div.stepcarousel').each(function() {
	var galid = $(this).attr('id');
	
	var opts = {
			galleryid: galid, //id of carousel DIV
			beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
			panelclass: 'panel', //class of panel DIVs each holding content
			panelbehavior: {speed:300, wraparound:true, persist:true},
			defaultbuttons: {enable: false },
			statusvars: ['viewStart', 'viewEnd', 'total'], // Register 3 "status" variables
			onslide: function(){},
			contenttype: ['inline'] // content type <--No comma following the very last parameter, always!
	};
	
	stepcarousel.setup(opts);
	
	jQuery(document).keydown(function(event) {
	    switch (event.keyCode) {
	        case 37: case 80: stepcarousel.stepBy(galid, -2); break; // left / p
	        case 38: stepcarousel.stepTo(galid, 2); break; // up 
	        case 39: case 78: stepcarousel.stepBy(galid, 2); break; // right / n
	        case 40: stepcarousel.stepTo(galid, total); break; // down 
	    }
	});
	
});

jQuery(window).load( function() {
	jQuery('#secondary').show();
	
	var galid = jQuery('div.stepcarousel').attr('id');
	var active = $('div.active', '#'+galid).index();
	if (active > 0) {
		stepcarousel.stepTo(galid, active );
	}
});

// These three lines replace the 223 line animatedcollapse.js library from the old site 
jQuery.fn.slideFadeToggle = function(speed, easing, callback) { 
        return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback); 
};

jQuery.fn.openCloseTab = function(speed, easing, callback) { 
	this.wrapInner('<div class="wrap"></div>');
	
	var tab = $('div.openclose', this);
	
	var left = parseInt( this.css('left') );
	var dat = { 
		'open': {
			'paddingRight': parseInt( $('#container').css('padding-right') )
		},
		'closed': {
			'paddingRight': parseInt( $('#container').css('padding-right') ) - this.width() 
		}
	};
	
	tab.data( 'dat',  dat);
	
	tab.click( function() {
		var dat = $(this).data('dat');
		
		if ( $(this).hasClass('open') ) {
			$('#container').stop().animate( {
				paddingRight: dat.closed.paddingRight+'px'
			}, speed, easing);
		
			$(this).removeClass('open').text('Open');
		}else {
			$('#container').stop().animate( {
				paddingRight: dat.open.paddingRight+'px'
			}, speed, easing);
			
			$(this).addClass('open').text('Close');
		}
	});
	
	if ( ! $.browser.webkit ) {
		tab.removeClass('hide');
		// Tab animation not working in WebKit yet.
	}
	
	return this; 
};

function pd_main_menu_hide($) {
	
	// Keep section open when on a Single post
	$('#menu-main li.current-menu-parent').parents('li.menu-item').addClass('current-menu-parent');
	
	// Open and close submenus
	$('#menu-main > li ul')
		.not('#menu-item-240 ul') // 240 = Team
		.prev('a')
			.click( function() {
				$(this).next('ul').slideFadeToggle(500);
				return false;
			});
			
	// Go nowhere if current menu item is clicked
	$('#menu-main li.current-menu-item > a')
		.click( function() {
			return false;
		});
	
}

function pd_site_title_hover($) {
	if ( $.browser.msie && !$('body').hasClass('singular-pt') ) {
		selector = '#nothing123alkfj';
	}else {
		selector = '#container';
	}
	
	$('#site-title a').hover(
		function() {
			$(selector).stop().animate({ 'opacity': '0' }, 400);
			$(this).stop().animate({ 'opacity': '.2' }, 400);
		},
		function() {
			$(selector).add(this).stop().animate({ 'opacity': '1' }, 400, function() {
				// IE opacity filter butchers text display
				// Disable the filters completely once Opacity = 100%
				$(this).css('filter', 'none').css('-ms-filter', 'none');
			});
		}
	);
}

function pd_dealers_list($) {
	$('#dealer_state_list a').click( function() {
		var state = $(this).attr('href');
		
		$('#dealer_state_list a.active').removeClass('active');
		$(this).addClass('active');
		
		$('#dealer_list > li')
			.not(state)
				.slideUp()
				.end()
			.filter(state)
				.slideDown();
		
		return false;
	});
}