$( 'document' ).ready( function(){
    if( $.browser.msie ) {
        if( jQuery.browser.version == '6.0' ) {
            $('#header > ul').addClass('rho-top-buttons');
            $('#header > ul > li').addClass('rho-top-button');
            $('#header > strong').addClass('rho-logo');
            $('#content_s > ul > li').hover( function() {
                $(this).children('ul').show();
            },function() {
                $(this).children('ul').hide();
            });
        }
        // ie8
        $('table.rho-table > tbody > tr > td').addClass( 'rho-td' );
        $('table.offers > tbody > tr > td:nth-child(2)').addClass('title');
        $('table.offers.np > tbody > tr > td:nth-child(1)').addClass('title');
        $('table.offers.np > tbody > tr > td:nth-child(2)').removeClass('title');
    }
    $( ".btn, input[type=submit], input[type=reset], input[type=checkbox].jq, #header > ul li" ).button();
    $( ".jqradio, .jqcheckboxgroup" ).buttonset();
    $.datepicker.setDefaults( $.datepicker.regional[$('.datepicker').attr('locale')] );
    $( ".datepicker" ).datepicker( {
        dateFormat : $('.datepicker').attr('format'),
        changeMonth : true,
        changeYear : true,
        showButtonPanel : true,
        minDate : new Date(1940,1 - 1,1),
        maxDate : '+1d',
        yearRange: '1940'
    });
    $( 'select' ).selectmenu({
        width: 220
    });
    $( '.jqselect' ).change( function(){
        var event = $(this).attr('rho-event');
        var id = $(this).val();

        $.get( event+'id/'+id+'/', function(data) {
            var response = eval( "("+data+")" );
        });
    });
    $( '.accordion' ).accordion({
        autoHeight: false
    });
    $( '.rho-multichecbox-select input[type=checkbox]' ).live( 'change', function() {
        var id = $(this).parents('tr').children().next().children('select').attr('id');
        if( $(this).is(':checked') ) {
            $('#'+id).attr( 'aria-haspopup', 'true' );
            $('#'+id).removeAttr( 'disabled' );
            $('#'+id).removeAttr( 'aria-disabled' );
            $('#'+id).removeClass('ui-selectmenu-disabled');
            $('#'+id).removeClass('ui-state-disabled');
            $('#'+id).next().removeAttr( 'aria-disabled' );
            $('#'+id).next().removeClass('ui-selectmenu-disabled');
            $('#'+id).next().removeClass('ui-state-disabled');
        }
        else {
            $('#'+id).attr( 'disabled', 'disabled' );
            $('#'+id).attr( 'aria-disabled', 'true' );
            $('#'+id).removeAttr( 'aria-haspopup' );
            $('#'+id).addClass('ui-selectmenu-disabled');
            $('#'+id).addClass('ui-state-disabled');
            $('#'+id).next().attr( 'aria-disabled', 'true' );
            $('#'+id).next().addClass('ui-selectmenu-disabled');
            $('#'+id).next().addClass('ui-state-disabled');
        }
    });
    $( '.filter input' ).keydown( function(event) {
        if( event.keyCode == 13 ) {
            var text = $(this).val();
            var href = $(this).attr( 'href' );
            if( text != '' ) {
                location.href = href + text;
            }
            else {
                location.href = href;
            }
        }
    });
    $( 'img[rho-event=delete], img[rho-event=edit], img[rho-event=status]' ).live( 'click', function() {
        var eventPath = $(this).parents('.rho-table').attr( 'rho-event-path' );
        var event     = $(this).attr('rho-event');
        var id        = $(this).parents('tr').attr('row-id');

        if( event == 'delete' ) {
            var label_yes = $( 'span[translate-id=text_yes]' ).text();
            var label_no  = $( 'span[translate-id=text_no]' ).text();

            var buttons = {};
            buttons[ label_no ]  = function() {
                $(this).dialog('close');
            };
            buttons[ label_yes ] = function() {
                rho_event( eventPath, event, id );
            };

            var _this = this;
            $(_this).parent().parent().addClass( 'selected' );

            $( ".dialog" ).dialog( {
                autopen: false,
                buttons: buttons,
                modal: true,
                close: function(event,ui) {
                    $(_this).parent().parent().removeClass('selected');
                }
            });
        }
        else if( event == 'edit' || event == 'status' ) {
            rho_event( eventPath, event, id );
        }
    });
    $( '.group-btn > img' ).click( function(){
        var id = $(this).attr('rho-group-id');
        var path = $(this).parents('.glist').attr('eventpath')+'id/'+id;

        var label_yes = $( 'span[translate-id=text_yes]' ).text();
        var label_no  = $( 'span[translate-id=text_no]' ).text();

        var buttons = {};
        buttons[ label_no ]  = function() {
            $(this).dialog('close');
        };
        buttons[ label_yes ] = function() {
            $.get(path,function(data) {
                var response = eval('('+data+')');
                if( response.status == true ) {
                    location.reload();
                }
                else {
                    alert( data );
                }
            });
        };

        $( ".dialog" ).dialog( {
            autopen: false,
            buttons: buttons,
            modal: true,
            close: function(event,ui) {
                $(_this).parents('rho-group').removeClass('selected');
            }
        });
    });
    $( 'table.offers > tbody > tr, table.candidates > tbody > tr' ).click( function() {
        var id = $(this).attr( 'row-id' );
        if( id ) {
            var path = $(this).children('td').children('.offerLink').attr("href");
            location.href = path;
        //var path = $(this).parents('table').attr('rho-event-path');
        //location.href = path+''+id;
        }
    });
    $( '#candidacy_status' ).change( function() {
        var offer_id = $(this).attr('offer_id');
        var user_id = $(this).attr('user_id');
        var path = $(this).attr('path');
        var status = $(this).val();
        if( user_id ) {
            $.get( path+"offer_id/"+offer_id+"/user_id/"+user_id+"/status/"+status+"/", function(data) {
                var response = eval("("+data+")");
                if( response.status == true ) {
            }
            });
        }
    });

    var cache = {}, lastXhr;
    $('#ac_cities').autocomplete({
        minLength: 4,
        source: function(request,response) {
            var term = request.term;
            if ( term in cache ) {
                response( cache[ term ] );
                return;
            }
            lastXhr = $.getJSON( "/cities", request, function( data, status, xhr ) {
                cache[ term ] = data;
                if ( xhr === lastXhr ) {
                    response( data );
                }
            });
        },
        select: function(event,ui){
            search_city(ui.item.value);
        }
    });

    $('#ac_cities').change( function(){
        search_city(this.value);
    });

    $(this).everyTime( 8000, 'update', function() {
        $( '.adverb-top li' ).last().hide( 'slide', 1400, function() {
            $( '.adverb-top ul' ).first().prepend( "<li>"+$(this).html()+"</li>" );
            $( '.adverb-top li' ).last().remove();
        });
    });
});

function rho_event( eventPath, event, id ) {
    $.get(eventPath+'event/'+event+'/id/'+id, function(data) {
        var response = eval( '('+data+')' );
        if( response.status == true ) {
            if( event == 'delete' ) {
                location.reload();
            }
            else if( event == 'edit' ) {
                location.href = response.content;
            }
            else if( event == 'status' ) {
                $('.rho-table tr[row-id='+id+'] img[rho-event=status]').parent().replaceWith( response.content );
            }
        }
    });
}

function rho_form_element_toggle( _id, _val ) {
    if( _val ) {
        $( "#"+_id ).attr('disabled','disabled');
    }
    else {
        $( "#"+_id ).attr('disabled','');
    }
}

function rho_form_element_r_toggle( _id, _val ) {
    if( _val ) {
        $( "#"+_id ).attr('disabled','');
    }
    else {
        $( "#"+_id ).attr('disabled','disabled');
    }
}

function rho_replace_formelement( _url, _id, _selectId ) {
    $.get( _url+''+_id, function(data) {
        var response = eval( "("+data+")" );
        if( response.status == true ) {
            $( "#"+_selectId ).replaceWith( response.content );
            $( ".btn, input[type=submit], input[type=reset], input[type=checkbox]" ).button();
            $( ".jqradio, .jqcheckboxgroup" ).buttonset();
            $( 'select' ).selectmenu({
                width: 220
            });
        }
    });
}

function rho_tozfurl( _this, _defparams ) {
    var url = $(_this).attr('action');
    for( i=0; i<_defparams.length; i++ ) {
        url = url + "/" + _defparams[i];
    }

    var params = $(_this).serializeArray();
    for( i=0; i<params.length; i++ ) {
        url = url + "/" + params[i].value;
    }
    url += "/";

    location.href = url;
}

function search_city(city) {
    city = city.toLowerCase();
    city = city.replace("ą","a");
    city = city.replace("ę","e");
    city = city.replace("ć","c");
    city = city.replace("ń","n");
    city = city.replace("ż","z");
    city = city.replace("ź","z");
    city = city.replace("ó","o");
    city = city.replace("ł","l");
    city = city.replace("ś","s");

    location.href = "/" + $('#ac_cities').attr('link')+"-"+city;
}
