var mobile = function(){

	//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;
  
  //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;
    	}
    	
    }
    

	return is_mobile;    
};

$j = jQuery.noConflict();
$j(document).ready(function() {
	var max_width = 785;
	var window_width = $j(window).width();

	if(is_mobile || window_width <= max_width) {
		$j('.thumbnails img').each(function() {
			$j(this).attr('src', $j(this).parent().attr('href'));
		});
	}
	
	$j(window).resize(function() {
		window_width = $j(window).width();
		if(window_width <= max_width) {
			$j('.thumbnails img').each(function() {
				$j(this).attr('src', $j(this).parent().attr('href'));
			});
		}
		else {
			$j('.thumbnails img').each(function() {
				$j(this).attr('src', $j(this).attr('data-thumb'));
			});
		}
	});
});
