/*jshint devel:true */

util.die.active = true

if( 'undefined'==typeof(bowser) ) {
  bowser = {}
}


var logerror = function() {
  var args = Array.prototype.slice.call(arguments)
  try {
    var desc = JSON.stringify({
      when:new Date().toISOString(),
      url:window.location.href,
      dev:navigator.device,
      app:'object'==typeof(app)&&app&&app.dump&&app.dump(),
      args:args
    });
    new Image().src = '/api/log/error?desc='+encodeURIComponent(desc)
  }
  catch( e ) {
    'undefined'!=typeof(console) && console && console.log( e ) && console.log( args )
  }
}


function recordevent(strdesc) {
  if( !recordevent.active ) return;
  recordevent.log.push((new Date().getTime()%10000000)+': '+strdesc)
}
recordevent.log = []
recordevent.active = false


var text = {
  password_mismatch:  'Your passwords do not match.',
  already_registered: 'Email address already registered.',
  enter_all_fields:   'Please enter values for all form fields.',
  login_failed:       'Unable to login.',
  save_failed:        'Unable to save.',
  nothingfound:       'No matching articles found.',
  invalidemail:       'Please enter a valid email address.',
  account_updated:    'Your account has been updated.',
  register_success:   'Your account has been registered.',
  login_success:      'Login successful.',
  logout_success:     'Logout successful.',
  results_found:      ' results found.',
  purchase_cancelled: 'Purchase cancelled. Your credit card was declined.',
  purchase_card_used: 'Purchase cancelled. The credit card entered is already in use by a different account.',
  remindersent:       'An email has been sent to you containing a link to update your password.',
  unknownemail:       'Your email address is not recognized. Please check that you have entered it correctly.',
  reset_password:     'Please enter a new password for your account.',
  enteremail:         'Please enter and save your email address.',
  maxinstalls:        'Oops! It looks like you have exceeded the number of devices (3) allowed per subscription. To enable another device, please contact <a href="mailto:helpdesk@businesspost.ie">helpdesk@businesspost.ie</a>.'
}


var addthis_share = {}

var loghist = []
function log(e) {
  var args = Array.prototype.slice.call(arguments)
  loghist.push(args)
  'undefined'!=typeof(console) && console && console.log( args )
  if( 'error' == args[0] ) {
    logerror(args)
  }
}

var __log__ = log

var C = {
  len: 20,
  //host: '127.0.0.1',
  //host: '192.168.100.111',
  host: 'www.businesspost.ie',

  // update to clear cache
  version:'0.9.5',

  forceusehost: false,
  dev:false,
  always_addtohome:false,
  always_swipemsg:false,
  numeditions:53,
  mobnumeditions:12,
  markdown:'client',
  enablecache:true,
  num_top_art:1,
  payhost:'payments.worldnettps.com',
  editionstart:0,
  cacheinterval:5*60*1000,
  chartaca_endpoint:'count.chartaca.com',
  defaultdomain: 'www.businesspost.ie',

  // AD REMOVE
  mobheightdeduct:99
  //mobheightdeduct:49
}
C.len2 = 2 * C.len
C.len3 = 3 * C.len

var domain = document.location.host || document.location.hostname || C.host
C.preview = 'preview.thepost.ie'==domain

if( -1!=domain.indexOf('127.0.0.1') || 0===domain.indexOf('192.168.') || -1!=domain.indexOf('.test') ) {
  C.dev = true;
}

if( C.dev ) {
  C.token_name = 'dev-sbp'
  C.payhost = 'test'+C.payhost
  C.editionstart = -1 
  C.chartaca_endpoint='chartaca.test/count'
}
else {
  if( C.preview ) {
    C.token_name = 'preview-sbp'
    C.payhost = 'test'+C.payhost
    C.editionstart = -1
  }
  else if( 0 === domain.indexOf('192.') ) {
    C.token_name = 'dev-sbp'
    C.payhost = 'test'+C.payhost
  }
  else {
    C.token_name = 'live-sbp'
  }
}

if( !C.dev ) {
  window.onerror = logerror
}

if( 'undefined'!=typeof(bowser) ) {
  if( bowser.msie && bowser.version <= 8 ) {
    //C.enablecache = false
  }
}

util.enablecache = C.enablecache


var env = {
  w: window,
  d: document
}

var mode = {
  touch: 'ontouchend' in env.d || false
}


C.tap = 'ontouchend' in env.d ? 'tap' : 'click'
C.quicktap = 'ontouchstart' in env.d ? 'touchstart' : 'click'


if( /iPad|iPhone|Android/.exec(navigator.userAgent) ) {
  mode.touch = true
}

C.SMALL = 1
C.LARGE = 2


var models = {}
var views = {}


var spin_opts = {
  lines: 16, // The number of lines to draw
  length: 0, // The length of each line
  width: 10, // The line thickness
  radius: 40, // The radius of the inner circle
  color: '#00864c', // #rbg or #rrggbb
  speed: 0.8, // Rounds per second
  trail: 23, // Afterglow percentage
  shadow: true // Whether to render a shadow
};
