/*

Mobile Detection Script
written by Matt Johnston, matt@mcqdigital.com


This is a basic script that checks the browser user agent to determine if it is a mobile client.
If the client is determined to be a mobile browser, either a confirmation pop-up or a pre-determined
div is displayed, depending on the options used for the script.


Script Options:

div_id = If parameter is present, the script will display the div with this id after a positive 
detection.

alert_text = If no div_id is present, the confirmation box will display this text along with 
"Cancel" and "Ok" buttons. Clicking the "Ok" button will cause the page to be redirected to the url
in the "redirect_url" paramter.

redirect_url = Website url to redirect to after a user clicks the "Ok" button on the pop-up confirmation
box.


examples: 

This will check for mobile browsers and display the <div id="mobilealert"></div> after a 
positive result.

mobileDetection({
	"div_id":"mobilealert"
	});


	
This will check for mobile browsers and display a confirmation pop up box with the 
text "Please visit our mobile site." If the user clicks the "Ok" button on the confirmation box,
the web browser will be redirected to http://www.google.com.


mobileDetection({
	"redirect_url":"http://www.google.com",
	"alert_text":"Please visit our mobile site."
	});

*/

function mobileDetection(opts) {
	
	/*
	Create an array of regular expresions to check against the user agent string.
	
	Mobile browser user agent information is taken from the website:
	http://www.zytrax.com/tech/web/mobile_ids.html
	
	*/
	var devices = ["iphone","android","windows ce","blackberry","^acs-", "^alav", "^alca", "^amoi", 
		"^benq", "^bird", "^blaz", "^brew", "^cell", "^cldc", "^cmd-", "^audi", "^aste", "^avan",
        "^dang", "^doco", "^eric", "^hipt", "^inno", "^ipaq", "^java", "^jigs", "^kddi", "^keji",
        "^leno", "^lg-c", "^lg-d", "^lg-g", "^lge-", "^maui", "^maxo", "^midp", "symbian",
        "^mits", "^mmef", "mobi", "^mot-", "^moto", "^mwbp", "^nec-", "^newt", "^noki", "^opwv",
        "palm", "^pana", "^pant", "^pdxg", "^phil", "^play", "^pluc", "^port",
        "^prox", "^qtek", "^qwap", "^sage", "^sams", "^sany", "^sch-", "^sec-", "^send", "^seri", 
        "^sgh-", "^shar", "^sie-", "^siem", "^smal", "^smar", "^sony", "^sph-",
        "^symb", "^t-mo", "^teli", "^tim-", "^tosh", "^tsm-", "^upg1", "^upsi", "^vk-v", "^voda",
        "^wap-", "^wapa", "^wapi", "^wapp", "^wapr", "^webc", "^winw", "^winw", "^xda", "^xda-"]
	
	var browser = navigator.userAgent.toLowerCase();
	
	for (i=0; i<devices.length; i++) {
		var browserRegex = new RegExp(devices[i]);
		
		if (browserRegex.test(browser)) {
			// if positive mobile browser match
			if (opts.div_id != undefined) {
				// display div
				$(document).ready(function() {
					var div = $('#'+opts.div_id);
					div.css('position','absolute');
					div.css('top',0);
					div.css('left',-1 * div.width());
					div.show();
					div.animate({
						left: '+=200'},
						500, function() {});
				});
		
			}
			else {
				// display confirmation pop-up
				if (confirm(opts.alert_text)) {
					document.location.href=opts.redirect_url;
				}
	
			}
			break;
		}
	}


}


