//set up the namespace
if (!com) var com = {};
if (!com.activestate) com.activestate = {};

com.activestate.one_click = {
    
    product:    "",         // pass in one of "komodo_ide, komodo_edit, pdk, tdk, activeperl, activepython, activetcl, activeperl_pro_studio, activetcl_pro_studio"
    button_id : "",         // pass in a "unique_to_this_page" id
    
    
    // the following variables' values are generated by the script.  
    // If you set them, they'll be overwritten later, so don't bother passing anything in for them.
    user_platform: "",
    downloads_url: "",
    return_url:    "",    
    tb_product: "",
    
    has_cookies: function(product) {
      var reminder_cookie = product + "_reminder";

      return $.cookie(reminder_cookie);
    },

    populate: function(badge_id, product) {
        // fix the komodo url brokenness
        var badge_product = product;
        var badge_class = product;
        if (badge_product.match("komodo_ide")) {
            badge_product = "komodo";
        }

        var user_platform = navigator.platform;
        var downloads_url = "https://www.activestate.com/" + badge_product + "/downloads";

        // set classes and attributes for the one-click badge
        $("#" + badge_id).addClass(badge_class);
        

        if (badge_product.match("pro_studio")) {
            $("#" + badge_id + " a.dl_trial").html("<br /><span>See Available Versions</span>").attr("href", downloads_url);
        }
        else {
            // get the data from the appropriate JSON file and populate button
            var json_file = "/util/current_bits/json/" + badge_class + "/index.json";
            $.getJSON( json_file , function(data) {

                var dl_link = "";                  
                var up_label = "";

               if (user_platform) {
                 up_label = data[user_platform].label;
               }

                if (up_label) {
                    dl_link = data[user_platform].link;
                }

                // display the appropriate links on and around the button   
                if (!dl_link) {
                    $("#" + badge_id + " a.dl_trial").html("<br /><span>See Available Versions</span>").attr("href", downloads_url);
                }
                else {
                    $("#" + badge_id + " a.dl_trial").html("<br /><span>" + data[user_platform].label + "</span>").attr("href", dl_link);
                    $("#" + badge_id + " .all_binaries").html("<span class='view_binaries'><a href='" + downloads_url + "'>Other Systems and Versions</a></span>");
                }
            })
        }
    },

    show_popup: function(product, button_id) {
      $("#" + button_id).click(function() {
        Shadowbox.open({
            content: "/util/thickbox_popup/one_click/index.html?product=" + product + "&r=" + new Date().getTime(),  // avoid caching the popup
            player:  "iframe",
            width:   "465px",
            height:  "430px",
            options: {
              onOpen:  function() {
                  // set the styles on the popup
                  $("#sb-info").css("display", "none");  
                  $("#sb-body").css("background-color", "transparent").css("border", "none");
                  $("#sb-loading").css("background-color", "transparent"); 
            
                  // set the reminder cookie
                  var reminder_cookie = product + "_reminder";
                  $.cookie( reminder_cookie, "1", { expires: 7, path: '/' });
            
                  // tell google about the popup  
                  if (pageTracker) { pageTracker._trackPageview('/' + product + '/pop_up'); }
                  
                  return; },
              onFinish: function() {
                   // set transparency on iframe
                  $("#sb-content").attr("allowtransparency", "true");
              },
              onClose: function() { 
                  // reset the styles on the popup for next time
                  $("#sb-info").css("display", "block"); 
                  $("#sb-body").css("background-color", "#060606").css("border", "1px solid #303030");
                  $("#sb-loading").css("background-color", "#060606");
            
                  // tell google about closing the popup
                  if (pageTracker) { pageTracker._trackPageview('/' + product + '/close_popup'); }
            
                  return; }
            }
        });
      });
    },

    generate_badge: function(badge_product, badge_id, thickbox_id) {
    
        // basic error checking
        if (!badge_id) {
            alert("Error: must supply a unique button id");
            return;
        }
        
        if (!badge_product) {
            alert("Error: must supply a valid product id.");
            return;
        }
        
        if (this.popup_url && !com.activestate.popup) {
            alert("Error: must include the ActiveState PopUp javascript library to have a popup dialogue appear!");
            return;
        }

        var user_platform = navigator.platform;
        var downloads_product = badge_product;
        if (badge_product.match("komodo_ide")) {
              downloads_product = "komodo";
        }
        var downloads_url = "https://www.activestate.com/" + downloads_product + "/downloads";
        var return_url    = "https://www.activestate.com/" + downloads_product + "/thanks";
        
        // set the thickbox vars for oneclick scripts
        var popup_url = "/util/thickbox_popup/one_click/index.html";
        
        $("#" + badge_id).addClass(badge_product);
        $("#" + badge_id).html("<div class='oneclick_dl'><div class='featured_binary'><h3><a class='dl_trial'></a></h3></div><br class='clear'/></div><div class='all_binaries'></div>");
        
        if ( badge_product.match("pro_studio") ) {
            $("#" + badge_id + " .featured_binary a.dl_trial").html("<br /><span>See Available Versions</span>").attr("href", downloads_url);
        }
        else {        
            // get the data from the appropriate JSON file and populate button
            var json_file = "/util/current_bits/json/" + badge_product + "/index.json";
            $.getJSON( json_file , function(data) {
                var dl_link = "";                  
                
                if (user_platform) {
                  if (data[user_platform]) {
                    dl_link = data[user_platform].link;
                  }
                }
             
                // display the appropriate links on and around the button   
                if (!dl_link) {
                    $("#" + badge_id + " .featured_binary a.dl_trial").html("<br /><span>See Available Versions</span>").attr("href", downloads_url);
                }
                else {
                    $("#" + badge_id + " .featured_binary a.dl_trial").html("<br /><span>" + data[user_platform].label + "</span>").attr("href", dl_link);
                    $("#" + badge_id + " .all_binaries").html("<span class='view_binaries'><a href='" + downloads_url + "'>Other Systems and Versions</a></span>");
                    
                    $("#" + badge_id + " .featured_binary a.dl_trial").click(function() {

                        var modal_box = new AS_OneClick_Thickbox();
                        modal_box.trigger_thickbox(badge_product, thickbox_id);
                        
                        return;
                        
                     });
                    
                }
            });
        }
    }
    
};