'use strict';

var helper = require('../lib/helper');

var baseUrl = 'http://tinhte.vn';

Array.prototype.queue = function (u) {
	console.log(this.length +  '. Add ' + u + '...');
	this.push(u);

	return this;
}
var FoundedLink = new Array();
																																												
module.exports = function(Crawler, config) {
	config.callback = function (error, result, $) {

		// Fetch next URL and Add to Queue
		var fetchUrlFrom = [
			'.PageNav > nav a', 
			'#navigation a', 
			'.primaryContent > h2.subHeading > a'
		];

		fetchUrlFrom.forEach(function(m) {
			getQueueLink(m, $, function(link) {
				link = helper.getFullPath(baseUrl, link);
				
				FoundedLink.queue(link);

				var fs = require('fs');
				fs.appendFile("logs/tinhte.logs.txt", link + "\n", function(err) {
					if(err) {
						return console.log(err);
					}
				}); 

				// Add that to Queue
				console.log('Add to queue ', link);
	        	c.queue(link);
	        });	
		});
	};

	// Start from root
	var c = new Crawler(config);
	c.queue(baseUrl);
}

var getQueueLink = function(regex, $, callback) {
	var queueLink = $(regex);

	if (queueLink.length) {
		queueLink.each(function(index, a) {
			var link = $(a).attr('href');
			if (link != undefined) {
				if (callback != undefined) {
					callback(link);
				}
			}
		});
	}
}

var checkDeny = function(url) {
	var regex = [
		'tinhte.vn/search/',
		'tinhte.vn/login/',
	]; 
}
@@ -46,16 +46,56 @@
 elper');
+%0Avar queue = require('../models/queue');
 %0A%0Avar ba
@@ -459,28 +459,21 @@
 e%0A%09%09var 
-fetchUrlFrom
+regex
  = %5B%0A%09%09%09
@@ -567,20 +567,13 @@
 %0A%0A%09%09
-fetchUrlFrom
+regex
 .for
@@ -967,30 +967,146 @@
 k);%0A
-%09        %7D);%09%0A%09%09%7D);%0A%09%7D
+%0A%09        %09queue.queue(link, result.uri);%0A%09        %7D);%09%0A%09%09%7D);%0A%09%7D;%0A%0A%09var startUrl = queue.dequeue();%0A%0A%09console.log('Start url: ', startUrl)
 ;%0A%0A%09
@@ -1125,16 +1125,18 @@
 m root%0A%09
+//
 var c = 
@@ -1157,24 +1157,26 @@
 nfig);%0A%09
+//
 c.queue(
 baseUrl)
@@ -1167,20 +1167,21 @@
 c.queue(
-base
+start
 Url);%0A%7D%0A

Edit tinhte module