// jQuery Code

jQuery(document).ready(function($){
	  
		if ($('ul.tabs').length > 0) {
				$("div.panes ul.pane").hide();
						
					$("ul.tabs").tabs("div.panes > ul", {
						tabs: 'li.item',
						effect: 'default',
						event: 'click'
					});
					
					$("ul.tabs a").click(function(){
						if ($(this).find('span').text() == 'An Overview') {
							$('a.view-all-specials-link span').text('');
						} else {
							$('a.view-all-specials-link span').text($(this).find('span').text());
						}
						$('a.view-all-specials-link').attr('href',$(this).attr('href'));
					});
				
				
				/* DOUBLE CLICK FUNCTIONALITY - NOT USED */
				/*
					$("ul.home-tabs").tabs("div.panes > ul", {
						tabs: 'li.item',
						effect: 'default',
						event: 'click'
					});
				
				$('ul.home-tabs li.item a').dblclick(function() {
					window.location = $(this).attr('href');
				});
				$("ul.tabs li.item a span").tooltip({
					position: 'top center',
					opacity: '1',
					offset: [15,0]
				});
				*/
		}
		
		if ($('ul.car-image-list').length > 0) {
			$("div.car-image-thumbnails .thumbnail-wrap").scrollable({
				size: 3,
				clickable: true
			});
			$("div.car-image-thumbnails .thumbnail-wrap .items a").click(function() {
				var url = $(this).attr("rel");
				var url2 = $(this).attr("href");
				var wrap = $(".car-image-large").fadeTo("medium", 0.5);
				var img = new Image();
				img.onload = function() {
					wrap.fadeTo("fast",1);
					wrap.find("a").attr("href",url);
					wrap.find("img").attr("src",url2);
				};
				img.src = url2;
			}).filter(":first").click().parent().addClass('active');
			
			// Fancy box functions for car-detail lightbox images
			$("a.fancybox").click(function() {
			
				function formatTitle(title, currentArray, currentIndex, currentOpts) {
					return '<div id="tip7-title" class="clearfix">Click image to cycle through images<span style="float:right;">Image ' + (currentIndex + 1) + ' of ' + currentArray.length + '</span></div>';
				}
			
				// get the image that was clicked on
				thisImage = $(this).attr('href');
				// thisIndexImage = $('a[rel="gallery"][href="'+thisImage+'"]');
				thisIndexImage = $('.active');
				thisIndex = $('ul.car-image-list li.item').index(thisIndexImage);
				var imageList = new Array();
				$('a[rel="gallery"]').each(function(index) {
						imageList[index] = 'http://sunsetimports.com'+$(this).attr('href');
						// imageList[index] = 'http://sunsetimports.synotac1.com'+$(this).attr('href');
				});
				// start the fancybox gallery
				$.fancybox(imageList, 
					{
						'index'	: thisIndex,
						'type'	: 'image',
						'titleFormat'	: formatTitle,
						'titlePosition'	: 'inside',
						'cyclic'	: true,
						'changeFade'	: 0
					}
				);
			});
		}
		
		if ($('#tooltip').length > 0) {
			$('form div.checkboxes').before($('#tooltip'));
			$('.what-is-this').tooltip({tip:'#tooltip',position:'top center',relative:true,predelay:100});
		};
		
		$('div.specials-desc a[href=#more]').click(function() {
			$.fancybox(
			{
				'content'			: $(this).parent().parent().parent().find('div.more').html(),
				'autoDimensions'	:false,
				'width'				:400,
				'height'			:'auto'
			}
			);
		});

});