<?php
function truncString($string, $length){ //truncates a string and adds an elipsis ...
	$end=strrpos(substr($string, 0, $length), " ");
	return substr($string, 0, $end)."...";
}

function topsellers($node){ //creates a link to a random topselling amazon book in specified node (homeschool is 69844)
	define("PVTKEY","wnQzJ0gPNLjDc49AWOu+m1mkEZE/B35oXsV1Zxg7");
	$request = 'Service=AWSECommerceService&'.
	'AWSAccessKeyId=0FY14YXN76X2KKF2F0G2&'.
	'Timestamp='.gmdate("Y-m-d\TH:i:s\Z").'&'.
	'Operation=BrowseNodeLookup&BrowseNodeId='.$node.'&ResponseGroup=TopSellers';
    $request = str_replace(',','%2C', $request);
    $request = str_replace(':','%3A', $request);
    $reqarr = explode('&',$request);
    sort($reqarr);
    $string_to_sign = implode("&", $reqarr);
    $string_to_sign = "GET\nwebservices.amazon.com\n/onca/xml\n".$string_to_sign;
    $signature = urlencode(base64_encode(hash_hmac("sha256", $string_to_sign, PVTKEY, True)));
    $request .= '&Signature='.$signature;
    $request = 'http://webservices.amazon.com/onca/xml?'.$request;
    $response = file_get_contents($request);

    if ($response){$simple_response = simplexml_load_string($response);}
	foreach($simple_response->BrowseNodes->BrowseNode->TopSellers->TopSeller as $t){
		$tASIN=$t->ASIN;
		$tTitle=$t->Title;
		settype($tASIN, "string");
		settype($tTitle, "string");
		$topList[$tASIN]=$tTitle;
	}
	$randItem=array_rand($topList);
	$asin=$randItem;
	$title=$topList[$randItem];
	$titleURL=htmlentities($title, ENT_QUOTES);
	$titleTrunc=truncString($title, '40');
	return "<a href='http://www.amazon.com/exec/obidos/ASIN/".$asin."/ref=nosim/bookspree2-20' target='_parent' title='".$titleURL."'>".$titleTrunc."</a>";
}

//widget code
echo "document.write(\"<ul>";
//define the target window for the link to open
if ($_GET['target']){$target=$_GET['target'];}else{$target = "_self";} //default is _self
if ($_GET['num']){$num=$_GET['num'];}else{$num = 6;}
$request="http://homeschoolbuzz.com/atom2.xml";
	$response = file_get_contents($request);
	$parsed_xml = simplexml_load_string($response);
	settype($parsed_xml, "array");

$count = 0;
foreach($parsed_xml['entry'] as $entry){
	if ($count < $num) { //when count reaches $num, stop
		settype($entry, "array");
		$attr=$entry['link']['4'];
		settype($attr, "array");
		$title=$attr['@attributes']['title'];
		$href=$attr['@attributes']['href'];
		//print $title.": ".$href."\n";
		echo "<li><a href='$href' target='$target'>$title</a></li>";
		++$count;
	}
}
/*
$news_ad = "<p>Compare prices: <a href=\'http://homeschoolbuzz.com/books.html?k=Saxon+Homeschool\'>Saxon Homeschool</a> at HomeSchoolBuzz.com</p>";
$book_ad = "<p>Compare prices: <a href=\'http://homeschoolbuzz.com/books.html?k=%22Apologia%22+Wile&v=Apologia+Science&c1=seealso/science.php\'>Apologia</a> at HomeSchoolBuzz.com</p>"; 
//alternate ads every 30 minutes
$localtime = localtime();
if ($localtime[1] >= 30) {
	$ad = "$news_ad";
}else{$ad = "$book_ad";}
*/
$ad=topsellers('69844');
echo "</ul><p>$ad</p>\")";
/* experimental to be used for impression data
foreach ($_SERVER as $key => $value) {
    $serv .= "$key: $value\n";
}
mail('webmaster@homeschoolbuzz.com', $_SERVER['HTTP_REFERER'], $serv);
*/
?>


