(function(){    
    
    CmdUtils.CreateCommand({
        name: "shorten",
        homepage: "http://labs.vunite.com/vubiqs/verbs.php",
        author:{
            name: "Liji Jinaraj",
            email: "support@vunite.com"
        },
        contributors: ["Jordan Brough"],
        license: "MIT",
        description: "Create short urls using API's like TinyURL, is.gd, bit.ly, hurl, snipurl",
        help: "shorten [url] using [tinyurl|isgd|bitly|hurl|snipurl]",
        icon: "http://vunite.com/wp-content/themes/vunite/images/favicont.ico",
        arguments: [ {role: 'object', nountype: noun_type_url, label: 'url to shorten'},
                {role: 'instrument', nountype: noun_arb_text, label: 'shortner'} ],

        preview: function(pblock, {object, instrument}){
            var api = this._processUrl(object.text, instrument.text, false);

            if (api) {
                jQuery.get(api.url, api.params, function(shortUrl){                    
                    CmdUtils.copyToClipboard(shortUrl);
                    html = "<input id='vuniteurlfield' type='text' style='font-size: 18px; width: 290px;' value='" + shortUrl + "' onfocus='this.select();'/><p style='line-height: 22px; margin: 0; padding: 0;'>Shortened Url copied to clipboard</p>";
                    pblock.innerHTML = html;
                });
            }
            else 
                pblock.innerHTML = "using [tinyurl | isgd | bitly | hurl | snipurl]";
        },

        execute: function({object, instrument}){
            var api = this._processUrl(object.text, instrument.text, true);

            if (api) {
                jQuery.get(api.url, api.params, function(shortUrl){
                    CmdUtils.copyToClipboard(shortUrl);
                    html = "<div id='vuniteshorten' style='z-index:999; position: fixed; background: #333; top: 0; left: 0; padding: 30px; width: 300px; border: 2px solid #aaa;'><input id='vuniteurlfield' type='text' style='font-size: 18px; width: 290px;' value='" + shortUrl + "'/><p style='color: #eee; line-height: 22px; margin: 0; padding: 0;'>Shortened Url copied to clipboard</p><p><a style='color: #eee; font-size: 12px;' href='' onclick='var el=document.getElementById(\"vuniteshorten\"); el.parentNode.removeChild(el); return false;'>[x]close</a></p></div>";

                    var doc = Application.activeWindow.activeTab.document;
                    doc.getElementsByTagName('body')[0].innerHTML += html;
                    var el = doc.getElementById('vuniteurlfield');
                    el.focus();
                    el.select();
                });
            }
        },

        _processUrl: function(originalUrl, using, useDefault){
        
            // add 'http://' if it's missing in url
            if( !/^http:\/\//.test(originalUrl) )
                originalUrl = 'http://' + originalUrl;
                
            var apis = this._getAPIs(originalUrl);

            if (using) 
                return apis[using];
            else {
                if (useDefault) 
                    return apis['tinyurl'];
                else 
                    return '';
            }
        },

        _getAPIs: function(originalUrl){
            var apis = new Object();
            apis.tinyurl = new Object();
            apis.tinyurl.url = "http://tinyurl.com/api-create.php";
            apis.tinyurl.params = {
                url: originalUrl
            };

            apis.isgd = new Object();
            apis.isgd.url = "http://is.gd/api.php";
            apis.isgd.params = {
                longurl: originalUrl
            };

            apis.bitly = new Object();
            apis.bitly.url = "http://bit.ly/api";
            apis.bitly.params = {
                url: originalUrl
            };

            apis.hurl = new Object();
            apis.hurl.url = "http://www.hurl.ws/api/";
            apis.hurl.params = {
                url: originalUrl,
                user: "ubiquity"
            };
            
            apis.snipurl = new Object();
            apis.snipurl.url = "http://snipr.com/site/snip";
            apis.snipurl.params = {
                r: "simple",
                link: originalUrl
            };

            return apis;
        }
    });
})();

/*
 --------------------------------------------------------------------------------------
 */

 CmdUtils.CreateCommand({
    name: "goto",
    homepage: "http://labs.vunite.com/vubiqs/verbs.php",
    author:{
        name: "Jordan Brough",
        email: "support@vunite.com"
    },
    license: "MIT",
    description: "Opens a specified URL or keyword in a new tab.",
    help: "goto [url]",
    icon: "http://vunite.com/wp-content/themes/vunite/images/favicont.ico",
    arguments: [ {role: 'object', nountype: noun_type_url, label: 'url'} ],
    preview: function(pblock, {object}){
        //create a variable for the entered url
        var url = object.text;
        if (url.length <= 0 || url == undefined) {
            pblock.innerHTML = "Enter a URL or keyword.";
            return;
        }
        if (this.isUrl(object.text)) {
        
            pblock.innerHTML = "this URL will open directly";
            return;
        }
        else {
        
            pblock.innerHTML = "goto will use I'm Feeling Lucky";
            return;
            
        }
        
    },
    
    execute: function({object}){
        //create a variable for the entered url
        var url = object.text;
        
        if (this.isUrl(object.text)) {
            //opens the url in the browser, currently always launches a new tab
            Utils.openUrlInBrowser(url);
        }
        else {
            //opens the (non-url) term by appending it to a "I'm feeling lucky search string"
            Utils.openUrlInBrowser("http://www.google.com/search?btnI=I%27m+Feeling+Lucky&ie=UTF-8&oe=UTF-8&q=" + url);
        }
        
    },
    
    isUrl: function isUrl(s){
        //simple regex for checking the url
        var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
        return regexp.test(s);
    }
    
});

/*
 --------------------------------------------------------------------------------------
 */

  CmdUtils.CreateCommand({
    name: "craigslist",
    homepage: "http://labs.vunite.com/vubiqs/verbs.php",
    author:{
        name: "Jordan Brough",
        email: "support@vunite.com"
    },
    license: "MIT",
    description: "Search craigslist.org",
    help: "craigslist [search term(s)] in [city]",
    icon: "http://vunite.com/wp-content/themes/vunite/images/favicont.ico",
    arguments: [ {role: 'object', nountype: noun_type_url, label: 'search term(s)'},
                {role: 'location', nountype: noun_arb_text, label: 'location'} ],

    preview: function(pblock, {object, location}){
        //create a variable for the entered search terms
        var terms = object.text;
        var city = location.text;

        if (terms.length <= 0 || terms == undefined ||  city.length <= 0 || city == undefined) {
            pblock.innerHTML = "Enter search term(s) and the city to search.  <br />Example:  craigslist cars in saltlakecity";
            return;
        }
        else {
            pblock.innerHTML = "Ubiquity will search " + city + " craigslist for '"+ terms + "'.";
            return;
        }
      
    },
    
    execute: function({object, location}){
        //create a variable for the entered search terms
        var terms = object.text;
        var city = location.text.replace(/ /g,"");
     
        if (city.length <= 0){
           city = "sfbay";
        }
            
        //opens the craigslist search
        Utils.openUrlInBrowser("http://"+ city +".craigslist.org/search/sss?query=" + terms);
    },

    
});