<?php

$path = "/speak/words/trackthatword/ttw/";

require ($DOCUMENT_ROOT.$path."db-pass.php");
require ($DOCUMENT_ROOT.$path."cms.php");

$mysqll = new MySQL;
$dork = $mysqll->connect($db_base, $db_host, $db_user, $db_word);

/*
Determine what we're doing--post data can include:
	$w - spelled version of a word
	$i - database ID of word
	$c - spelled category
		 (categories when passed only include first word)
	$s - search term

Arrays to be loaded, if word is chosen:
	$word[] - queried word info
	$cat1_next - next word
	$cat1_prev - previous word
	$cat2_next - next word
	$cat2_prev - previous word
	$cat3_next - next word
	$cat3_prev - previous word
	$all_next - next word
	$all_prev - previous word

Categories
	$category[] = array of all categories
	$category_map[] = alternative names
	$c_map[] = category search map
*/

$category = Array("20th Century Miscellany", "College", "Cowboy", "DARE", "Emoticons & Text Messaging", "Hip Hop", "Historically American", "Internet & Technology", "Popular Media", "Regional American", "Slayer Slang", "Spanglish", "Surfer Slang", "Teen Youth", "Texas", "Western American");
$category_map = Array("20th Century Misc.", "College", "Cowboy", "DARE", "Emoticons & IM", "Hip Hop", "Historically American", "Internet & Tech", "Popular Media", "Regional American", "Slayer Slang", "Spanglish", "Surfer Slang", "Teen & Youth", "Texas", "Western American");
$c_map = Array("20th", "college", "cowboy", "dare", "emoticons", "hip", "historically", "internet", "popular", "regional", "slayer", "spanglish", "surfer", "teen", "texas", "western");

// put the maps into an array called by the search name
for ($aa=0;$aa < count($category); $aa++) {
	$mappedcat[$c_map[$aa]]['real'] = $category[$aa];
	$mappedcat[$c_map[$aa]]['alternative'] = $category_map[$aa];
}

// if $w or $i passed, load array

if ($w || $i) {
	if ($w) {
		$query = "SELECT * FROM ".$db_table." WHERE word_word = '".$w."'";
	} else {
		$query = "SELECT * FROM ".$db_table." WHERE word_id='".$i."'";
	}
	$word_q = new Query($query);
	$word = $word_q->fetchArrays();
	if ($word[0]['word_cat1']) {
		$m_t = explode(" ",$word[0]['word_cat1']);
		$mapped_1 = strtolower($m_t[0]);
		// get next word id in category
		$cat1_next_q = new Query("SELECT DISTINCT word_id FROM ".$db_table." WHERE word_word>'".$word[0]['word_word']."' AND (word_cat2='".$word[0]['word_cat1']."' OR word_cat1='".$word[0]['word_cat1']."' OR word_cat3='".$word[0]['word_cat1']."') ORDER BY word_word LIMIT 1");
		$cat1_next = $cat1_next_q->fetchArrays();
		if (!$cat1_next) {
			$cat1_first_word_q = new Query("SELECT DISTINCT word_id FROM ".$db_table." WHERE (word_cat2='".$word[0]['word_cat1']."' OR word_cat1='".$word[0]['word_cat1']."' OR word_cat3='".$word[0]['word_cat1']."') ORDER BY word_word LIMIT 1");
			$cat1_first_word = $cat1_first_word_q->fetchArrays();
			$cat1_next[0]['word_id'] = $cat1_first_word[0]['word_id'];
		}
		// get previous word id
		$cat1_prev_q = new Query("SELECT DISTINCT word_id FROM ".$db_table." WHERE word_word < '".$word[0]['word_word']."' AND (word_cat2='".$word[0]['word_cat1']."' OR word_cat1='".$word[0]['word_cat1']."' OR word_cat3='".$word[0]['word_cat1']."') ORDER BY word_word DESC LIMIT 1");
		$cat1_prev = $cat1_prev_q->fetchArrays();
		if (!$cat1_prev) {
			$cat1_last_word_q = new Query("SELECT DISTINCT word_id FROM ".$db_table." WHERE (word_cat2='".$word[0]['word_cat1']."' OR word_cat1='".$word[0]['word_cat1']."' OR word_cat3='".$word[0]['word_cat1']."') ORDER BY word_word DESC LIMIT 1");
			$cat1_last_word = $cat1_last_word_q->fetchArrays();
			$cat1_prev[0]['word_id'] = $cat1_last_word[0]['word_id'];
		}

	}
	if ($word[0]['word_cat2']) {
		$m_t = explode(" ",$word[0]['word_cat2']);
		$mapped_2 = strtolower($m_t[0]);
		// get next word id in category
		$cat2_next_q = new Query("SELECT DISTINCT word_id FROM ".$db_table." WHERE word_word>'".$word[0]['word_word']."' AND (word_cat2='".$word[0]['word_cat2']."' OR word_cat1='".$word[0]['word_cat2']."' OR word_cat3='".$word[0]['word_cat2']."') ORDER BY word_word LIMIT 1");
		$cat2_next = $cat2_next_q->fetchArrays();
		if (!$cat2_next) {
			$cat2_first_word_q = new Query("SELECT DISTINCT word_id FROM ".$db_table." WHERE (word_cat2='".$word[0]['word_cat2']."' OR word_cat1='".$word[0]['word_cat2']."' OR word_cat3='".$word[0]['word_cat2']."') ORDER BY word_word LIMIT 1");
			$cat2_first_word = $cat2_first_word_q->fetchArrays();
			$cat2_next[0]['word_id'] = $cat2_first_word[0]['word_id'];
		}
		// get previous word id
		$cat2_prev_q = new Query("SELECT DISTINCT word_id FROM ".$db_table." WHERE word_word < '".$word[0]['word_word']."' AND (word_cat2='".$word[0]['word_cat2']."' OR word_cat1='".$word[0]['word_cat2']."' OR word_cat3='".$word[0]['word_cat2']."') ORDER BY word_word DESC LIMIT 1");
		$cat2_prev = $cat2_prev_q->fetchArrays();
		if (!$cat2_prev) {
			$cat2_last_word_q = new Query("SELECT DISTINCT word_id FROM ".$db_table." WHERE (word_cat2='".$word[0]['word_cat2']."' OR word_cat1='".$word[0]['word_cat2']."' OR word_cat3='".$word[0]['word_cat2']."') ORDER BY word_word DESC LIMIT 1");
			$cat2_last_word = $cat2_last_word_q->fetchArrays();
			$cat2_prev[0]['word_id'] = $cat2_last_word[0]['word_id'];
		}
	}

	if ($word[0]['word_cat3']) {
		$m_t = explode(" ",$word[0]['word_cat3']);
		$mapped_3 = strtolower($m_t[0]);
		// get next word id in category
		$cat3_next_q = new Query("SELECT DISTINCT word_id FROM ".$db_table." WHERE word_word>'".$word[0]['word_word']."' AND (word_cat3='".$word[0]['word_cat3']."' OR word_cat2='".$word[0]['word_cat3']."' OR word_cat1='".$word[0]['word_cat3']."') ORDER BY word_word LIMIT 1");
		$cat3_next = $cat3_next_q->fetchArrays();
		if (!$cat3_next) {
			$cat3_first_word_q = new Query("SELECT DISTINCT word_id FROM ".$db_table." WHERE (word_cat3='".$word[0]['word_cat3']."' OR word_cat2='".$word[0]['word_cat3']."' OR word_cat1='".$word[0]['word_cat3']."') ORDER BY word_word LIMIT 1");
			$cat3_first_word = $cat3_first_word_q->fetchArrays();
			$cat3_next[0]['word_id'] = $cat3_first_word[0]['word_id'];
		}
		// get previous word id
		$cat3_prev_q = new Query("SELECT DISTINCT word_id FROM ".$db_table." WHERE word_word < '".$word[0]['word_word']."' AND (word_cat3='".$word[0]['word_cat3']."' OR word_cat2='".$word[0]['word_cat3']."' OR word_cat1='".$word[0]['word_cat3']."') ORDER BY word_word DESC LIMIT 1");
		$cat3_prev = $cat3_prev_q->fetchArrays();
		if (!$cat3_prev) {
			$cat3_last_word_q = new Query("SELECT DISTINCT word_id FROM ".$db_table." WHERE (word_cat3='".$word[0]['word_cat3']."' OR word_cat2='".$word[0]['word_cat3']."' OR word_cat1='".$word[0]['word_cat3']."') ORDER BY word_word DESC LIMIT 1");
			$cat3_last_word = $cat3_last_word_q->fetchArrays();
			$cat3_prev[0]['word_id'] = $cat3_last_word[0]['word_id'];
		}
	}


	// get next word id
	$all_next_q = new Query("SELECT word_id FROM ".$db_table." WHERE word_word>'".$word[0]['word_word']."' ORDER BY word_word LIMIT 1");
	$all_next = $all_next_q->fetchArrays();
	if (!$all_next) {
		$first_word_q = new Query("SELECT word_id FROM ".$db_table." ORDER BY word_word LIMIT 1");
		$first_word = $first_word_q->fetchArrays();
		$all_next[0]['word_id'] = $first_word[0]['word_id'];
	}
	// get previous word id
	$all_prev_q = new Query("SELECT word_id FROM ".$db_table." WHERE word_word < '".$word[0]['word_word']."' ORDER BY word_word DESC LIMIT 1");
	$all_prev = $all_prev_q->fetchArrays();
	if (!$all_prev) {
		$last_word_q = new Query("SELECT word_id FROM ".$db_table." ORDER BY word_word DESC LIMIT 1");
		$last_word = $last_word_q->fetchArrays();
		$all_prev[0]['word_id'] = $last_word[0]['word_id'];
	}
}

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Do You Speak American . Track That Word! | PBS</title>
	<link rel="stylesheet" href="ttw.css" type="text/css">
</head>

<body>

<div id="holder">

	<div id="title">
<?php
if ($c) {
	if ($c2) {
		echo("Track That Word!");
	} else {
		echo($mappedcat[$c]['real']);
	}
} else if ($word[0]['word_word']) {
	if (strlen($word[0]['word_word']) > 30) {
		$title = " <span style=\"font-size: 12px;\">".bracket_decode($word[0]['word_word'])."</span>";
	} else {
		$title = bracket_decode($word[0]['word_word']);
	}
	if ($word[0]['word_year']) {
		$year = bracket_decode($word[0]['word_year']);
		$year = " <span style=\"font-size: 12px;\">(".$year.")</span>";
	}
	echo($title.$year);
} else if ($s) {
	echo("Search Results");
} else {
	echo("Track That Word!");
}

?>
	</div>

	<div id="definition">
<?php
if ($word) {
	if ($mapped_1 == "dare") {
		$texter = squiggle_decode($word[0]['word_blob']);
		$texter = "<div id=\"dare\">" . $texter ;
		if ($word[0]['word_image']) {
			$texter = $texter . "<p style=\"margin-left: 15px;\"><img src=\"../dare/".$word[0]['word_image'].".gif\"  alt=\"dare map\" border=\"0\" /></p><br />";
		}
	} else {
		$texter = bracket_decode($word[0]['word_blob']);
		$texter = "<div id=\"regular\">" . $texter;
	}
	echo($texter . "</div>");
} else if ($c) {
	// get all of the words in this category
	$querystring = "word_cat1 = '".$mappedcat[$c]['real']."' OR word_cat2 = '".$mappedcat[$c]['real']."' OR word_cat3 = '".$mappedcat[$c]['real']."'";
	if ($c2) {
		$querystring = $querystring. ") OR (word_cat1 = '".$mappedcat[$c2]['real']."' OR word_cat2 = '".$mappedcat[$c2]['real']."' OR word_cat3 = '".$mappedcat[$c2]['real']."'";
		$listo = $listo . "<b>" . $mappedcat[$c2]['alternative'];
	}
	if ($c3) {
		$querystring = $querystring. ") OR (word_cat1 = '".$mappedcat[$c3]['real']."' OR word_cat2 = '".$mappedcat[$c3]['real']."' OR word_cat3 = '".$mappedcat[$c3]['real']."'";
		$listo = $listo . ", " . $mappedcat[$c3]['alternative'];
	}
	if ($c4) {
		$querystring = $querystring. ") OR (word_cat1 = '".$mappedcat[$c4]['real']."' OR word_cat2 = '".$mappedcat[$c4]['real']."' OR word_cat3 = '".$mappedcat[$c4]['real']."'";
		$listo = $listo . ", " . $mappedcat[$c4]['alternative'];
	}
	if ($c5) {
		$querystring = $querystring. ") OR (word_cat1 = '".$mappedcat[$c5]['real']."' OR word_cat2 = '".$mappedcat[$c5]['real']."' OR word_cat3 = '".$mappedcat[$c5]['real']."'";
		$listo = $listo . ", " . $mappedcat[$c5]['alternative'];
	}
	if ($c2) {
		$listo = $listo . "</b> and ";
	}
	$listo = $listo . "<b>" . $mappedcat[$c]['alternative'];
	$collect_cat_q = new Query("SELECT DISTINCT word_id, word_word, word_year FROM ".$db_table." WHERE (".$querystring.") ORDER BY word_word");
	$collect_cat = $collect_cat_q->fetchArrays();
	echo("<p style=\"margin-top: 0px;\">There are ".count($collect_cat)." words in ".$listo."</b>:</p><ul>");
	for ($cc=0;$cc<count($collect_cat);$cc++) {
		echo("<li><a href=\".?i=".$collect_cat[$cc]['word_id']."\">".$collect_cat[$cc]['word_word']."</a>");
		if ($collect_cat[$cc]['word_year']) {
				echo(" (".$collect_cat[$cc]['word_year'].")");
			}
		echo("</li>");
	}
	echo("</ul><br />");
} else if ($s) {

//  this is the old query:
// 	$collect_search_q = new Query("SELECT DISTINCT word_id, word_word, word_year 
//  FROM ".$db_table." WHERE word_word = '".$s2."' ORDER BY word_word, word_year");

	$s2 = $mysqll->mysql_escape_string(trim(stripslashes(strip_tags($s))));
	$collect_search_q = new Query("SELECT word_id, word_word, word_year FROM ".$db_table." WHERE MATCH (word_word) AGAINST ('".$s2."*' IN BOOLEAN MODE) ORDER BY word_word, word_year");
	$collect_search = $collect_search_q->fetchArrays();
	if ($collect_search) {
		$s3 = eregi_replace("%","",$s);
		echo("<p style=\"margin-top: 0px;\">".count($collect_search)." word(s) matched ".$s3.":</p><ul>");
		for ($cc=0;$cc<count($collect_search);$cc++) {
			echo("<li><a href=\".?i=".$collect_search[$cc]['word_id']."\">".$collect_search[$cc]['word_word']."</a>");
			if ($collect_search[$cc]['word_year']) {
				echo(" (".$collect_search[$cc]['word_year'].")");
			}
			echo("</li>");
		}
		echo("</ul><br />");
	} else {
		echo("<p style=\"margin-top: 0px;\">There were no words which matched your request.</p><ul>");
	}
	
} else {
	$collect_all_q = new Query("SELECT word_id, word_word FROM ".$db_table." ORDER BY word_word");
	$collect_all = $collect_all_q->fetchArrays();
	echo("<p style=\"margin-top: 0px;\">There are ".count($collect_all)." words in the dictionary:</p><ul>");
	for ($cc=0;$cc<count($collect_all);$cc++) {
		echo("<li><a href=\".?i=".$collect_all[$cc]['word_id']."\">".$collect_all[$cc]['word_word']."</a></li>");
	}
	echo("</ul><br />");
}

?>	
	</div>

	<div id="source">
<?php
if ($word) {
	if ($mapped_1 == "dare") {
		$sourcer = "Source: Dictionary of American Regional English, Harvard University Press";
	} else {
		$sourcer = bracket_decode($word[0]['word_source']);
	}
	echo($sourcer);
} else if ($c) {
	echo("Click on any word to view its definition.");
} else if ($s) {
	echo("Your search results are above.");
} else {
	echo("This is the complete Track That Word dictionary!");
}

?>
	</div>

<?php

if ($mapped_1) {
	$imagename = $mapped_1;
} else if ($c) {
	$imagename = $c;
} else if ($s) {
	$imagename = "search";
} else {
	$imagename = "opener";
}
echo("<div id=\"picture\"><img src=\"../images/c_".$imagename.".jpg\" width=\"144\" height=\"114\" alt=\"category picture\" border=\"0\" /></div>");



if ($word) {

?>
	<div id="categories">

<?php
if ($mapped_1) {
?>
<table width="134" cellpadding="0" cellspacing="0" border="0">
<tr><td width="18"><a href="<?php echo(".?i=".$cat1_prev[0]['word_id']); ?>" class="butt"><img src="../images/b_prev.gif" width="18" height="16" alt="previous" border="0" class="block" /></a></td><td width="98"><p class="browse"><a href="<?php echo(".?c=".$mapped_1); ?>" class="butt"><?php echo($mappedcat[$mapped_1]['alternative']); ?></a></p></td><td width="18"><a href="<?php echo(".?i=".$cat1_next[0]['word_id']); ?>" class="butt"><img src="../images/b_next.gif" width="18" height="16" alt="next" border="0" class="block" /></a></td></tr>
</table>

<?php
}
if ($mapped_2) {
?>

<table width="134" cellpadding="0" cellspacing="0" border="0" class="linetop">
<tr><td width="18"><a href="<?php echo(".?i=".$cat2_prev[0]['word_id']); ?>" class="butt"><img src="../images/b_prev.gif" width="18" height="16" alt="previous" border="0" class="block" /></a></td><td width="98"><p class="browse"><a href="<?php echo(".?c=".$mapped_2); ?>" class="butt"><?php echo($mappedcat[$mapped_2]['alternative']); ?></a></p></td><td width="18"><a href="<?php echo(".?i=".$cat2_next[0]['word_id']); ?>" class="butt"><img src="../images/b_next.gif" width="18" height="16" alt="next" border="0" class="block" /></a></td></tr>
</table>

<?php
}
if ($mapped_3) {
?>

<table width="134" cellpadding="0" cellspacing="0" border="0" class="linetop">
<tr><td width="18"><a href="<?php echo(".?i=".$cat3_prev[0]['word_id']); ?>" class="butt"><img src="../images/b_prev.gif" width="18" height="16" alt="previous" border="0" class="block" /></a></td><td width="98"><p class="browse"><a href="<?php echo(".?c=".$mapped_3); ?>" class="butt"><?php echo($mappedcat[$mapped_3]['alternative']); ?></a></p></td><td width="18"><a href="<?php echo(".?i=".$cat3_next[0]['word_id']); ?>" class="butt"><img src="../images/b_next.gif" width="18" height="16" alt="next" border="0" class="block" /></a></td></tr>
</table>

<?php
}
?>

	</div>
	
<?php

}

?>

	<div id="controls">
	
<form action="<?php echo($PHP_SELF); ?>" method="post">
	<table cellpadding="0" cellspacing="0" border="0">
		<tr><td>
		<select name="c">
<option value="">New Category</option>
<?php
for ($bb=0;$bb<count($category);$bb++) {
	echo("<option value=\"".$c_map[$bb]."\">".$mappedcat[$c_map[$bb]]['alternative']."</option>");
}
?>	
		</select>
		</td><td>
		 <input type="image" src="../images/b_go.gif" alt="go" class="image" />
		 </tr>
	 </table>
</form>
<form action="<?php echo($PHP_SELF); ?>" method="post" class="line">
	<table cellpadding="0" cellspacing="0" border="0">
		<tr><td>
		<input type="text" value=" search" name="s" class="search" />
		</td><td>
		 <input type="image" src="../images/b_go.gif" alt="go" class="image" />
		 </tr>
	 </table>
</form>
<table width="134" cellpadding="0" cellspacing="0" border="0">
<tr><td width="18">
<?php
if ($word) {
	echo("<a href=\".?i=".$all_prev[0]['word_id']." class=\"butt\"><img src=\"../images/b_prev.gif\" width=\"18\" height=\"16\" alt=\"previous\" border=\"0\" class=\"block\" /></a>");
}
?>
</td><td width="98"><p class="browse"><a href="." class="butt">&nbsp;Browse All&nbsp;</a></p></td><td width="18">
<?php
if ($word) {
	echo("<a href=\".?i=".$all_next[0]['word_id']." class=\"butt\"><img src=\"../images/b_next.gif\" width=\"18\" height=\"16\" alt=\"next\" border=\"0\" class=\"block\" /></a>");
}
?>
</td></tr>
</table>
	</div>
	
	<div id="back">
<script language="javascript">
<!--
if (history.length > 1) {
	document.write('<a href="javascript:history.back()">back</a>');
}
-->
</script>	
	</div>

	
</div>
	
</body>

</html>

<?php

$dweeb = $mysqll->close();

?>
