if ( window.location.search && window.location.search.indexOf( 'from_mobile=1' ) != -1 ) {
	document.cookie  =  'from_mobile=1; path=/';
}
else if ( window.location.search && window.location.search.indexOf( 'from_mobile=0' ) != -1 ) {
	document.cookie  =  'from_mobile=0; path=/';
}

var mobile_device_agents  =  [ 'iPhone',
							   'iPod',
							   'BlackBerry9500/4.7.0.34 Profile/MIDP-2.0 Configuration/CLDC-1.1',
							   'BlackBerry8100/4.2.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/100',
							   'BlackBerry8300/4.2.2 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/-1',
							   'BlackBerry9630/4.7.1.38 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/105',
							   'BlackBerry9700/5.0.0.208 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/603',
							   'Mozilla/5.0 (webOS/1.0; U; en-US) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 Pre/1.0',
							   'BlackBerry8310/4.5.0.110 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/102',
							   'BlackBerry8120/4.3.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/1' ];

var mobile_site_uris  =  [ '/',
						   '/who_we_are/',
						   '/what_we_do/',
						   '/our_work/our_clients/',
						   '/contact_us/' ];

var from_mobile_override  =  ( document.cookie.indexOf( 'from_mobile=1' ) != -1 );
var is_mobile_device      =  false;
var mobile_site_has_page  =  false;

if ( ! from_mobile_override ) {

	// Are we viewing on a mobile user agent?
	for ( i in mobile_device_agents ) {
		if ( navigator.userAgent.indexOf( mobile_device_agents[i] ) != -1 ) {
			is_mobile_device  =  true;
			break;
		}
	}

	if ( is_mobile_device ) {

		// Extract the path-part
		var cur_uri  =  window.location.href.replace( /\?.*$/, '' ).replace( /^[^/]+\/\/\/?[^/]+/, '' ).replace( /[^/]*$/, '' );

		// Does it exist on the mobile site?
		for ( i in mobile_site_uris ) {
			if ( mobile_site_uris[i] == cur_uri ) {
				mobile_site_has_page  =  true;
				break;
			}
		}

		if ( mobile_site_has_page ) {

			var mobile_url  =  window.location.href.replace( /\/\/www/, '//m' ).replace( /\?.*$/, '' );

			// Hack for the one URI that's different
			if ( cur_uri == '/our_work/our_clients/' ) {
				mobile_url  =  mobile_url.replace( /\/our_work\/our_clients\//, '/clients/' );
			}

			document.location  =  mobile_url;
		}
	}
}
