$(document).ready(function(){

//Ресайз
function fixHeight(block){
	pageHeight = $("#completePageContent").height();
	elemHeight = $(block).height();
	
	if(elemHeight < pageHeight) $(block).css('height', pageHeight);
	
	//Костыли для ...
	if ($.browser.msie && $.browser.version == 6) {
	$(document).pngFix();
	}
}

$("#moduleNavigation").click(function() {
	$(".dropDownContainer").hide();
	$("#topMenuContainer li").removeClass('active');
});
$("#metaNavigation").click(function() {
	$(".dropDownContainer").hide();
	$("#topMenuContainer li").removeClass('active');
});
$("#completePageContent").click(function() {
	$(".dropDownContainer").hide();
	$("#topMenuContainer li").removeClass('active');
});



$("#topMenuContainer li") .click(function() {
	$(this).find(".dropDownContainer").toggle();
	$(this).toggleClass('active');
});

$(".showSub") .mouseover(function() {
	if($("#topMenuContainer li").hasClass("active") == true){
	$(".dropDownContainer").hide();
	$("#topMenuContainer li").removeClass('active');
	
	$(this).parents("li:first").find(".dropDownContainer").toggle();
	$(this).parents("li:first").toggleClass('active');
	}
});



$(".showSub").mouseover(function() {
	$(this).toggleClass('hover')
});


$("#addFaq").click(function() {
	var options = {};
	$("#faqCont").show('blind',options,1000, function(){
		fixHeight('#faqCont');
	});
});

$("#addOtz").click(function() {
	var options = {};
	$("#otzCont").show('blind',options,1000, function(){
		fixHeight('#otzCont');
	});
});

$(".closeWindow").click(function() {
	var options = {};
	$(".fullNewsCont").hide(1000);
});

$(".link").click(function() {
	var options = {};
	if($(this).parents(".leftSubItem:first").find("ul.sub").css("display") == 'none'){
	$(".sub").hide(500);
	}
	$(this).parents(".leftSubItem:first").find("ul.sub").toggle('blind',options,"normal");
	return;
});

    $("#collapse").click(function () {
	  if($.browser.opera || $.browser.msie || $.browser.mozilla){
	  $("#moduleNavigation").toggleClass("smHeight", 500);
	  }
	  else
	  {
	  $("#moduleNavigation").toggleClass("smHeight");
	  }
	  $("#collapse").toggleClass("closed");
	  return false;
    });

$(".showFull").click(function() {
	var options = {};
	var curId = $(this).attr('id');
	
	$("#full_" + curId).show('blind',options,1000, function(){
		fixHeight();
	});
});

Math.round()

//faqCont fullFaqText

$(".fullFaqLink").click(function () {
var options = {};
$("#faqTextCont").show('blind',options,1000, function(){
		fixHeight('#faqTextCont');
	});

var pageHtml = $(this).attr('title');
//alert(pageHtml);

$("#fullFaqText").load(pageHtml)

});


$('#vacNazv').ready(function() {
	$('#vacNazv').val($('h1').html());
});

$('#info').click(function() {
	$("#helpContInn").show();
});
$('#helpClose').click(function() {
	$("#helpContInn").hide();
});

$('#dilerContainer').ready(function() {
	$("#dilerContainer").load('/how/list.php');
});

$('#okrug').change(function() {
	$("#obl").load('/how/kray.php?spravId=' + $(this).val());
	//loadDilers();
});
$('#obl').change(function() {
	//loadDilers();
});

$('#polKred').change(function() {
	if($('#polKred').attr('checked') == false)
	{
		
		$('#pogKred').attr('checked', 'true');

	}
	
});
$('#pogKred').change(function() {
	if($('#pogKred').attr('checked') == false)
	{
		
		$('#polKred').attr('checked', 'true');

	}
	
});

$('#submFilter').click(function() {
loadDilers();								
});							  


function loadDilers(){
	var path = '?';
	if($('#polKred').attr('checked') == true){path += 'f_10_3=1'}
	if($('#pogKred').attr('checked') == true){path += '&f_11_3=1'}
	
	if($("#okrug").val() != '-1' && $("#obl").val() != '-1'){
		path += '&f_1_3=' + $("#okrug").val() + ',' + $("#obl").val()
	}
	
	//alert('ss');
	
	$("#dilerContainer").load('/how/list.php' + path)
}

$(".city").change(function() {
	//filterCalcList();
	if($(this).val() != ''){
	$("#diler").load('/calc/diler.php?spravId=' + $(this).val(), function(){
		$("#diler").change(function() {
			$("#prog").load('/calc/progs.php?spravId_2=' + $(this).val());
			var curDiler = $(this).val();
			$(".carLink").each(function() {
				if(curDiler != ''){
					curHref = $(this).attr('href').split('?')[0] + '?diler=' + curDiler;
				}
				if(curDiler == ''){
					curHref = $(this).attr('href').split('?')[0];
				}
				$(this).attr('href', curHref);
			});
		});
	});
	}
	else
	{
		$("#diler").html('<option>Все дилеры</option>')
	}
	
	$(".fProgram").attr('checked', false);
	$(".carCont").css('outline', 'none');
	
	var cityVal = $(".city").val();
	//var progVals = new Array();
	
	$(".catFilterItem").each(function (i) {
		if($(this).attr('id').indexOf(cityVal) == '-1'){ 
			$(this).hide();
		}
		else
		{
			$(this).show();
		}
	});
	
});
$(".catFilterRad input").click(function() {
	filterCalcList();
});

function addNbsp(arg){
	var formattedVal = arg;
	formattedVal = formattedVal.toString();
	formattedVal = formattedVal.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ');
	return formattedVal;
}

$(".fVal").click(function() {
			val = $(this).val();
			curs = 1;
			
			if(val == '2'){
				curs = $("#usdCurs").val()*1;
			}
			if(val == '3'){
				curs = $("#eurCurs").val()*1;
			}
			
	$(".catFilterRad").each(function(i) {
		curVal = $(this).find('input').val();
			curMin = addNbsp(Math.round(curVal.split('|')[0] / curs));
			curMax = addNbsp(Math.round(curVal.split('|')[1] / curs));
			curHtml = curMin + ' - ' + curMax;
				if(i == 0) curHtml = 'менее ' + curMax;
				if(i == ($(".catFilterRad").size() - 1)) curHtml = 'более ' + curMin;
			
			$(this).find('span').html(curHtml);
	});
	
	
});

$("#prog").change(function() {
	filterCalcList();
});



function filterCalcList(){
	var progVals = $("#prog").val();
	var paymVals = '';
	var count = 0;
	
	/*$(".fProgram").each(function (i) {
		if(this.checked == true){
			progVals.push(this.value);
			count++;
		}
	});*/
	$(".fPayment").each(function (i) {
		if(this.checked == true){
			paymVals = (this.value);
			platMin = paymVals.split('|')[0]*1;
			platMax = paymVals.split('|')[1]*1;
		}
	});
	//alert(cityVal);
	//alert(progVals);
	//alert(paymVals);
	
	$(".carCont").each(function (i) {
		curProgs = $(this).find(".progs").val();
		curMin = $(this).find(".min").val()*1;
		curMax = $(this).find(".max").val()*1;
		statusOk2 = 0;
		statusOk3 = 0;
		
			//alert(progVals.length);
			//сверяем программы кредитования
			if(progVals == '') {statusOk2++;}
				else{
						if(curProgs.indexOf(progVals) != '-1'){
							statusOk2++
						}
						else
						{
							statusOk2--
						}
				}
			
			if(paymVals == ''){
				statusOk3++
			}
			//fVal
			
			//alert(platMin);
			
			if(paymVals != '' && platMin >= curMin && platMax <= curMax){
				statusOk3++
			}
			//если все значения фильтра пусты
			if(progVals == '' && paymVals == '') {statusOk2 = -1}
				
			
			
		//Временная заглушка (без выбора города/дилеров/программ
		statusOk2 = 1;
		
		if(statusOk2 > 0 && statusOk3 > 0){
			$(this).find('img').css('border', '1px solid #092E96');
		}
		else
		{
			if($(this).parent('div:first').is('.black')){
				$(this).find('img').css('border', '1px solid #000000');
			}
			else
			{
				$(this).find('img').css('border', '1px solid #FFFFFF');
			}
		}
		
	})
}

$(".carCont").mouseover(function () {
	$(this).find('img').css('border', '1px solid #092E96');
});
$(".carCont").mouseout(function () {
	if($(this).parent('div:first').is('.black')){
				$(this).find('img').css('border', '1px solid #000000');
			}
			else
			{
				$(this).find('img').css('border', '1px solid #FFFFFF');
			}
});
$(".catList").each(function (i) {
	curHgt = $(this).parent('td:first').height();					 
	//$(this).css('height', curHgt + 'px');
});


});