/* m.vw.com Redirect
The purpose of this script is to redirect any visitors from vw.com to m.vw.com if they are using a mobile device.
Tablets are not considered mobile devices and should be handled by vw.com
There are 3 exceptions where users should not be redirected regardless of device that are outlined directly below
*/
(function(){

  //Only execute on the following domains (prevents this script from redirecting users from web pages on web.vw.com that can handle mobile devices)
  //Do not use http:// and a version of a domain with and without www must be listed here
  var included_domains = [
  	'vw.com',
  	'www.vw.com'
  ];

	//URLs including domain name that should not redirect mobile visitors (do not include www, it will be automatically tested for)
	var excluded_urls = [
		'http://web.vw.com/golf-r-drivers-forever?vanity=%3Cwww.vw.com/2012golfr%3E'
	];
	
	//URL variable to detect for that will override any redirect behavior in this script
	var mobile_redirect_override_string = 'nomobile=1';
	
	//Test to make sure we're on a domain where we redirect mobile visitors
  var included_domain = false;
  for(var i=0;i<included_domains.length;i++) {
		if(included_domains[i] == document.domain) {
			included_domain = true;
			break;
		}
  }
	
	//Test for excluded urls defined above
	var excluded_url = false;
	for(var i=0;i<excluded_urls.length;i++) {
		if(document.URL.indexOf(excluded_urls[i]) != -1) {
			excluded_url = true;
			break;
		}
	}

	//Test for mobile redirect override string defined above
	var mobile_override = false;
	if(document.URL.indexOf(mobile_redirect_override_string) != -1) {
		mobile_override = true;
	} 

	//Stop execution
  if (!included_domain || excluded_url || mobile_override) {
    return;
  }

	//Test to determine if a user is on a mobile device because they should now be redirected
  var user_agent = navigator.userAgent;
  var is_mobile = false;
  var new_url = 'http://m.vw.com/';
  
  //Original User agents to match
  var agents = [
      'iPhone',
      'iPod',
      'BlackBerry\s*[89]',
      ' P(re|ixi)\'',
      'Nexus One',
      ' DroidX? ',
      'dream',
      ' G1 ',
      'myTouch',
      'HTC[_ ]Magic',
      'Google Ion',
      'ADR6200',
      'Desire',
      'ADR6300',
      'Incredible',
      'Hero',
      'A6277',
      'G2[ _]Touch',
      'VX(11000|9200)',
      'MB([23]00|501)',
      'SPH-M900',
      'APA9292',
      'PC36100',
      'Ally',
      'HD2',
      'NokiaE(7[123]|63)',
      'Nokia(5[25]30|5800)',
      'SCP-?6760',
      'SCH-?U960',
      'samu960',
      'Behold\s*(2|II)',
      'calgary|devour|eris|legend',
      'A6366',
      'Galaxy|GT-I(5700|9000)',
      'PalmCentro|Palm-D062',
      'SonyEricssonX10|x10a',
      'Touch[ _]Pro',
      'PPC6850SP',
      'HTC6875'
    ];
    
    //OS level agents rather than device
    //These are stored in a separate array since we want to do a case sensitive lookup to avoid false positives
    //Android is left out since we need to deal with Android tablets
    var mobile_os_agents = [
      'iPhone',
      'BlackBerry',
      'Windows Phone OS 7',
      'Symbian',
      'webOS'
    ];
    
    //Combine all of our regex patterns to a single regex for speed
    var agent_test = new RegExp(agents.join('|'), 'ig'); 
    var mobile_os_agent_test = new RegExp(mobile_os_agents.join('|'), 'g'); 
    
    //Test for mobile OS
    if (agent_test.test(user_agent) || mobile_os_agent_test.test(user_agent)){
    	is_mobile = true;
    }
    
    //Tests for android. These are separate due to having to exclude android tablets
    if(user_agent.match(/Android/)) {
    	is_mobile = true;
    
    	//Any honeycomb devices are not considered mobile
    	if(user_agent.match(/android 3/i) || user_agent.match(/honeycomb/i)) {
    		is_mobile = false;
    	}
    
    	//All pre honeycomb android tablets
    	var android_tablets = [
    		'Advent|Vega', //Advent Vega
				'archos', //Archos all tablets
				'augen', //AUGEN Electronics
				'camangi', //Camangi
				'csl', //CSL
				'cherry', //Cherry Mobile
				'coby|kyros', //Coby Kyros
				'dell streak', //Dell Dell Streak
				'entourage', //Entourage eDGe
				'hardkernel|odroid-t', //Hardkernel ODROID-T
				'maylong', //Maylong M-150
				'maipad|mx005', //Maipad MX005
				'nationite|midnite', //Nationite MIDnite
				'notion|adam', //Notion Ink Adam
				'smart devices|smartq', //Smart Devices SmartQ-V5 SmartQ-V7SmartQ-T7
				'1&1|smartpad', //1&1 SmartPad
				'velocity|cruz', //Velocity Micro Cruz
				'dawa|d7', //Dawa D7
				'viewsonic', //ViewSonic GTablet ViewPad
				'sch-i800|sgh-t849|gt-p1000|sgh-t849|shw-m180S|galaxy_tab|galaxy tab', //Samsung Galaxy Tab
				'folio', //Toshiba Folio 100
				'zte', //ZTE Light
    	];
    	
    	//Test for any pre honeycomb android tablets
    	var android_tablet_test = new RegExp(android_tablets.join('|'), 'ig');
    	if(android_tablet_test.test(user_agent)) {
    		is_mobile = false;
    	}
    	
    }
    
    //Redirect if necessary
    if(is_mobile) {
    	window.location = new_url;
    }
    
})();
