var celltouch = celltouch || {};

celltouch.mobile = {
    checkmobile: function(agent){
        var list = ["Macintosh", "windows NT", "windows 98", "windows me"];
        for(var i in list){
            var re = new RegExp(list[i], "i");
            if(re.test(agent)){
                return false;
            }
        }
        return true;
    },
    
    createCookie: function(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    },
    
    readCookie: function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    },
    
    eraseCookie: function(name) {
        celltouch.mobile.createCookie(name,"",-1);
    },
    
    checktag: function(){
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        if(vars['mobile'] == "0")
        {
            celltouch.mobile.createCookie("mobileredirect", "false", 2000);
            return false;
        }
        if(vars['mobile'] == "clear" || vars['mobile'] == "1")
        {
            celltouch.mobile.eraseCookie("mobileredirect");
        }
        var x = celltouch.mobile.readCookie('mobileredirect');
        
        if(x){
            return false;
        }
    
    
        return true;
    },

    redirect: function(address){
        if( celltouch.mobile.checkmobile(navigator.userAgent) && celltouch.mobile.checktag()){
            document.location = address;
        }
        
    }
};


