PDC = PDC || {};
PDC.schools = {};

$.extend(PDC.schools, {
    canAdd: function(link){
        if (PDC.guest())
            return true;

        if ($('input[name=city_id]').val()) {
            $('#school_add_links').hide();
            $('#school_add_form').show();
        }
        else {
            PDC.ui.error( $(link).attr('alt') );
        }
        
        return false;
    },
    
    checkForm: function(form)
    {
        var date = new Date,
            min_year = 1955;

        var sName = $('input[name="schoolname"]', form).length ? $('input[name="schoolname"]', form).val() : true,
            sFrom = $('input[name="fromyear"]', form).val(),
            sTo   = $('input[name="toyear"]', form).val();
        
        var allRight = !!sName
                && !!sFrom && sFrom > min_year && sFrom <= date.getFullYear()
                && !!sTo && sTo > min_year 
                && sFrom <= sTo;
                
        return allRight;
    },
    
    schoolSubmit: function() 
    {
        if (PDC.schools.checkForm()) {
            $('#school_city_id').val( $('input[name=city_id]').val() );
            return true;
        }

        PDC.ui.error( SCHOOLS_ADD_ERR );
        return false;
    },
    
    updateGeo: function(cid){
        PDC.ui.show_loading();
        
        PDC.loadModule('#schools_list', 'schools:schools_list', {city_id: cid}, function(response){
            PDC.ui.hide_loading();
            $('#schools_list').html(response).removeClass('gray info');
            $('#schools_list .ui-pager a').each( function(){
                $(this)
                    .attr('href', '?city_id='+ $(sel).val() +'&page='+ $(this).attr('page'))
                    .addClass('ui-corner-all');
            })
        });
    },
    
    addDlg: function (link, sid) {
        var $el = $('<div />').addClass('hidden').appendTo(document.body);
        
        PDC.ui.show_loading();
        PDC.loadModule($el, 'schools:add_form_small', {school_id: sid}, function(){
            PDC.ui.hide_loading();
            PDC.ui.dialog('Years attended', $el, {
                width: 200,
                modal: true,
                close: function(ev, ui) {
                    $(this).dialog('destroy');
                    $el.remove();
                },
                buttons: {
                    Cancel: function() { $(this).dialog('close') },
                    Add: function() {
                        if ( PDC.schools.checkForm( $('form', $el) ) ){
                            $(this).dialog('close');
                            var processing = $('<span class="right gray">processing...</span>');
                            $(link).replaceWith( processing );

                            var params = {
                                school_id: sid,
                                fromyear: $('input[name="fromyear"]', $el).val(),
                                toyear: $('input[name="toyear"]', $el).val(),
                                act: 'process'
                            };
                            PDC.requestModule('schools:add_form', params, function(response){
                                $(processing).remove();
                            });
                        }
                        else {
                            $('#school_add_error').show();
                        }
                    }
                } // buttons
            });// dialog
        });
        
        return false;
    },
    
    // my schools
    initMySchools: function(){
        $('li[sid]').hovered();
        $('li[sid] div .ui-icon').addClass('pointer');
        
        // init remove
        $('li[sid] .ico-cross-gray')
            .hover(
                function() { $(this).replaceClass('ico-cross-gray', 'ico-cross-red') },
                function() { $(this).replaceClass('ico-cross-red', 'ico-cross-gray') }
            )
            .click( function(){
                var self = $(this);
                var sid  = $(this).parents('li').attr('sid');
                
                PDC.ui.confirm( ARE_YOU_SURE, DELETE_SCHOOL, function(){ 
                    $(self).parents('li').addClass('deleted');
                    PDC.requestModule('schools:manager', {act: 'delete', school_id: sid}, function(response){
                        if (!response.error)
                            $(self).parents('li').remove();
                    });
                }, null, true);
            })
            .each( function() {
                PDC.schools.attachHint(this, DELETE_SCHOOL);
            });

        // init edit
        $('li[sid] .ico-stiker-text')
            .each( function() {
                PDC.schools.attachHint(this, EDIT_SCHOOL);
            })
            .click( function(){
                PDC.schools.editDialog( this ); 
            });
            
        // init emmbers
        $('.members').each( function() {
            PDC.schools.attachHint(this, MEMBERS);
        });
    },
    
    attachHint: function(el, hint){
        var li = $(el).parents('li');
        $(el).hover (
            function() { $('span.li_hint', li).html( hint + " &nbsp;" ) },
            function() { $('span.li_hint', li).html( '' ) }
        );
    },
    
    editDialog: function(link){
        var $el = $('<div />').addClass('hidden').appendTo(document.body);
        var sid = $(link).parents('li').attr('sid');
        
        PDC.ui.show_loading();
        PDC.loadModule($el, 'schools:add_form_small', {school_id: sid}, function(){
            PDC.ui.hide_loading();

            $('input[name="fromyear"]', $el).val( $('.yFrom', $(link).parents('li')).html() );
            $('input[name="toyear"]', $el).val( $('.yTo', $(link).parents('li')).html() );
            
            PDC.ui.dialog('Edit years', $el, {
                width: 200,
                modal: true,
                close: function(ev, ui) {
                    $(this).dialog('destroy');
                    $el.remove();
                },
                buttons: {
                    Cancel: function() { $(this).dialog('close') },
                    Save: function() {
                        if ( PDC.schools.checkForm( $('form', $el) ) ){
                            $(this).dialog('close');
                            
                            var from = $('input[name="fromyear"]', $el).val();
                            var to   = $('input[name="toyear"]', $el).val()
                            
                            $('.yFrom', $(link).parents('li')).html( from );
                            $('.yTo', $(link).parents('li')).html( to );
                            
                            var params = {
                                school_id: sid,
                                fromyear: from,
                                toyear: to,
                                act: 'save'
                            };
                            
                            PDC.requestModule('schools:manager', params, function(response){
                                if (!response.error) 
                                    $(link).parents('li').css('backgroundColor', '#fea').animate({backgroundColor: '#fff'}, 1500);
                            });
                        }
                    }
                } // buttons
            });// dialog
        });
        
        return false;
    }
});
