var searchOccurrenceId = 0;

function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function clean(sString)
{
	sString = sString.replace(":", " ")
	return trimAll(sString.replace("'", " "));
}

function transform(sString)
{
	var phraseFlag=false;
	var andFlag=false;

	if(phraseFlag)
	{
		if((sString.indexOf(" ") != -1) &&
		   (sString.indexOf('"') == -1) &&
		   (sString.indexOf(' AND ') == -1) &&
		   (sString.indexOf(' OR ') == -1))
		{
			sString = '"' + sString + '"';
		}
	}
	else if(andFlag)
	{
		if((sString.indexOf(" ") != -1) &&
		   (sString.indexOf('"') == -1) &&
		   (sString.indexOf(' AND ') == -1) &&
		   (sString.indexOf(' OR ') == -1))
		{
			sString = trimAll(sString);
			while(sString.indexOf("  ") != -1)
			{
				sString = sString.replace(/  /g, " ");
			}
			sString = sString.replace(/ /g, " AND ");
		}
	}
	else
	{
		if((sString.indexOf(" ") != -1) &&
		   (sString.indexOf('"') == -1) &&
		   (sString.indexOf(' AND ') == -1) &&
		   (sString.indexOf(' OR ') == -1))
		{
			sString = trimAll(sString);
			while(sString.indexOf("  ") != -1)
			{
				sString = sString.replace(/  /g, " ");
			}
		}
	}
	return sString;
}

function onSubmitForm()
{
	try
	{
		var baseUrl = getUrl();
		document.forms['search'].action = baseUrl;
	}
	catch(e)
	{
		alert("Exception: " + e);
	}

	return true;
}

function clearForm()
{
	for(c=0; c < getMaxFields(); c++)
	{
		eval("document.forms['search'].query_" + c + ".value=''");
	}

	if(isAdvanced())
	{
		for(c=0; c < getMaxFields(); c++)
		{
			eval("document.forms['search'].label_" + c + ".value=''");
		}
	}

	return false;
}

function isAdvanced()
{
	var field = document.forms['search'].label_0;
	if(field == null)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function getMaxFields()
{
	return 3;
}

function getUrl()
{
	try
	{
		var baseUrl = "http://www.trendpedia.com/simple_search.php?";

		var language = "qq";
		var country = "xx";
		var scope = "";
		var date = "l3m";
		var search_type = document.forms['search'].type.value;
		var origin = document.forms['search'].orig.value;

		if(document.forms['search'].language)
		{
			language = document.forms['search'].language.value;
		}

		if(document.forms['search'].country)
		{
			country = document.forms['search'].country.value;
		}

		if(document.forms['search'].scope)
		{
			scope = document.forms['search'].scope.value;
		}

		if(document.forms['search'].date)
		{
			date = document.forms['search'].date.value;
		}

		baseUrl += "language=" + language;
		baseUrl += "&country=" + country;
		baseUrl += "&scope=" + scope;
		baseUrl += "&type=" + search_type;
		baseUrl += "&orig=" + origin;
		baseUrl += "&d=" + date;

		var hash = '#';
		hash += "language=" + language;
		hash += "|country=" + country;
		hash += "|scope=" + clean(scope);
		hash += "|orig=" + origin;
		hash += "|d=" + date;

		for(c=0; c < getMaxFields(); c++)
		{
			var query = eval("document.forms['search'].query_" + c + ".value");
			var queryTerm = clean(query);

			var label = null;
			var labelTerm = '';

			if(isAdvanced())
			{
				label = eval("document.forms['search'].label_" + c + ".value");
			}

			if(label != null)
			{
				labelTerm = clean(label);
			}

			if(queryTerm != '' && labelTerm != '')
			{
				baseUrl += "&series_" + c + "=" + transform(queryTerm);
				baseUrl += "&label_" + c + "=" + labelTerm;

				hash += "|series_" + c + "=" + transform(queryTerm);
				hash += "|label_" + c + "=" + labelTerm;
			}
			else if(queryTerm != '' && labelTerm == '')
			{
				baseUrl += "&series_" + c + "=" + transform(queryTerm);
				baseUrl += "&label_" + c + "=" + queryTerm;

				hash += "|series_" + c + "=" + transform(queryTerm);
				hash += "|label_" + c + "=" + queryTerm;
			}
			else if(queryTerm == '' && labelTerm != '')
			{
				baseUrl += "&series_" + c + "=" + transform(labelTerm);
				baseUrl += "&label_" + c + "=" + labelTerm;

				hash += "|series_" + c + "=" + transform(labelTerm);
				hash += "|label_" + c + "=" + labelTerm;
			}
		}
		baseUrl += hash;
	}
	catch(e)
	{
		alert("Exception: " + e);
	}

	return baseUrl;
}

function setButtonMouseDown(button)
{
	button.className = "trendpedia-searchButtonDown";
}

function setButtonMouseUp(button)
{
	button.className = "trendpedia-searchButtonUp";
}

function setButtonMouseOver(button)
{
	button.className = "trendpedia-searchButtonOver";
}

function submitFacebook(u, t)
{
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

function submitExternalLink(sService)
{
	//alert(searchOccurrenceId);

	//var baseUrl = getUrl();
	var baseUrl = "http://www.trendpedia.com/link.php?id=" + searchOccurrenceId;

	var serviceUrl = "";
	var title = getTitle();
	var urlField = "url";
	var titleField = "title";

	if(sService == "delicious")
	{
		serviceUrl = "http://del.icio.us/post";
		baseUrl += "&orig=DELICIOUS";
	}
	else if(sService == "digg")
	{
		serviceUrl = "http://digg.com/submit";
		baseUrl += "&orig=DIGG";
	}
	else if(sService == "reddit")
	{
		serviceUrl = "http://reddit.com/submit";
		baseUrl += "&orig=REDDIT";
	}
	else if(sService == "facebook")
	{
		serviceUrl = "http://www.facebook.com/sharer.php";
		urlField = "u";
		titleField = "t";
		baseUrl += "&orig=FACEBOOK";
	}
	else if(sService == "stumbleupon")
	{
		serviceUrl = "http://www.stumbleupon.com/submit";
		baseUrl += "&orig=STUMBLEUPON";
	}
	else if(sService == "email")
	{
		var link_location = baseUrl + "&orig=EMAIL";
		link_location = encodeURIComponent(link_location);

		serviceUrl = "mailto:?" +
		"subject=" + title +
		"&body=" + "I found this awesome trend on Trendpedia and I wanted to share it with you. Catch the trend here:%0A%0A" + link_location;
		//alert(serviceUrl);
		window.location = serviceUrl;
	}

	baseUrl = encodeURIComponent(baseUrl);

	if(sService != "email" && sService != "facebook")
	{
		submitUrl = serviceUrl + "?" +
					urlField + "=" + baseUrl +
					"&amp;" +
					titleField + "=" + title;

		//window.location = submitUrl;
		window.open(submitUrl);
		//alert(submitUrl);
	}
	else if(sService == "facebook")
	{
		submitFacebook(baseUrl, title);
	}
}
