function open_panel(){
		$('#login-in-form, #user-cabinet').slideToggle();
		$(this).toggleClass('open');
	};
	
$(document).ready(function(){
	$('td.link_td').click(function(){
			var id = $(this).parent().attr("id");
			var estate_type = $(this).parent().attr("title");
			//alert(estate_type);
			window.location.replace("/sell/"+estate_type+"/oid/"+id+"/");	
		}		
	);
	
	$('td.link_td').hoverIntent({    

	    sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
	    interval: 200, // number = milliseconds for onMouseOver polling interval    
	    timeout: 500, // number = milliseconds delay before onMouseOut    
	    over:function(){
	        //$(this).parent().addClass("hover");
	        var id = $(this).parent().attr("id");
			var alt = $(this).parent().attr("alt");
			if(alt) { get_map(id, alt) };
		  
	    },
	    //out:function(){
	    //    //$(this).parent().removeClass("hover");
	    //}
	});
	
	$('.visible tr').hoverIntent({    
	    sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
	    interval: 200, // number = milliseconds for onMouseOver polling interval    
	    timeout: 500, // number = milliseconds delay before onMouseOut    
	    over:function(){
	        $(this).addClass("hover");		  
	    },
	    out:function(){
	        $(this).removeClass("hover");
	    }
		});
	
	
	


	function get_map(id, alt) {
        var map = new YMaps.Map(document.getElementById("map"+id));
        
        var geocoder = new YMaps.Geocoder(alt); 
		
		// Создание обработчика для успешного завершения геокодирования
        YMaps.Events.observe(geocoder, geocoder.Events.Load, function () {
            if (this.length()) {
                geoResult = this.get(0);
                //map.addOverlay(geoResult);
                map.setBounds(geoResult.getBounds());
                
                // Создание стиля
	            var styleExample = new YMaps.Style();
	            styleExample.iconStyle = new YMaps.IconStyle();
	            styleExample.iconStyle.href = "/media/img/common/flag.png"; // Ссылка на значок
	            styleExample.iconStyle.size = new YMaps.Point(29, 35); // Размер значка
	            styleExample.iconStyle.offset = new YMaps.Point(0, -30); // Смещение значка (для визуального выравнивания)
	
	            // Создадим и применим к метке новый стиль
	            var placemark = new YMaps.Placemark(map.getCenter(), {style : styleExample});
	            placemark.name = alt;
				map.addOverlay(placemark);
            }else {
                //alert("Ничего не найдено")
            }
        });

        // Процесс геокодирования завершен неудачно
        YMaps.Events.observe(geocoder, geocoder.Events.Fault, function (geocoder, error) {
            alert("Произошла ошибка на карте: " + error);
        });
        
        //YMaps.Events.observe(map, map.Events.Click, function () {
        //    alert("Щелк!");
        //});
	};
	
	$('.visible th:first').addClass('first');
	$('.visible th:last, .visible tr:last, .visible td:last').addClass('last');
	$('.visible tr td:nth-child(2)').addClass('second');
	$('.visible tr td:nth-child(1)').addClass('first');
	$('.visible tr:even').addClass('second');
	
	$('#login-in a, #user-link a').click(function () {
		open_panel();
	});

	
	$('input.fieldfocus,textarea.fieldfocus').fieldFocus();
	
	$('.photogallery').tabs();
});


$(document).ajaxSend(function(event, xhr, settings) {
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    function sameOrigin(url) {
        // url could be relative or scheme relative or absolute
        var host = document.location.host; // host + port
        var protocol = document.location.protocol;
        var sr_origin = '//' + host;
        var origin = protocol + sr_origin;
        // Allow absolute or scheme relative URLs to same origin
        return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
            (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
            // or any other URL that isn't scheme relative or absolute i.e relative.
            !(/^(\/\/|http:|https:).*/.test(url));
    }
    function safeMethod(method) {
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }

    if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
    }
});
