$(document).ready(
	function() {

      $('.lightbox').fancybox();

		// Kotik
		/*$.preloadImages('/images/kotik/green_cat.gif');
		$('#kotik')
			.mouseover(function() {
				$(this).find('img').attr('src', '/images/kotik/green_cat.gif');
			})
			.mouseout(function() {
				$(this).find('img').attr('src', '/images/kotik/green_cat_inv.gif');
			});*/
      $(document).ready(function(){
         $('#seo_market').mouseover(
               function(){
                  $('.over').show().siblings().hide();
               }).mouseout(function(){
                  $('.simple').show().siblings().hide();
               });
      });

		
		
		// Test-drive LINK
		$('#test-drive-top a').click(function() {
			showTestDriveForm();
			return false;
		});
		
		
		// Cars thumbnails menu - hover
		var srcList = [];
		$('#cars-list .car img').each(function() {
			var src = $(this).attr('src').replace('_active', '');
			var file = getExt(src);
			var src_active = file.name + '_active.' + file.ext;
			srcList.push(src);
			srcList.push(src_active);
		});
		$.preloadImages(srcList);
		
		
		$('#cars-list .car')
			.mouseenter(function() {
				if (!$(this).hasClass('active')) {
					var color = $('.deco-link a', $(this)).attr('carcolor');
					$('.deco-link', $(this))
						.css('background-color', '#' + color)
						.find('a').css({
							'color': '#fff',
							'text-decoration': 'none'
						});
					var img = $('a img', $(this));
					var file = getExt(img.attr('src'));
					img.attr('src', file.name + '_active.' + file.ext);					
				}
			})
			.mouseleave(function() {
				$('.deco-link', $(this))
					.attr('style', '')
					.find('a')
						.attr('style', '');
						
				if (!$(this).hasClass('active')) {
					var img = $('a img', $(this));
					var file = getExt(img.attr('src'));
					img.attr('src', file.name.replace('_active', '') + '.' + file.ext);
				}	
					
			})
			
		// Secondary menu container 
		$('.secondary-menu-container .menu-item, #secondary-menu-container .menu-item')
			.mouseenter(function() {
				if (!$(this).hasClass('active')) {
					$(this).addClass('hover');
				}
			})
			.mouseleave(function() {
				$(this).removeClass('hover');
			})
			.click(function() {
				window.location = $(this).find('a').attr('href');
			});
		
		// Jquery LightBox
       	$('.picture-gallery a').lightBox({fixedNavigation: true});
		
		$('a.lightbox').lightBox({fixedNavigation: true});
		
		// MBTables
		$('.mbTable').mbtable();
		$('.sortable').tablesorter({sortList: [[1,0]], headers:{ 0: {sorter: false}}, widgets: ['zebra'] }); 
	}
)

var shadowFormHere = false;
function showTestDriveForm() {
	$.shadow();
	if (shadowFormHere) {
		$('#shadow-form').show();
	}
	else { 
		$.get('/flash/shadow.html', function(result){
			$('#shadow-form').show();
			$('#flashPlace').html(result);
		});
		shadowFormHere = true;
	}
}

function closeForm() {
	$.unshadow();
	$('#shadow-form').hide();
}
// javascript:showTestDriveForm()
function getExt(file) {
	var index = file.lastIndexOf('.');
	var result = {};
	result.name = file.substr(0, index);
	result.ext = file.substr(index + 1);
	return result;	
}
