$(document).ready(function(){	
	$('*').click(function(){
		if ($(this).parents(".af").length > 0 || $(this).parents('.search-form-fields').length>0) {
			par = $(this).parents('.btn');
			if (par.length == 0) {	
				if($(this).hasClass('alink')) {
					window.location = $(this).attr('href');
				} else {
					return false;
				}
			}
		} else {
			$(".auth-form-lite,.search-form-small").hide();
		}
	});
	
	$(".feedback-footer,.remove_all").addClass("png");
        //$(".container").height($("body").height()-168);
    $(".btn").click(function(){
		$(this).parents("form").submit();
		return false;
	});
	/*Min/Max width/height*/
	$(".content").minmax();

	/*Left menu toggler*/
	$(".widget-title").click(function(e){
		if($(this).parent().find('div').html() != null) {
			e.preventDefault();
			if(!$(this).parent().hasClass("active")) {			
				$(this).parent().addClass("active");
			} else {
				$(this).parent().removeClass("active");
			}
		}
	});
        /*$(".widget-title").toggle(
            function(){
				if($(this).parent().find('div').html() != null) {
					//$(this).parent().addClass("active");
				}
            },
            function(){
                //$(this).parent().removeClass("active");
            }
        );*/
	/*End of left menu toggler*/
	
        $(".gadget-title").click(function(){
            return false;
        });
	
	/*Auth form*/
	$("#auth").click(function(){
		$(".auth-form-lite").show();
		return false;
	});
	$("input[name='USER_LOGIN']").focus(function(){
			if($(this).val()=='Логин') {
				$(this).val('');
				$("input[name='USER_PASSWORD']").val('');			
				}
		});
	
	$("input[name='USER_LOGIN']").focusout(function(){
			if($(this).val()=='') {
				$(this).val('Логин');
				$("input[name='USER_PASSWORD']").val('123456');			
				}
	});
	$(".auth-form-lite button").click(function(){
		$(this).parents("form").submit();
	});
	$(".auth-form-lite .title").click(function(){
		$(".auth-form-lite").hide();
	});
	/*End of auth form*/
	
	/*Search form*/
	$("#search").click(function(){
		$(".search-form-small").show();
		return false;
	});
	$(".search-form-small .title").click(function(){
		$(".search-form-small").hide();
	});
	$("input[name='q']").focus(function() {
		if($(this).val()=='Ноутбук') $(this).val('');
	});
	$("input[name='q']").focusout(function() {
		if($(this).val()=='') $(this).val('Ноутбук');
	});
	/*End of search form*/
	
	/*Feedback*/
	$("#feedback").click(function(){
		var body = $("body");
		body.append("<div class='overlay' style='height:"+body.height()+"px'></div>");
		$(".feedback").css("margin-top","-"+($(".feedback").height()/2+"px")).show();
		return false;
	});
     $(".close").click(function(){
        var body = $("body");
        $(".feedback-form").css("margin-top",0).hide();
        body.find(".overlay").remove();
        return false;
    });
	/**/	
	$("input[name='name']").focus(function() {
		e = $(this);
		if(e.val()=='ФИО') e.val('');
	});
	$("input[name='name']").focusout(function() {
		e = $(this);
		if(e.val()=='') e.val('ФИО');
	});
        $("input[name='name1']").focus(function() {
		e = $(this);
		if(e.val()=='Ваше имя') e.val('');
	});
	$("input[name='name1']").focusout(function() {
		e = $(this);
		if(e.val()=='') e.val('Ваше имя');
	});
	$("input[name='email']").focus(function() {
		e = $(this);
		if(e.val()=='E-mail') e.val('');
	});
	$("input[name='email']").focusout(function() {
		e = $(this);
		if(e.val()=='') e.val('E-mail');
	});
        $("input[name='email1']").focus(function() {
		e = $(this);
		if(e.val()=='E-mail, телефон') e.val('');
	});
	$("input[name='email1']").focusout(function() {
		e = $(this);
		if(e.val()=='') e.val('E-mail, телефон');
	});
	$("input[name='subject']").focus(function() {
		e = $(this);
		if(e.val()=='Тема') e.val('');
	});
	$("input[name='subject']").focusout(function() {
		e = $(this);
		if(e.val()=='') e.val('Тема');
	});
	$("input[name='code']").focus(function() {
		e = $(this);
		if(e.val()=='Код с картинки') e.val('');
	});
	$("input[name='code']").focusout(function() {
		e = $(this);
		if(e.val()=='') e.val('Код с картинки');
	});
	$(".ta textarea").focus(function() {
		e = $(this);
		if(e.val()=='Сообщение') e.val('');
	});
	$(".ta textarea").focusout(function() {
		e = $(this);
		if(e.val()=='') e.val('Сообщение');
	});

    /*Feedback form submit*/
    var options = {
	beforeSubmit:  showRequest,
    success:       showResponse,
    resetForm: true,
    dataType: "json"
    };

    $('#feedbackForm .btnn').click(function() {
        $('#feedbackForm').ajaxSubmit(options);
        return false;
    });

    function showResponse(data)  {
		$("#feedbackForm").parent().find(".error").find("img").remove();
        if(data.error=='1') {
             $(".error").html('<span style="color:green">Ваше сообщение успешно отправлено</span>');
             $("#cp").attr("src","/bitrix/tools/captcha.php?captcha_sid="+data.captcha);
             $("#cc").val(data.captcha);
        } else {
            $(".error").html('');
            for(i=0;i<data.error.length;i++) {
                 $(".error").append(data.error[i]+'<br/>');
            }
            $("input[name='name']").val(data.fdata.name);
            $("input[name='email']").val(data.fdata.email);
            $("input[name='subject']").val(data.fdata.subject);
            $("input[name='msg']").val(data.fdata.msg);
            $("#cp").attr("src","/bitrix/tools/captcha.php?captcha_sid="+data.captcha);
            $("#cc").val(data.captcha);
        }
    }
	
	function showRequest() {
		$(".feedback .error").html("<img src='/bitrix/templates/spcomp/images/ajax-loader.gif' alt='' />");
	}
	
    /*End feedback form submit*/

    $(".contact-page .address").click(function(){
       $(".address").removeClass("active1");
       $(this).addClass("active1");
       this_id = parseInt($(this).attr("rel"));
       $(".gmaps").html($(".map"+this_id).html());
       $(".map"+this_id).addClass("active");
    });

    /*Show feedback job form*/
    $(".feedback-job").click(function(){
		var body = $("body");
		body.append("<div class='overlay' style='height:"+body.height()+"px'></div>");
                form_obj  = $(".feedback-job-form");
                form_obj.find(".error").html('');
                form_obj.find("h2").html($(this).parent(".jobs-item").find("h3").html());
		form_obj.css("margin-top","-"+(form_obj.height()/2+"px")).show();
                $("input[name='vacancy_name']").val($(this).parent(".jobs-item").find("h3").html());
		return false;
	});
	
    /*Style file input*/
    $("input[type=file]").filestyle({
         image: "/bitrix/templates/spcomp/images/choose-file.gif",
         imageheight : 28,
         imagewidth : 68,
         width : 205
     });
     $(".file").val("Прикрепить файл");

    /*  $("#feedbackJobForm").submit(function(){
        if($("input[name='file']").val()=='') alert("Вы не прикрепили файл")
        return false;
     });*/
	 
	/*Sort tooltips*/
    $(".asc,.desc").hover(function(){
        $(this).find("img").show();
    },function(){
        $(this).find("img").hide();
    });
	
	/**/
	$(".section-item").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});
	
	$('.filter a').click(function(){
		var rel = $(this).attr('rel');
		if ($(this).hasClass('active')) {
			$('#'+rel).attr('checked',false);
		} else {
			$('#'+rel).attr('checked',true);
		}
		$('#filter_form').submit();
	});
	$('.small-photos a').click(function(){
		$('.big-photo img').attr('src',$(this).attr('href'));
		return false
	});
	$('.increase').click(function(){
		var base = $('#base_price');
		var base_val = base.val();
		var count = $('#buy_count');
		var count_val = parseInt(count.text()) + 1;
		$('#price_text').text(count_val * base_val);
		count.text(count_val);
		return false
	});
	$('.decrease').click(function(){
		var base = $('#base_price');
		var base_val = base.val();
		var count = $('#buy_count');
		var count_val = parseInt(count.text()) - 1;
		if (count_val > 0) {
			$('#price_text').text(count_val * base_val);
			count.text(count_val);
		}
		return false
	});
	$('.tocart').click(function(){
		var id = $(this).attr('rel');
		var count = 1;
		if ($('#buy_count').length > 0) {
			count = parseInt($('#buy_count').text());
		}
		$.ajax({
			data: {
				'id': id,
				'count': count
			},
			url: '/ajax.php?a=add',
			dataType: 'json',
			type: 'POST',
			success: function(data){
				if (data['ok'] != '1') {    
					$("body").append("<div class='overlay' style='height:"+$("body").height()+"px'></div>");
					$(".adding .error").html("<span style='color: red'>Товар не удалось добавить в коризну</span>");
					$(".adding").css("margin-top","-"+($(".adding").height()/2+"px")).show();
				} else {
                    $(".basket-line").find(".qty").html("("+data['count']+")");
                    $(".basket-line").find(".price").html(data['price']);
                    $(".basket-line").show();				
					$("body").append("<div class='overlay' style='height:"+$("body").height()+"px'></div>");
					$(".adding .error").html("<span style='color: green'>Товар добавлен в корзину</span>");
					$(".adding").css("margin-top","-"+($(".adding").height()/2+"px")).show();
				}
			}
		});
		return false
	});
	
	$(".small-photos a:first-child").css("margin","5px 24px 5px 0");
	$(".small-photos a:last-child").css("margin","5px 0 5px 24px");
	
	$("input[name='USER_PASSWORD'],input[name='USER_LOGIN']").keydown(function(e){
		key = e.charCode || e.keyCode || 0; 
		if(key==13) {
			$(this).parents("form").submit();
		}
	});

        $('.archive_button').toggle(function(){
            $('#archive .cat-table').show();
        },function(){
            $('#archive .cat-table').hide();
        });

});
