Interface.property = {
    id : 0,
    lang: '',

    uploaderLimit: 33,

    view: {
        lat:     '',
        lng:     '',
        zoom:    14,
        active:  '',
        cluster: 1,
        address: '',

        foto: {
            next: function() {
                cursor = parseInt($('#current_slide').text());
                box    = $('#property-image-container img').eq(0);

                fotos = $('#tabs-1 img');
                if(cursor < fotos.length) {
                    box.attr('src',fotos.eq(cursor).attr('src'));
                    $('#current_slide').text(cursor + 1);
                }
            },

            prev: function() {
                cursor = parseInt($('#current_slide').text());
                box    = $('#property-image-container img').eq(0);

                fotos = $('#tabs-1 img');
                if(cursor > 1) {
                    box.attr('src',fotos.eq(cursor-2).attr('src'));
                    $('#current_slide').text(cursor - 1);
                }
            }
        },

        init: function() {
            // Delete back button, if it`s necessary
            if (history.length < 2) {
                $('a.back').remove();
            }

            // Init tabs menu
            var width = 0;
            var totalspace = 945;
            var numpaddings = ($('#property-menu a').length)*2;

            $('#property-menu a').each(function(){
                width += $(this).width();
            });

            var paddwidth = Math.ceil((totalspace-width)/numpaddings);
            $('#property-menu a').css('padding','0 '+ (paddwidth-1) + 'px');

            // PDF lisnk (open and save)
            $('a.pdf-link').each(function(){
                $(this).attr('href', $(this).attr('alt'));
            });

            //On Click Event
            $("ul#property-menu li").click(function() {
                $("ul#property-menu li").removeClass("active");
                $(this).addClass("active");
                $(".property-inner").hide();

                //Find the href attribute value to identify the active tab + content
                var activeTab = $(this).find("a").attr("href");

                // First map init
                if (activeTab=='#tabs-2' && !Geo.isInit) {
                    // For the first - init map
                    Geo.initMap(document.getElementById("map"), {
                        center: {
                            lat: Interface.property.view.lat,
                            lng: Interface.property.view.lng,
                            zoom: Interface.property.view.zoom
                        },
                        streetView: true
                    });

                    Geo.getPointMarker(
                        Interface.property.view.cluster,
                        (Interface.property.view.address=='') ? Lang._('Approximate location') : null
                    );

                    // If we have more than one point in cluster, initialize clustering procedure
                    if(Interface.property.view.cluster > 1) {
                        Geo.buildClusterFromMarker();
                    }
                }

                $(activeTab).show(); //Fade in the active ID content
                return false;
            });

            //When page loads...
            $(".property-inner").hide(); //Hide all content
            if ('' == Interface.property.view.active) {
                Interface.property.view.active = location.hash || '#photos';
            }

            $("ul#property-menu li" + Interface.property.view.active).click(); //Show active tab conten

            // Show link on the fullsize sources
            $("a[rel^='prettyPhoto']").prettyPhoto({
                changepicturecallback: function() {
                    if($.browser.msie && $.browser.version > 6) {
                        $('.pp_content').css('height', 'auto');
                        $('.pp_details').css('margin-bottom', '0px');
                    }
                },
                counter_separator_label: ' of ',
                theme: 'facebook',
                markup: '<div class="pp_pic_holder"> \
                            <div class="pp_top"> \
                                    <div class="pp_left"></div> \
                                    <div class="pp_middle"></div> \
                                    <div class="pp_right"></div> \
                            </div> \
                            <div class="pp_content_container"> \
                                    <div class="pp_left"> \
                                    <div class="pp_right"> \
                                            <div class="pp_content"> \
                                                    <div class="pp_fade"> \
                                                            <div class="pp_loaderIcon"></div> \
                                                            <div class="pp_hoverContainer"> \
                                                                    <a class="pp_next" href="#">next</a> \
                                                                    <a class="pp_previous" href="#">previous</a> \
                                                            </div> \
                                                            <div id="pp_full_res"></div> \
                                                            <div class="pp_details clearfix"> \
                                                                    <a class="pp_close" href="#">Close</a> \
                                                                    <p class="pp_description"></p> \
                                                                    <div class="pp_nav"> \
                                                                            <p class="currentTextHolder">0 ' + Lang._('of') + ' 0</p> \
                                                                    </div> \
                                                            </div> \
                                                    </div> \
                                            </div> \
                                    </div> \
                                    </div> \
                            </div> \
                            <div class="pp_bottom"> \
                                    <div class="pp_left"></div> \
                                    <div class="pp_middle"></div> \
                                    <div class="pp_right"></div> \
                            </div> \
                    </div> \
                    <div class="pp_overlay"></div> \
                    <div class="ppt"></div>'

            });

            a = $('.tip-a-friend > a').eq(0);
            img = $('.tip-a-friend > a > img')

            a.mouseout(function(){
                return img.mouseout();
            });

            a.mouseover(function(){
                return img.mouseover();
            });

            a.click(function(){
                img.click();
                return false;
            });
        }
    },

    details: function() {
        // Open selected box
        $.each($('input[name^="is_"]:checked'),function(){
            id = $(this).attr('id').replace('is_','');
            if (id) {
                $('#'+id).removeClass('hidden');
            }
        });

        // When check IS field, open text box for typing value
        $('input[name^="is_"]').change(function(){
            id = $(this).attr('id').replace('is_','');
            text = $('#'+id);

            if($(this).attr('checked')) {
                text.removeClass('hidden');
                text.focus();
                text.select();
            } else {
                text.addClass('hidden');
                text.val(0);
            }
        });
    },

    drawing:  {
        del: function (link) {
            var name = $(link).parent('div').parent('div').attr('id').replace('foto_','');

            $.getJSON(
                '/property/ajax-delete-attachment/name/' + name,
                {
                    entityId: Interface.property.id
                },
                function(data){
                    if( !data || !data.ok ) {
                        alert(data.errors.toString());
                        return false;
                    }
                    // Delete container with foto
                    $('#foto_' + name).remove();
                }
            );
        },

        init: function() {
            button = $('.uploader h3');
            var html = button.html();
            new AjaxUpload(button,{
                action: '/' + Interface.property.lang + '/property/wizard/drawing/' + Interface.property.id,
                name: 'drawing',
                data: {
                    'ajax': 'XmlHttpRequest'
                },
                onSubmit : function(file, ext){
                    // change button text, when user selects file
                    button.html(html.replace(Lang._('Upload draw'),Lang._('Uploading')));

                    // If you want to allow uploading only 1 file at time,
                    // you can disable upload button
                    this.disable();

                    // Uploding -> Uploading. -> Uploading...
                    interval = window.setInterval(function(){
                        var text = button.html();
                        if (text.length < Interface.property.uploaderLimit){
                            button.html(text + '.');
                        } else {
                            button.html(html.replace(Lang._('Upload draw'),Lang._('Uploading')));
                        }
                    }, 200);
                },
                onComplete: function(file, response){
                    $('#container').html(response);

                    button.html(html);
                    window.clearInterval(interval);
                    // enable upload button
                    this.enable();
                }
            });

        },

        main:  {
            set: function(link) {
                link = $(link);
                // Do nothing for disabled controlls
                if (link.hasClass('disabled')) {
                    return false;
                }

                var name = link.parent('div').parent('div').attr('id').replace('foto_','');
                $.getJSON(
                    '/property/ajax-main-drawing/drawing/' + name,
                    {property: Interface.property.id},
                    function(data){
                        if( !data || !data.ok ) {
                            alert(data.errors.toString());
                            return false;
                        }

                        // Toggle visible label for main foto
                        Interface.property.drawing.main.show(data.attach.mainDrawing);
                    }
                );
            },

            show: function(name) {
                id = '#foto_' + name;
                // Find necessary name. If foto with such name does not exist, use first uploaded
                if (0 == $(id).length) {
                    id = '#' + $('#publish').find('div[id^="foto_"]:last').attr('id');
                }

                var foto = $(id);
                // Show and hide Main label
                $('div.block div>span').css('display','none');
                foto.find('span').css('display','block');

                // Disable controls for making foto main
                $('.disabled').removeClass('disabled');
                foto.find('a').eq(0).addClass('disabled');
            }
        }
    },

    foto: {
        init: function() {
            button = $('.uploader h3');
            var html = button.html();

            new AjaxUpload(button,{
                action: '/' + Interface.property.lang + '/property/wizard/foto/' + Interface.property.id,
                name: 'foto',
                data: {
                    'ajax': 'XmlHttpRequest'
                },
                onSubmit : function(file, ext){
                    // change button text, when user selects file
                    button.html(Lang._('Uploading'));

                    // If you want to allow uploading only 1 file at time,
                    // you can disable upload button
                    // this.disable();

                    // Uploding -> Uploading. -> Uploading...
                    interval = window.setInterval(function(){
                        var text = button.html();
                        if (text.length < Interface.property.uploaderLimit - 7){
                            button.html(text + '.');
                        } else {
                            button.html(Lang._('Uploading'));
                        }
                    }, 300);
                },
                onComplete: function(file, response){
                    $('#container').html(response);

                    button.html(html);
                    window.clearInterval(interval);
                    // enable upload button
                    // this.enable();
                }
            });
        },

        del: function(link) {
            var name = $(link).parent('div').parent('div').attr('id').replace('foto_','');

            $.getJSON(
                '/property/ajax-delete-attachment/name/' + name,
                {entityId: Interface.property.id},
                function(data){
                    if( !data || !data.ok ) {
                        alert(data.errors.toString());
                        return false;
                    }

                    // Delete container with foto
                    $('#wizard-photos').find('div[id="foto_' + name + '"]').remove();
                    Interface.property.foto.main.show(data.attach.main);
                }
            );
        },

        main:  {
            set: function(link) {
                link = $(link);
                // Do nothing for disabled controlls
                if (link.hasClass('disabled')) {
                    return false;
                }

                var name = link.parent('div').parent('div').attr('id').replace('foto_','');
                $.getJSON(
                    '/property/ajax-main-foto/foto/' + name,
                    {property: Interface.property.id},
                    function(data){
                        if( !data || !data.ok ) {
                            alert(data.errors.toString());
                            return false;
                        }

                        // Toggle visible label for main foto
                        Interface.property.foto.main.show(data.attach.main);
                    }
                );
            },

            show: function(name) {
                id = '#foto_' + name;
                // Find necessary name. If foto with such name does not exist, use first uploaded
                if (0 == $(id).length) {
                    id = '#' + $('#wizard-photos').find('div[id^="foto_"]:last').attr('id');
                }

                var foto = $(id);
                // Show and hide Main label
                $('div.block div>span').css('display','none');
                foto.find('span').css('display','block');

                // Disable controls for making foto main
                $('.disabled').removeClass('disabled');
                foto.find('a').eq(0).addClass('disabled');
            }
        }
    },

    general: {
        init: function() {
            $('#property_types li.all > input[type="checkbox"]').unbind()
                .bind('change',{},function(){checkAllPrettyCheckboxes(this,$('#property_types'));});

            // Select "For sale" and bind to change
            // it show on don't show "Per month" label
            if ($('#goal option').eq(1).is(':selected')) {
                $('span.per-month').css('display','inline');
                $('#vat_input').parent().parent().hide();
                $('#agency_fees').parent().hide();
            }

            $('*[id^=goal_input]').bind('click',{}, function(){
                data=$(this).attr('id').split('---');
                if (data.length!=2) {
                    return true;
                }

                Interface.property.general.label((data[1]=='rent'),'span.per-month');
                if ('rent' == data[1]) {
                    $('#vat_input').parent().parent().hide();
                    $('#agency_fees').parent().hide();
                } else {
                    $('#vat_input').parent().parent().show();
                    $('#agency_fees').parent().show();
                }

                return true;
            });

            $('#wizard_step_general_form').submit(function(){
                $(this).find('input[value="--"], select[value="--"]').attr('disabled','disabled');
                return true;
            });

            // Open selected box
            $.each($('input[name^="is_"]:checked'),function(){
                id = $(this).attr('id');
                text = $('#' + id + '~*');

                if (id) {
                    text.removeClass('hidden');
                }
            });

            // When check IS field, open text box for typing value
            $('input[name^="is_"]').change(function(){
                id = $(this).attr('id');
                hideIt = $('#' + id + '~*:not([class="input-placeholder"])');

                if($(this).attr('checked')) {
                    hideIt.removeClass('hidden');
                    hideIt.focus();
                    hideIt.val(0);
                    hideIt.select();
                } else {
                    hideIt.addClass('hidden');
                    hideIt.val(-1);
                }
            });
        },

        check: function() {
            // Bind action to drop down
            FadeCheck.bind('fade_type', 'open', function(){
                $('#fade_specail_wishes').parent('div').css('display','none');
            });

            FadeCheck.bind('fade_type', 'close', function(){
                $('#fade_specail_wishes').parent('div').fadeIn('slow');
            });

            FadeCheck.titleParams.length = 38;
        },


        label: function(condition, label) {
            if (condition) {
                $(label).css('display', 'inline');
            } else {
                $(label).hide();
            }

            return false;
        }

    }
};


