var noun_type_comicstrip = {
	_name: "comic strip",

	suggest: function( text, html ) {
		var comics =	[	"BC",
							"Betty",
							"Boondocks",
							"Born Loser",
							"Dilbert",
							"Foxtrot",
							"Garfield",
							"Get Fuzzy",
							"Herman",
							"Marmaduke",
							"Monty",
							"Peanuts",
							"Penny Arcade",
							"Off The Mark",
							"TMNT",
							"User Friendly",
							"Wizard Of Id",
							"Working Daze",
							"Working It Out",
							"xkcd",
							"Ziggy"
						];
		var suggestions = [];
		var title = "";
		var comic = "";
		var index = 0;

		for ( var i = 0; i < comics.length; i++ ) {
			comic = comics[i];
			index = i + 1;
			title = index + ". " + comic;
			if ( text.length == 0 || comic.match( text, "i" ) || (index + "").match( text, "i" ) ){
				suggestions.push( CmdUtils.makeSugg( title, title, null ) );
			}
		}

		// Return the list of input objects
		return suggestions;
	}
};

CmdUtils.CreateCommand( {
	names: [ 'comicstrip' ],
	arguments: [ {role: 'object', nountype: noun_type_comicstrip, label: 'title'} ],
	description:"Displays the lastest comic for a number of comic strips and allows you to paste them to the webpage you are viewing.",
	author: { name: "Erik Vergobbi Vold", email:"erikvvold@gmail.com" },
	version: "1.3",
	homepage: "http://erikvold.com/tools/ubiquity/comicstrip/comicstrip.cfm",
	icon: "http://assets.comics.com/img/v1/favicon.ico",
	imgHTML: "",
	preview: function ( pblock, args ) {
		var searchText = jQuery.trim( args.object.text ).toLowerCase();
		var tempHTML = "";
		var _previewImageEndHTML = '" border="0" style="max-width: 485px;" />';
		var _regImgEndHTML = '" />';
		var extractGoComicsImg = function ( pageHTML ) {
			return pageHTML.match( /http:\/\/imgsrv\.gocomics\.com\/dim\/[^\s"']+/i )[0];
		};
		var extractComicsImg = function ( pageHTML ) {
			return pageHTML.match( /http:\/\/assets.comics.com\/dyn\/str_strip\/\S+\.full\.gif/i )[0];
		};

		pblock.innerHTML = "";

		//BC
		if ( searchText == "1" || searchText.match( /bc/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://www.gocomics.com/bc',
				error: null,
				success: function( page ) {
					try {
						tempHTML='<img src="' + extractGoComicsImg( page );
						imgHTML = tempHTML + _regImgEndHTML;
						tempHTML+=_previewImageEndHTML;
						pblock.innerHTML = tempHTML;
					}
					catch ( e ) {
						CmdUtils.previewAjax( pblock, {
							type: "GET",
							url: 'http://comics.com/bc/',
							error: null,
							success: function( page ) {
								tempHTML = '<img src="' + extractComicsImg(page);
								imgHTML = tempHTML + _regImgEndHTML;
								tempHTML += _previewImageEndHTML;
								pblock.innerHTML = tempHTML;
							}
						});
					}
				}
			});
		}
		//BETTY
		else if ( searchText == "2" || searchText.match( /betty/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://comics.com/betty/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="' + extractComicsImg(page);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//BOONDOCKS
		else if ( searchText == "3" || searchText.match( /(the)?boon(-|_| )?docks/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://www.gocomics.com/boondocks',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="' + extractGoComicsImg(page);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//BORNLOSER
		else if ( searchText == "4" || searchText.match( /born(-|_| )?loser/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://comics.com/the_born_loser/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="' + extractComicsImg(page);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//DILBERT
		else if ( searchText == "5" || searchText.match( /dilbert/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://dilbert.com/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="http://dilbert.com/' + page.match(/dyn\/str_strip\/\S*\.gif/i);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//FOXTROT
		else if ( searchText == "6" || searchText.match( /foxtrot/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://www.gocomics.com/foxtrot',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="' + extractGoComicsImg(page)
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//GARFIELD
		else if ( searchText == "7" || searchText.match( /garfield/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://ivanov.in/garfield/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="http://images.ucomics.com/' + page.match(/(comics\/ga\/\d{4}\/\S+\.(jpg|gif))/i)[1];
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//GETFUZZY
		else if( searchText == "8" || searchText.match( /get(-|_| )?fuzzy/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://comics.com/get_fuzzy/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="' + extractComicsImg(page);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//HERMAN
		else if ( searchText == "9" || searchText.match( /herman/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://comics.com/herman/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="' + extractComicsImg(page);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//MARMADUKE
		else if ( searchText == "10" || searchText.match( /marmaduke/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://comics.com/marmaduke/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="' + extractComicsImg(page);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//MONTY
		else if ( searchText == "11" || searchText.match( /monty/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://comics.com/monty/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="' + extractComicsImg(page);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//PEANUTS
		else if ( searchText == "12" || searchText.match( /peanuts/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://comics.com/peanuts/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="' + extractComicsImg(page);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//PENNY ARCADE
		else if ( searchText == "13" || searchText.match( /penny(-|_| )?arcade/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://www.penny-arcade.com/comic/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="http://www.penny-arcade.com' + page.match(/src="(\/images\/\d{4}\/\S+\.(jpg|gif))/i)[1];
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//OFF THE MARK
		else if ( searchText == "14" || searchText.match( /off(-|_| )?the(-|_| )?mark/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://comics.com/off_the_mark/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="' + extractComicsImg(page);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//TEENAGE MUTANT NINJA TURTLES
		else if ( searchText == "15" || searchText.match( /t(-|_| )?m(-|_| )?n(-|_| )?t/i ) != null || searchText.match( /teenage(-|_| )?mutant(-|_| )?ninja(-|_| )?turtles/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://www.gocomics.com/teenagemutantninjaturtles',
				error: null,
				success: function( page ){
					tempHTML = '<img src="' + extractGoComicsImg(page);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//USER FRIENDLY
		else if ( searchText == "16" || searchText.match( /user(-|_| )?friendly/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://www.userfriendly.org/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="http://www.userfriendly.org/' + page.match(/cartoons\/archives\/\S+\.(jpg|gif)/i)[0];
					imgHTML = tempHTML + '" />';
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//WIZARD OF ID
		else if ( searchText == "17" || searchText.match( /wizard(-|_| )?of(-|_| )?id/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://comics.com/wizard_of_id/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="' + extractComicsImg(page);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//WORKING DAZE
		else if ( searchText == "18" || searchText.match( /working(-|_| )?daze/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://comics.com/working_daze/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="' + extractComicsImg(page);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//WORKING IT OUT
		else if ( searchText == "19" || searchText.match( /working(-|_| )?it(-|_| )?out/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://comics.com/working_it_out/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="' + extractComicsImg(page);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//XKCD
		else if ( searchText == "20" || searchText.match( /xkcd/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://xkcd.com/',
				error: null,
				success: function( page ) {
					tempHTML = '<img src="http://' + page.match(/imgs\.xkcd\.com\/comics\/\S+\.(jpg|gif|png)/i)[0];
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		//ZIGGY
		else if ( searchText == "21" || searchText.match( /ziggy/i ) != null ) {
			CmdUtils.previewAjax( pblock, {
				type: "GET",
				url: 'http://www.gocomics.com/ziggy',
				error: null,
				success: function( page ){
					tempHTML = '<img src="' + extractGoComicsImg(page);
					imgHTML = tempHTML + _regImgEndHTML;
					tempHTML += _previewImageEndHTML;
					pblock.innerHTML = tempHTML;
				}
			});
		}
		else{
			// Default Preview
			var _tempDisplayArray = [
				'1: BC<br>',
				'2: Betty<br>',
				'3: The Boondocks<br>',
				'4: Born Loser<br>',
				'5: Dilbert<br>',
				'6: Foxtrot<br>',
				'7: Garfield<br>',
				'8: Get Fuzzy<br>',
				'9: Herman<br>',
				'10: Marmaduke<br>',
				'11: Monty<br>',
				'12: Peanuts<br>',
				'13: Penny Arcade<br>',
				'14: Off The Mark<br>',
				'15: Teenage Mutant Ninja Turtles (TMNT)<br>',
				'16: User Friendly<br>',
				'17: Wizard Of Id<br>',
				'18: Working Daze<br>',
				'19: Working It Out<br>',
				'20: xkcd<br>',
				'21: Ziggy'
			];
	
			// Show Selection List
			pblock.innerHTML = _tempDisplayArray.join('');
		}

		return true;
	},
	execute: function ( args ) {
		CmdUtils.setSelection( imgHTML );
		return true;
	}
});