var cityRegionsCache = new Array();
var countryCache = new Array();// cache for the runned queries
var regionCache = new Array();// cache for the runned queries
var cityCache = new Array();// cache for the runned queries
var clickable = true;

function refreshCaptcha() {
    if(clickable){
        var captchaId = $("#captcha-id").val();
        if (captchaId != "") {
            clickable = false;
            $.ajax({
                type: 'GET',
                url: '/ajaxrequest/refreshcaptcha/id/' + captchaId,
                dataType : "json",
                success: function(response){
                    $("#"+captchaId).attr("src",response['src']);
                    $("#"+captchaId).attr("id",response['id']);
                    $("#captcha-id").attr("value",response['id']);
                    clickable = true;
                }
            });
        }
    }
}
function makeSearch() {
    $('#search_form #form_act').val(1);
    $('#search_form').submit();
}
function refineSearch() {
    $('#search_form #form_act').val(2);
    $('#search_form').submit();
}
function newSearch() {
    $('#search_form #form_act').val(3);
    $('#search_form').submit();
}
function searchByLocation(countryId, regionId) {
    $('#search_form #cnt_id').val(countryId);
    $('#search_form #reg_id').val(regionId);
    $('#search_form').submit();
}
function searchByPropertyGroup(group) {
    $('#search_form #drop-property_type').find('input').each(function() {
        if ($(this).attr("rel") == 'property_type' + group) {
            $(this).attr("checked", "checked");
        }
        else {
            $(this).attr("checked", null);
        }
    });
    $('#search_form').submit();
}
function searchByPropertyType(type) {
    $('#search_form #drop-property_type').find('input').each(function() {
        if ($(this).attr("id") == 'property_type-' + type) {
            $(this).attr("checked", "checked");
        } else {
            $(this).attr("checked", null);
        }
    });
    $('#search_form').submit();
}
function searchByPrice(priceFrom, priceTo, rentSale) {
    if (rentSale == 1) {
        suffix = '_rent';
    } else {
        suffix = '';
    }
    $('#search_form #pricefrom' + suffix).val(priceFrom);
    $('#search_form #priceto' + suffix).val(priceTo);
    $('#search_form').submit();
}
function updatechosen(){
    $(".fselect").each(function(i,obj){
        var relobj=$(this).attr("rel")+" :checked";
        var leng=$(relobj).length;
        $(this).text((leng)?"Выбрано "+(leng):$(this).attr("any"));
        $($(this).attr("rel")).bind("click",function(e){
            // updatechosen();
            e.stopPropagation();
        });

    });
}
function fselectclick(e,obj){

    rel=$(obj).attr("rel");
    if($(rel+" :visible").length){
        $(rel).hide();
    }else{
        $(rel).show();
    }
    $(".fselect:not([rel="+rel+"])").each(function(i,obj){
        rel=$(this).attr("rel");
        $(rel).hide();
    });
    // updatechosen();
    e.stopPropagation();
}
function buildRegion(msg,value){
    $("div#filter_city div.box_form_1").html("");
    $.each(msg,function (k,x){
        if(k){
            $("div#filter_region").append(
                '<label class="checkbox" for="region_id-'+k+'" rel="'+value+'"><input type="checkbox" class="checkbox for_select_3" name="region_id['+k+']" id="region_id-'+k+'" value="'+k+'">'+x+'</label>'
                );
        }
    });
}
function buildCity(msg,value){
    $.each(msg,function (k,x){
        if(k){
            $("div#checkboxes_city").append(
                '<label class="checkbox" for="city_id-'+k+'" rel="'+value+'"><input type="checkbox" class="checkbox for_select_4" name="city_id['+k+']" id="city_id-'+k+'" value="'+k+'">'+x+'</label>'
                );
        }
    });
}
function buildDistrict(msg,value){

    $.each(msg,function (k,x){
        if(k){
            $("div#checkboxes_raion").append(
                '<label class="checkbox" for="dst_id-'+k+'" rel="'+value+'"><input type="checkbox" class="checkbox for_select_5" name="dst_id['+k+']" id="dst_id-'+k+'" value="'+k+'">'+x+'</label>'
                );
        }
    });
}

$(document).ready(function() {
    
    
    
    $(".onlinecam").click(function(){
        var url = $(this).attr('rel');
        if(url != undefined)
        {
            $('<a href="'+url+'"></a>').fancybox({
                'autoScale' : false,
                'transitionIn' : 'none',
                'transitionOut' : 'none',
                'overlayShow'	: true,
                'type' : 'iframe',
                'width': 640,
                'height' : 390,
                'titleShow':false,
                'scrolling'   : 'no'
            }).click();
        }
        return false;
    });
    
    
    $("div#country").click(function(event) {
        $("div.country").toggleClass("hidden");
        event.stopPropagation();
    });
    //fix for CkEditor old verison
    $('.cms_content img').each(function(){
        var marginX = $(this).attr("hspace");
        var marginY = $(this).attr("vspace");
        marginX = parseInt(marginX);
        marginY = parseInt(marginY);
        if(!isNaN(marginX)){
            if(marginX>0){
                $(this).css({
                    "margin-left":marginX+"px",
                    "margin-right":marginX+"px"
                }).removeAttr("hspace");
            }
        }
        if(!isNaN(marginY)){
            if(marginY>0){
                $(this).css({
                    "margin-top":marginY+"px",
                    "margin-bottom":marginY+"px"
                }).removeAttr("vspace");;
            }
        }
    });
    
    //datepicker
    jQuery.datepicker._gotoToday = function(id){
        var target=$(id);
        var today = new Date();
        var day =today.getDate();
        var month = today.getMonth()+1;
        var year=today.getFullYear();
        var buf=(day<10?"0"+day:day)+"/"+(month<10?"0"+month:month)+"/"+year;
        $(target).attr("value",buf);
        this._hideDatepicker();
        return true;
    };
    var options = {
        "dateFormat" : "dd/mm/yy",
        changeYear : true,
        showButtonPanel : true,
        changeMonth : true,
        "duration" : "fast"
    }; 
    if($(".datepicker").length > 0)
    {
        $(".datepicker").datepicker(options);
    }
    
    
    
    
    //ajax loading box;
    $('#filter_country_region').before('<div id="bruce"></div>');
    $('body').ajaxStart(function(){
        $('.advanced #bruce').show();
        $('#filter_region input, #checkboxes_city input, #checkboxes_raion input').attr('disabled','disabled');
    });
    $('body').ajaxStop(function(){
        $('.advanced #bruce').hide();
        $('#filter_region input, #checkboxes_city input, #checkboxes_raion input').removeAttr('disabled');
    });
    updatechosen();

    $("select#country_id").live("change",function(){
        var value = this.value;
        $('div#filter_region .checkbox').each(function(i,m){
            $(this).removeAttr('checked');
            $(this).change();
        });
        $('div#filter_region .checkbox').remove();

        if (!$(countryCache[value]).length){
            $.ajax({
                type: "GET",
                url:"/ajaxfeeds/dmregion/q/"+value,
                dataType: 'json',
                success: function(msg){
                    buildRegion(msg,value);
                },
                error:function(msg){
                    alert('connection lost');
                }
            });
        }else{
            $('div#filter_region .checkbox').removeAttr('checked');
            $('div#filter_region .checkbox').change();
            buildRegion(countryCache[value],value);
        }
    });



    $("div#filter_region input.checkbox").live("change",function(){
        var value = this.value;
        if($(this).attr('checked')){

            if (!$(regionCache[value]).length){
                $.ajax({
                    type: "GET",
                    url:"/ajaxfeeds/dmcity/q/"+value,
                    dataType: 'json',
                    success: function(msg){
                        // alert();
                        regionCache[value] = msg;
                        buildCity(msg,value);
                    },
                    error:function(msg){
                        alert('connection lost');
                    }
                });
            }else{
                buildCity(regionCache[value],value);
            }
        }else{
            //uncheck remove
            $('div#checkboxes_city label[rel='+value+'] input').removeAttr('checked');
            $('div#checkboxes_city label[rel='+value+'] input').change();
            $('div#checkboxes_city label[rel='+value+']').remove();
        /*if(regionCache[value]){
                $.each(regionCache[value],function(k,x){
                        //force district removal
                        $('#city_id-'+k).removeAttr('checked');
                        $('#city_id-'+k).change();
                        $('[for=city_id-'+k+']').remove();

                })
            }*/


        }
    });

    $("div#filter_city input.checkbox").live("change",function(){
        var value = this.value;
        if($(this).attr('checked')){
            if (!$(cityCache[value]).length){
                $.ajax({
                    type: "GET",
                    url:"/ajaxfeeds/dmdistrict/q/"+value,
                    dataType: 'json',
                    success: function(msg){
                        cityCache[value]=msg;
                        buildDistrict(msg,value);
                    },
                    error:function(msg){
                        alert('connection lost');
                    }
                });
            }else{
                buildDistrict(cityCache[value],value);
            }
        }else{
            //uncheck remove
            // $('div#checkboxes_raion label[rel='+value+'] input').removeAttr('checked');
            // $('div#checkboxes_raion label[rel='+value+'] input').change();
            $('div#checkboxes_raion label[rel='+value+']').remove();
        /*if(cityCache[value]){
                $.each(cityCache[value],function(k,x){
                        $('div#checkboxes_raion [for=dst_id-'+k+']').remove();
                })
            }*/
        }
    });

    // $("select#country_id").change();

    $(".fselect").bind("mouseenter mouseleave", function() {
        $(this).toggleClass("hover");
    });

    $(".fselect").bind("mousedown mouseup", function() {
        $(this).toggleClass("pressed");
    });

    $(".fselect").bind("click",function(e){
        fselectclick(e,this)
    });

    $("a.check").bind("click", function (e){
        $($(this).attr("rel")+" input").each(function (){
            this.checked=true;
        });
        e.stopPropagation();
        updatechosen();
        return false;
    });

    $("a.uncheck").bind("click", function (e){
        $($(this).attr("rel")+" input").each(function (){
            this.checked=false;
        });
        e.stopPropagation();
        updatechosen();
        return false;
    });


    $(".wrapper").bind("click",function(e){
        updatechosen();
        e.stopPropagation();
    });
    $(".dropdown").bind("click",function(e){
        updatechosen();
        e.stopPropagation();
    });

    $(document.body).bind("click",function(){
        $(".fselect").each(function(i,obj){
            rel=$(this).attr("rel");
            $(rel).hide();
        });
        updatechosen();
    });


    $(".button_advanced_search,#link_advanced_search").bind("click", function() {
        $("#property_filter").toggleClass("advanced").toggleClass("normal");
        $("#property_filter.advanced .wrapper:not(:visible)").show();
        $("#property_filter.normal .wrapper:visible").hide();
        $(".button_advanced_search").toggleClass("button_close");
        $("#link_advanced_search").toggleClass("link_close");
        return false;
    });

    $("#advanced_search").bind("click", function() {
        $("#normalsearch").attr('checked','checked');

    });
    $("#results").bind("change",function(){
        window.location = this.value;
    });
    // submit form on Change event of select
    $("select.autosubmit").bind("change",function(){
        var test = $(this.form).find("button[name=_submit]");
        if(test.length > 0){
            test.click();
        }
        else{
            this.form.submit();
        }
    });


    $("#pricefslave").bind("change", function() {
        $("#pricefmaster")[0].selectedIndex = this.selectedIndex;
    });

    $("#pricefmaster").bind("change", function() {
        $("#pricefslave")[0].selectedIndex = this.selectedIndex;
    });

    $("#pricetslave").bind("change", function() {
        $("#pricetmaster")[0].selectedIndex = this.selectedIndex;
    });
    $("#pricetmaster").bind("change", function() {
        $("#pricetslave")[0].selectedIndex = this.selectedIndex;
    });

    $("input.check_property").bind("click", function() {
        $("#select_type").hide();
    });

    // Agents
    $("#off_radio").bind("change", function() {
        $("#prop_countries").hide();
        $("#off_countries").show();
    });

    $("#prop_radio").bind("change", function() {
        $("#off_countries").hide();
        $("#prop_countries").show();
    });

    if($("#rent_sale").val()==1) {
        $("#filter_rent_box").show();
        $("#filter_sale_box").hide();
    }else{
        $("#filter_rent_box").hide();
        $("#filter_sale_box").show();
    }

    $("#rent_sale").bind('change',function (){
        if($("#rent_sale").val()==1) {
            $("#filter_rent_box").show();
            $("#filter_sale_box").hide();
        }else{
            $("#filter_rent_box").hide();
            $("#filter_sale_box").show();
        }
    });

    $("a.select_all").bind("click", function() {
        var rel = this.rel;
        var state = ($("#"+rel.toString()+" input:not(:checked)").length>0)?true:false;
        $("#"+rel.toString()+" input").each( function() {
            this.checked = state;
            $(this).change();
        });
    // da se vika samo za regionite inache bugva
    /*if(rel=="for_select_3"){
            afterAjaxCallBack();
        }*/
    });

    $("a.select_type").bind("click", function() {
        var rel=$(this).attr('rel');
        var state = ($("input[rel="+rel.toString()+"]:not(:checked)").length>0)?true:false;
        $('input[rel='+rel+']').each(function (){
            this.checked = state;
        });
    });
    $("a.select_type").disableSelection();
    $("a.select_all").disableSelection();


    $(".fancybox").fancybox({
        'overlayShow' : true,
        'width'         : 740,
        'height'        : 620,
        'padding'       : 20,
        'autoDimensions': false,
        'autoScale'     : false
    });

    $(".help").tooltip({
        // use div.tooltip as our tooltip
        // tip: '.tooltip',

        // use the fade effect instead of the default
        effect: 'fade',

        // make fadeOutSpeed similar to the browser's default
        fadeOutSpeed: 50,

        // the time before the tooltip is shown
        predelay: 200

    // tweak the position
    // position: "bottom center",
    // offset: [-50, -80]
    });
    $(".fancybox_iframe").fancybox({
        'autoScale' : false,
        'transitionIn' : 'none',
        'transitionOut' : 'none',
        'overlayShow'	: true,
        'type' : 'iframe',
        'titleShow':false
    });

    $("span.label_optional").each(function(){
        $(this).parent().find("label.optional").append(this);
    });

    $(".prop_stats_total").tooltip({
        // use the fade effect instead of the default
        //effect: 'fade',

        // make fadeOutSpeed similar to the browser's default
        fadeOutSpeed: 50,

        // the time before the tooltip is shown
        predelay: 200,

        // tweak the position
        position: "center left",
        offset: [0, -30]
    });


    // currency

    $("select.currency").change(function() {
        var currency = this.value;
        $.ajax({
            type: "GET",
            url: "/ajaxfeeds/currency/currency/" + currency,
            success: function(obj) {
                location.reload();
            }
        });
    });

    var priceElements = $("select#pricefslave , select#pricetslave");
    function showLoading(jqueryCollection,parent){
        jqueryCollection.css("cssText","display: none !important");
        $(parent).append('<span id="rent-sale-ajax-loader"><img src="/images/load/ajax-loader-rentsalebox.gif" /></span>');
    }
    function hideLoading(jqueryCollection){
        jqueryCollection.css("cssText","display: inline !important");
        $("#rent-sale-ajax-loader").remove();
    }
    function changeRentSaleBox(jsonResponse){
        $("div#filter_price h3").html(jsonResponse['label']);
        priceElements.each(function(){
            var firstOption = $(this.options[0]).clone();
            $(this).html("");
            $(this).append(firstOption);
            var appendString = "";
            $.each(jsonResponse['prices'],function(key,value){
                appendString +='<option value="'+key+'">'+value+'</option>';
            });
            $(this).append(appendString);
            hideLoading(priceElements);
        });
    }
    var jsonCache = new Array();
    $("select#rent_sale").bind("change",function(){
        var value = parseInt(this.value);
        if(value==0){
            return;
        }
        showLoading(priceElements, "#filter_price");
        if(jsonCache[value]===undefined){
            $.ajax({
                type: "GET",
                url: "/ajaxfeeds/prices/type/" + value,
                dataType : "json",
                success: function(jsonResponse) {
                    jsonCache[value] = jsonResponse;
                    changeRentSaleBox(jsonCache[value]);
                }
            });
        }else{
            changeRentSaleBox(jsonCache[value]);
        }

    });


    $(".offer_video").fancybox({
        'autoScale'     :   false,
        'transitionIn'  :   'none',
        'transitionOut' :   'none',
        'overlayShow'	:   true,
        'titleShow'     :   false,
        'titlePosition'	:   'inside'
    });
    
    $(".offer_onlinecam").fancybox({
        'autoScale' : false,
        'transitionIn' : 'none',
        'transitionOut' : 'none',
        'overlayShow'	: true,
        'type' : 'iframe',
        'width': 640,
        'height' : 390,
        'overlayShow'	:   true,
        'titleShow'     :   false,
        'titlePosition'	:   'inside'
    });

    // hack za property/show
    var propShowLink = $("#main_navigation a.prop_nav_hide_me");
    if(propShowLink.length > 0)
    {
        var parentLi = propShowLink.parent();
        if(parentLi.parent().find("li").length == 1)
        {
            // Ако има само една статия - имота, скрива целия ul
            parentLi.parent().css("display","none");
        }
        else
        {
            // В противен случай скрива само li
            parentLi.css("display","none");
        }
    }

    //captcha refresher :D
    $("span.button_refresh").live("click",refreshCaptcha);
    //sort menus
    $(".sort_menu").hover(
        function() {
            $("li.sort_menu_item").not(".selected").show();
        },
        function() {
            $("li.sort_menu_item").not(".selected").hide();
        }
        );
    $(".sort_menu_item").hover(
        function() {
            $(this).addClass("sort_hover");
        },
        function() {
            $(this).removeClass("sort_hover");
        }
        );

    //footer links hacks 
    if($('.footer-links').length>0)
    {
        $('#footer-links-botton').live('click',function(){
            $('.footer-links').css({
                'top': $('#footer-links-botton').position().top-385 ,
                'left' : $('#footer-links-botton').position().left+20,
                'display':'block'
            });
            $('body').bind('click',function(){
                $('.footer-links').css({
                    'display':'none'
                });
            });
        });

    }else{
        $('#footer-links-botton').css({
            'display':'none'
        });
    }

    //hackerii za office kartite
    $(".gmap").fancybox({
        'autoScale'     :   false,
        'transitionIn'  :   'none',
        'transitionOut' :   'none',
        'overlayShow'	:   true,
        'titleShow'     :   false,
        'titlePosition'	:   'inside',
        onStart:function(selectedArray){
            
            latitude = parseFloat($(selectedArray).attr("latitude"));
            longitude = parseFloat($(selectedArray).attr("longitude"));
            zoom = parseFloat($(selectedArray).attr("zoom"));
            
            if( ( isNaN(latitude) || latitude == 0 ) || ( isNaN(longitude) || longitude == 0) || ( isNaN(zoom) || zoom == 0 ) ){
                latitude = 42.61903985005047;
                longitude = 25.49279912660293;
                zoom = 7;
            }

            var mapCheck = $("div#map");

            if(mapCheck.length > 0){
                map.clearOverlays();
                marker = new GMarker(new GLatLng(latitude, longitude), {
                    draggable: false
                });
                map.addOverlay(marker);
                map.setCenter(new GLatLng(latitude, longitude), zoom);
            }
            else{
                $("div#wrapper_gmap").append('<div id="map" style="width: 500px; height: 400px"></div>');
                loadMap(latitude, longitude, zoom, latitude, longitude, false);
            }

            $("#wrapper_gmap").removeClass("hidden");
        },
        onClosed:function(){
            $("#wrapper_gmap").addClass("hidden");
        }
    });
    $("a#broker_question").fancybox({
        'autoScale' : false,
        'transitionIn' : 'none',
        'transitionOut' : 'none',
        'overlayShow'	: true,
        'type' : 'iframe',
        'titleShow':false,
        'width':650,
        'scrolling':'no',
        'autoDimensions': false,
        'hideOnOverlayClick': false,
        'hideOnContentClick': false,
        'enableEscapeButton': false,
        'showCloseButton':true
    })
});
