//////////////////////////////////////
// Adk.Halloween.js
// (c) 2010 Adknowledge Inc.
//////////////////////////////////////

if(typeof(Adk) == "undefined" || !Adk) {
  Adk = {};
}

Adk.Halloween = {
  version: "1.00",
  modalID: "halloween_modal",
  
  btnHover: function() {
    
    if($(".btn").length > 0) {
      $(".btn").removeClass("btn").addClass("btn_hover");
    }
    else {
      $(".btn_hover").removeClass("btn_hover").addClass("btn");
    }
  }
}

Adk.Halloween.prototype = {
  
  init: function(placeholderID, h, uid) {
    this._h = h;
    this._uid = uid;
    this._placeholderID = placeholderID;
    this._init = false;

    if(!this._h || !this._uid || !this._placeholderID) {
      console.log("Adk.Halloween.init(): A required param is not set.");
    } 
    else {
      this._init = true;
    }
  },
    
  displayModal: function(placeholderID, h, uid) {
      
    Adk.Halloween.prototype.init(placeholderID, h, uid);
    
    if(this._init && !$('#'+Adk.Halloween.modalID).length) {
      
      var params = "h="+this._h+"&uid="+this._uid+"&callback=?";
      
      if(this._custom_params) {
        params = this._custom_params + "&" + params;
      }
      
      var murl = "http://super.kitnmedia.com/tools/halloween?";
      
      $.getJSON(murl + params, function(obj) {
        
        if(obj.data == "") {
          console.log("Adk.Halloween.displayModal(): No available offer.");
        } 
        else {
           $(document.body).append(obj.data);
           Adk.Halloween.prototype.alignModal();
        }
         
      });
      
    }

  },
  
  setCustom: function(custom_name, custom_value) {
    
    var custom = custom_name + "=" + custom_value;
    
    if(this._custom_params) {
      this._custom_params = this._custom_params + "&" + custom;
    }
    else {
      this._custom_params = custom;
    }
  },
    
  closeModal: function() {
    if($('#'+Adk.Halloween.modalID)) $('#'+Adk.Halloween.modalID).remove();
  },
    
  alignModal: function() {
    var placeholder = $('#'+this._placeholderID);
    var placeholder_pos = placeholder.position();
    var halloween_modal = $('#'+Adk.Halloween.modalID);
    
    var left = typeof(placeholder) !== 'undefined' ? placeholder_pos.left + (placeholder.width() / 2) - (halloween_modal.width() / 2) : placeholder.left;
    var top = typeof(placeholder) !== 'undefined' ? placeholder_pos.top + (placeholder.height() / 2) - (halloween_modal.height() / 2) - 70 : placeholder.top;
    
    var leftpos = left;
    var toppos = top;
    
    halloween_modal.css({left: leftpos, top: toppos});
  }
    
}


