var login_form_open = false;

$(function() {
    // hint on select boxes
    $('option[value=""]').addClass('unselected');

    // form hints
    if (!(typeof document.body.style.maxHeight === "undefined")) {
        $('input, select, textarea').focus(function() {$('.hint').hide(); $(this).siblings('.hint').show();});
        $('input, select, textarea').blur(function() {$(this).siblings('.hint').hide();});
    }
    $('img.hint-icon').mouseover(function() {$('.hint').hide(); $(this).siblings('.hint').show();});
    $('img.hint-icon').mouseout(function() {$(this).siblings('.hint').hide();});

    // let textareas adhere to maxlength
    $('textarea[maxlength]').textLimiter();

    // popup for editorial
    $('a[rel="editorial-popup"]').click(function() {
        open_popup ('editorial_popup', $(this).attr('href'), 700, 600, {scrollbars: true, toolbar: true, location: true, resize: true});
        return false;
    });

    $('a[rel="thickbox-unique"]').click(function(){
        var title = $(this).attr('title') || null;
        var uri   = $(this).attr('href');
        
        uri = uri.replace('?', '/' + Math.floor(Math.random() * 10001) + '?');

        tb_show(title,uri,false);
        $(this).blur();
        return false;
    });


    // inline login form
    $('#main-log-in a').click(function() {
        $('#popup-login-form').fadeIn();
        $('#main-log-in').addClass('account selected');
        $('#main-log-in a').hide();

        return false;
    });

    $('#main-log-in-close').click(function() {
        $('#popup-login-form').fadeOut();
        $('#main-log-in').removeClass('account selected');
        $('#main-log-in a').show();

        return false;
    });

    $('input,textarea[title!=""]').hint();
});

// add jQuery regex selectors
jQuery.expr[':'].regex = function(elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ? 
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels,'')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}

// hide rows - use jQuery to hide form rows and child inputs

function hide_rows (rows) {
    for (i in rows) {
        var id = rows[i];
        $('#' + id + '-row').hide();
    }
}

function show_rows (rows) {
    for (i in rows) {
        var id = rows[i];
        $('#' + id + '-row').show();
    }
}

