﻿var MEURL = (function () {
    var getCookie = function (cookieName) {
        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 == cookieName) {
                return unescape(y);
            }
        }
    },

     fnGetUrlPrefix = function (url) {
         var iPos = url.indexOf("//") + 2;
         var domain = url.substring(0, url.indexOf("/", iPos));
         var subdomains = domain.split('.');
         var newPrefix;
         var length = subdomains.length - 2;
         for (var i = 0; i < length; i++) {
             if (i == 0) {
                 newPrefix = subdomains[0];
             } else {
                 newPrefix = newPrefix + "." + subdomains[i];
             }
         }

         return newPrefix;
     },

    setCookie = function (cookieName, cookieValue, exdays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + exdays);
        var cValue = escape(cookieValue) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString() + ";domain=iwowwe.com");
        document.cookie = cookieName + "=" + cValue;
    },
        splitDomain = function () {
            //split the hostname into parts
            return location.hostname.toLowerCase().split('.');
        },
        getSubDomain = function () {
            return splitDomain()[0];
        },
        getSubFolder = function () {
            var startURL = document.URL.lastIndexOf('/') + 1;
            if (startURL === document.URL.length) {
                startURL = document.URL.lastIndexOf('/', startURL - 1);
            }

            var endURL = document.URL.length;
            if (endURL === '/') {
                endURL--;
            }
            var subFolder = document.URL.substr(startURL, endURL - startURL);
            if (subFolder === '/') {
                return undefined;
            }
            if (subFolder.indexOf('.') > 0) {
                return undefined;
            }
            return subFolder;
        },
        getQueryString = function () {
            var queryString = {};
            location.href.replace(
                new RegExp("([^?=&]+)(=([^&]*))?", "g"),
                function ($0, $1, $2, $3) { queryString[$1.toLowerCase()] = $3; }
            );
            return queryString['sponsor'];
        },
        requestedMEURL = function () {
            if (!VOenvironment.isKnownEnvironment(splitDomain()[0]) && splitDomain()[0] != 'iwowwe') {
                irequestedMEURL = splitDomain()[0];
            } else {
                irequestedMEURL = getSubFolder();
                if (irequestedMEURL != undefined) {
                    env = VOenvironment.getEnvironment();
                    if (env != 'www') {
                        window.location = "http://" + irequestedMEURL + "." + env + ".iwowwe.com";
                    } else {
                        window.location = "http://" + irequestedMEURL + ".iwowwe.com";
                    }
                }
                irequestedMEURL = getQueryString() || getCookie();
            }
            return irequestedMEURL;
        };

    return {
        init: function (nameDiv, phoneDiv, photoDiv) {

            var irequestedMEURL = requestedMEURL();
            //Only fetch the data if we have a valid MEURL
            if (irequestedMEURL != undefined && irequestedMEURL.indexOf('.') == -1) {
                // $.getJSON("http://" + VOenvironment.getEnvironment() + ".videocontrolpanel.net/SetSalesCookie.aspx?MEURL=" + irequestedMEURL + "&callback=?",
                $.getJSON(fnGetUrlPrefix(window.location.href) + ".videocontrolpanel.net/SetSalesCookie.aspx?MEURL=" + irequestedMEURL + "&callback=?",
                    function (data) {
                        //  setLinks(irequestedMEURL);
                        setCookie("MEURL", irequestedMEURL, 1);
                        setCookie("Sponsor", data.Sponsor, 1);
                        nameDiv.html(data.Name);
                        phoneDiv.html(data.Phone);
                        if (data.ProfilePic) {
                            photoDiv.html("<img src='" + data.ProfilePic + "'/>");
                        }
                        // Show these for affiliates
                        $('#nav li.affilate_menu').show();
                        $('#buy_btn2, #buy_btn4').show();
                        $('#addThisWrapper').show();
                        $('#tag_affiliate').show();
                        $('#free_link').show();

                        // Hide these for affiliates
                        $('#wowwe_social').hide();
                        $('#tag_retail').hide();
                        $('#buy_btn').hide();
                    });
            }
        },
        URL: function () {
            return location.protocol + "//" + location.host + "/" + requestedMEURL();
        },

        GetCookie: function (cookieName) {
            return getCookie(cookieName);
        },

        GetSubDomain: function () {
            return getSubDomain();
        },

        GetUrlPrefix: function (url) {
            return fnGetUrlPrefix(url);
        }
    };
} ());

$(document).ready(function () {
    MEURL.init($("#MEURLName"), $("#MEURLPhone"), $("#MEURLPhoto"));
});
