// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/*
 * These functions handle the search field stuff
 */
function filter_playlist(filter_by) {
    var entries = $('playlist').select('.entry');
    var shown_entries = 0;
    entries.each(function(entry) {
        if (filter_by == 'none' || entry.select('.icon')[0].getAttribute('name') == filter_by) {
            entry.show();
            shown_entries++;
        } else {
            entry.hide();
        }
    });
    if (shown_entries == 0 && $('miss') == null) {
        $('playlist').insert('<div id="miss">Niets gevonden!</div>');
    } else if (shown_entries > 0 && $('miss') != null)
        $('miss').remove();
    // update breadcrumbs and direct link
    var direct_link_field = $('direct_link_field');
    if (filter_by == 'none') {
        $('bc_filter').update('');
        direct_link_field.value = direct_link_field.value.gsub(/(\?|&)filter=.*?(&|$)/, '');
    } else {
        $('bc_filter').update(' (gefilterd op ' + filter_by + ')');
        newlink = direct_link_field.value.gsub(/filter=.*?(?=&|$)/, 'filter=' + filter_by);
        if (newlink == direct_link_field.value) {
            if (direct_link_field.value.match(/\?/))
                direct_link_field.value += '&filter=' + filter_by;
            else
                direct_link_field.value += '?filter=' + filter_by;
        }
    }
    $('filter_options').hide();
}

function filter_playlist_by_name(filter_category) {
    $$('.entry').each(function(entry) {
        if (filter_category == null || entry.readAttribute('name') == filter_category)
            entry.show();
        else
            entry.hide();
    });
}

function clear_search_field(default_search_text) {
  if ($('search_field').value == default_search_text)
    $('search_field').value = '';
  else
    $('search_field').select();
}

/*
 * Playlist sorting (alfabetical or date)
 */
function sort_playlist(sort_type) {
    var entries = $('playlist').select('.entry');
    switch (sort_type) {
        case 'a_z':
        if ($('sort_pl_a_z').className == '') {
            entries.sort(function(a, b) {
                return strcmp(b.select('span.pl_title')[0].firstChild.nodeValue, a.select('span.pl_title')[0].firstChild.nodeValue);
            });
            $('sort_pl_a_z').className = 'invert';
        } else {
            entries.sort(function(a, b) {
                return strcmp(a.select('span.pl_title')[0].firstChild.nodeValue, b.select('span.pl_title')[0].firstChild.nodeValue);
            });
            $('sort_pl_a_z').className = '';
        }
        break;
        case '1_31':
        if ($('sort_pl_1_31').className == '') {
            entries.sort(function(a, b) {
                return parseInt(a.select('.pl_title')[0].getAttribute('name')) - parseInt(b.select('.pl_title')[0].getAttribute('name'));
            });
            $('sort_pl_1_31').className = 'invert';
        } else {
            entries.sort(function(a, b) {
                return parseInt(b.select('.pl_title')[0].getAttribute('name')) - parseInt(a.select('.pl_title')[0].getAttribute('name'));
            });
            $('sort_pl_1_31').className = '';
        }
        break;
    }
    $('playlist').childElements().each(function(entry) {entry.remove()});
    entries.each(function(entry) {$('playlist').appendChild(entry)});
}

function strcmp(a, b) {
    if      (a < b) return -1;
    else if (a > b) return  1;
    else            return  0;
}

/*
 * This handles the showing and hiding of comments in the playlist
 */
function show_comments(comment_id) {
    var cur_comment = $$('.comments').find(function(comment) {
        return comment.style.cssText.match(/display: none/i) == null;
    });
    if (cur_comment != null) {
        Effect.SlideUp(cur_comment, {duration: 0.3});
        if (cur_comment.id == comment_id) {
            return false;
        }
    }
    if ($(comment_id) != null) {
        Effect.SlideDown(comment_id, {duration: 0.3});
    }
    return false;
}

/*
 * This highlights the currently chosen item from the playlist, and un-higlights the other items.
 */
function set_current_item(item_id) {
    $$('#playlist a.item').each(function(entry) {
        if (entry.id == item_id) {
            entry.className = 'item current';
        } else {
            entry.className = 'item';
        }
    });
}

Element.addMethods({
    non_empty_text_nodes: function(element) {
        element = $(element);
        var textnodes = [];
        $A(element.childNodes).each(function(node) {
            if (node.nodeType == 1) {
                var result = node.non_empty_text_nodes(node);
                textnodes = textnodes.concat(result);
            } else if (node.nodeType == 3 && node.nodeValue.search(/^\s+$/g) == -1) {
                textnodes.push(node);
            }
        });
        return textnodes;
    },
    
    contains_string: function(element, str) {
        element = $(element);
        if (str == '' || element.non_empty_text_nodes().find(function(textnode) {
                            return (textnode.nodeValue.search(new RegExp(str, 'im')) > -1);
                         })) {
            return element;
        }
    }
});

function show_table_rows_containing(str) {
    $$('tr').each(function(row) {
        var cols = row.select('td.searchable');
        if (cols.length > 0) {
            if (str == '' || cols.find(function(col) {return col.contains_string(str);})) {
                row.show();
            } else {
                row.hide();
            }
        }
    });
}

var observing_resize  = false;
var media_image_ratio = 1;

function auto_resize_player() {
    auto_resize_event(player_resizer);
}

function auto_resize_media() {
    auto_resize_event(player_resizer);
    Event.observe(window, 'load', player_resizer);
    player_resizer();
}

function auto_resize_image() {
    media_image_ratio = $('media_image').width / $('media_image').height;
    image_resizer();
    $('media_image').style.visibility = 'visible';
    auto_resize_event(image_resizer);
}

function auto_resize_event(callback) {
    if (observing_resize != false) {
        Event.stopObserving(window, 'resize', observing_resize);
    }
    observing_resize = callback;
    Event.observe(window, 'resize', observing_resize);
}

function player_resizer(obj) {
    if ($('player')) {
        $('player').style.height = $('playlist').offsetHeight + 'px'; // use playlist, IE6 doesn't know how high $('player') is
        $('player').style.width  = ($('media').offsetWidth - 390) + 'px';
    }
}

function image_resizer() {
    var width     = $('media').offsetWidth - 390;
    var height    = $('playlist').offsetHeight;
    var div_ratio = width / height;
    if (div_ratio >= media_image_ratio) {
        $('media_image').style.width  = Math.round(height * media_image_ratio) + 'px';
        $('media_image').style.height = height + 'px';
        $('media_image').style.left   = Math.round((width - height * media_image_ratio) / 2) + 'px';
        $('media_image').style.top    = 0;
    } else {
        $('media_image').style.width  = width + 'px';
        $('media_image').style.height = Math.round(width / media_image_ratio) + 'px';
        $('media_image').style.left   = 0;
        $('media_image').style.top    = Math.round((height - width / media_image_ratio) / 2) + 'px';
    }
}

function playerReady(obj) {
    player_resizer();
}
