/*jslint undef: true, widget: true, browser: true, onevar: true, eqeqeq: true */
var BrowserDetect = {
  is_touch: false,
  is_rotate: false,
  is_tablet: false,
  is_smartphone: false,
  is_portable: false,
  init: function () {
   this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
   this.version = this.searchVersion( navigator.userAgent ) || this.searchVersion( navigator.appVersion ) || 0;
   this.OS      = this.searchString(this.dataOS) || "Unknown OS";
   this.getDeviceCategory();
  },
  getDeviceCategory: function() {
    var screen_width, screen_height, is_small, test_width = 700;

    screen_width  = screen.width;
    screen_height = screen.height;

    if( window.devicePixelRatio && window.devicePixelRatio > 1 ) {
      test_width *= window.devicePixelRatio;
    }

    is_small = ( screen_width < test_width || screen_height < test_width );

    // If the screen orientation is defined we are in a modern mobile OS
    this.is_rotate = typeof window.orientation !== 'undefined' ? true : false;
    // If touch events are defined we are in a modern touch screen OS
    this.is_touch = ('ontouchstart' in document.documentElement) ? true : false;

    this.is_portable = this.is_touch && this.is_rotate;

    this.is_smartphone = this.is_portable && is_small;
    this.is_tablet     = !this.is_smartphone && this.is_portable;
  },
  searchString: function( data ) {
    var i, dataString, dataProp,
        dataLength = data.length;

    for( i=0; i<dataLength; i++ ) {
      dataString = data[i].string;
      dataProp = data[i].prop;
      this.versionSearchString = data[i].versionSearch || data[i].identity;
      if( dataString ) {
        if( dataString.indexOf( data[i].subString ) !== -1 ) {
          return data[i].identity;
        }
      } else if( dataProp ) {
        return data[i].identity;
      }
    }
  },
  searchVersion: function (dataString) {
   var index = dataString.indexOf(this.versionSearchString);
   if( index === -1 ) {
     return;
   }
   return parseFloat( dataString.substring( index + this.versionSearchString.length + 1 ) );
  },
  dataBrowser: [
   { string: navigator.userAgent, subString: "Firefox", identity: "iw_ff", versionSearch: "Firefox" },
   { string: navigator.userAgent, subString: "Chrome", identity: "iw_chrome" },
   { string: navigator.userAgent, subString: "MSIE", identity: "iw_ie", versionSearch: "MSIE" },
   { string: navigator.platform, subString: "iPad", identity: "iw_ipad", versionSearch: "Version" },
   { string: navigator.platform, subString: "iPhone", identity: "iw_iphone", versionSearch: "Version" },
   { string: navigator.vendor, subString: "Apple", identity: "iw_safari", versionSearch: "Version" },
   { string: navigator.userAgent, subString: "OmniWeb", versionSearch: "OmniWeb/", identity: "iw_omni" },
   { prop: window.opera, identity: "iw_opera" },
   { string: navigator.vendor, subString: "iCab", identity: "iw_icab" },
   { string: navigator.vendor, subString: "KDE", identity: "iw_konquerer" },
   { string: navigator.vendor, subString: "Camino", identity: "iw_camino" },
   { string: navigator.userAgent, subString: "Netscape", identity: "iw_netscape" },
   { string: navigator.userAgent, subString: "Gecko", identity: "iw_moz", versionSearch: "rv" },
   { string: navigator.userAgent, subString: "Mozilla", identity: "iw_netscape", versionSearch: "Mozilla" }
  ],
  dataOS : [
   { string: navigator.platform, subString: "Win", identity: "Windows" },
   { string: navigator.platform, subString: "Mac", identity: "Mac" },
   { string: navigator.userAgent, subString: "iPhone", identity: "iPhone/iPod" },
   { string: navigator.platform, subString: "Linux", identity: "Linux" }
   ]
};

BrowserDetect.init();

var iw_family_input = document.getElementById( 'browser_family' );

if( iw_family_input ) {
  iw_family_input.value = BrowserDetect.browser;
  document.getElementById( 'browser_version' ).value = Math.floor( BrowserDetect.version );
  document.getElementById( 'browser_is_phone' ).value = ( BrowserDetect.is_smartphone ) ? 1 : 0;
  document.getElementById( 'browser_is_tablet' ).value = ( BrowserDetect.is_tablet ) ? 1 : 0;
}

