var fullUrl = jQuery('script[src$="jquery-reciply.js"]').attr('src');
var transportEnd = fullUrl.indexOf("//");

//If the transport seperator (//) isn't in the script, it must be a relative link, so use the window location.
if (transportEnd == -1) {
    reciplyHost = window.location.hostname;
    
    if (window.location.port != "") {
        reciplyHost += ":" + window.location.port;
    }
}
else {
    fullUrl = fullUrl.substr(fullUrl.indexOf("//") + 2);
    reciplyHost = fullUrl.substr(0, fullUrl.indexOf("/"));
}

var reciplyUrl = "http://" + reciplyHost + "/";

var reciply_mpmetrics;

var retries = 0;
var dependentScripts = [];
dependentScripts[0] = { 'url': 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', 'condition': function () { return (typeof $ != "undefined"); } }
dependentScripts[1] = { 'url': 'http://api.mixpanel.com/site_media/js/api/mixpanel.js', 'condition': function () { return (typeof MixpanelLib != "undefined"); } }
dependentScripts[2] = { 'url': reciplyUrl + "static/js/jquery-require.js", 'condition': function () { return ((typeof jQuery != "undefined") && (typeof jQuery.require != "undefined")); } }

var startup = function() {
    
    try {
        try {
            reciply_mpmetrics = new MixpanelLib('fda96b958def64d81d4cfc39fe424d30', 'reciply_mpmetrics');
            
            if ( document.location.host != reciplyHost ) {
                reciply_mpmetrics.track("Script Retrieved", { 'url': document.location.href, 'site': document.location.host });
            }
        }
        catch(err) {
            null_fn = function () {};
            reciply_mpmetrics = { track: null_fn,  track_funnel: null_fn,  register: null_fn,  register_once: null_fn, register_funnel: null_fn };
        }
    }
    catch(err) {
        console.log(err.message);
    }
    
    jQuery(".reciply-addtobasket-widget").hide();
    
    if (jQuery(".reciply-addtobasket-widget").size() > 0) {
        
        if (reciply_mpmetrics) {
            reciply_mpmetrics.track("Widget On Page", { 'url': document.location.href, 'site': document.location.host });
        }
        
        jQuery.require(reciplyUrl + "static/js/jquery-reciply-full.js");
    }
}

var getScript = function (filename) {
    var script = document.createElement('script');
    script.setAttribute("type", "text/javascript");
    script.setAttribute("src", filename)
    
    if (typeof script != "undefined")
    document.getElementsByTagName("head")[0].appendChild(script);
}

var testAllLoaded = function () {
    var allLoaded = true;
    // and we havn't given up trying...
    if (retries > 25) {
        //alert("given up!");
        return false;
    }
    else {
        for (i = 0; i < dependentScripts.length; i++) {
            if (!dependentScripts[i].condition()) {
                allLoaded = false;
                break;
            }
        }
        
        if (allLoaded) {
            startup();
        }
        else {
            retries += 1;
            setTimeout(testAllLoaded, 200);
        }
    }
}

for (i = 0; i < dependentScripts.length; i++) {
    if (!dependentScripts[i].condition()) {
        getScript(dependentScripts[i].url);
    }
}

testAllLoaded();



