// ==UserScript==
// @name                Fast tagger
// @version             1.3
// @date                2009-07-02
// @author              Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace           etsy.com
// @description         Allows you to enter tags separated by commas, and click a button to set them on the page.
// @include             http://www.etsy.com/create_listing2.php?*
// @include             http://www.etsy.com/edit_listing2.php?*
// ==/UserScript==

var tagLand = document.getElementById( 'tagLand' );

var div = document.createElement( 'div' );
//div.style.fontSize = '11px';
div.style.padding = '5px';
div.style.border = '1px solid #cccccc';
div.style.width = '500px';
div.style.marginBottom = '10px';
div.innerHTML = 'Fast tags:<br /><textarea style="margin-top: 5px; border: 1px solid #000000; width: 494px"></textarea><br /><div id="fast_tagger_errors" style="padding-top: 4px; display: none; color: #ff0000"></div><div style="padding-top: 4px"><a style="cursor: pointer">delete tags</a> &bull; <a style="cursor: pointer">add to tags</a> &bull; <a style="cursor: pointer">replace tags</a></div>';
tagLand.parentNode.style.paddingTop = '0px';
var tagSrc = div.getElementsByTagName( 'textarea' )[ 0 ];
var tagInput = document.getElementById( 'tagInput' );
var links = div.getElementsByTagName( 'a' );
links[ 0 ].addEventListener( 'click', function () { execAction( 'delete' ) }, false );
links[ 1 ].addEventListener( 'click', function () { execAction( 'add' ) }, false );
links[ 2 ].addEventListener( 'click', function () { execAction( 'replace' ) }, false );
tagLand.insertBefore( div, tagLand.firstChild );
var errorDiv = document.getElementById( 'fast_tagger_errors' );
tagSrc.focus();

function execAction ( type ) {
    errorDiv.style.display = 'none';
    if ( type == 'replace' ) {
        var array = validateTags( true );
        if ( array.length ) {
            deleteTags();
            addTags( array, true );
        }
    } else if ( type == 'delete' ) {
        deleteTags();
    } else if ( type == 'add' ) {
        var array = validateTags();
        if ( array.length ) {
            addTags( array );
        }
    }
}

function validateTags ( assumeSetFirst ) {
    var tagErrors = [];
    var firstTagOK = true;
    var select = tagLand.getElementsByTagName( 'select' )[ 0 ];
    if ( tagSrc.value == "" ) return true;
    var tagStr = tagSrc.value.replace( /</g, '&lt;' );
    var array = tagStr.split( /,\s*/ );
    var firstTagStr;
    if( assumeSetFirst || select.selectedIndex < 1 ) {
        firstTagOK = false;
        var firstTag = array.shift();
        var first = firstTag.replace( / /g, '_' );
        for( o = 0; o < select.options.length; o++ ) {
            if( select.options[ o ].value == first ) {
                firstTagOK = true;
                break;
            }
        }
    }
    if ( ! firstTagOK ) {
        tagErrors.push( 'First tag "' + firstTag + '" not a valid category' );
    } else {
        for ( var t = array.length - 1; t >= 0; t-- ) {
            var tag = array[ t ].toLowerCase();
            tag = tag.replace( / /g, "_" );
            var idx = unsafeWindow.getArrayIndex( unsafeWindow.allTags, tag );
            if ( ! assumeSetFirst && idx != null ) {
                tagErrors.push( 'Skipped duplicate tag "' + tag + '"' );
                array.splice( t, 1 );
                continue;
            }
            var myRegxp = /^[0-9a-zA-Z\s_]*$/;
            if( myRegxp.test( tag ) == false ) {
                tagErrors.push( 'Skipped tag "' + tag + '" - you may only use letters and numbers in your tags' );
                array.splice( t, 1 );
                continue;
            }
        }
    }
    if ( tagErrors.length > 0 ) {
        errorDiv.innerHTML = tagErrors.join( '<br />' );
        errorDiv.style.display = '';
    }
    if ( ! firstTagOK ) {
        array = [];
    } else if ( firstTag != null ) {
        array.unshift( firstTag );
    }
    return array;
}

function deleteTags () {
    // remove existing free-form tags
    while( unsafeWindow.allTagPointers.length > 1 ) {
        unsafeWindow.removeTag( unsafeWindow.allTagPointers[ 1 ] );
    }
    // remove first tag
    tagLand.getElementsByTagName( 'select' )[ 0 ].selectedIndex = 0;
    unsafeWindow.mainCategorySelector( '- - -' );
    // hide some of the extra bits and pieces we don't want
    var id = document.getElementById( 'inputDiv' );
    if ( id ) id.style.visibility = 'hidden';
    var mcr = document.getElementById( 'mainCategoryReccomendations' );
    if ( mcr ) mcr.style.display = 'none'
}

function addTags ( array, noValidate ) {
    var select = tagLand.getElementsByTagName( 'select' )[ 0 ];
    if( select.selectedIndex < 1 ) {
        // we don't have a first tag selected on the page
        // deal with it differently
        var firstTag = array.shift();
        firstTag = firstTag.replace( / /g, '_' );
        for( o = 0; o < select.options.length; o++ ) {
            if( select.options[ o ].value == firstTag ) {
                // found it in the main categories list
                select.selectedIndex = o;
                unsafeWindow.mainCategorySelector( firstTag );
                break;
            }
        }
    }
    // loop through the tags, adding them
    for( t = 0; t < array.length; t++ ) {
        unsafeWindow.addTag( array[ t ] );
    }
}




