// Create an anonymous closure scope, to avoid polluting the global window object
function redirectMobile(url)
{
	// Ensure navigator is detected, then wrap code in try/catch
	if (typeof navigator !== 'undefined')
	try
	{
		// Create a local variable alias for navigator
		var nav=navigator,

			// Exception variable
			e,

			// Check that userAgent exists, else empty string
			ua = nav && nav.userAgent || '',

			// Free-for-all global search for mobile keywords regexp
			re = /(\b|[^a-z])\s*((BlackBerry)|(iPhone)|(iPod)|(iPad)|(iOS)|(A?n?droid)|(SGH)|(Nokia\w*)|(Symbian)|(J2ME)|(MIDP)|(CLDC)|(NMI)|(Sec)|(Opwv)|(MP)|(HTC)|(MMP)|(AHONG)|(Alcatel)|(WAP)|(IEMobile)|(Mobile)|(BREW)|(Smartphone)|(ATTWS)|(AU\W*\w*)|(UP\W*\w*)|(NF\/\w*)|(Amoi)|(\w*WAPDevice)|(AUDIOVOX)|(Becker)|(Bilbo)|(BIRD)|(DoCoMo)|(dopod\w*)|(eTouch\w*)|(EZOS)|(EzWAP)|(WAP\W*Browser)|(Vodafone)|(Huawei)|(Obigo)|(INNO\d+)|(\w*Telco\w*)|(jBrowser)|(KDDI)|(Browser\W*MAUI)|(Palm\w*)|(Pre\W*\d+)|(Pixi\W*\d+)|(\w*phone\w*)|(portalmmm)|(RIM\d+)|(Rover)|(SAGEM)|(SoftBank)|(T\W*Mobile))\s*(\b|[^a-z])/i,

			// Strict head search for mobile prefix keywords regexp
			// This avoid false match like detecting wrongly a Sony laptop browser, for instance.
			rh = /^(\b|[^a-z]*)\s*((Sony)|(SonyEricsson)|(Ericsson)|(SIE)|(Siemens)|(Samsung)|(Panasonic)|(Philip\w*)|(LgE?)|(Mot[orlaVZ]*))/i,

			// RegExp for MP4 <video> tag
			mp4= /((iPhone)|(iPod)|(iPad)|(iOS)|(A?n?droid))/i,

			// Try to match against one or the other
			m  = re.exec(ua) || rh.exec(ua),

			// Verify if it successfully match or not
			b  = m && m[2] || 0,

			// Check for <video> tag support
			v=0,
			c=0,
			jc=(window.jQuery && jQuery.cookie);

		if(b)
		{
			m = mp4.exec(ua);
			v = (m && m[1] || '').toLowerCase() || 0;
			
			if(v&&v=='ipad') v = 4;
			else v = v ? (v=='iphone' ? 3 : 2) : 1;
		}

		// if mobile and not iPad
		if(url && v > 0 && v != 4)
		{
			if(window.gup)
			{
				c=gup("m",0);
				if(c > 0)
				{
					if(jc)
					{
						jQuery.cookie('m',1,{expires:1,path:'/',ratio:3600*1000});
						var curl0=window.location.href||"",curl=curl0.replace(/[\?\&\#]+m=[0-9]+/gmi,'');
						if(curl!=curl0)
						{
							top.window.location.href=curl;
						}
					}
					return v;
				}
			}

			if(jc && (parseInt(jQuery.cookie('m'),10)||0) > 0)
			{
				return v;
			}

			top.window.location.href=url;
		}

		return v;
	}
	catch(e)
	{
	}

	return 0;
}

function gscMobile()
{
	var url=window.location.href||"",r;
	
	if(url.indexOf("/studentcentre/") >= 0)
	{
		r="/studentcentre/mobile/";
	}
	else
	{
		r="/mobile/";
	}
	
	if(url.indexOf("greenshield.") >= 0)
	{
		redirectMobile("http://greenshield.ca"+r);
	}
	else
	{
		redirectMobile(r);
	}
}

gscMobile();
