<?

if (preg_match('%robotplanet%i', $_SERVER['SERVER_NAME']))
{
	header("Location: http://www.bogstavsuppe.dk/");
	exit;
}

if (!preg_match('%bogstavsuppe%i', $_SERVER['SERVER_NAME']))
{
	include 'wrong_domain/index.php';
	exit;
}

include 'init.inc';

mysql_select_db('calendar', $GLOBALS['db']);
$html = '';

$feed_id = (int)$_REQUEST['id'];
$topic = $_REQUEST['topic'];
$reloadurl = '/?id='.$feed_id;

if ($feed_id==0)
{
	$feed_id = 1;
	$reloadurl = '/';
}

// get language for page title
$q = mysql_query("SELECT language FROM rss_feeds WHERE id=".$feed_id, $GLOBALS['db']);
$r = mysql_fetch_assoc($q);
$lang = $r['language'];
switch (strtolower($lang))
{
case 'da':
	$title_short = "Bogstavsuppe";
	$title_long = "Bogstavsuppe &amp; Ordsalat";
	$twitter_tag = '#bogstavsuppe';
	break;
case 'de':
	$title_short = "Buchstabensuppe";
	$title_long = "Buchstabensuppe &amp; Wortsalat";
	$twitter_tag = '#buchstabensuppe';
	break;
default:
	$title_short = "Alphabet Soup";
	$title_long = "Alphabet Soup &amp; Word Salad";
	$twitter_tag = '#wordsalad';
	break;
}

$where = '';
$err_no_entries = "This ID does not contain any data";
if ($topic!='')
{
	$where = " AND MATCH(searchable) AGAINST('".mysql_real_escape_string($topic)."')";
	$err_no_entries = "No data found with the specified keyword(s)";
}

$sql = "SELECT title FROM rss_feed_entries e WHERE e.rss_id=".$feed_id.$where." ORDER BY RAND() LIMIT 36";
$q = mysql_query($sql, $GLOBALS['db']);
$count = 0;
while (($r1 = mysql_fetch_assoc($q)) && ($r2 = mysql_fetch_assoc($q)))
{
	$html .= "<div class=\"outer\"><div class=\"inner\" onclick=\"clicked(".$count.")\"><h2>";
	$text = break_at_word($r1['title'], $r2['title'], $wordcount);
	$html .= $text;
	$urlenc = urlencode('"'.$text.'"');		// also add quotes

	if ($twitter_tag[0]=='@')
	{
		$urlwithtag = str_replace('#', '%23', $twitter_tag).'%20'.$urlenc;
	} else
	{
		$urlwithtag = $urlenc.'%20'.str_replace('#', '%23', $twitter_tag);
	}

	if (strlen($urlwithtag)<140)
	{
		$urlenc = $urlwithtag;
	}
	$html .= "</h2><div class=\"buttons\" id=\"buttons".$count."\"><a href=\"https://twitter.com/intent/tweet?text=".$urlenc."\"><img src=\"design/icon_twitter.png\" alt=\"\"></img></a></div></div></div>";
	$count++;
}
$html .= "\n";
if ($count==0)	$html .= 	"<div class=\"outer\"><div class=\"inner\"><h2>".$err_no_entries."</h2></div></div>";



function grab_beginning($title)
{
	$result = '';
	$words = explode(' ', trim($title));
	$cnt = max(1, count($words)/2 + mt_rand(-1, 1));
	for ($z=0; $z<$cnt; $z++)
	{
		$result .= $comma.$words[$z];
		$comma = ' ';
	}
	return $result;
}

function grab_ending($title)
{
	$result = '';
	$words = explode(' ', trim($title));
	$cnt = max(1, count($words)/2);
	for ($z=$cnt; $z<count($words); $z++)
	{
		$result .= $comma.$words[$z];
		$comma = ' ';
	}
	return $result;
}

function break_at_word($string1, $string2, $wordcount)
{
	$output = '';
	$comma = '';
	$position1 = find_place_to_break($string1);
	$position2 = find_place_to_break($string2);
	$words1 = explode(' ', trim($string1));
	$words2 = explode(' ', trim($string2));
	for ($a=0; $a<$position1; $a++)
	{
		$output .= $comma.$words1[$a];
		$comma = ' ';
	}
	if (isset($_GET['debug']))
	{
		$comma .= '°';
	}
	for ($a=$position2; $a<count($words2); $a++)
	{
		$output .= $comma.$words2[$a];
		$comma = ' ';
	}
	return $output;
}

function find_place_to_break($sentence)
{
	$words = explode(' ', trim($sentence));
	$weights = array(' '=>0.3, ','=>0.6, '.'=>1.0, ':'=>1.0, '?'=>1.0, '!'=>1.0, '-'=>0.6, ';'=>0.6);
	$bestpos = -1;
	$bestvalue = 0;
	for ($z=0; $z<count($words); $z++)
	{
		$weight = 0.6-cos($z/count($words)*3.141593*2)/2 + mt_rand(0,100)/2000;
		$splitletter = $words[$z][strlen($words[$z])-1];
		$letterweight = 0.5 + $weights[$splitletter];
		$output .= $words[$z].' '.round($weight*100).'% * '.$splitletter.'='.round($letterweight*100).'%<br>';
		$score = $weight * $letterweight;
		if ($score>$bestvalue)
		{
			$bestvalue = $score;
			$bestpos = $z;
		}
	}
	return $bestpos;
}

?>
<!DOCTYPE HTML>
<html>
<head>
 <title><? echo $title_long; ?></title>
 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
 <link rel="stylesheet" type="text/css" href="design/style.css">
</head>
<body>

<a href="<? echo $reloadurl; ?>" title="click to reload"><h1><? echo $title_short; ?></h1></a>

<? echo $html; ?>

<!--  Nåja, mere behøver der vel ikke at være?  -->

</body>
</html>
