var CurrentDomain = document.location.hostname.toLowerCase();
var CountryCode = null;
var CcRegEx = /^[A-Z][A-Z]$/;
var IsContentHidden = false;
var CorrectStore;

// determine if this site is one of the stores
if (CurrentDomain.substring(0, CurrentDomain.indexOf(".")) === "shop") {
  var IsStore = true;
}
else {
  var IsStore = false;
}

// check for cookie data
var GoLocVal = jaaulde.utils.cookies.get('GoLoc');
if (CcRegEx.test(GoLocVal)) {
  CountryCode = GoLocVal;
  ValidCC();
}
// hide store content
else if (IsStore) {
  AddCss('#Wrapper {min-height: 530px}');
  AddCss('#LayoutColumn2 {display: none;}');
  IsContentHidden = true;
}

window.onload = function() {
  // no good cookie data
  if (CountryCode === null) {
    // insert animated loader image on store
    if (IsContentHidden) {
      var oldHTML = document.getElementById('Wrapper').innerHTML;
      var newHTML = '<div id="StoreLoadMsg" style="margin: 0 auto; text-align:center;"><img style="margin-top: 230px;" src="/templates/__custom/images/custom/ajax-loader.gif" alt="Loading..." /></div>';
      document.getElementById('Wrapper').innerHTML = newHTML + oldHTML;
    }
    
    // set path for local easyXDM file
    if (IsStore) {
      var XDM_LOCAL_PATH = 'http://' + CurrentDomain + '/templates/__custom/js/easyXDM/name.html';
    }
    else {
      var XDM_LOCAL_PATH = 'http://www.gaelleorganic.com/wp-content/themes/cudazi-luxury/js/easyXDM/name.html';
    }
    
    // set timeouts for remote connection and message
    var XdmCnctTO = setTimeout("InvalidCC()",2000);
    var XdmMsgTO = setTimeout("InvalidCC()",5000);
    
    // get country code for current user from remote site
    var socket = new easyXDM.Socket({
      remote: "http://tools.gaelleorganic.com/GetCountryCode.php",
      remoteHelper: "http://tools.gaelleorganic.com/easyXDM/name.html",
      swf: "http://tools.gaelleorganic.com/easyXDM/easyxdm.swf",
      local: XDM_LOCAL_PATH,
      // fires when connection is made
      onReady: function() {
        clearTimeout(XdmCnctTO);
        socket.postMessage("GetCC");
      },
      // fires when data is received
      onMessage: function(message, origin) {
        clearTimeout(XdmMsgTO);
        CountryCode = message;
        VerifyCC();
        // allow only one remote message
        socket.destroy();
      }
    });
  }
}

// verify country code
function VerifyCC() {
  if (CcRegEx.test(CountryCode)) {
    CreateCookie_Loc();
    ValidCC();
  }
  else {
    InvalidCC();
  }
}

function ValidCC() {
  CorrectStore = GetCorrectStore(CountryCode);
  
  if (IsStore) {
    if (CurrentDomain === CorrectStore) {
      if (IsContentHidden) {
        // hide the loader image and show the content
        document.getElementById('StoreLoadMsg').style.display = 'none';
        document.getElementById('LayoutColumn2').style.display = 'inline';
      }
    }
    else {
      // redirect to correct store
      window.location.replace(window.location.protocol + "//" + CorrectStore + document.location.pathname);
    }
  }
  else {
    RewiteUrls();
  }
}

function InvalidCC() {
  if (ContentHidden) {
    document.getElementById('StoreLoadMsg').innerHTML = '<p style="padding-top: 240px;">Our store is temporarily unavailable. Please try again later.</p>';
  }
}

function CreateCookie_Loc() {
  if (CountryCode === "AU" || CountryCode === "NZ") {
    jaaulde.utils.cookies.set('GoLoc', CountryCode, {domain: 'gaelleorganic.com.au'});
  }
  else
    jaaulde.utils.cookies.set('GoLoc', CountryCode, {domain: 'gaelleorganic.com'});
}

// rewrite store URLs on blog
function RewiteUrls() {
  if (CountryCode === "AU" || CountryCode === "NZ") {
    for (var i=0; i<document.links.length; i++) {
      document.links[i].href = document.links[i].href.replace(/shop.gaelleorganic.com\//gi, CorrectStore + "/");
    }
  }
}

function GetCorrectStore(CCode) {
  if (CCode === "AU" || CCode === "NZ") {
    return "shop.gaelleorganic.com.au";
  }
  else {
    return "shop.gaelleorganic.com";
  }
}

// http://yuiblog.com/blog/2007/06/07/style/
// insert CSS into head
function AddCss(cssCode) {
  var styleElement = document.createElement("style");
  styleElement.type = "text/css";
  if (styleElement.styleSheet) {
    styleElement.styleSheet.cssText = cssCode;
  }
  else {
    styleElement.appendChild(document.createTextNode(cssCode));
  }
  document.getElementsByTagName("head")[0].appendChild(styleElement);
}
