YAHOO.namespace( "omh.clipboard" );
var Dom = YAHOO.util.Dom;
var clip = YAHOO.omh.clipboard;

clip.onLinkClick = function( event )
{
    inElement = event.target
    if ( inElement.createTextRange ) 
    {
        var range = inElement.createTextRange();
        if (range && BodyLoaded==1)
          range.execCommand('Copy');
    } 
    else 
    {
        var flashcopier = 'flashcopier';

        if( !document.getElementById( flashcopier ) ) 
        {
            var divholder = document.createElement('div');
            divholder.id = flashcopier;
            document.body.appendChild( divholder );
        }

        document.getElementById( flashcopier ).innerHTML = '';
        var divinfo = '<embed src="/site_media/flash/clipboard.swf" FlashVars="clipboard='+encodeURIComponent(inElement.href)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';

        document.getElementById(flashcopier).innerHTML = divinfo;
    }
    
    inElement.firstChild.nodeValue = "URL has been copied";
    // inElement.href = "#";
    
    YAHOO.util.Event.preventDefault( event );
}

clip.onUrlKeyup = function( event )
{
    newText = event.target.value;
    validText = "URL appears to be valid";
    invalidText = "URL does not seem to be valid...";
    url = "/microurl/validate_url/";
    clip.validate( clip.urlNote, newText, clip.currentUrlValue, 
                   clip.urlNoteText, validText, invalidText, url);

    newText = clip.currentUrlValue;    
}

clip.onAliasKeyup = function( event )
{        
    newText = event.target.value;
    validText = "Alias is available";
    invalidText = "Alias is already taken";
    url = "/microurl/validate_alias/";
    clip.validate( clip.aliasNote, newText, clip.currentAliasValue, 
                   clip.aliasNoteText, validText, invalidText, url);

    newText = clip.currentAliasValue;
}

clip.validate = function( node, newText, currentText, initText, validText, invalidText, url )
{
    if ( newText == "" )
    {
        Dom.removeClass(node, "invalid" );
        Dom.removeClass(node, "valid" );
        node.textContent = initText;
        return;
    }

    if ( newText == currentText )
    {        
        return;
    }

    var callback = 
    {
        success: function( o )
        {
            if ( o.responseText == "OK" )
            {
                Dom.removeClass( node, "invalid" );
                Dom.addClass( node, "valid" );
                node.textContent = validText;
                
            }
            else
            {
                Dom.removeClass( node, "valid" );
                Dom.addClass( node, "invalid" );
                node.textContent = invalidText;
            }   
        },
    };

    YAHOO.util.Connect.asyncRequest( 'POST', url, callback, "data=" + newText );
}

clip.init = function()
{
    copyElement = Dom.get( 'copy' );
    YAHOO.util.Event.addListener( copyElement, "click", clip.onLinkClick );
    
    urlInput = Dom.get( 'id_url' );
    YAHOO.util.Event.addListener( urlInput, "keyup", clip.onUrlKeyup );

    aliasInput = Dom.get( 'id_alias' );
    YAHOO.util.Event.addListener( aliasInput, "keyup", clip.onAliasKeyup );
    
    clip.aliasNote = Dom.get( 'alias_note' );
    clip.aliasNoteText = clip.aliasNote.textContent;
    clip.urlNote = Dom.get( 'url_note' );
    clip.urlNoteText = clip.urlNote.textContent;
    
    urlInput.focus();
}

YAHOO.util.Event.onDOMReady( clip.init );