<?php
/*
Plugin Name: Cat2Tag v 2.0
Plugin URI: http://agkamai.com/cat2tag
Description: Creating categories has never been so easy. Packed with <a href="http://moofx.mad4milk.net/">moo.fx</a> and <a href="http://prototype.conio.net/">Prototype</a>.
Author: <a href="http://agkamai.org">Edmundo Hidalgo</a> & <a href="http://jrm.cc">Jeff Minard</a>
Version: 2.0
Author URI:
*/

/* Instructions:

Upload this script to your plugins folder which in this case is /path/to/wordpress/wp-content/plugins most of the time, then activate it in your Plugins Panel, and voilá, you're ready to go.

There are some variables to personalize this script which are:

$c2t_show_categories_list <- If you want to show up or not the categories list.
$c2t_suggest_tags <- Turns on or off the suggestions feature
$c2t_smallest <- Smallest font unit
$c2t_largest <- Largest font unti
$c2t_unit <- The, uh, font unit (pt, em, px, etc)

You're free to edit this plugin, but remember to give credits.

*/

// EDIT FROM HERE SAFELY

// Do you want to show the categories list? Use true or false
$c2t_show_categories_list false;

// Do you want suggested tags on? Use true or false
$c2t_suggest_tags true;

// Edit the smallest and largest font, and what units to measure in
$c2t_smallest .7;
$c2t_largest 2;
$c2t_unit 'em';
    

// EDIT FROM HERE UNDER YOUR OWN RISK

// General Functions
function c2t_get_name_cats() {
    global 
$post_ID$wpdb;
    
    
$cat_result '';

    if(
$post_ID) {
        
$cat_list $wpdb->get_results("
        SELECT cat_name
        FROM $wpdb->categories,$wpdb->post2cat
        WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID'
        "
);
        
        if(
$cat_list) {
            foreach (
$cat_list as $cats_list) {
                
$cat_result .= ('' == $cat_result) ? $cats_list->cat_name ', '$cats_list->cat_name;
            }
        }
    }
    return 
$cat_result;    
}

// Lets "add the tags" to the post
function c2t_add_tags_to_post($id) {
    if(
$_POST['c2t_tags']) {
        global 
$wpdb;
        
// First we need to catch the text field
        
$field_tags trim($_POST['c2t_tags']);
        
$tags explode(",",$field_tags);
        
$post_tags = array(); // Just to make sure we made a list
        
        
if ($tags) {
            foreach(
$tags as $tag) {
                
$tag trim($tag);
                if(
"" != $tag) {
                    
$tag_name sanitize_title($tag,0);
                    
$exists $wpdb->get_row("
                    SELECT * FROM $wpdb->categories 
                    WHERE category_nicename = '$tag_name'
                    "
);
                    
                    
// If exists then just add it to the list, if not, then add the new tag and then add it to the list
                    
if($exists) {
                        if(!
in_array($exists->cat_ID$post_tags)) {
                            
array_push($post_tags,$exists->cat_ID);
                        }
                    } else {
                        
$id_result $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->categories'");
                        
array_push($post_tags,$id_result->Auto_increment);
                        
$wpdb->query("
                        INSERT INTO $wpdb->categories 
                        (cat_ID, cat_name, category_nicename, 
                        category_description, category_parent) 
                        VALUES ('0', '$tag', '$tag_name', '', '0'); "
);
                    }
                }
            }
        }
        
        if (
$post_tags) {
            
// Delete tags from the post
            
$old_tags $wpdb->get_col("SELECT category_id FROM $wpdb->post2cat WHERE post_id = $id");
        
            if(
$old_tags) {
                foreach (
$old_tags as $old_tag) {
                    if (!
in_array($old_tag$post_tags)) { 
                        
$wpdb->query("
                        DELETE FROM $wpdb->post2cat 
                        WHERE category_id = $old_tag 
                        AND post_id = $id LIMIT 1; 
                        "
);
                    }
                }
            }
        
            
// Add tags to the post
            
foreach ($post_tags as $new_tag) {
                if (!
in_array($new_tag$old_tags)) { 
                    
$wpdb->query("
                    INSERT INTO $wpdb->post2cat (post_id, category_id) 
                    VALUES ($id, $new_tag); 
                    "
);
                }
            }
        }
    }
    
// All done!
}

// Administrator forms and visuals

function c2t_show_all_tags() {
    global 
$wpdb$c2t_smallest$c2t_largest$c2t_unit;
    
    echo 
'<div id="allTags"><h3>All Categories</h3>';
    
    
$cat_counts $wpdb->get_results("    
        SELECT cat_name,
        COUNT($wpdb->post2cat.post_id) AS cat_count
        FROM $wpdb->categories 
        LEFT JOIN $wpdb->post2cat ON (cat_ID = category_id)
        LEFT JOIN $wpdb->posts ON (ID = post_id)
        GROUP BY category_id
        ORDER BY cat_count
        "
);
        
    
$categories $wpdb->get_results($query);
    
    if(
$categories) {
        foreach(
$categories as $cat) {
            
$cats[$cat->cat_name] = $cat->cat_count;
        }
        
        
ksort($cats);
        
reset($cats);
        
        
$spread max($cats) - min($cats); 
        if (
$spread <= 0$spread 1
        
$fontspread $c2t_largest $c2t_smallest;
        
$fontstep   $spread $fontspread;
        if (
$fontspread <= 0$fontspread 1
        
        foreach(
$cats as $catname => $count) {
            echo 
"<a href=\"javascript:c2t_set_tag('"addslashes($catname) ."')\" title=\"Add the '$catname' category to the field.\" style=\"font-size: ".($c2t_smallest + ($count/$fontstep))."$c2t_unit;\">$catname</a> \n";
        }
    } else {
        echo 
'Ah, shoot! <strong>You need to add some categories before you start using this feature.</strong> Write some in the categories field.';
    }
    
    echo 
"\n</div>\n";
}

function 
c2t_get_catlist() {
    global 
$wpdb;
    
$cats $wpdb->get_results("
        SELECT cat_name
        FROM $wpdb->categories
        ORDER BY cat_name ASC
        "
);
    
    if(
$cats) {
        
$first false;
        foreach(
$cats as $cat) {
            if(
$first) {
                echo 
",'"addslashes(addslashes($cat->cat_name)) ."'";
            } else {
                echo 
"'"addslashes(addslashes($cat->cat_name)) ."'";
                
$first true;
            }
        }
    }
}

// We do need an input text field for the tags
function c2t_add_tags_input() {
    global 
$postdata;
    
$tags trim(c2t_get_name_cats($postdata->ID));
?>    
    <div id="the-tags">
    
    <fieldset class="dbx-box">
        <h3 class="dbx-handle" >Categories</h3>
        <div class="dbx-content">
        <input type="text" name="c2t_tags" id="c2t_tags" value="<?php echo $tags?>" tabindex="10" autocomplete="off" /><br>
        <div id="c2t_suggest"> </div>
        <small>Separate categories with commas, it's alright 
        you can use spaces. <a href="javascript:void(0)" id="c2t_clicky">See the available categories</a>.</small>
        <div id="c2t_tags">
            <?php c2t_show_all_tags(); ?>
        </div>
        </div>
    </fieldset>
    
    </div>
<?php
}

function 
c2t_header_stuff() {
    global 
$c2t_show_categories_list$wpdb;
    
?>
    <script type="text/javascript">
        var taglist = new Array(<?php c2t_get_catlist(); ?>);
        <?php c2t_js_file() ?>
    </script>
    <style type="text/css">
        <?php c2t_css_file() ?>        
    </style>
    <?php
}

function 
c2t_css_file() {
?>
#c2t_tags {
    padding: 5px;
}
    
#allTags h3  { 
    margin: 5px 0 0 0; 
    margin-bottom: 5px; 
    padding: 0; 
    font-size: 0.9em; 
}
    
#allTags a  { 
    margin-left: 5px; 
    font-size: 1.35em; 
    text-decoration: none; 
    border: none; 
}

#c2t_suggest { padding: 5px; }

#the-tags .dbx-box { margin: 0 0 10px 0; }

#the-tags .dbx-handle {
    font-weight: normal;
    font-size: 1em; 
    margin: 0 0 10px 0; 
    padding: 3px;
}

#the-tags #c2t_tags { width: 99%; }

#the-tags small {
    font-size: 0.85em; 
    margin: 5px 0 0 0; 
    display: block;
}
<?php
}

function 
c2t_js_file() {
global 
$c2t_show_categories_list$c2t_suggest_tags;
?>
    /*  Prototype JavaScript framework
     *  (c) 2005 Sam Stephenson <sam@conio.net>
     *
     *  Prototype is freely distributable under the terms of an MIT-style license.
     *
     *  For details, see the Prototype web site: http://prototype.conio.net/
     *
    /*--------------------------------------------------------------------------*/
    
    
    //note: this is a stripped down version of prototype, to be used with moo.fx by mad4milk (http://moofx.mad4milk.net).
    
    var Class = {
      create: function() {
        return function() { 
          this.initialize.apply(this, arguments);
        }
      }
    }
    
    Object.extend = function(destination, source) {
      for (property in source) {
        destination[property] = source[property];
      }
      return destination;
    }
    
    Function.prototype.bind = function(object) {
      var __method = this;
      return function() {
        return __method.apply(object, arguments);
      }
    }
    
    function $() {
      var elements = new Array();
    
      for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string')
          element = document.getElementById(element);
    
        if (arguments.length == 1) 
          return element;
    
        elements.push(element);
      }
    
      return elements;
    }
    
    //-------------------------
    
    document.getElementsByClassName = function(className) {
      var children = document.getElementsByTagName('*') || document.all;
      var elements = new Array();
      
      for (var i = 0; i < children.length; i++) {
        var child = children[i];
        var classNames = child.className.split(' ');
        for (var j = 0; j < classNames.length; j++) {
          if (classNames[j] == className) {
            elements.push(child);
            break;
          }
        }
      }
      
      return elements;
    }
    
    //-------------------------
    
    if (!window.Element) {
      var Element = new Object();
    }
    
    Object.extend(Element, {
      remove: function(element) {
        element = $(element);
        element.parentNode.removeChild(element);
      },
    
      hasClassName: function(element, className) {
        element = $(element);
        if (!element)
          return;
        var a = element.className.split(' ');
        for (var i = 0; i < a.length; i++) {
          if (a[i] == className)
            return true;
        }
        return false;
      },
    
      addClassName: function(element, className) {
        element = $(element);
        Element.removeClassName(element, className);
        element.className += ' ' + className;
      },
      
      removeClassName: function(element, className) {
        element = $(element);
        if (!element)
          return;
        var newClassName = '';
        var a = element.className.split(' ');
        for (var i = 0; i < a.length; i++) {
          if (a[i] != className) {
            if (i > 0)
              newClassName += ' ';
            newClassName += a[i];
          }
        }
        element.className = newClassName;
      },
      
      // removes whitespace-only text node children
      cleanWhitespace: function(element) {
        element = $(element);
        for (var i = 0; i < element.childNodes.length; i++) {
          var node = element.childNodes[i];
          if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) 
            Element.remove(node);
        }
      }
    });
    /*
    moo.fx, simple effects library built with prototype.js (http://prototype.conio.net).
    by Valerio Proietti (http://mad4milk.net) MIT-style LICENSE.
    for more info (http://moofx.mad4milk.net).
    10/24/2005
    v(1.0.2)
    */
    
    //base
    var fx = new Object();
    fx.Base = function(){};
    fx.Base.prototype = {
        setOptions: function(options) {
        this.options = {
            duration: 500,
            onComplete: ''
        }
        Object.extend(this.options, options || {});
        },
    
        go: function() {
            this.duration = this.options.duration;
            this.startTime = (new Date).getTime();
            this.timer = setInterval (this.step.bind(this), 13);
        },
    
        step: function() {
            var time  = (new Date).getTime();
            var Tpos   = (time - this.startTime) / (this.duration);
            if (time >= this.duration+this.startTime) {
                this.now = this.to;
                clearInterval (this.timer);
                this.timer = null;
                if (this.options.onComplete) setTimeout(this.options.onComplete.bind(this), 10);
            }
            else {
                this.now = ((-Math.cos(Tpos*Math.PI)/2) + 0.5) * (this.to-this.from) + this.from;
                //this time-position, sinoidal transition thing is from script.aculo.us
            }
            this.increase();
        },
    
        custom: function(from, to) {
            if (this.timer != null) return;
            this.from = from;
            this.to = to;
            this.go();
        },
    
        hide: function() {
            this.now = 0;
            this.increase();
        },
    
        clearTimer: function() {
            clearInterval(this.timer);
            this.timer = null;
        }
    }
    
    //stretchers
    fx.Layout = Class.create();
    fx.Layout.prototype = Object.extend(new fx.Base(), {
        initialize: function(el, options) {
            this.el = $(el);
            this.el.style.overflow = "hidden";
            this.el.iniWidth = this.el.offsetWidth;
            this.el.iniHeight = this.el.offsetHeight;
            this.setOptions(options);
        }
    });
    
    fx.Height = Class.create();
    Object.extend(Object.extend(fx.Height.prototype, fx.Layout.prototype), {    
        increase: function() {
            this.el.style.height = this.now + "px";
        },
    
        toggle: function() {
            if (this.el.offsetHeight > 0) this.custom(this.el.offsetHeight, 0);
            else this.custom(0, this.el.scrollHeight);
        }
    });
    
    fx.Width = Class.create();
    Object.extend(Object.extend(fx.Width.prototype, fx.Layout.prototype), {    
        increase: function() {
            this.el.style.width = this.now + "px";
        },
    
        toggle: function(){
            if (this.el.offsetWidth > 0) this.custom(this.el.offsetWidth, 0);
            else this.custom(0, this.el.iniWidth);
        }
    });
    
    //fader
    fx.Opacity = Class.create();
    fx.Opacity.prototype = Object.extend(new fx.Base(), {
        initialize: function(el, options) {
            this.el = $(el);
            this.now = 1;
            this.increase();
            this.setOptions(options);
        },
    
        increase: function() {
            if (this.now == 1) this.now = 0.9999;
            if (this.now > 0 && this.el.style.visibility == "hidden") this.el.style.visibility = "visible";
            if (this.now == 0) this.el.style.visibility = "hidden";
            if (window.ActiveXObject) this.el.style.filter = "alpha(opacity=" + this.now*100 + ")";
            this.el.style.opacity = this.now;
        },
    
        toggle: function() {
            if (this.now > 0) this.custom(1, 0);
            else this.custom(0, 1);
        }
    });
    
    /*
    moo.fx pack, effects extensions for moo.fx.
    by Valerio Proietti (http://mad4milk.net) MIT-style LICENSE
    for more info visit (http://moofx.mad4milk.net).
    Wednesday, November 16, 2005
    v1.0.4
    */
    
    //text size modify, now works with pixels too.
    fx.Text = Class.create();
    fx.Text.prototype = Object.extend(new fx.Base(), {
        initialize: function(el, options) {
            this.el = $(el);
            this.setOptions(options);
            if (!this.options.unit) this.options.unit = "em";
        },
    
        increase: function() {
            this.el.style.fontSize = this.now + this.options.unit;
        }
    });
    
    //composition effect, calls Width and Height alltogheter
    fx.Resize = Class.create();
    fx.Resize.prototype = {
        initialize: function(el, options) {
            this.h = new fx.Height(el, options); 
            if (options) options.onComplete = null;
            this.w = new fx.Width(el, options);
            this.el = $(el);
        },
    
        toggle: function(){
            this.h.toggle();
            this.w.toggle();
        },
    
        modify: function(hto, wto) {
            this.h.custom(this.el.offsetHeight, this.el.offsetHeight + hto);
            this.w.custom(this.el.offsetWidth, this.el.offsetWidth + wto);
        },
    
        custom: function(hto, wto) {
            this.h.custom(this.el.offsetHeight, hto);
            this.w.custom(this.el.offsetWidth, wto);
        },
    
        hide: function(){
            this.h.hide();
            this.w.hide();
        }
    }
    
    //composition effect, calls Opacity and (Width and/or Height) alltogheter
    fx.FadeSize = Class.create();
    fx.FadeSize.prototype = {
        initialize: function(el, options) {
            this.el = $(el);
            this.el.o = new fx.Opacity(el, options);
            if (options) options.onComplete = null;
            this.el.h = new fx.Height(el, options);
            this.el.w = new fx.Width(el, options);
        },
    
        toggle: function() {
            this.el.o.toggle();
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] == 'height') this.el.h.toggle();
                if (arguments[i] == 'width') this.el.w.toggle();
            }
        },
    
        hide: function(){
            this.el.o.hide();
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] == 'height') this.el.h.hide();
                if (arguments[i] == 'width') this.el.w.hide();
            }
        }
    }
    
    //intended to work with arrays.
    var Multi = new Object();
    Multi = function(){};
    Multi.prototype = {
        initialize: function(elements, options){
            this.options = options;
            this.el = this.getElementsFromArray(elements);
            for (i=0;i<this.el.length;i++){
                this.effect(this.el[i]);
            }
        },
    
        getElementsFromArray: function(array) {
            var elements = new Array();
            for (i=0;i<array.length;i++) { 
                elements.push($(array[i])); 
            }
            return elements;
        }
    }
    
    //Fadesize with arrays
    fx.MultiFadeSize = Class.create();
    fx.MultiFadeSize.prototype = Object.extend(new Multi(), {
        effect: function(el){
            el.fs = new fx.FadeSize(el, this.options);
        },
    
        showThisHideOpen: function(el, delay, mode){
            for (i=0;i<this.el.length;i++){
                if (this.el[i].offsetHeight > 0 && this.el[i] != el && this.el[i].h.timer == null && el.h.timer == null){
                    this.el[i].fs.toggle(mode);
                    setTimeout(function(){el.fs.toggle(mode);}.bind(el), delay);
                }
                
            }
        },
    
        hide: function(el, mode){
            el.fs.hide(mode);
        }
    });
    
    var Remember = new Object();
    Remember = function(){};
    Remember.prototype = {
        initialize: function(el, options){
            this.el = $(el);
            this.days = 365;
            this.options = options;
            this.effect();
            var cookie = this.readCookie();
            if (cookie) {
                this.fx.now = cookie;
                this.fx.increase();
            }
        },
    
        //cookie functions based on code by Peter-Paul Koch
        setCookie: function(value) {
            var date = new Date();
            date.setTime(date.getTime()+(this.days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
            document.cookie = this.el+this.el.id+this.prefix+"="+value+expires+"; path=/";
        },
    
        readCookie: function() {
            var nameEQ = this.el+this.el.id+this.prefix + "=";
            var ca = document.cookie.split(';');
            for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
            }
            return false;
        },
    
        custom: function(from, to){
            if (this.fx.now != to) {
                this.setCookie(to);
                this.fx.custom(from, to);
            }
        }
    }
    
    fx.RememberHeight = Class.create();
    fx.RememberHeight.prototype = Object.extend(new Remember(), {

        effect: function(){
            this.fx = new fx.Height(this.el, this.options);
            this.prefix = 'height';
        },
        
        toggle: function(){
            if (this.el.offsetHeight == 0) this.setCookie(this.el.scrollHeight);
            else this.setCookie(0);
            this.fx.toggle();
        },
        
        resize: function(to){
            this.setCookie(this.el.offsetHeight+to);
            this.fx.custom(this.el.offsetHeight,this.el.offsetHeight+to);
        },
    
        hide: function(){
            if (!this.readCookie()) {
                this.fx.hide();
            }
        }
    });
    
    fx.RememberText = Class.create();
    fx.RememberText.prototype = Object.extend(new Remember(), {
        effect: function(){
            this.fx = new fx.Text(this.el, this.options);
            this.prefix = 'text';
        }
    });
    
    
    //use to attach effects without using js code, just classnames and rel attributes.
    ParseClassNames = Class.create();
    ParseClassNames.prototype = {
        initialize: function(options){
            var babies = document.getElementsByTagName('*') || document.all;
            for (var i = 0; i < babies.length; i++) {
                var el = babies[i];
                //attach the effect, from the classNames;
                var effects = this.getEffects(el);
                for (var j = 0; j < effects.length; j++) {
                    if (j == 1 && options) options.onComplete = null;
                    el[effects[j]+"fx"] = new fx[effects[j]](el, options);
                }
                //execute methods, from rel
                if (el.rel) {
                    el.crel = el.rel.split(' ');
                    if (el.crel[0].indexOf("fx_") > -1) {
                        var event = el.crel[0].replace('fx_', '');
                        var tocompute = this.getEffects($(el.crel[1]));
                        el["on"+event] = function(){
                            for (var f = 0; f < tocompute.length; f++) {
                                $(this.crel[1])[tocompute[f]+"fx"][this.crel[2] || "toggle"](this.crel[3] || null, this.crel[4] || null);
                            }
                        }
                    }
                }
            }
        },
    
        getEffects: function(el){
            var effects = new Array();
            var css = el.className.split(' ');
            for (var i = 0; i < css.length; i++) {
                if (css[i].indexOf('fx_') > -1) {
                    var effect = css[i].replace('fx_', '');
                    effects.push(effect);
                }
            }
            return effects;
        }
    }
    
    // There's no such thing as Trim in JS
    function trim(value) {
       var temp = value, obj = /^(\s*)([\W\w]*)(\b\s*$)/;
       if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
       var obj = /  /g;
       while (temp.match(obj)) { temp = temp.replace(obj, " "); }
       return temp;
    }
    
    // Escaping chars
    function escapeString(s) {
        var ts = s, re = new RegExp("\\\\'","gi");
        if(re.test(ts)) ts = ts.replace(re,"'");
        return ts;
    }
    
    // Tag placing on the text field
    function c2t_set_tag(tag) {
        var tags=document.forms[0].c2t_tags.value, tagArray=tags.split(', '), hasit=false, n;
        
        if (trim(tagArray[0]) == '') tagArray.splice(0,1);
        
        for(n=0;n<tagArray.length; n++) {
            tagArray[n] = trim(tagArray[n]);
            if(tagArray[n] == tag) { tagArray.splice(n,1); hasit=true; n-=1; }
        }
        
        if(!hasit) { tagArray.push(tag) }
        
        var inputVal = tagArray.join(', ');
        //window.alert(inputVal);
        finalVal = document.forms[0].c2t_tags.value = inputVal;
        document.forms[0].c2t_tags.focus();
    }
    
    //Tags recommendation very like del.icio.us
    function c2t_suggest(rec) {
        var inputArray=rec.split(', '), recCont='', d=document.getElementById('c2t_suggest'), i, j=0, lastItem='', result=new Array();
        
        if(inputArray[0] == '') inputArray.splice(0,1);
        //if(inputArray[inputArray.length-1] == '') inputArray.splice(inputArray.length-1,1);
        if(inputArray.length > 0) { 
            lastItem = trim(inputArray[inputArray.length-1]);
        }
        
        if(lastItem == '') {
            recCont = '';
        } else {
            var re = new RegExp("^"+lastItem,"i");
            
            for(i=0;i<taglist.length;i++) {
                if(re.test(taglist[i])) {
                    recCont += ' <a href="javascript:c2t_replace(\''+taglist[i]+'\',\''+lastItem+'\')">'+escapeString(taglist[i])+'</a>';
                }
            }
        }
        
        if(recCont == '') { 
            d.innerHTML = '';
            d.style.display = 'none';
        } else {
            recCont = '<strong>Suggestions: </strong>'+recCont;
            d.innerHTML = recCont;
            d.style.display = 'block';
        }
    }
    
    function c2t_replace(tag,itm) {
        var tags=document.forms[0].c2t_tags.value, tagArray=tags.split(', '), hasit=false, n;
        
        if (tagArray[0] == '') tagArray.splice(0,1);
        if(tagArray[tagArray.length-1] == '') tagArray.splice(tagArray.length-1,1);
        
        if(tagArray.length > 0) { 
            if (itm == tagArray[tagArray.length-1]) tagArray[tagArray.length-1] = tag;
        }
            
        var inputVal = tagArray.join(', ');
        finalVal = document.forms[0].c2t_tags.value = inputVal;
        document.forms[0].c2t_tags.focus();    
    }
    
    // Lets take advantage of the built-in JS functions of Wordpress
    addLoadEvent( function() {
            try {
            <?php if(!$c2t_show_categories_list) { ?>
            var catdiv = document.getElementById('categorydiv');
            catdiv.style.display = 'none';
            <?php ?>
        
            var c2t_click = document.getElementById('c2t_clicky'), c2t_inputfield = document.forms[0].c2t_tags, c2t_s = document.getElementById('c2t_suggest');
            
            var effect = new fx.FadeSize('allTags',{duration: 300, opacity: 0});
            effect.hide('height');
            
            c2t_s.style.display = 'none';
        
            c2t_click.onclick = function() {
                effect.toggle('height');
            }
            
            <?php if($c2t_suggest_tags) { ?>
            c2t_inputfield.onkeyup = function() {
                c2t_suggest(c2t_inputfield.value);
            }
            <?php ?>
            } catch(e) {}
        }
    );
<?php }


// Hooks to make this plugin work
if(function_exists('add_action')) {
    
add_action('admin_head''c2t_header_stuff');
    
add_action('simple_edit_form''c2t_add_tags_input');
    
add_action('edit_form_advanced''c2t_add_tags_input');
    
add_action('edit_post''c2t_add_tags_to_post');
    
add_action('publish_post''c2t_add_tags_to_post');
    
add_action('save_post''c2t_add_tags_to_post');
} else {
    echo 
"You cannot access this file directly.";
}
?>