var jobs = {

    admin: {
        action: {
            'package_activate': function (refid) {
                var out;
                if (window.confirm('Are you sure that you want to Activate this package?')) {
                    $.ajax({ url: '/Admin/Action/0/package/' + refid + '/activate', type: 'POST', datatype: 'json', async: false, success: function (data) { out = data } });
                }
                return out;
            },
            'package_deactivate': function (refid) {
                var out;
                if (window.confirm('Are you sure that you want to Deactivate this package?')) {
                    $.ajax({ url: '/Admin/Action/0/package/' + refid + '/deactivate', type: 'POST', datatype: 'json', async: false, success: function (data) { out = data } });
                }
                return out;
            },
            'package_signup': function (refid) {
                var out;
                if (window.confirm('Are you sure that you want to Sign-Up this package?')) {
                    $.ajax({ url: '/Admin/Action/0/package/' + refid + '/signup', type: 'POST', datatype: 'json', async: false, success: function (data) { out = data } });
                }
                return out;
            },
            'package_unsign': function (refid) {
                var out;
                if (window.confirm('Are you sure that you want to Un-Sign this package?')) {
                    $.ajax({ url: '/Admin/Action/0/package/' + refid + '/unsign', type: 'POST', datatype: 'json', async: false, success: function (data) { out = data } });
                }
                return out;
            },
            'package_setprice': function (refid) {
                var out, newval;
                if (newval = window.prompt('Please enter a price for this package', '0.00')) {
                    $.ajax({ url: '/Admin/Action/0/package/' + refid + '/setprice?' + encodeURIComponent(newval), type: 'POST', datatype: 'json', async: false, success: function (data) { out = data } });
                }
                return out;
            },
            'package_delete': function (refid) {
                var out;
                if (window.confirm('Are you sure that you want to Delete this package?')) {
                    $.ajax({ url: '/Admin/Action/0/package/' + refid + '/delete', type: 'POST', datatype: 'json', async: false, success: function (data) { out = data } });
                }
                return out;
            },

            'job_activate': function (refid) {
                var out;
                if (window.confirm('Are you sure that you want to Activate this job?')) {
                    $.ajax({ url: '/Admin/Action/0/job/' + refid + '/activate', type: 'POST', datatype: 'json', async: false, success: function (data) { out = data } });
                }
                return out;
            },
            'job_deactivate': function (refid) {
                var out;
                if (window.confirm('Are you sure that you want to Deactivate this job?')) {
                    $.ajax({ url: '/Admin/Action/0/job/' + refid + '/deactivate', type: 'POST', datatype: 'json', async: false, success: function (data) { out = data } });
                }
                return out;
            },
            'job_delete': function (refid) {
                var out;
                if (window.confirm('Are you sure that you want to Delete this job?')) {
                    $.ajax({ url: '/Admin/Action/0/job/' + refid + '/delete', type: 'POST', datatype: 'json', async: false, success: function (data) { out = data } });
                }
                return out;
            },

            'client_resetpassword': function (refid) {
                var out, newval;
                if (newval = window.prompt('Please enter a new password for this account', 'password' + ('0' + new Date().getSeconds()).slice(-2))) {
                    $.ajax({ url: '/Admin/Action/0/client/' + refid + '/resetpassword?' + encodeURIComponent(newval), type: 'POST', datatype: 'json', async: false, success: function (data) { out = data } });
                }
                return out;
            },

            'candidate_resetpassword': function (refid) {
                var out, newval;
                if (newval = window.prompt('Please enter a new password for this account', 'password' + ('0' + new Date().getSeconds()).slice(-2))) {
                    $.ajax({ url: '/Admin/Action/0/candidate/' + refid + '/resetpassword?' + encodeURIComponent(newval), type: 'POST', datatype: 'json', async: false, success: function (data) { out = data } });
                }
                return out;
            },

            _ends: true
        },
        linkactions: function () {
            $('select#adminactions').live('change', function (e) {
                if (this.selectedIndex > 0) {
                    var opt = this.options[this.selectedIndex].value.split(':'), fn = jobs.admin.action[opt[0] + '_' + opt[2]], data;
                    if (typeof fn === 'function' && (data = fn(opt[1]))) {
                        if (data.ok)
                            window.location.reload();
                        else
                            alert(data.error || 'An error occurred.');
                    }
                }
                this.selectedIndex = 0;
                this.blur();
            });
        }
    },

    settooltips: function ($ctrls) {
        $ctrls.live('mouseover mouseout', function (e) {
            //var offset = e.target.offset();
            if (event.type == 'mouseover') {
                $('<p id="tooltip">' + this.innerText + '</p>').appendTo('body').css({ top: (e.pageY -e.offsetY+$(e.target).height()+2) + "px", left: (e.pageX -e.offsetX ) + "px" }).fadeIn('fast');
            }
            else {
                $("#tooltip").remove();
            }
        });
    },

    disable: function () {
        $('<div id="disablecover" class="disable"></div>')
                .appendTo(document.body)
                .css({ opacity: 0.6, zIndex: 99 })
    },
    enable: function () {
        $('div#disablecover', document.body).remove();
    },

    popup: function (data, css) {
        var $popup = $('<div class="popup"><button type="button" class="close">&#160;</button>' + data + '</div>').css(css).appendTo(document.body);
        var docw = $(window).width(), doch = $(window).height();
        var left = (docw - $popup.outerWidth()) / 2, top = (doch - $popup.outerHeight()) / 2;
        jobs.disable();
        $popup
            .css({ left: left + 'px', top: top + 'px', zIndex: 100 })
            .show();
        $('button.close', $popup).click(function () {
            $popup.hide();
            jobs.enable();
        });
    },

    jobdetail: {
        init: function (form) {
            var $salarytext = $('input#salarytext', form).addClass('locked').attr('readonly', 'readonly');

            var $salary = $('select#salarylower,select#salaryupper', form),
                $jobrate = $('select#jobratelower,select#jobrateupper', form),
                $negotiable = $('input#salarynegotiable', form);

            var fnsalarytext = function (from, to, post) {
                return from != '...' || to != '...' ? [from != '...' ? from : '', to != '...' ? ' to ' + to : '+', post].join('') : '';
            },
            fnslots = function () {
                var required = $('input[name="siteid"]:checked', form).length * parseInt($('select#duration', form).val(), 10);
                $('input#slotsrequired', form).val(required > 1 ? '(' + required + ' Ad/Weeks required)' : '' || required == 1 ? '(' + required + ' Ad/Week required)' : '');
            }

            $salary.change(function () {
                $salarytext.val(fnsalarytext($('option:selected', $salary.filter('select#salarylower')).text(), $('option:selected', $salary.filter('select#salaryupper')).text(), ' per annum'));
                $jobrate.val('');
                $negotiable.removeAttr('checked');
            });
            $jobrate.change(function () {
                $salarytext.val(fnsalarytext($('option:selected', $jobrate.filter('select#jobratelower')).text(), $('option:selected', $jobrate.filter('select#jobrateupper')).text(), ' per hour'));
                $salary.val('');
                $negotiable.removeAttr('checked');
            });
            $negotiable.change(function () {
                $salarytext.val(this.checked ? 'Negotiable' : '');
                $salary.val('');
                $jobrate.val('');
            });
            $('input#editsalarytext', form).click(function () {
                $salarytext.removeClass('locked').removeAttr('readonly');
            });

            $('select#duration,input[name="siteid"', form).change(fnslots);

            fnslots();
        }
    },

    candidate: {
        cv:
        {
            url: '/Candidate/CV/Create',
            create: function () {
                $.ajax(jobs.candidate.cv.url, {
                    async: false,
                    data: ({}),
                    type: 'GET',
                    success: function (data, ts, xhr) {
                        jobs.popup(data, { width: '600px' });
                    },
                    error: function (xhr) {
                        alert('Call failed : ' + (/<head[^>]*>[\s\S]*<title[^>]*>([\s\S]*)<\/title>[\s\S]*<\/head>/i.test(xhr.responseText) ? RegExp.$1 : '(unspecified)'));
                    }

                });
            }
        },

        letter:
        {
            url: '/Candidate/Letter/Create',
            create: function () {
                $.ajax(jobs.candidate.letter.url, {
                    async: false,
                    data: ({}),
                    type: 'GET',
                    success: function (data, ts, xhr) {
                        jobs.popup(data, { width: '600px' });
                    },
                    error: function (xhr) {
                        alert('Call failed : ' + (/<head[^>]*>[\s\S]*<title[^>]*>([\s\S]*)<\/title>[\s\S]*<\/head>/i.test(xhr.responseText) ? RegExp.$1 : '(unspecified)'));
                    }

                });
            }
        },

        cvpopup: function () {
            var $popup = $('div.popup#candidate-addcv');
            jobs.cover($popup.parent());

            var docw = $(window).width(), doch = $(window).height();
            var left = (docw - $popup.outerWidth()) / 2, top = (doch - $popup.outerHeight()) / 2;
            $popup
            .css({ left: left + 'px', top: top + 'px', zIndex: 100 })
            .show();

        },

        letterpopup: function () {

            var $popup = $('div.popup#candidate-addletter');
            jobs.cover($popup.parent());

            var docw = $(window).width(), doch = $(window).height();
            var left = (docw - $popup.outerWidth()) / 2, top = (doch - $popup.outerHeight()) / 2;
            $popup
            .css({ left: left + 'px', top: top + 'px', zIndex: 100 })
            .show();
        }

    }
}
