/**
Copyright Fruitbowl Media Limited 2011

License: This is licensed to www.eyeforpharma.com/mobile/ only for the period jan-2011 -> Jan-2012

To use this code or enquire about pricing, contact us on www.Fruitbowlmedia.com

@author Liam Gooding, Fruitbowl Media Limited

**/

function FruitbowlMobileDetection(){
	
	this.uagent = navigator.userAgent.toLowerCase();
	
	// detect for ANY smartphone
	this.isSmartphone = function(){
		
		if( (this.uagent.search("iphone") > -1) || (this.uagent.search("ipod") > -1) || (this.uagent.search("series60") > -1) || (this.uagent.search("symbian") > -1) || (this.uagent.search("android") > -1) || (this.uagent.search("windows ce") > -1) || (this.uagent.search("blackberry") > -1) ){
			return true;
		} else {
			return false;
		}
	};
	
	// detect if user is forcing desktop
	this.forceDesktop = function(){
		// look for force
		var force = _getQueryString()["force"];
		var forceCookie = _getCookie("forceCookie");
		
		if(force == "desktop" || forceCookie == "desktop"){
			_setCookie("forceCookie", "desktop", 1);
			return true;
		} else {
			return false;
		}
	};
	
	// detect if user is forcing mobile
	this.forceMobile = function(){
		// look for force
		var force = _getQueryString()["force"];
		var forceCookie = _getCookie("forceCookie");
		
		if(force == "mobile" || forceCookie == "mobile"){
			_setCookie("forceCookie", "mobile", 1);
			return true;
		} else {
			return false;
		}
	};
	
	// function for pulling values from query string
	function _getQueryString() {
	  var result = {}, queryString = location.search.substring(1), re = /([^&=]+)=([^&]*)/g, m;
	
	  while (m = re.exec(queryString)) {
	    result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
	  }
	
	  return result;
	}
	
	// set a cookie. Cookie name, cookie value, days to expire
	function _setCookie(c_name,value,exdays){
		var exdate=new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
		document.cookie=c_name + "=" + c_value;
	}
	
	// get a cookie value by name
	function _getCookie(c_name){
		var i,x,y,ARRcookies=document.cookie.split(";");
		for (i=0;i<ARRcookies.length;i++){
			x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
			x=x.replace(/^\s+|\s+$/g,"");
			if (x==c_name){
				return unescape(y);
			}
		}
	}

};
