// DOM element creator for jQuery and Prototype by Michael Geary
// http://mg.to/topics/programming/javascript/jquery
// Inspired by MochiKit.DOM by Bob Ippolito
// Free beer and free speech. Enjoy!

$.defineTag = function( tag ) {
    $[tag.toUpperCase()] = function() {
        return $._createNode( tag, arguments );
    };
};

(function() {
    var tags = [
        'a', 'br', 'button', 'canvas', 'div', 'fieldset', 'form',
        'h1', 'h2', 'h3', 'hr', 'img', 'input', 'label', 'legend',
        'li', 'ol', 'optgroup', 'option', 'p', 'pre', 'select',
        'span', 'strong', 'table', 'tbody', 'td', 'textarea',
        'tfoot', 'th', 'thead', 'tr', 'tt', 'ul' , 'em'];
    for( var i = tags.length - 1;  i >= 0;  i-- ) {
        $.defineTag( tags[i] );
    }
})();

$.NBSP = '\u00a0';

$._createNode = function( tag, args ) {
    var fix = { 'class':'className', 'Class':'className' };
    var e;
    try {
        var attrs = args[0] || {};
        e = document.createElement( tag );
        $(e).attr(attrs);
        if (attrs.style) $(e).css(attrs.style);
        for( var attr in attrs ) {
            var a = fix[attr] || attr;
            e[a] = attrs[attr];
        }
        for( var i = 1;  i < args.length;  i++ ) {
            var arg = args[i];
            if( arg == null ) continue;
            if( arg.constructor != Array ) append( arg );
            else for( var j = 0;  j < arg.length;  j++ )
                append( arg[j] );
        }
    }
    catch( ex ) {
        console.log( 'Cannot create <' + tag + '> element:\n' +
            args.toSource() + '\n' + args + '\n' + ex);
        e = null;
    }

    function append( arg ) {
        if( arg == null ) return;
        var c = arg.constructor;
        switch( typeof arg ) {
            case 'number': arg = '' + arg;  // fall through
            case 'string': arg = document.createTextNode( arg );
        }
        e.appendChild( arg );
    }

    return e;
};

jQuery.fn.helpBubble = function(){
    this.each(function(){
        // options
        var distance = 10;
        var time = 250;
        var hideDelay = 500;

        var hideDelayTimer = null;

        // tracker
        var beingShown = false;
        var shown = false;

        var trigger = $('.bubbletrigger', this);
        var popup = $('.bubblepopup', this).css('opacity', 0);
        
        var css = {
            top: -50,
            left: -10,
            display: 'block' // brings the popup back in to view            
        };
        
        if(jQuery.fn.metadata){
            //if we can extract extra information
            var options = popup.metadata();
            if(options && options.bubble_top) css.top = options.bubble_top;
            if(options && options.bubble_left) css.left = options.bubble_left;
        }
        
        // set the mouseover and mouseout on both element
        $([trigger.get(0), popup.get(0)]).mouseover(function () {
          // stops the hide event if we move from the trigger to the popup element
          if (hideDelayTimer) clearTimeout(hideDelayTimer);

          // don't trigger the animation again if we're being shown, or already visible
          if (beingShown || shown) {
            return;
          } else {
            beingShown = true;

            // reset position of popup box
            popup.css(css)

            // (we're using chaining on the popup) now animate it's opacity and position
            .animate({
              top: '-=' + distance + 'px',
              opacity: 1
            }, time, 'swing', function() {
              // once the animation is complete, set the tracker variables
              beingShown = false;
              shown = true;              
            });
          }
        }).mouseout(function () {
          // reset the timer if we get fired again - avoids double animations
          if (hideDelayTimer) clearTimeout(hideDelayTimer);
        
          // store the timer so that it can be cleared in the mouseover if required
          hideDelayTimer = setTimeout(function () {
            hideDelayTimer = null;
            popup.animate({
              top: '-=' + distance + 'px',
              opacity: 0
            }, time, 'swing', function () {
              // once the animate is complete, set the tracker variables
              shown = false;
              // hide the popup entirely after the effect (opacity alone doesn't do the job)
              popup.css('display', 'none');
            });
          }, hideDelay);
        });
    });
};


function getMovieName(movieName) {
    if ($.browser.msie) {
        return document[movieName];
    }else{
        return window[movieName];
    }
}
