if(location.href != "http://socialsafe.net/")
{
    if (!Array.indexOf) {
        Array.prototype.indexOf = function (obj, start) {
            for (var i = start; i < this.length; i++) {
                if (this[i] == obj) {
                    return i;
                }
            }
            return -1;
        }
    }        
}

var priceHeaders;
var price = "£2.49";
var currency = "USD";
var language = "en-GB";

       
var positionLeft;
var positionTop;

var pricesPound = ["£2.49","£4.99","£9.99"];
var pricesEuro = ["\u20ac2.99","\u20ac5.99","\u20ac11.99"];
var pricesDollar = ["$3.49","$6.99","$13.99"];

var pricesUpgradeStandardPound = ["£0.00","£2.99","£7.99"];
var pricesUpgradeStandardEuro = ["\u20ac0.00","\u20ac3.49","\u20ac9.49"];
var pricesUpgradeStandardDollar = ["$0.00","$3.99","$10.99"];

var pricesUpgradeProPound = ["£0.00","£0.00","£5.49"];
var pricesUpgradeProEuro = ["\u20ac0.00","\u20ac0.00","\u20ac6.49"];
var pricesUpgradeProDollar = ["$0.00","$0.00","$7.49"];

var standardLicenceOptimal = {
    EUR: "7CAEF13C-CF35-4546-AE1F-9EFA00D02D96", 
    GBP: "1D41C86C-AA33-4A3A-92B9-9EFA00CE34CC", 
    USD: "018100B3-7172-42FC-8C14-9EFA00D02D98"
};
var standardLicencePaypal = {
    EUR: "NXV9GZUKH3BAS", 
    GBP: "8A8YYAFZR7SAN", 
    USD: "RD9EU87J6LRUG"
};
var proLicenceOptimal = {    
    EUR: "ED89684D-E4DC-4F22-B377-9EFB00C6AFF8", 
    GBP: "E0DCC088-9B10-41B0-AD2E-9EFB00C6AFF7", 
    USD: "4A53C00D-D91E-424A-BAA4-9EFB00C6AFF7"
};
var proLicencePaypal = {
    EUR: "9AVSXX5KW8P56", 
    GBP: "RNRPC2Z7Q8R2Y", 
    USD: "STUTT94XMPMAY"    
};

var enterpriseLicenceOptimal = {
    GBP:    "32B46884-8437-4C4C-91AF-9EFB00C6F604",
    EUR:    "B3E65211-A89C-4E12-8D69-9EFB00C6F604",
    USD:    "9E0E4EC2-981F-458D-BFCA-9EFB00C6F604"
};

var enterpriseLicencePaypal = {
    EUR: "2A9V9AWDA7YFA", 
    GBP: "VJWAAXY9NWG94", 
    USD: "L33UQH3K4GKBY"
};

var upgradeStandardProOptimal = {
    GBP:    "82EE685F-3CB8-4053-964F-9EFB00C75816",
    EUR:    "297C5204-5AAA-4F7F-89B1-9EFB00C75816",
    USD:    "2357A348-1A92-4E35-88B5-9EFB00C75816"
};

var upgradeStandardProPaypal = {
    EUR: "EVK5LMWD2UWXE", 
    GBP: "V96GD4ED5ZHGG", 
    USD: "ZV5XV6977UFDG"
};

var upgradeStandardEnterpriseOptimal = {
    GBP:    "8200D0C4-1111-4284-BC9F-9EFB00C7AA93",
    EUR:    "B076855E-DE1A-4AF6-8B0F-9EFB00C7AA93",
    USD:    "26C8ADFA-E4CF-4FAB-B9D3-9EFB00C7AA93"
};

var upgradeStandardEnterprisePaypal = {
    EUR: "L78CLU2UPEAUE", 
    GBP: "BM7GE738BSV8E", 
    USD: "NDRA6FZ9HD4NC"
};

var upgradeProEnterpriseOptimal = {
    GBP:    "C339B951-2523-4E86-9413-9EFB00C7ED2A",
    EUR:    "867923EA-B65A-4B44-89D1-9EFB00C7ED2A",
    USD:    "AADF7854-1CE8-487F-87EC-9EFB00C7ED2A"
}

var upgradeProEnterprisePaypal = {
    EUR: "LEWTJAXD98NHL", 
    GBP: "5WWAEAMQWA9PQ", 
    USD: "ET65F8AJZFG82"
}

$(function() {
    
    var euro_languages = ['de-at', 'fr-be', 'bg', 'el', 'cs',
    'da', 'et', 'fi', 'fr', 'fr-fr', 'de-de', 'hu', 'ga', 'it', 
    'lv', 'lb', 'mt','nl', 'pl', 'pt', 'ro', 'sk', 'sl', 'es-es', 
    'sv'];
    language = language.toLowerCase();
    
    //get the url of the page - buy / upgrade etc
    var pageType = window.location.pathname.split("/")[1];
    
    
    if(language == "en-gb") 
    {
        currency = "GBP";        
    } 
    else if(jQuery.inArray(language, euro_languages)>= 0) 
    {
        currency = "EUR";
    }
    
    try
    {
        setEnterprisePrice(currency,pageType);
    }
    catch(e)
    {
        
    }
    
    if($("#price-banner").length > 0)
    {
        var banner = $("#price-banner");
        src = banner.attr('src');
        
        var suffix = src.toString().substr(src.length-7, src.length);        
        src = src.replace(suffix,currency.toLowerCase()+'.png');
        
        banner.attr('src',src);
    }
    else if($(".price").length > 0)
    {
        priceHeaders = $(".price");
        
        var priceArray = getPrices(pageType);         
        
        setImage(currency);
        changePrices(priceArray);   
            
    }
    
    $("#currencyPound").click( function()
    {       
        currency = "GBP";        
     
        resetCurrencyImages();        
        setImage(currency);
        
        changePrices(getPrices(pageType));        
    });
    
    $("#currencyEuro").click( function()
    {
       currency = "EUR";        
     
        resetCurrencyImages();        
        setImage(currency);
                
        changePrices(getPrices(pageType));
    });
    
    $("#currencyDollar").click( function()
    {
        currency = "USD";        
     
        resetCurrencyImages();        
        setImage(currency);
               
        changePrices(getPrices(pageType));        
    });
    
    //licence type is being used to mediate analytics events
    var licenceType = "";
    
    $(".buy-button").click(function(event){
       
        event.preventDefault();
       
        //get the classes of the selected item and split into an array
        var classes = $(this).attr("class").toString().split(" ");
        //set a flag to see if the clicked button is an upgrade button
//        var upgradeFound = findString("upgrade-button", classes);
       
        $("#payment-options").hide();        
        $("#optimal-user-details").hide();
       
        if($(this).attr("id") == "standard-buy-button")
        {
            positionLeft = $(this).attr("offsetLeft"); 
            positionTop = $(this).attr("offsetTop");
            
            changeForms(standardLicenceOptimal,standardLicencePaypal);
            licenceType = "standard"; 
        }
        else if($(this).attr("id") == "pro-buy-button")
        {              
            
//            if(upgradeFound)
//            {
//                changeForms(upgradeStandardProOptimal, upgradeStandardProPaypal);
//            }
//            else
//            {
                changeForms(proLicenceOptimal, proLicencePaypal);
//            }
            
            positionLeft = $(this).attr("offsetLeft"); 
            positionTop = $(this).attr("offsetTop");
            licenceType = "pro";
        }
        else if($(this).attr("id") == "enterprise-buy-button")
        {
//            if(upgradeFound)
//            {
//                changeForms(upgradeStandardEnterpriseOptimal, upgradeStandardEnterprisePaypal);
//                positionLeft = $(this).attr("offsetLeft"); 
//                positionTop = $(this).attr("offsetTop");
//            }
//            else
//            {
                changeForms(enterpriseLicenceOptimal, enterpriseLicencePaypal);
                positionLeft = $(this).attr("offsetLeft") + 20; 
                positionTop = $(this).attr("offsetTop") + 290;
//            }
                        
            licenceType = "enterprise"; 
        }
//        else if($(this).attr("id") == "upgrade-from-pro")
//        {
//            changeForms(upgradeProEnterpriseOptimal, upgradeProEnterprisePaypal);
//            positionLeft = $(this).attr("offsetLeft")-10; 
//            positionTop = $(this).attr("offsetTop");
//                       
//             
//        }
       
       //differentiate between enterprise and buy pages
       if(licenceType != "enterprise")
       {
          //prefix for buy pages
          _gaq.push(['_trackEvent', 'purchase', "buy-"+licenceType+'-subscribe-now']);
       }
       else
       {
          _gaq.push(['_trackEvent', 'purchase', licenceType+'-subscribe-now']); 
       }
       
        
        $("#payment-options").css({
            "left":(positionLeft + 30)+"px",
            "top":(positionTop -103)+"px"
            });
        
        $("#optimal-user-details").css({
            "left":(positionLeft - 55)+"px",
            "top":(positionTop -103)+"px"
        });
        
        $("#payment-options").animate({
            opacity: "toggle"
        },500);     
       
        return false;
       
    });
    
    //hide the payment options dialog when mouse leaves element
    $("#payment-options").mouseleave(function(){
        $(this).hide();
    });

    
    
    //submit paypal form
    $("#pay_with_paypal").click(function () {        
        $('#paypal_form').submit();
        
       if(licenceType != "enterprise")
       {
          //prefix for buy pages
          _gaq.push(['_trackEvent', 'purchase', "buy-"+licenceType+'-pay-paypal']);
       }
       else
       {
          _gaq.push(['_trackEvent', 'purchase', licenceType+'-pay-paypal']); 
       }
        
        console.dir($('#paypal_form'));
        return false;
    });
    
    //submit optimal form
    $("#pay_with_optimal").click(function () {
       //hide the previous element, show the next and give the input focus
        $("#payment-options").hide();
        $("#optimal-user-details").show();
        $("#first_name").focus();            
        
        if(licenceType != "enterprise")
        {
          //prefix for buy pages
          _gaq.push(['_trackEvent', 'purchase', "buy-"+licenceType+'-pay-optimal-name']);
        }
        else
        {
          _gaq.push(['_trackEvent', 'purchase', licenceType+'-pay-optimal-name']); 
        }
        
        return false;
    });
    
    $("#optimal_pay_button").click(function() {
        if($("#first_name").val().toString().length > 0 && $("#first_name").val().toString().length > 0)
        {
            $("#user_input_form").submit();
            if(licenceType != "enterprise")
            {
              //prefix for buy pages
              _gaq.push(['_trackEvent', 'purchase', "buy-"+licenceType+'-pay-optimal']);
            }
            else
            {
              _gaq.push(['_trackEvent', 'purchase', licenceType+'-pay-optimal']); 
            }     
        }
        else
        {
            if($("#first_name").val().toString().length == 0)
            {
               $("#first_name").css({"border": "1px solid red"});
            }
            
            if($("#last_name").val().toString().length == 0)
            {
               $("#last_name").css({"border": "1px solid red"});
            }
        }
        
        return false;
    });
    
    $("#enterprise-currencyPound").click(function() {
        currency = "GBP";
        
        setEnterprisePrice(currency,pageType);                        

    });
    
    $("#enterprise-currencyEuro").click(function() {
        currency = "EUR";
        
        setEnterprisePrice(currency,pageType);              
    });
    
    $("#enterprise-currencyDollar").click(function() {
        currency = "USD";
        setEnterprisePrice(currency,pageType);        
    });
    
    
    //analytics
    $("#free-download-button").click(function() {
        _gaq.push(['_trackEvent', 'download', 'buy-free-download-button']); 
    });
    
});

function setEnterprisePrice(currency, pageType)
{
    var priceText;
    
    if(currency == "EUR")
    {
        if(pageType == "upgrade-pro")
        {
            priceText = "<span>&euro;18.49</span>&euro;6.49"; 
        }
        else
        {
            priceText = "<span>&euro;23.99</span>&euro;11.99";               
        }
    }
    else if(currency == "USD")
    {
        if(pageType == "upgrade-pro")
        {
            priceText = "<span>&#36;21.49</span>&#36;7.49";
        }
        else
        {
            priceText = "<span>&#36;27.99</span>&#36;13.99";
        }
    }
    else
    {
        if(pageType == "upgrade-pro")
        {
            priceText = "<span>&pound;15.49</span>&pound;5.49";    
        }
        else
        {
            priceText = "<span>&pound;19.99</span>&pound;9.99";                
        }
    }
    
    $(".enterprise-current-price")[0].innerHTML = priceText;
    resetCurrencyImages();
    setImage(currency);
}

function findString(string, array)
{
    return(array.indexOf(string, 0) > -1);
}

function changePrices(prices)
{
    //change each of the prices in the headings
    for(var i=0; i<priceHeaders.length; i++)
    {
        var item = priceHeaders[i];
        item.innerHTML = prices[i];
    }    
}

function changeForms(optimalCodes, paypalCodes)
{
    //change the hidden inputs with the correct currency code
    $("#selected_price_id").val(optimalCodes[currency].toLowerCase());  
    $("#hosted_button_id").val(paypalCodes[currency]);          
}


function getPrices(pageType) 
{
    //return an array of prices based on page and currency
    var returnValue;
        
    if(pageType == "buy")
    {
        if(currency == "EUR")
        {
            returnValue = pricesEuro;
        }
        else if(currency == "USD")
        {
            returnValue = pricesDollar;
        }
        else
        {
            returnValue = pricesPound
        }  
    }
    else if(pageType == "upgrade-std")
    {
        if(currency == "EUR")
        {
            returnValue = pricesUpgradeStandardEuro;
        }
        else if(currency == "USD")
        {
            returnValue = pricesUpgradeStandardDollar;
        }
        else
        {
            returnValue = pricesUpgradeStandardPound;
        }          
    }
    else if(pageType == "upgrade-pro")
    {
        if(currency == "EUR")
        {
            returnValue = pricesUpgradeProEuro;
        }
        else if(currency == "USD")
        {
            returnValue = pricesUpgradeProDollar;
        }
        else
        {
            returnValue = pricesUpgradeProPound;
        }          
    }
    
    return returnValue;
}

function setImage(currency)
{
    var img;
    
    if(currency == "GBP")
    {
        img = "/UserStorage/Images/payment/SS_Currency_Pound_Sign_Ovr.png";
        $("#currencyPound").attr('src',img);
        $("#enterprise-currencyPound").attr('src',img);
    }
    else if(currency == "EUR")
    {
        img = "/UserStorage/Images/payment/SS_Currency_Euro_Sign_Ovr.png";
        $("#currencyEuro").attr('src',img);
        $("#enterprise-currencyEuro").attr('src',img);
    }
    else
    {
        img = "/UserStorage/Images/payment/SS_Currency_Dollar_Sign_Ovr.png";
        $("#currencyDollar").attr('src',img);
        $("#enterprise-currencyDollar").attr('src',img);
    }   
}

function resetCurrencyImages() 
{   
    $("#currencyPound").attr('src',"/UserStorage/Images/payment/SS_Currency_Pound_Sign_Def.png");
    $("#currencyEuro").attr('src',"/UserStorage/Images/payment/SS_Currency_Euro_Sign_Def.png");
    $("#currencyDollar").attr('src',"/UserStorage/Images/payment/SS_Currency_Dollar_Sign_Def.png");
    $("#enterprise-currencyPound").attr('src',"/UserStorage/Images/payment/SS_Currency_Pound_Sign_Def.png");
    $("#enterprise-currencyEuro").attr('src',"/UserStorage/Images/payment/SS_Currency_Euro_Sign_Def.png");
    $("#enterprise-currencyDollar").attr('src',"/UserStorage/Images/payment/SS_Currency_Dollar_Sign_Def.png");    
}


