var cookieVerify   = '42BelowAgeVerification';
var cookieCountry  = '42BelowCountry';
var cookieRedirect = '42BelowRedirect';
var verifyUrl      = '/Age-Verification';

/**
 * checks to see if the user needs to be redirected
 */
function checkIfWeShouldToBeHere() {
    if (location.pathname == verifyUrl && location.hostname != 'www.cocktailworldcup.com') {
        window.location = 'http://www.cocktailworldcup.com' + verifyUrl;
        return;
    }

    if ($.cookies.get(cookieVerify)) {
        if ($.cookies.get(cookieRedirect)) {
            redirectURL = $.cookies.get(cookieRedirect);
            
            $.cookies.del(cookieRedirect);
            
            window.location = decodeURI(redirectURL);
        } else if (location.pathname == verifyUrl) {
            window.location = '/';
        }
        
        if ($.cookies.get(cookieCountry)) {
            // define any other subdomains here
            var subdomains      = [];
            subdomains['Italy']       = 'it';
            subdomains['USA']         = 'usa';
            subdomains['New Zealand'] = 'nz';
            
            // Redirect to country specific site (if one exists)
            var country = $.cookies.get(cookieCountry);
            if (subdomains[country] != null) {
                var url = subdomains[country] + '.cocktailworldcup.com';
                
                if (location.host != url) {
                    if (location.pathname != verifyUrl) {
                        url = url + location.pathname;
                        
                        var query = window.location.search.substring(1);
                        if (query.length > 0) {
                            url = url + '?' + query;
                        }
                    }
                    document.location = 'http://' + url;
                }
            }
        }
    }
}

/**
 * verify the users age & redirect them to the correct country website
 * 
 * Note: For cwc: $.cookies.setOptions({path: '/', domain: '.cwc.mycms.co.nz'});
 */
function checkVerifyCookie() {
    var local = new RegExp('^http://local.+');
    
    if (local.test(location.toString())) {
        $.cookies.setOptions({path: '/', domain: '.local.cocktailworldcup.com'});
    } else {
        $.cookies.setOptions({path: '/', domain: '.cocktailworldcup.com'});
    }
    
    // check to see if we need to delete the verification cookies 
    var deleteCookieTest = new RegExp('\\?change$');
    if (deleteCookieTest.test(location.toString())) {
        $.cookies.del(cookieVerify);
        $.cookies.del(cookieRedirect);
        $.cookies.del(cookieCountry);
    }
    
    if ( ! $.cookies.get(cookieVerify) && location.pathname != verifyUrl) {
        // not verified, so redirect to the verifyUrl
        if ( ! $.cookies.get(cookieRedirect)) {
            $.cookies.set(cookieRedirect, location.pathname);
        }
        
        // redirect to verification
	/*
        if (location.pathname != verifyUrl && location.hostname != 'www.cocktailworldcup.com') {
            window.location = 'http://www.cocktailworldcup.com' + verifyUrl;
        }
	*/
        if (location.pathname != verifyUrl || 
           (location.pathname == verifyUrl && location.hostname != 'www.cocktailworldcup.com')) {
            $.cookies.del(cookieVerify);
            $.cookies.del(cookieCountry);
            $.cookies.del(cookieRedirect);
            window.location = 'http://www.cocktailworldcup.com' + verifyUrl;
        }
    } else {
       checkIfWeShouldToBeHere();
    }
}

