jQuery(document).ready(function() {
	/*добавление NEW в меню*/
	jQuery(".item2:contains('Sonata'), .item2:contains('Solaris'), .menu_menu4 span:contains('Sonata'), .menu_menu4 span:contains('Solaris'), .item2:contains('Elantra'), .menu_menu4 span:contains('Elantra')").append('<span class="new"> NEW</span>');
	
	/*добавление машинки в левое меню*/
	if(jQuery('.moduletable_menu4').length) {
		var position = jQuery('.moduletable_menu4').position().top+jQuery('.moduletable_menu4').height()+30;
		jQuery('#left').append('<div style="position:absolute;left:18px;top:'+position+'px;"><img src="templates/aa3/images/menu_solaris.png"></div>');
	}
	
	/*поддержка jQuery UI dialog для удаленного контента*/
	jQuery('.popup').click(function(){
		accent.popup(jQuery(this).attr('title'), jQuery(this).attr('width'), jQuery(this).attr('href'));
		return false;
	});
	
	/*выравнивание кнопок*/
	max = 0;
	jQuery.each(jQuery('.complectation_buttons .button2'), function(i,e){
		max = Math.max(max, jQuery(e).width());
	});
	jQuery('.complectation_buttons .button2').width(max);

	/*замена звездочек на галочки*/
	if(jQuery('.com').length > 0) {
		jQuery('.com').html(jQuery('.com').html().replace(/\*/gi, '<span class="required">&nbsp;</span>'));
		jQuery('.remark').text('*');
	}

	/*замена подменю автомобилей*/
	setTimeout('accent.replace_cars_submenu()', 100);
	
	/*табы на странице комплектаций*/
	jQuery('.section .box').css({display: 'none'});
	jQuery('.section .button3').eq(0).click(function(){
		jQuery('.section .box').removeClass('visible');
		jQuery('.section .box').eq(0).addClass('visible');
	});
	jQuery('.section .button3').eq(1).click(function(){
		jQuery('.section .box').removeClass('visible');
		jQuery('.section .box').eq(1).addClass('visible');
	});

	jQuery('.date_input').datepicker({
		onSelect: function(){
			jQuery(this).removeAttr('href');
			jQuery(this).change();
		}
	});
});

/*центрирование jQuery UI dialog*/
jQuery(window).resize(function(){
	jQuery('.ui-dialog').position({at:'center center',my:'center center',of:window});
});

var accent = {
	dialog: null,
	popup: function(title, width, url){
		accent.dialog = jQuery('<span title="'+title+'">Загрузка...</span>').dialog({
			modal: false,
			width: width,
			open: function(event, ui){
				jQuery.get(url+'?tmpl=component', function(data){
					jQuery('.ui-dialog-content').html(data);
					jQuery('.ui-dialog').position({at:'center center',my:'center center',of:window});
					
					/*сравнение комплектаций*/
					jQuery('.ui-dialog .serv').css({
						'max-height': (jQuery(window).height()-128)+'px', 
						'max-width': (jQuery('.ui-dialog .serv').parents('.ui-dialog').width()-15)+'px', 
						overflow: 'auto', 
						display: 'block', 
						margin: '0 0 0 10px'
					});
					if(jQuery('.ui-dialog .serv').length > 0) {
						jQuery('.ui-dialog .serv').html(jQuery('.ui-dialog .serv').html().replace(/\*/gi, '<span class="required">&nbsp;</span>'));
						jQuery('.remark').text('*');
					}
				})
			},
			close: function(){
				jQuery(accent.dialog).remove();
			}
		});
	},
	replace_cars_submenu: function(){
		/*замена подменю автомобилей*/
		if(jQuery('#menu47-1-section').length > 0 && jQuery('#menu47-1-section-replacement').length > 0) {
			jQuery('#menu47-1-section').html(jQuery('#menu47-1-section-replacement').html());
		} else {
			setTimeout('accent.replace_cars_submenu()', 100);
		}
	}
}

/*сравнение комплектаций*/
function hide(){
	var toHide = []
	var nothingToKeep = true
	var chckbx = document.getElementById('direct').getElementsByTagName('input')
	for(var i = 0, len = chckbx.length; i < len; i++){
		if(chckbx[i].type == 'checkbox'){
			if(chckbx[i].checked){
				nothingToKeep = false
			}
			else{
				toHide.push(chckbx[i].value)
			}
		}
	}

	if(nothingToKeep){
		alert('Не выбрано ни одной комплектации для сравнения!')
	}
	else{
		var th = document.getElementById('tbl').getElementsByTagName('th')
		var td = document.getElementById('tbl').getElementsByTagName('td')
		for(i = 0, len = th.length; i < len; i++){
			for(var j = 0, ln = toHide.length; j < ln; j++){
				var pattern = new RegExp(toHide[j])
				if(th[i].className.match(pattern)){
					th[i].style.display = 'none'
				}
			}
		}
		for(i = 0, len = td.length; i < len; i++){
			for(j = 0, ln = toHide.length; j < ln; j++){
				pattern = new RegExp(toHide[j])
				if(td[i].className.match(pattern)){
					td[i].style.display = 'none'
				}
			}
		}
	}
}
function show(){
	var th = document.getElementById('tbl').getElementsByTagName('th')
	var td = document.getElementById('tbl').getElementsByTagName('td')
	for(var i = 0, len = th.length; i < len; i++){
		th[i].style.display = ''
	}
	for(i = 0, len = td.length; i < len; i++){
		td[i].style.display = ''
	}
}
