var img_index = 0;
$(document).ready(function(){
	ieHover(".brand-nav li", "hover");
	ieHover(".brand-box li", "hover");
	ieHover(".offer-block div", "hover");
	
    $.get('/ajax?action=getbrand', function(data) {
			$('.selection select[name=brand]').append(data);
			$('#credit-form select[name=brand]').append(data);
			if (typeof(brand_id) != "undefined" && brand_id != ''){
				$('#credit-form select[name=brand] option[value='+brand_id+']').attr("selected", "selected");
				$('#credit-form select[name=brand]').change();
			}
	});
	
	$('<select class="forms-03" name="brand"><option value="0">выберите марку</option></select>').replaceAll("#credit-form input[name=brand]");
	$('<select class="forms-03" name="model"><option value="0">выберите модель</option></select>').replaceAll("#credit-form input[name=model]");
	$('<select class="forms-03" name="cost"><option value="0">выберите комплектацию</option></select>').replaceAll("#credit-form input[name=cost]");
	
	$("#credit-form select[name=brand]").change(function(){
		$("#credit-form select[name=model] option").remove();
		$("#credit-form select[name=cost] option").remove();
		$.get('/ajax?action=getmodel&brand='+$("option:selected", this).val(), function(data) {
			$('#credit-form select[name=model]').append(data).show();
			if (typeof(model_id) != "undefined" && model_id != ''){
				$('#credit-form select[name=model] option[value='+model_id+']').attr("selected", "selected");
			}
			$('#credit-form select[name=model]').change();
		});
	});
	
	$("#credit-form select[name=model]").change(function(){
		$("#credit-form select[name=cost] option").remove();
		$.get('/ajax?action=getcompl&model='+$("option:selected", this).val(), function(data) {
			$('#credit-form select[name=cost]').append(data).show();
		});
	});
	
	$("#credit-form").submit(function() {
		if ($("#credit-form select[name=brand] option:selected").val() == 0) {
			alert("выберите марку");
			return false;
		} else if ($("#credit-form select[name=model] option:selected").val() == 0) {
			alert("выберите модель");
			return false;
		} else if ($("#credit-form select[name=cost] option:selected").val() == 0) {
			alert("выберите комплектацию");
			return false;
		} else {
			$("#credit-form select[name=brand] option:selected").val($("#credit-form select[name=brand] option:selected").html());
			$("#credit-form select[name=model] option:selected").val($("#credit-form select[name=model] option:selected").html());
			$("#credit-form select[name=cost] option:selected").val($("#credit-form select[name=cost] option:selected").html());
			return true;
		}
	});
	
	if (brand != '') {
		$('.grey-border-box').show();
		$('.grey-border-box #'+ brand).show();
	}		
    
	$('.colors img:first').addClass('active');
	
	$(".tab_content").hide();
	$("ul.tabs li:first a").addClass("active"); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li a").click(function() {

		$("ul.tabs li a").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		if (activeTab == '#compl')
			$('.viewall').show();
		else 
			$('.viewall').hide();
		return false;
	});

	$(".car-description .previews a").click(function(){
		$(".car-description .current").removeClass("current");
		$(this).parents("li").addClass("current");
		img_index = parseInt($(this).attr("class").replace("img", ""));
		$(".car-description .full-size a").attr("href", $(this).attr("href"));
		$(".car-description .full-size-img").attr("src", '/uploads/images/tradein/r3_' + $(this).attr("href").replace("/uploads\/images\/tradein/", ""));
		return false;
	});
	
	$(".img-wrap .full-size img").click(function(){
		//alert(img_index)
		jQuery.slimbox(img_array, img_index, {counterText: "Изображение {x} из {y}"});
		return false;
	});
	
	$("#feedbackform").submit(function() { 
                
		if($("#feedbackform #name").val() == "" || $("#feedbackform #name").val() == "Имя")   
			$("#div-feedbackform").html("Сообщите ваше имя!");
		
		else if($("#feedbackform #phone1").val() == "" || $("#feedbackform #phone1").val() == "Телефон")
			$("#div-feedbackform").html("Сообщите ваш телефон!");
			
		else if($("#feedbackform #extra").val() == "" || $("#feedbackform #extra").val() == "Сообщение")
			$("#div-feedbackform").html("Напишите ваше сообщение!");
			
		else 
		{
			$.post("/ajax?action=feedback",
			{ name: $("#feedbackform #name").val(), 
			  phone: $("#feedbackform #phone1").val(),
			  mail: $("#feedbackform #mail").val(),
			  extra: $("#feedbackform #extra").val()
			},
				function(data){
					$("#div-feedbackform").html(data);
			});
		}
		
		return false;
	});
	
	$("table tr.car").hover(function(){
        $(this).toggleClass("tr_hover");
		$("table tr.car td a").toggleClass("hover");
    });
});

function ieHover(h_list, h_class){
	if($.browser.msie && $.browser.version < 7){
		if(!h_class) var h_class = 'hover';
		$(h_list).mouseenter(function(){
			$(this).addClass(h_class);
		}).mouseleave(function(){
			$(this).removeClass(h_class);
		});
	}
}

function PopUp(url, name, width,height,center,posleft,postop)
{
	showx = "";
	showy = "";
	
	if (posleft != 0) { X = posleft; }
	if (postop  != 0) { Y = postop  }
	
	if ((parseInt (navigator.appVersion) >= 4 ) && (center))
	{
		X = (screen.width  - width ) / 2;
		Y = (screen.height - height) / 2;
	}
	
	if (posleft == 0)
	{
		X = (screen.width  - width ) / 2;
	}	
	if ( X > 0 )
	{
		showx = ',left='+X;
	}

	if(postop  == 0)
	{
		Y = (screen.height - height) / 2;
	}	
	if ( Y > 0 )
	{
		showy = ',top='+Y;
	}
	
	Win = window.open( url, name, 'width='+width+',height='+height+ showx + showy + ',resizable=no,scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no');
	return Win;
}

function em(box, server, zone, img)
{
 var at = String.fromCharCode(64);
 if (img == 1)
 	return '<a href="'+'mai'+'lto'+':'+box+at+server+'.'+zone+'">';
 else
 	return '<a href="'+'mai'+'lto'+':'+box+at+server+'.'+zone+'">'+box+at+server+'.'+zone+'<'+'/a>';
}

function calculate()
{
    with (document.forms['fm1'])
    {
        if(cost.value < 120000)
        {
            alert("Кредит предоставляется на автомобили дороже 120 000 руб."); 
            return false; 
        }
        
        var fpr = firstpersent.value;
        var sum = cost.value*(1-fpr/100);
        var pc = 8;
        
        firstpay.value = Math.round(cost.value*fpr/100);
        monthpay.value = Math.round((sum*pc/1200)/(1-Math.pow(1/(1+pc/1200), period.value)));
    }
}
