<?

  ####################
  # 04022012    BHT     add geoip country logic


  ##############
  ##############
  # handle click on background

  # we need to get a random active background entry
  # but we have to match the page we're on to the do_where constraint
  # if this is the homepage that can be 1 or 3
  # on any other page it is 1 or 2

  # any page
  $match = " and ((do_where='1') or (do_where='2')) ";

  # adjust if this is a home page
  $cval = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
  if ( $cval == $this->{'System/BaseURL'} ) {
     # this is homepage
     $match = " and ((do_where='1') or (do_where='3')) ";
     }


  # we want to extend match by checking the country via geoip
  require_once "Net/GeoIP.php";

  $geoip = Net_GeoIP::getInstance("/var/www/html/recipebridge.com/html/geo/GeoIP.dat");

try {
    $cc =  $geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']);
    #echo $geoip->lookupCountryCode('71.0.170.218');
} catch (Exception $e) {
    // Handle exception
}

  # do we have some defined country?
  if ($cc != '') {
     # apply it to the match
     $match = $match." and ((country='ANY') or (country='".$cc."')) ";
     }

  $Query = " SELECT * FROM backgrounds where status='1' $match order by rand() limit 1;";
#echo '<br>Q:'.$Query;
  $rbg = $DatabaseObj->fetchAll($Query);

  foreach ($rbg as $Result) {

     #################
     # we need to deal with the new background options
     # if they are set we will apply them
     if ($Result->do_background_color != '-1') {
        # we have some background color
        $the_color = $Result->do_background_color;
        # background color -- this is a css modification
?>

<style type="text/css">
.page { background-color: <?=$the_color?>; }
html { background-color: <?=$the_color?>; }
</style>

<?
        } # end background options color

     # TOP LEFT and POSITION
        $position = $Result->do_position;

        $top = $Result->do_top;
        # we have to modify the top based on the home page or other page
        # what is our page?



#echo "<br><br>".$this->{'System/BaseURL'}."<br><br>";
#print_r($_SERVER);

        # adjust positon if FIXED to offset header (and zero top)
        $cval = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

        if ($position == 'absolute')
        if ( $cval == $this->{'System/BaseURL'} ) {
           $top =  44;
        } else {
           $top = 111;
        }

        $left = $Result->do_left;

?>
<style type="text/css">
.bg-body { top: <?=$top?>px;
           left: <?=$left?>px;
           position: <?=$position?>;
}
</style>
<?


    # WIDTH -- if zero do not set!
        $width = $Result->do_width;


        if ($width != '0') {
           # we have some set width
?>
<style type="text/css">
.bg-body { width: <?=$width?>%;
}
</style>
<?
           } # end width set


     if ($Result->new_window == '0') {
        $nw = '';
     } else {
        $nw = ' target="_blank" ';
     }

     $t_file = '/var/www/html/recipebridge.com/html/backgrounds/'.$Result->file_name;
     $t_url = $this->{'System/BaseURL'}.'backgrounds/'.$Result->file_name;
     if (file_exists($t_file)) {
        # yes show it
        # we are showing the image directly...and activating the click logic
        #echo '<a '.$nw.' href="'.$Result->url.'"> ';
        echo '<img id="bgbody" class="bg-body" src="'.$t_url.'" alt="image description" />';

 
        ############
        # script to activate click
?>
<script language="javascript" type="text/javascript">
$(document).ready(function() {

function getEventTarget(e) {
  var targ;
  if (!e) var e = window.event;
  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;
  if (targ.nodeType == 3) // defeat Safari bug
    targ = targ.parentNode;
  return targ;
}


  $(document).click( function (e) {
    var target = getEventTarget(e);
var bg = 'NO';

    if (target.id == 'header') {
       bg = 'YES';
       }
    if (target.id == 'main') {
       bg = 'YES';
       }
    if (target.id == 'wrapper') {
       bg = 'YES';
       }
     if (target.id == 'bgbody') {
       bg = 'YES';
       }
//alert(target.id);
    if (bg == "YES") {
      <?
        ### !!! we have been clicked...
        # do we open in a new window?
        if ($nw != '') {
           # yes!...
      ?>
          window.open('<?=$Result->url;?>','_blank');
      <?
        } else {
          # no not a new window
      ?>
         top.location.href='<?=$Result->url;?>';
      <?
        } # end else
      ?>
      //alert('background clicked');
    }
  });


});
</script>
<?


        # end script
        ############


     } else {
        # no such background file?

        # use default
        echo '<img id="bgbody" class="bg-body" src="images/bg-img02.jpg" alt="image description" />';
     }

   } # end foreach (should be only 1)


?>