/*

  ねこすけ LPO

  http://www.necosuke.jp/

*/
<?php

if(!$this->acct) {
  return;
}

$landing = array();
$conversion = array();

foreach(
  $this->acct->findDependentRowset(
    'Lpo',
    null,
    $this->acct
      ->getTable()
      ->select()
      ->where('status = ?', 'active')
      ->order('name')
  ) as $val
) {
  if($val->landing_url && ($val->landing_url != 'http://')) {
    $urls = parse_url($val->landing_url);
    if(array_key_exists('path', $urls)) {
      $landing[$val->landing_url] = null;
    } else {
      $landing[$val->landing_url . '/'] = null;
    }
  }

  if($val->conversion_url && ($val->conversion_url != 'http://')) {
    $urls = parse_url($val->conversion_url);
    if(array_key_exists('path', $urls)) {
      $conversion[$val->conversion_url] = null;
    } else {
      $conversion[$val->conversion_url . '/'] = null;
    }
  }
}

if(count($landing) == 0 && count($conversion) == 0) return;

include('../html/pub/jquery.js');

?>

jQuery.noConflict()(document).ready(function($) {
  var base_url = '<?php echo $this->base_url; ?>';

  var m =new RegExp(
    '^' + base_url + 'index/lpo-content-preview/id/(\\d+)',
    'i'
  ).exec(document.referrer);

  if(m != null) {
    $.ajax({
      dataType: 'jsonp',
      data: {},
      cache: false,
      url: base_url + 'lpo/content/id/' + m[1],
      success: function(data) {
        $.each(data, function() {
          $(this.selector).html(this.content);
        });
      }
    });

    return;
  }

  switch(document.location.href.split('?').shift()) {
<?php foreach($landing as $key => $val): ?>
    case '<?php echo $key; ?>':
      break;
<?php endforeach; ?>
<?php foreach($conversion as $key => $val): ?>
    case '<?php echo $key; ?>':
      break;
<?php endforeach; ?>
    default:
      switch(document.location.href) {
<?php foreach($landing as $key => $val): ?>
      case '<?php echo $key; ?>':
        break;
<?php endforeach; ?>
<?php foreach($conversion as $key => $val): ?>
      case '<?php echo $key; ?>':
        break;
<?php endforeach; ?>
      default:
        return;
      }
  }

  $.ajax({
    dataType: 'jsonp',
    data: {
      url: document.location,
      ref: document.referrer
    },
    cache: false,
    url: base_url + 'lpo/log/id/<?php echo $this->acct->hash; ?>',
    success: function(data) {
      $.each(data, function() {
        $(this.selector).html(this.content);
      });
    }
  });
});
