#!/usr/bin/perl
require "weblib.lib";
use List::Util 'shuffle';
&getparams(0,"float");
$param{float}="none" if $param{float} eq "-1";
$param{float}="none" unless $param{float};
if($param{asin}){push(@ASIN,$param{asin});}
else{
	open(ASIN,"/home/todd/private/AmazonASINs");
	while(<ASIN>){
		chop;
		next if /#/ && $param{html} ne "all";
		($ASIN,$title,$link,$image)=split(/%%/);
		push(@ASIN,$ASIN);
		$TITLE{$ASIN}=$title;
		$LINK{$ASIN}=$link;
		$IMAGE{$ASIN}=$image;
	}
	close ASIN;
	@ASIN=shuffle(@ASIN) unless $param{html} eq "all";
}
if($param{html}){
	$style=<<EOF;
style=div.item {font: 8pt/9pt Verdana; display: inline-block; text-align: center; float: left;}
div.collection {display: inline-block; border: 5px ridge grey; margin: 5px; padding: 5px;}
iframe {width:122px; height:122px; overflow:hidden; margin:0px; border:0px; float:$param{float};}
EOF
	&htmlopen("Amazon $param{html}|",$style);
	if($param{html} eq "all"){
		print qq{<div style="display: inline-block;"><h1>Amazon Todd Rundgren Products</h1>};
		print "This page is currently going through a major overhaul. Thanks for your patience<hr>\n";
		print "Purchasing these products through these links (will eventually) help the TR Connection\n";
		foreach $ASIN(@ASIN){
			if($ASIN=~s/#//){
				print qq{</div><div class="collection"><h2>$ASIN</h2>\n};
				next;
			}
			print qq{<div class="item"><a href="https://www.amazon.com/dp/$ASIN">$ASIN</a><br>};
			if($TITLE{$ASIN}){print qq{<div style="height: 3em; width: 122px;">$TITLE{$ASIN}</div>};}
			else{print qq{Working on<br>getting the<br>title here<br>};}
			print &AmazonAd($ASIN,$param{float});
			print "</div>\n";
		}
		print "</div>\n";
	}
	else{
		print qq{<script src="$weblib::URL/$param{float}"></script>\n};
	}
	&htmlclose;
}
else{
	print "Content-type: text/javascript\n\n";
	print qq{document.write("},&AmazonAd(shift(@ASIN),$param{float}),qq{");};
}
sub AmazonAd{
	local($ASIN,$position)=@_;
	if($param{html}){
		if(-f "/home/todd/www/pictures/covers/$IMAGE{$ASIN}"){
			return qq{<div class="item" style="border: 1px solid blue; width: 124px; height: 122px;"><img src="http://trconnection.com/pictures/covers/$IMAGE{$ASIN}" style="max-width: 122px;"></div>};
		}
		else{
			return qq{<iframe src='http://trconnection.com/coming.html' frameborder='0'></iframe>};
		}
	}
else{
	return qq{<iframe src='https://www.amazon.com/dp/$ASIN?t=thetoddrundgrenc' style='width:122px;height:242px;overflow:hidden;margin:0px;border:0px;float:$position;' frameborder='0'></iframe>};
	}
}
