// -----------------------------------------------------------------
// search.js
//
// Public search engine API
// Copyright (c) 2007 Amber-Lace Entertainment, all rights reserved.
// -------------------------------------------------------------------

// Useful functions

function head()
{
	var tmp = document.getElementsByTagName('head');
	return tmp[0];
}

function body()
{
	var tmp = document.getElementsByTagName('body');
	return tmp[0];
}

function include(src)
{
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = src;

	head().appendChild(script);
}

function Query()
{
	this.keys = new Array();
	this.values = new Array();

	this.get = function(key)
	{
		var value = null;
		for(var i = 0; i < this.keys.length; i++) {
			if(this.keys[i] == key) {
				value = this.values[i];
				break;
			}
		}
		return value;
	}

	this.parse = function(querystring)
	{
	//	var query = window.location.search.substring(1);
		var query = querystring;
		var pairs = query.split("&");

		for(var i = 0; i < pairs.length; i++) {
			var pos = pairs[i].indexOf('=');
			if(pos >= 0) {
				var argname = pairs[i].substring(0,pos);
				var value = pairs[i].substring(pos+1);
				this.keys[this.keys.length] = argname;
				this.values[this.values.length] = value;
			}
		}
	}
}

// ------------------------
// Searcher Class
// ------------------------

function Searcher()
{
	this.query = '';
	this.type = null;
	this.niche = null;
	
	this.search = function(page) {

		this.displayLoading();

		var nc = Math.round(1000000 * Math.random());

		var url = 'http://www.nudechaser.com/pubapi/s.php?nc=' + nc + '&q=' + this.query;
		if(this.type != null) {
			url += '&type=' + this.type;
		}
		if(page != 0) {
			url += '&p=' + page;
		}
		if(this.niche != null) {
			url += '&n=' + escape(this.niche);
		}

		include(url);

		body().scrollTop = 0;
	}
	
	this.displayLoading = function() {
		document.getElementById('NCSearchResults').innerHTML = '<p>Searching...</p>';
	}
}

// --------------------------------
// Search Control Class
// --------------------------------

function SearchControl()
{
	// Constructor Code

	this.action = arguments[0];
	this.omit_options = arguments[1];
	this.size = arguments[2];

	if(this.size == null) {
		this.size = 50;
	}

	this.type = null;
	this.searchHTML = '';

	this.query = new Query();
	this.query.parse(window.location.search.substr(1));

	// Generate the control HTML

	this.genSearchHTML = function()
	{
		var tmp = '<div id="NCSearchControl"><form id="NCSearchControlForm"';

		if(this.action != null) {
			tmp += ' action="' + this.action + '"';
		} else {
			tmp += ' action="javascript:SearchControl.execute(document.getElementById(\'NCSearchControlForm\').q.value);"';
		}

		tmp += '><p><input type="text" name="q" size="' + this.size + '" class="textbox" /> <input type="button" value="Search" class="button" onclick="javascript:document.getElementById(\'NCSearchControlForm\').submit();" /></p>';

		if(this.omit_options != true) {
			tmp += '<p><input type="radio" name="t" value="pics" /> pictures <input type="radio" name="t" value="vids" /> movies <input type="radio" name="t" value="both" /> both</p>';
		}

		tmp += '</form></div>';
		this.searchHTML = tmp;
	}

	// Add a searcher object

	this.addSearcher = function(obj) 
	{ 
		this.searcher = obj;
	}

	// Draw the control

	this.draw = function(element) 
	{
		this.controlElement = element;

		this.genSearchHTML();

		this.controlElement.innerHTML = this.searchHTML;

		var sf = document.getElementById('NCSearchControlForm');

		sf.q.focus(); 

		if(this.omit_options != true) {

			var t = this.query.get('type');
			if(t == 'pics') { 
				sf.t[0].checked = true; 
			} else if(t == 'vids') { 
				sf.t[1].checked = true; 
			} else {
				sf.t[2].checked = true;
			} 
		}

		var q = this.query.get('q');
		if(q != null) {
			this.execute(q);
		}
	}

	// Execute a search

	this.execute = function(q) 
	{
		this.searcher.type = this.getType();
		this.searcher.query = q;
		this.searcher.search(0);

		var sf = document.getElementById('NCSearchControlForm');
		if(sf) {
			sf.q.value = unescape(q);	
		}
	}

	// Explicitly set a type

	this.setType = function(t)
	{
		switch(t) {
		case 'pics':
		case 'vids':
		case 'both':
			this.type = t;
			break;
		default:
			this.type = 'both';
			break;
		}
	}
	// Returns selected type

	this.getType = function()
	{
		if(this.type != null) {
			return this.type;
		}

		if(this.omit_options != true) {

			var sf = document.getElementById('NCSearchControlForm');
			if(sf) {
				var type = null;

				for(var i = 0; i < sf.t.length; i++) {
					if(sf.t[i].checked) {
						switch(i) {
						case 0: type = 'pics'; break;
						case 1: type = 'vids'; break;
						case 2: type = 'both'; break;
						}
					}
				}
	
				return type;
			}
		}

		return 'both';
	}
	// Navigate to a page

	this.page = function(i) 
	{
		this.searcher.search(i);
	}

	this.genSearchHTML();
}

function MiniSearch()
{
	this.searchHTML = '<div id=\"NCSearchControl\"><form id=\"NCSearchControlForm\" action=\"http://www.nudechaser.com/\"><p><input type=\"text\" name=\"q\" size=\"25\" class=\"textbox\">&nbsp;<input type=\"submit\" value=\"Search\" class=\"button\"></p><p><a href=\"http://www.nudechaser.com\"><img src=\"http://www.nudechaser.com/images/nudechasersm.png\" width=\"80\" height=\"17\" alt=\"Powered by NudeChaser.com\"></a></p></form></div>';

	this.draw = function(element)
	{
		this.controlElement = element;
		this.controlElement.innerHTML = this.searchHTML; 
	}
}

function NCSClickReport(id)
{
	var path = 'http://www.nudechaser.com/pubapi/report.php?i=' + id;
	include(path);
}
