$(function(){
	
	$("#reset_filter").live("click",function(){
		$("#cat_filter :checked").removeAttr("checked");
		$(this).parents("form").trigger("submit");
	})
	$("div.info_pop q").live("click",function(e){
        e.preventDefault();
        $("div.info_pop").remove();
    });
    $("#apply_filter").live("click",function(e){
            e.preventDefault();
            $('#cat_filter').submit();
    });
	
	 // неактивные блоки в корзине

    $("div.dop_form > div.df_top input").change(function(){
        var z = $(this).parent().children("input:checked").length;
        if(z > 0) {
            $(this).parent().parent().parent().removeClass("inactive");
        } else {
            $(this).parent().parent().parent().addClass("inactive");
        }
    });
	
	
    
    // ---------------------------------------------------------------------
	
	if($("#menu").length > 0) {
		var $menu_a = $("#menu a.mb");
		var menu_off;
		var menu_cont;
		var menu_hover = false;
		$menu_a.click(function(e){
			$("#pm").remove();
			var rel = $(this).attr("rel");
			if(rel !== "" && $("#"+rel).length){
				e.preventDefault();
				menu_off = $(this).offset();
				menu_cont = $("#" + rel).html();
				var html = '<div id="pm"><div id="pm_top"></div><div id="pm_cont"><ul>';
				html += menu_cont;
				html += '</ul></div><div id="pm_bot"></div></div>';
				$("body").append(html);
				leftP = (menu_off.left - 45 < 0)?0:menu_off.left - 45;
				$("#pm").css("left",leftP).css("top",menu_off.top + 22).css("z-index","10000");
				$("#pm").mouseenter(function(){
					menu_hover = true;
				});
				$("#pm").mouseleave(function(){
					menu_hover = false;
				});
			}
		});
		$(document).mousedown(function(){
			if(menu_hover == false) {
				$("#pm").remove();
			}
		});
               $("#pm_cont a").live("click",function(){
                   		$("#pm").remove();
		})
                
	}
	
	
	
	
    $("#searchString").click(function(){
        if($(this).val() == 'Поиск по сайту') $(this).val('');
    })
    
    $("#searchString").blur(function(){
        if($(this).val() == '') $(this).val('Поиск по сайту');
    })
    
    $("#login").click(function(){
        if($(this).val() == 'email') $(this).val('');
    })
    
    $("#login").blur(function(){
        if($(this).val() == '') $(this).val('email');
    })
    
    $("#pass").click(function(){
        if($(this).val() == 'Пароль') $(this).val('');
    })
    
    $("#pass").blur(function(){
        if($(this).val() == '') $(this).val('Пароль');
    })
    
	$(".topelemright form").submit(function(){
		if ($("#searchString").val().length <= 2){
			alert("Строка поиска не менее трех символов.")
			return false;
		}
		else if ($("#searchString").val()=='Поиск по сайту'){
			alert("Введите строку поиска!")
			return false;
		}
	})
	
	/*$("#apply_filter").click(function(){
		console.log($(this).parents("form").serialize());
		return false;
	})*/
})

function writeAddress(name, domain){
	document.write('<a href="mailto:'+name+'@'+domain+'">'+name+'@'+domain+'</a>')
}

function intval( mixed_var, base ) {
    var tmp;

    if( typeof( mixed_var ) == 'string' ){
        tmp = parseInt(mixed_var);
        if(isNaN(tmp)){
            return 0;
        } else{
            return tmp.toString(base || 10);
        }
    } else if( typeof( mixed_var ) == 'number' ){
        return Math.floor(mixed_var);
    } else{
        return 0;
    }
}

// Показываем/прячем фильтр в каталоге

    function dance_with_filter() {
        $("#filter_control").change(function(){
            change_filter_state();
        });

        function change_filter_state() {
            var z = $("#filter_control:checked").length;
            if(z > 0) {
                $("#filter_bord").removeClass("nofilter");
            } else {
                $("#filter_bord").addClass("nofilter");
            }
        }
		
        change_filter_state();
    }
    
