$(function() {
	var cache = [];
	$.presentation = function() {
		$(document).everyTime("5s", function(i) {
			min=1;
			max=5;
			i=Math.floor(Math.random()*(max-min+1))+min;
			$('#promo_image').css('background', "url('/templates/images/demo1/"+i+".jpg')");
			$('.promo_text').hide();
			$('#promo_'+i).show();
		});
	};
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
		  var cacheImage = document.createElement('img');
		  cacheImage.src = arguments[i];
		  cache.push(cacheImage);
		}
		$.presentation(1, 8);
	};
	$.preLoadImages(
		"/templates/images/demo1/1.jpg",
		"/templates/images/demo1/2.jpg",
		"/templates/images/demo1/3.jpg",
		"/templates/images/demo1/4.jpg",
		"/templates/images/demo1/5.jpg",
		"/templates/images/demo1/6.jpg",
		"/templates/images/demo1/7.jpg",
		"/templates/images/demo1/8.jpg",
		"/templates/images/uk.png"
	);
	$('a.lightbox').lightBox();
    $('a.fancybox').fancybox();
});
$(document).ready(function(){
	$("#callback").click(function(){
		$.fancybox({
			href: '#callback_form',
                        onClosed: function() {
                            $('#callback_error').hide();
                        },
			autoScale: 'true'
		});
	});
        $('#callback_form_submit').bind('submit', function(){
            if ($("#subject").val().length < 1 || $("#contacts").val().length < 1) {
                $("#callback_error").show();
                $.fancybox.resize();
                return false;
            };
            $.fancybox.showActivity();
            $.ajax({
			type		: "POST",
			cache	: false,
			url		: "/callback/",
			data		: $(this).serializeArray(),
            success: function(data) {
			$.fancybox('<b>Спасибо, Ваше сообщение отправленно.<br>В ближайшее время с Вами свяжется наш менеджер.</b>');
		}
            });
            return false;
        });
	$("body").append('<div id="fade"></div>');
	$("body #fade").css("height", $(document).height());
	$("#query").hover(function() {
		$(this).val('');
		$(this).css('color', 'black');
		$(this).css('background-color', 'pink');	
 	}, function() {
 		$(this).css('background-color', 'white');
		$(this).css('color', 'grey');
		$(this).val('поиск');
	});
	$("#eng_flag").hover(function() {
		$(this).attr("src", "/templates/images/uk.png");
	}, function() {
		$(this).attr("src", "/templates/images/uk_bw.png");
	});
	$("#rus_flag").hover(function() {
		$(this).attr("src", "/templates/images/ru.png");
	}, function() {
		$(this).attr("src", "/templates/images/ru_bw.png");
	});
	$(".link").hover(function() {
		$(this).css('background', "url('/templates/images/demo1/bg_menu.jpg')");	
 	}, function() {
		$(this).css('background', 'none');
	});
	$(".footer_link").hover(function() {
		$(this).css('color', "red");	
 	}, function() {
		$(this).css('color', 'white');
	});
	$("#search_title").click(function() {
		$("#search_title").val("");
		$("#search_title").css('color', 'black');
	});
	$("#brands").change(function(){
		$('#brands, #groups').css('background-color', 'white');
		if ( $('#stat').val()!="1" ) {
	        $.ajax({
	            type: "GET",
	            url: "/function/ajax.php",
	            data: "b="+$.browser.opera+"&"+"q=get_groups&subitem="+ $('#brands').val() + "",
	            beforeSend: function() {
	        		$("body #fade").show();
				 	$('#load').show();
	            },
	            success: function (data, textStatus)
	            {
	            	$('#groups option').remove();
	                $("#groups").append( $(data) );
	            },
	            complete: function(){
	            	$(document).oneTime("1s", function() {
	            		$("body #fade").hide();
	           			$('#load').hide();
	            	});
	            	$('#groups').css('background-color', 'pink');
	            	$('#brands').attr('disabled', 'disabled');
	            }
	        });
		};
    });
	$("#groups").change(function(){
		$('#brands, #groups').css('background-color', 'white');
		if ( $('#brands').val()=="-1" ) {
	        $.ajax({
	            type: "GET",
	            url: "/function/ajax.php",
	            data: "b="+$.browser.opera+"&"+"q=get_brands&subitem="+ $('#groups').val() + "",
	            beforeSend: function() {
	        		$("body #fade").show();
				 	$('#load').show();
	            },
	            success: function (data, textStatus)
	            {
	            	$('#brands option').remove();
	                $("#brands").append( $(data) );
	            },
	            complete: function(){
	            	$(document).oneTime("1s", function() {
	            		$("body #fade").hide();
	           			$('#load').hide();
	            	});
	            	$('#stat').val("1");
	            	$('#brands').css('background-color', 'pink');
	            	$('#groups').attr('disabled', 'disabled');
	            }
	        });
		};
	});
	$('#submit').click(function(){
		$('#groups').attr('disabled', '');
		$('#brands').attr('disabled', '');
		$('#form').submit();
	});
	$('#reset').click(function(){
		$('#brands, #groups').css('background-color', 'white');
		$.ajax({
            type: "GET",
            url: "/function/ajax.php",
            data: "b="+$.browser.opera+"&"+"q=get_groups&subitem=0" + "",
            beforeSend: function() {
				$("body #fade").show();
			 	$('#load').show();
            },
            success: function (data, textStatus)
            {
            	$('#groups option').remove();
                $('#groups').append( $(data) );
                $('#groups').attr('disabled', '');
            }
        });
		$.ajax({
            type: "GET",
            url: "/function/ajax.php",
            data: "b="+$.browser.opera+"&"+"q=get_brands&subitem=0" + "",
            success: function (data, textStatus)
            {
            	$('#brands option').remove();
                $("#brands").append( $(data) );
            },
            complete: function(){
            	$(document).oneTime("1s", function() {
            		$("body #fade").hide();
           			$('#load').hide();
            	});
            	$('#brands').attr('disabled', '');
            	$('#stat').val("0");
            }
        });
	});
});
