smartling_UtilFunctions = {
    setCookie : function(name, value, expiresDays, path, domain, secure) {
        if (expiresDays) {
            var today = new Date();
            var expires = new Date(today.getTime() + (expiresDays * 1000 * 3600 * 24));
        }
        var curCookie = name + "=" + escape(value) +
            ((expiresDays) ? "; expires=" + expires.toGMTString() : "") +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            ((secure) ? "; secure" : "");
        document.cookie = curCookie;
    },

    getCookie : function(name) {
        var prefix = name + "="
        var cookieStartIndex = document.cookie.indexOf(prefix);
        if (cookieStartIndex == -1) {
            return null;
        }
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
        if (cookieEndIndex == -1) {
            cookieEndIndex = document.cookie.length;
        }
        return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
    },

    isRedirectNeeded : function(domain, url) {
        var currentHost = window.location.host.split(':')[0];
        return ((domain == currentHost) && (url.indexOf('://' + currentHost) == -1));
    },

    getUrlWithCurrentPath : function(url) {
        var path = window.location.pathname;
        if (url.lastIndexOf('/') == url.length - 1) {
            path = path.substring(1, path.length);
        }
        return url + path;
    }
};

smartling_UserRedirect = function(originalDomain, cookieDomain, sites) {
    var cookieName = '_LastVisitedHost';

    var getCurrentLocale = function() {
        return (navigator.userLanguage)?(navigator.userLanguage):(window.navigator.language);
    };
    
    var getCurrentLanguage = function() {
        return getCurrentLocale().split('-')[0];
    };
    
    var doRedirect = function(url) {
        smartling_UtilFunctions.setCookie(cookieName, url, 360, '/', cookieDomain, '');
        if (smartling_UtilFunctions.isRedirectNeeded(originalDomain, url)) {
            window.location = smartling_UtilFunctions.getUrlWithCurrentPath(url);
        }
    };

    var isCookieSet = function() {
        return (document.cookie.indexOf(cookieName + '=') == -1)?false:true;
    };
    
    var isCookiesEnabled = function() {
        smartling_UtilFunctions.setCookie(cookieName, 1, 360, '/', cookieDomain, '');
        return isCookieSet();
    };
    
    var doRedirectByBrowserPreferences = function() {
        if (isCookiesEnabled()) {
           var locale = getCurrentLocale();
           if (sites[locale]) {
               doRedirect(sites[locale]);
           } else {
               var language = getCurrentLanguage();
               if (sites[language]) {
                   doRedirect(sites[language]);
               }
           }
        }
    };

    var getValidLastVisitedHost = function() {
        var url = smartling_UtilFunctions.getCookie(cookieName);
        for(var language in sites) {
            if (url == sites[language]) {
                return url;
            }
        }
        return null;
    };

    var _initialize = function() {
        if (!isCookieSet()) {
            doRedirectByBrowserPreferences();
        } else {
            var url = getValidLastVisitedHost();
            if (url) {
                doRedirect(url);
            } else {
                doRedirectByBrowserPreferences();
            }
        }
    };

    _initialize();
};