makeSearchCommand({
  name: "gt-movie-search",
  description: "Search <a href=\"http://www.gametrailers.com\">Gametrailers.com</a>'s movies",
  icon: "http://www.gametrailers.com/favicon.ico",
  url:"http://www.gametrailers.com/search.php?str_type=movies&s={QUERY}",
  preview: function(previewBlock, directObject) {
	if(!directObject.text || directObject.text.length < 1) {
	  previewBlock.innerHTML = "Searches for movies on Gametrailers";
	  return;
	}

	previewBlock.innerHTML = "Searchings Gametrailers for movies matching <b>" + directObject.summary + "</b>";

	var apiUrl = "http://www.gametrailers.com/search.php";
	var apiParams = {
	  str_type: "movies",
	  s: directObject.text
	};

	jQuery.ajax({
	  type: "GET",
	  url: apiUrl,
	  data: apiParams,
	  dataType: "html",
	  error: function() {
		previewBlock.innerHTML = "Error searching Gametrailers.";
	  },
	  success: function(responseData) {
			const MAX_RESULTS = 5;
			
			var movieRows = [];
			
			// start will contain the start position for the code for the current movie
			var start = 0;
			for (var i = 0; i < MAX_RESULTS; i++) {
				var row = {};
				// get the new start position
				start = responseData.indexOf('search_movie_row', start + 1);
				if (start == -1) break;
				// the code for just this movie
				var movieCode = responseData.substring(start, responseData.indexOf('COMMENT', start));
				
				if (movieCode.indexOf('newestlist_movie_format_SDHD') != -1) {
					j = movieCode.indexOf('newestlist_movie_format_SDHD');
					row.hdURL = movieCode.substring(movieCode.indexOf('href="', j) + 6, movieCode.indexOf('"', movieCode.indexOf('href="', j) + 6));
					j = movieCode.indexOf(row.hdURL);
					row.sdURL = movieCode.substring(movieCode.indexOf('href="', j) + 6, movieCode.indexOf('"', movieCode.indexOf('href="', j) + 6));
				} else if (movieCode.indexOf('newestlist_movie_format_SD') != -1) {
					j = movieCode.indexOf('newestlist_movie_format_SD');
					row.sdURL = movieCode.substring(movieCode.indexOf('href="', j) + 6, movieCode.indexOf('"', movieCode.indexOf('href="', j) + 6));
				} else if (movieCode.indexOf('newestlist_movie_format_HD') != -1) {
					j = movieCode.indexOf('newestlist_movie_format_HD');
					row.hdURL = movieCode.substring(movieCode.indexOf('href="', j) + 6, movieCode.indexOf('"', movieCode.indexOf('href="', j) + 6));
				}
				row.defaultURL = (row.hdURL ? row.hdURL : row.sdURL);
				
				var gameStart = movieCode.indexOf('gamepage_content_row_title') + 'gamepage_content_row_title'.length;
				row.gameURL = movieCode.substring(movieCode.indexOf('href="', gameStart) + 6, movieCode.indexOf('" class=', gameStart));
				row.game = movieCode.substring(movieCode.indexOf('title">', gameStart) + 7, movieCode.indexOf('</a>', gameStart));
				
				var titleStart = movieCode.indexOf('search_movie_title');
				row.title = movieCode.substring(movieCode.indexOf('<b>', titleStart) + 3, movieCode.indexOf('</b>', titleStart));
				
				row.img = movieCode.substring(movieCode.indexOf('img src="') + 9, movieCode.indexOf('" class'));
				
				movieRows[i] = row;
			}
			
			previewBlock.innerHTML = '';
			if (movieRows.length > 0) {
				for (i = 0; i < movieRows.length; i++) {
					row = movieRows[i];
					var h = '<div style="margin-top: 10px; height: 80px;">';
					h += '<a href="http://www.gametrailers.com' + row.defaultURL + '" style="width: 184px; height: 78px; display: block; float: left;">'
					h += '<img src="http://www.gametrailers.com' + row.img + '" style="width: 180px; height: 74px;"/>';
					h += '</a>';
					
					h += '<div style="width: 300px; height: 80px; float: left; position: relative; overflow: hidden">';
					h += '<div style="width: 280px; margin: 0px 10px">';
					h += 'Game: ';
					h += '<a href="http://www.gametrailers.com/' + row.gameURL + '" style="font-size: 12px; font-weight: bold">' + row.game + '</a>';
					h += '</div>';
					
					h += '<div style="width: 280px; margin: 0px 10px">';
					h += 'Title: ';
					h += '<a href="http://www.gametrailers.com' + row.defaultURL + '" style="font-size: 12px; font-weight: bold">' + row.title + '</a>';
					h += '</div>';
					
					h += '<div style="width: 150px; height: 20px; bottom: 0px; left: 0px; position: absolute; text-align: center;">';
					if (row.hdURL) {
						h += '<a href="http://www.gametrailers.com' + row.hdURL + '" style="font-size: 12px">Watch in HD</a>';
					}
					h += '</div>';
					h += '<div style="width: 150px; height: 20px; bottom: 0px; right: 0px; position: absolute; text-align: center;">';
					if (row.sdURL) {
						h += '<a href="http://www.gametrailers.com' + row.sdURL + '" style="font-size: 12px">Watch in SD</a>';
					}
					h += '</div>';
					h += '</div>';
					
					h += '</div>';
					previewBlock.innerHTML += h;
				}
			} else {
				previewBlock.innerHTML = 'Unable to parse data or no results found';
			}
	  }
	});
  }
	
});
