function setLocality(value) {
	selectItem(value, 'region');
}

function setEstateType(value) {
	selectItem(value, 'type');
    if ($('#hp-buildings-offer').length > 0) {
        if ($('html').scrollTop() > parseInt($('#hp-buildings-offer').get(0).offsetTop)) {
            $('html').scrollTop(parseInt($('#hp-buildings-offer').get(0).offsetTop));
        }
    }
    $('select#type').stop().animate({'backgroundColor': '#fffdbe'}, 250, function(){
        $(this).animate({'backgroundColor': 'white'}, 750);
    });
}

function changePictureSrc(source,sourceBig,desc,targetId,targetBigId,targetDescId) {
	if (document.getElementById(targetId) && document.getElementById(targetBigId) && document.getElementById(targetDescId)) {
		document.getElementById(targetId).src = '/web/default/tools/loadingAnimation.gif';
		document.getElementById(targetId).src = source;
		document.getElementById(targetId).alt = desc;
		document.getElementById(targetBigId).href = sourceBig;
		document.getElementById(targetBigId).title = desc;
		document.getElementById(targetDescId).innerHTML = desc;		
	}
}

$(document).ready(function(){
    // links under label are used for SEO but we want them to be like usual labels
    $('#extended-filter label a').click(function(){
        parentElm = $(this).parent().parent();
        if ($(parentElm).find('input:checkbox')) {
            isChecked = $(parentElm).find('input:checkbox').attr('checked');
            if (isChecked != '') {
                $(parentElm).find('input:checkbox').removeAttr('checked');
            } else {
                $(parentElm).find('input:checkbox').attr('checked', 'checked');
            }
        }
        return false;
    });
    // dropdown toggles it's div
    if ($('#districtsCheckboxes')) {
        $('select#region').change(function(){
            $('#districtsCheckboxes div').removeClass('shown').find('input:checkbox').removeAttr('checked');
            $('#districtsCheckboxes div#regionDiv_' + $(this).val()).addClass('shown');
        });
    }
});


$(document).ready(function() {
    try {
        $("#photos a.smallImg").click(function(){
            $("#photos a.smallImg").removeClass('active');
            $(this).addClass('active');
            var imageHref = $(this).attr('href');
            var imageTitle = $(this).find('img').attr('alt');
            $("a.photosShowBig img").attr('src', imageHref.replace('/pictures/', '/pictures_medium/')).attr('alt', imageTitle);
            $("a.photosShowBig").attr('href', imageHref).attr('alt', imageTitle);
            return false;
        });

        $("a.photosShowBig").click(function(){
            var bigImagesArr = new Array();
            $("#photos a.smallImg").each(function(i, elm) {
                obj = {
                    'href': $(elm).attr('href').replace('/pictures_medium/', '/pictures/'),
                    'title': $(elm).find('img').attr('alt'),
                    'selected': $(elm).hasClass('active'),
                    titlePosition: 'inside',
                    beforeNext: function(selectedArray, selectedIndex, selectedOpts){
                        if (typeof(photoGalleryApi) != 'undefined') {
                            photoGalleryApi.click(selectedIndex+1);
                            $("#photos a.smallImg.active").trigger('click');
                        }
                    },
                    beforePrev: function(selectedArray, selectedIndex, selectedOpts){
                        if (typeof(photoGalleryApi) != 'undefined') {
                            photoGalleryApi.click(selectedIndex-1);
                            $("#photos a.smallImg.active").trigger('click');
                        }
                    }
                };
                bigImagesArr.push(obj);
            });
            $.fancybox(bigImagesArr);
            return false;
        });
    } catch (e) {}
});
