/**
 * 
 * Custom js (jQuery) functions of the application
 *
 */

String.prototype.ellipsis = function (limit) {
    var center = ' ... ';
    var len = this.length;
    if (len > limit) { 
        var half = Math.floor( (limit - center.length)/2 );
        return this.substr(0, half) + center + this.substr(len-half, half);
    }
    return this.toString();
}

// simple accordeon menu
jQuery.fn.accordeonMenu = function () {
    var menu = $(this);
    $('> li > a:not(.active) ~ .sub', menu).hide();
    var items = $('> li > a', menu).click(function () {
        items.removeClass('active');
        $(this).addClass('active').siblings('.sub').slideDown('normal');
        items.not('.active').siblings('.sub').slideUp('normal');
    });
    return menu;
};//eof accordeonMenu

jQuery.fn.digitsOnly = function () {
    var $this = $(this);
    $this.keypress(function (e) {
        // allow digits only
        if (e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
            return false;
        }
    });
    return $this;
};


$(document).ready(function () {
    $('#accordion').accordeonMenu();        

    $('a.menu_item').click(function () {
        $.cookie('selected_path', $(this).attr('__path'), {path: '/'});
    });

    $('#search_btn').click(function () {
        var criteria = $('#search_criteria').val();

        if (!criteria) {
            return false;
        }

        var search_form = $('#search_form'),
            search_url  = search_form.attr('action');
        
        // append search criteria to uri
        search_url += '/' + encodeURI(criteria);

        search_form.attr('action', search_url);
        return true;
    });


    $('.edit_content').nyroModal({
        width: 700,
        height: 300
    });

});
