/* IE6nomore
------------------------------------------------------------------------------------------------ */

$.IE6nomore = function() {

    if(!$('#ie6nomore').length)
        return;

    $('#ie6nomore').css({
        'left'      : 0,
        'top'       : 0,
        'bottom'    : 0,
        'right'     : 0,
        'position'  : 'absolute',
        'z-index'   : 9999
    });

};

/* Spam protection
------------------------------------------------------------------------------------------------ */

function getAdr(prefix, postfix, text) {
    document.write('<a href="mailto:' + prefix + '@' + postfix + '">' + (text ? text.replace(/&quot;/g, '"').replace(/%EMAIL%/, prefix + '@' + postfix) : prefix + '@' + postfix) + '</a>');
}

/* Get url vars
------------------------------------------------------------------------------------------------ */

$.getUrlVars = function() {

    var vars    = [],
        hash    = '',
        hashes  = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    $(hashes).each(function() {
        hash = this.split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    });

    return vars;
};

/* Grid
------------------------------------------------------------------------------------------------ */

$.grid = function() {

    if ($.getUrlVars()['grid'] == 1) {
        $.cookie('grid', 'show', { path: '/', expires: 7 });
    }

    if ($.getUrlVars()['grid'] == 0) {
        $.cookie('grid', '', { path: '/', expires: 7 });
    }

    if ($.cookie('grid') == 'show') {
        $('#content').addClass('showgrid');
    }

};

/* Create onchange select plugin
------------------------------------------------------------------------------------------------ */

(function($){
    $.fn.createOnchangeSelect = function(options){
        var settings = $.extend({
            'label': false,
            'group': false,
            'id': 'selection'
        }, options);

        return this.each(function(){
            var el = this;
            if(!$('li a', el).length)
                return;
            if(settings.group){
                // Create form and select
                $(el).append(
                    form = $('<form>').attr('action', '').append(
                        select = $('<select>').attr({
                            'id': settings.id,
                            'name': settings.id
                        }).addClass($(this).attr('class'))
                    )
                );
                // Create optgroup
                $(settings.group, el).each(function(){
                    $(select).append(
                        optgroup = $('<optgroup>').attr('label', $(this).text())
                    );
                    // Fill select with options
                    $.fn.createOnchangeSelect.fillSelect($(this).next('ul'), optgroup);
                });
            }
            else{
                $('ul, ol', el).each(function(){
                    // Create form and select
                    $(this).after(
                        form = $('<form>').attr('action', '').append(
                            select = $('<select>').attr({
                                'id': settings.id,
                                'name': settings.id
                            }).addClass($(this).attr('class'))
                        )
                    );
                    // Fill select with options
                    $.fn.createOnchangeSelect.fillSelect(el, select);
                });
            }
            // Onchange
            $(select).change(function(){
                location.href=this.value;
            });
            // Create label
            if(settings.label){
                $(form).prepend(
                    label = $('<label>').attr('for', settings.id).text($(settings.label).text())
                );
            }
            // Remove non javascript html code
            $('ul, ol, '+settings.group, el).remove();
            $(settings.label).remove();
        });
    }

    // Fill select with options
    $.fn.createOnchangeSelect.fillSelect = function(el, select){
        $('li a', el).each(function(){
            $(select).append(
                option = $('<option>').val(this.href).text($(this).text())
            );
            if($(this).hasClass('active'))
                $(option).attr('selected', 'selected');
        });
    }
})(jQuery);

/* DOM
------------------------------------------------------------------------------------------------ */

$(document).ready(function() {

    // IE6nomore

    $.IE6nomore();

    // Grid
    
    $.grid();
    
    // Forms

    $('#contact').forms({ 
        'confirmpage'   : 'iframe'
    });

    $('textarea').autogrow({
        'minheight' : 200
    });

    // News object

    $('#news_selection').createOnchangeSelect({
        'label' : '#news_selection h3'
    });

    // Infolists

    $('.selection').createOnchangeSelect({ 'group': '.group' });

    // Menu
    
    $('#nav').menu();

});

