


if (!('placeholder' in document.createElement('input'))) {
    var $input = $('#search-field'), text = $input.attr('placeholder');
    if ($input.val() == '' && text != '') {
        $input.addClass('placeholder').val(text);
    }
    $input.bind({
        focus : function() {
            $this = $(this);
            if ($this.val() == $this.attr('placeholder')) {
                $this.removeClass('placeholder').val('');
            }
        },
        blur : function() {
            $this = $(this), text = $input.attr('placeholder');
            if ($this.val() == '' && text != '') {
                $this.addClass('placeholder').val(text);
            }
        }
    }).closest('form').bind('submit', function () {
        if ($input.val() == $input.attr('placeholder')) {
            $input.val('').removeClass('placeholder');
        }
    });
}


$('#lec6-feature-buy_1').find('form').bind('change', function () {
	var $this = $(this), $text = $('#results-count').text('Searching...');

	
	if (!('placeholder' in document.createElement('input'))) {
    	$this.find(':input[placeholder]').each(function () {
    	    var $this = $(this), text = $this.attr('placeholder');
            if ($this.val() == text) {
                $this.val('').removeClass('placeholder');
            }
    	});
	}

	
    $.ajax({
        'url'      : '/idx/inc/php/ajax/json.php?searchCount',
        'data'     : $this.serialize(),
        'type'     : 'POST',
        'dataType' : 'json',
        'cache'    : false,
        'success'  : function (data, textStatus, XMLHttpRequest) {
            if (typeof(data.count) != 'undefined' && data.count > 0) {
            	var properties_count = (data.count).format();
                $text.text(properties_count + ' Listings Found');
            } else {
            	$text.text('No Listings Found');
            }
        }
    });

    
    if (!('placeholder' in document.createElement('input'))) {
        $this.find(':input[placeholder]').each(function () {
            var $this = $(this), text = $this.attr('placeholder');
            if ($this.val() == '' && text != '') {
                $this.addClass('placeholder').val(text);
            }
        });
    }

}).trigger('change');



$.easing.drop = function (x, t, b, c, d) {
    return -c / 2 * (Math.cos(Math.PI*t/d) - 1) + b;
};


$.tools.overlay.addEffect('drop', function(css, done) {
    var conf = this.getConf(),
    overlay = this.getOverlay();
    css.top += $(window).scrollTop() - 800;
    css.left += $(window).scrollLeft();
    css.position = 'absolute';
    overlay.stop(true);
    overlay.css(css).show();
    overlay.animate({ top: 0,  opacity: 1}, 650, 'drop', done);
}, function(done) {
    this.getOverlay().stop(true).animate({top:'-=2000', opacity:0}, 950, 'drop', function() {
        $(this).hide();
        done.call();
    });
});


var pid = null;


var REW_IDX_DASHBOARD = {
    init : function () {
        this.load();
    },
    load : function () {
        $.ajax({
            url : '/idx/inc/php/ajax/json.php?module=idx-dashboard',
            type : 'get',
            data : {
                options : {
                    ajax : true,
                    init : true
                }
            },
            dataType : 'json',
            success  : function (json) {

                
                var module = json.module;
                if (module) {

                    
                    if (module.stylesheet) {
                        $('head').append('<style type="text/css">' + module.stylesheet + '</style>');
                    }

                    
                    if (module.content) {

                        
                        $('body').append('<div id="' + module.uid + '">' + module.content + '</div>');

                        
                        rewForm($('#' +  module.uid + ' form'));

                    }

                    
                    REW_IDX_DASHBOARD._setupPanels();
                    REW_IDX_DASHBOARD._setupListings();
                    REW_IDX_DASHBOARD._setupPreferences();
                    REW_IDX_DASHBOARD._setupMessages();
                    REW_IDX_DASHBOARD._setupEvents();
                    REW_IDX_DASHBOARD.show(true);

                }

            }
        });
    },
    selectPanel : function (href) {
        $('#dashboard-nav ul:first-child').data('tabs').getPanes().each(function () {
            var $this = $(this).hide();
            if ($this.attr('id') == href.substr(1, href.length)) {
                $this.show();
            }
        });
        href = (href == '#dashboard_password') ? '#dashboard_preferences' : href;
        href = (href == '#dashboard_message')  ? '#dashboard_messages' : href;
        href = (href == '#dashboard_compose')  ? '#dashboard_messages' : href;
        $('#dashboard-nav').find('li').removeClass('current').find('a[href="' + href + '"]').closest('li').addClass('current');
    },
    _setupPanels : function () {

        
        var $tabset = $('#dashboard-nav ul:first-child').tabs('.page', {
            'tabs'   : 'li',
            'effect' : 'fade'
        });

        
        $('.dashboard').delegate('a.panel', 'click', function () {
            var href = $(this).attr('href');
            REW_IDX_DASHBOARD.selectPanel(href);
            return false;
        });

    },
    _setupListings : function () {

        
        $('#dashboard_main ul.tabs').tabs('#listings > div', {
            'tabs' : 'li'
        });

        
        $('#listings .scrollable').scrollable();

    },
    _setupEvents : function () {

        
        $('.dashboard').delegate('a.action-remove', 'click', function () {

            
            var $this = $(this), $item = $this.closest('li, tr'), id = $item.attr('data-id'), type = $item.attr('data-type');

            
            //var confirm = confirm('Are you sure you want to remove this item?');
            var confirm = true;

            
            if (id > 0 && confirm) {

                
                $item.fadeOut('fast', function() {

                    
                    $item.remove();

                    
                    REW_IDX_DASHBOARD.notice({className : 'loading', text : 'Processing Request...'});

                    
                    pid = Math.random() * 5;

                    
                    $.ajax({
                        url  : '/idx/inc/php/ajax/html.php?module=idx-dashboard&options[ajax]=true',
                        type : 'post',
                        data : {
                            pid    : pid,
                            action : 'delete',
                            type   : type,
                            row    : id
                        },
                        dataType : 'json',
                        success : function (json) {

                            
                            if (typeof(json.pid) != 'undefined' && json.pid != pid) return;

                            
                            if (json.success) {

                                
                                REW_IDX_DASHBOARD.notice({className : 'success', text : json.success.join('<br>')});

                                
                                var $count = $('#count_' + type), count = parseInt($count.text().replace(',', '')) - 1;
                                var count = count < 0 ? 0 : count.format();
                                $count.text(count);

                                
                                switch (type) {
                                    case 'searches' :
                                        $('#list-searches, #dashboard_searches').find('tr[data-id="' + id + '"][data-type="' + type + '"]').remove();
                                        break;
                                    case 'messages' :
                                        $('#list-messages, #dashboard_messages').find('tr[data-id="' + id + '"][data-type="' + type + '"]').remove();
                                        break;
                                }

                                
                                if (type == 'searches' || type == 'messages') {
                                    if (count == 0) {
                                        $('#list-' + type + ', #dashboard_' + type).find('table').replaceWith('<p class="none">You currently have no ' + (type == 'searches' ? 'saved searches' : 'messages') + '.</p>');
                                    }
                                }

                            }

                            
                            if (json.errors) REW_IDX_DASHBOARD.notice({className : 'error', text : json.errors.join('<br>')});

                        },
                        error : REW_IDX_DASHBOARD.error
                    });

                });

                
                $item.removeAttr('data-id');

            }

            return false;
        });

        
        $('.dashboard').delegate('a.action-view', 'click', function () {

            
            var $this = $(this), $item = $this.closest('li, tr'), id = $item.attr('data-id'), type = $item.attr('data-type');

            
            if (id > 0) {

                
                REW_IDX_DASHBOARD.notice({className : 'loading', text : 'Processing Request...'});

                
                pid = Math.random() * 5;

                
                $.ajax({
                    url  : '/idx/inc/php/ajax/html.php?module=idx-dashboard&options[ajax]=true',
                    type : 'post',
                    data : {
                        pid : 'pid',
                        action : 'view',
                        type   : type,
                        row    : id
                    },
                    dataType : 'json',
                    success : function (json) {

                        
                        if (typeof(json.pid) != 'undefined' && json.pid != pid) return;

                        
                        if (json.html) {

                            
                            REW_IDX_DASHBOARD.notice({className : 'success', delay : 1000, text : ''});

                            
                            switch (type) {
                                case 'messages' :
                                    $('#dashboard_message').html(json.html);
                                    REW_IDX_DASHBOARD.selectPanel('#dashboard_message');
                                    break;
                            }

                        }

                        
                        if (json.errors) REW_IDX_DASHBOARD.notice({className : 'error', text : json.errors.join('<br>')});

                    },
                    error : REW_IDX_DASHBOARD.error
                });

            }

            return false;
        });

    },
    _setupPreferences : function () {

        
        $('#dashboard_preferences').delegate('form', 'submit', function () {
            var $this = $(this), data = $this.serialize(), pid = Math.random() * 5;
            $.ajax({
                url  : '/idx/inc/php/ajax/html.php?module=idx-dashboard&options[ajax]=true',
                type : 'post',
                data : {
                    pid    : pid,
                    action : 'update',
                    type   : 'profile',
                    data   : data
                },
                dataType : 'json',
                success : function (json) {
                    if (typeof(json.pid) != 'undefined' && json.pid != pid) return;
                    if (json.success) REW_IDX_DASHBOARD.notice({className : 'success', text : json.success.join('<br>')});
                    if (json.errors) REW_IDX_DASHBOARD.notice({className : 'error', text : json.errors.join('<br>')});
                },
                error : REW_IDX_DASHBOARD.error
            });
            return false;
        });

        
        $('#dashboard_password').delegate('form', 'submit', function () {
            var $this = $(this), data = $this.serialize(), pid = Math.random() * 5;
            $.ajax({
                url  : '/idx/inc/php/ajax/html.php?module=idx-dashboard&options[ajax]=true',
                type : 'post',
                data : {
                    pid    : pid,
                    action : 'update',
                    type   : 'password',
                    data   : data
                },
                dataType : 'json',
                success : function (json) {
                    if (typeof(json.pid) != 'undefined' && json.pid != pid) return;
                    if (json.success) REW_IDX_DASHBOARD.notice({className : 'success', text : json.success.join('<br>')});
                    if (json.errors) REW_IDX_DASHBOARD.notice({className : 'error', text : json.errors.join('<br>')});
                },
                error : REW_IDX_DASHBOARD.error
            });
            return false;
        });

    },
    _setupMessages : function () {

        
        $('#dashboard_compose').delegate('form', 'submit', function () {

            
            var $this = $(this), data = $this.serialize();

            
            var $subject = $('#dashboard_compose input[name="subject"]'), $message = $('#dashboard_compose textarea[name="message"]');

            
            REW_IDX_DASHBOARD.notice({className : 'loading', text : 'Processing Request...'});

            
            pid = Math.random() * 5;

            
            $.ajax({
                url  : '/idx/inc/php/ajax/html.php?module=idx-dashboard&options[ajax]=true',
                type : 'post',
                data : {
                    pid    : pid,
                    action : 'message',
                    type   : 'new',
                    data   : data
                },
                dataType : 'json',
                success : function (json) {

                    
                    if (typeof(json.pid) != 'undefined' && json.pid != pid) return;

                    
                    if (json.success) {

                        
                        REW_IDX_DASHBOARD.notice({className : 'success', text : json.success.join('<br>')});

                        
                        $subject.val('');
                        $message.val('');

                        
                        var $count = $('#count_messages'), count = parseInt($count.text().replace(',', '')) + 1;
                        $count.text(count.format());

                        
                        REW_IDX_DASHBOARD.loadMessages();

                    }

                    
                    if (json.errors) REW_IDX_DASHBOARD.notice({className : 'error', text : json.errors.join('<br>')});

                },
                error : REW_IDX_DASHBOARD.error
            });

            return false;
        });

        
        $('#send-message').bind('click', function () {
            $(this).closest('form').trigger('submit');
            return false;
        });

        
        $('#dashboard_message').delegate('form', 'submit', function () {

            
            var $this = $(this), data = $this.serialize();

            
            var row = $('#dashboard_message input[name="row"]').val();

            
            var $message = $('#dashboard_message textarea[name="message"]');

            
            REW_IDX_DASHBOARD.notice({className : 'loading', text : 'Processing Request...'});

            
            pid = Math.random() * 5;

            
            $.ajax({
                url  : '/idx/inc/php/ajax/html.php?module=idx-dashboard&options[ajax]=true',
                type : 'post',
                data : {
                    pid    : pid,
                    action : 'message',
                    type   : 'reply',
                    data   : data
                },
                dataType : 'json',
                success : function (json) {

                    
                    if (typeof(json.pid) != 'undefined' && json.pid != pid) return;

                    
                    if (json.success) {
                        REW_IDX_DASHBOARD.notice({className : 'success', text : json.success.join('<br>')});
                        REW_IDX_DASHBOARD.loadMessages();
                        $message.val('');
                    }

                    
                    if (json.errors) REW_IDX_DASHBOARD.notice({className : 'error', text : json.errors.join('<br>')});

                },
                error : REW_IDX_DASHBOARD.error
            });

            return false;
        });

        
        $('#reply-message').live('click', function () {
            $(this).closest('form').trigger('submit');
            return false;
        });

    },
    show : function (init) {

        var $overlay = $('.dashboard').data('overlay');
        if ($overlay) {

            
            $overlay.load();

        } else {

            
            $('.dashboard').overlay({
                oneInstance : true,
                load : true,
                top  : 0,
                mask : {
                    color: '#000',
                    opacity: 0.66
                },
                effect: 'drop'
            });

            
            $('.action-close').bind('click', function() {
                $('.dashboard').overlay().close();
            });

        }

    },
    notice : function (options) {
        var opts = $.extend({
            delay : 4000,
            className : ''
        }, options);
        $('#dashboard_notices').html('<div class="msg ' + opts.className + '">' + opts.text + '</div>');
        $('#dashboard_notices').find('.msg.' + opts.className).delay(opts.delay).fadeOut();
    },
    error : function () {
        REW_IDX_DASHBOARD.notice({className : 'error', text : 'An error has occurred while processing your request.'});
    },
    loadMessages : function () {
        $.ajax({
            url : '/idx/inc/php/ajax/json.php?module=idx-dashboard',
            type : 'get',
            data : {
                options : {
                    ajax : true,
                    init : true
                }
            },
            dataType : 'json',
            success  : function (json) {

                
                var module = json.module;
                if (module) {

                    
                    if (module.content) {

                        
                        $module = $(module.content);

                        
                        var html = $module.find('#list-messages').html();
                        $('#' + module.uid).find('#list-messages').html(html);

                        
                        var html = $module.find('#dashboard_messages').html();
                        $('#' + module.uid).find('#dashboard_messages').html(html);

                        
                        REW_IDX_DASHBOARD.selectPanel('#dashboard_messages');

                    }

                }

            }
        });
    }
};


$('#load-dashboard').live('click', function () {
    var $this = $(this);

    
    REW_IDX_DASHBOARD.init();

    
    $this.unbind('click').bind('click', function () {
        REW_IDX_DASHBOARD.show();
        return false;
    });

    return false;
});
