/*****************************************************************************
 * Gossamer Links
 *
 *   Website  : http://gossamer-threads.com/
 *   Support  : http://gossamer-threads.com/scripts/support/
 *   Revision : $Id: imagepreview.js,v 1.6 2008/11/03 21:05:23 aaron Exp $
 *
 * Copyright (c) 2008 Gossamer Threads Inc.  All Rights Reserved.
 * Redistribution in part or in whole strictly prohibited. Please
 * see LICENSE file for full details.
 *****************************************************************************/

// The object wrapper function.
function Eaglestar() {}

/**
 * Initialize tooltips.
 */
Eaglestar.prototype.initToolTips = function() {
    $('.tooltip').bind('mouseover', function() {
        var text = $(this).attr('title');
        $(this).attr('title', '');
        var id = $(this).attr('id');
        var newId = id+'_tooltip';

        // Add the tooltip text to the document.
        $('body').prepend('<div class="tooltip-info" id="'+newId+'">'+text+'</div>');
        $('#'+newId).css('position', 'absolute');
        $('#'+newId).css('z-index', '15');

        // Show the tooltip text.
        Eaglestar.showToolTip(id, newId);
    });

    $('.tooltip').bind('mouseout', function() {
        var id = '#'+$(this).attr('id')+'_tooltip';
        var text = $(id).html();
        $(id).hide().remove();
        $(this).attr('title', text);
    });
};

/**
 * Position a tooltip textbox according to it's 'parent' location.
 * @param parentId: id of the element who's location we want.
 * @param thisId: id of the tooltip text element.
 * @param justifyRight: if specified, align to the right of the parent.
 */
Eaglestar.prototype.showToolTip = function(parentId, thisId, justifyRight) {
    var offset = $('#'+parentId).offset();
    if (justifyRight) {
        $('#'+thisId).css('left', offset.left - $('#'+thisId).width());
    }
    else {
        $('#'+thisId).css('left', offset.left);
    }
    $('#'+thisId).css('top', offset.top + $('#'+parentId).height());
};

/**
 * Initialize a pattern match condition for an input.
 * @param opts.id: the id of the input element.
 * @param opts.pattern: the regex defining valid inputs on id.
 * @param opts.message: the message to display if the input is not valid.
 * @param opts.messageId: the id of the element which displays the message.
 */
Eaglestar.prototype.initPatternMatch = function(opts) {
    Eaglestar["pattern_string_"+opts.id] = opts.pattern;
    Eaglestar["pattern_message_"+opts.id] = opts.message;
    Eaglestar["pattern_put_"+opts.id] = opts.messageId;

    $('#'+opts.id).each(Eaglestar.patternMatch);
    $('#'+opts.id).keypress(Eaglestar.patternMatch);
    $('#'+opts.id).keyup(Eaglestar.patternMatch);
    $('#'+opts.id).change(Eaglestar.patternMatch);
};

/**
 * Match the element's current value against a predefined regular expression.
 */
Eaglestar.prototype.patternMatch = function() {
    var regex = new RegExp(Eaglestar["pattern_string_"+$(this).attr('id')]);
    var string = $(this).val();
    var match = string.match(regex);
    
    if (!match) {
        $('#'+Eaglestar["pattern_put_"+$(this).attr('id')]).empty();
        $('#'+Eaglestar["pattern_put_"+$(this).attr('id')]).append(Eaglestar["pattern_message_"+$(this).attr('id')]);
        $(this).css('background-color', 'red');
    }
    else {
        $(this).css('background-color', '');
        $('#'+Eaglestar["pattern_put_"+$(this).attr('id')]).empty();
    }
};

/**
 * Initialize a max-char length descriptor for an input.
 * @param opts.id: the id of the input element.
 * @param opts.length: the maximum number of chars.
 * @param opts.messageId: the id of hte elment which displays the message.
 */
Eaglestar.prototype.initMaxLength = function(opts) {
    Eaglestar["length_put_"+opts.id] = opts.messageId;
    Eaglestar["length_max_"+opts.id] = opts.length;
    Eaglestar["length_type_"+opts.id] = opts.type;

    if (opts.type == 'word') {
        $('#'+Eaglestar["length_put_"+opts.id]).append(opts.length+" words remaining");
    }
    else {
        $('#'+Eaglestar["length_put_"+opts.id]).append(opts.length+" characters remaining");
    }

    $('#'+opts.id).each(Eaglestar.maxLength);
    $('#'+opts.id).keypress(Eaglestar.maxLength);
    $('#'+opts.id).keyup(Eaglestar.maxLength);
    $('#'+opts.id).change(Eaglestar.maxLength);
};

/**
 * Check the current value of the input field against the maximum input size.
 * Update the remaining number of characters/words as the user types.
 * If too many inputs are provided, make the background go red and show
 * a message illustrating why.
 */
Eaglestar.prototype.maxLength = function() {
    var rem = 0;

    // Handle the case where we've specified a max number of words.
    if (Eaglestar["length_type_"+$(this).attr('id')] == 'word') {
        var string = $(this).val();
        var words = string.split(/\s+/);
        rem = Eaglestar["length_max_"+$(this).attr('id')] - words.length;
        $('#'+Eaglestar["length_put_"+$(this).attr('id')]).empty();

        // Show a message tailored to the number of remaining words.
        if (rem < 0) {
            $(this).css('background-color', 'red');
            $('#'+Eaglestar["length_put_"+$(this).attr('id')]).append("Too many words!");
        }
        else if (rem == 1) {
            $(this).css('background-color', '');
            $('#'+Eaglestar["length_put_"+$(this).attr('id')]).append(rem+" word remaining");
        }
        else {
            $(this).css('background-color', '');
            $('#'+Eaglestar["length_put_"+$(this).attr('id')]).append(rem+" words remaining");
        }
    }

    // Handle the case where we've specified a max number of characters.
    else {
        rem = Eaglestar["length_max_"+$(this).attr('id')] - $(this).val().length;
        $('#'+Eaglestar["length_put_"+$(this).attr('id')]).empty();

        // Show a message tailored to the number of remaining characters.
        if (rem < 0) {
            $(this).css('background-color', 'red');
            $('#'+Eaglestar["length_put_"+$(this).attr('id')]).append("Too many characters!");
        }
        else if (rem == 1) {
            $(this).css('background-color', '');
            $('#'+Eaglestar["length_put_"+$(this).attr('id')]).append(rem+" character remaining");
        }
        else {
            $(this).css('background-color', '');
            $('#'+Eaglestar["length_put_"+$(this).attr('id')]).append(rem+" characters remaining");
        }
    }
};

// Create the instance of our object.
var Eaglestar = new Eaglestar();
