if((typeof Prototype=='undefined') ||
    (typeof Element == 'undefined') ||
    (typeof Element.Methods=='undefined') ||
    parseFloat(Prototype.Version.split(".")[0] + "." +
               Prototype.Version.split(".")[1]) < 1.5){
    throw("UltraPop requires the Prototype JavaScript framework >= 1.5.0");
}

UltraPop = Class.create();
UltraPop.prototype = {
  el:null,
  iframe:null,

  initialize:function(el){
    if(el.getAttribute("UltraPop_init")) return;
    this.el = $(el);
    this.processIFrame();
    this.positionIFrame();
    this.overrideElBindings();
    this.el.setAttribute("UltraPop_init", true);
  },

  processIFrame:function(){
    this.iframe = $(document.createElement("IFRAME"));
    this.el.up().appendChild(this.iframe);
    this.iframe.setStyle({zIndex:99, border:0, padding:0, margin:0});
    if(!this.el.visible()) this.iframe.hide();
    this.el.setStyle({zIndex:100});
  },

  positionIFrame:function(){
    $(this.iframe).absolutize();
    $(this.iframe).clonePosition(this.el);
    return this.el;
  },

  hideAllUltraPops:function(){
    $$(".UltraPop").invoke('hide');
  },

  showExclusive:function(){
    this.hideAllUltraPops();
    this.el.show();
  },

  toggleExclusive:function(){
    if(!this.el.visible()){
        this.hideAllUltraPops();
    }
    this.el.toggle();
  },

  overrideElBindings:function(){
    this.el._show = this.el.show;
    this.el._hide = this.el.hide;
    this.el._toggle = this.el.toggle;
    this.el._update = this.el.update;
    this.el.show = this.showInterject.bind(this);
    this.el.hide = this.hideInterject.bind(this);
    this.el.toggle = this.toggleInterject.bind(this);
    this.el.update = this.updateInterject.bind(this);
    this.el.showExclusive = this.showExclusive.bind(this);
    this.el.toggleExclusive = this.toggleExclusive.bind(this);
  },

  showInterject:function(){
    this.iframe.show();
    this.el._show();
    this.positionIFrame();
    return this.el
  },

  hideInterject:function(){
    this.iframe.hide();
    this.el._hide();
    return this.el;
  },

  toggleInterject:function(){
    this.iframe.toggle();
    this.el._toggle();
    if(this.el.visible()) this.positionIFrame();
    return this.el;
  },

  updateInterject:function(html){
    this.el._update(html);
    this.positionIFrame();
    return this.el;
  }

}

// UltraPop2 will correct an issue with relative positioned elements however it does not retain the elements listeners.
UltraPop2 = Class.create();
UltraPop2.prototype = {
  el:null,
  iframe:null,
  elPos:null,
  elPosOffSetX:null,
  elPosOffSetY:null,
  newIframe:null,
  newDiv:null,

  initialize:function(el){
    this.el = $(el);
    if(this.el.readAttribute("UltraPop2_init")) return;
    var tmp = $(el).previous('.UltraPop2Trigger');
    this.elPos = tmp.cumulativeOffset();
    this.elPosOffSetX = this.elPos[0];
    this.elPosOffSetY = this.elPos[1] + tmp.getHeight() + 5;
    this.overrideElBindings();
    this.el.setAttribute("UltraPop2_init", true);

    return this;
  },

  overrideElBindings:function(){
    this.el._show = this.el.show;
    this.el.show = this.createPopUp.bind(this);
  },

  createPopUp:function(){
    this.removeAllPops();

    this.newDiv = $(document.createElement('div'))
                  .update(this.el.innerHTML)
                  .addClassName(this.el.className + " UltraPopTemp")
                  .setStyle({'position':'absolute', 'zIndex':'20', 'left':(this.elPosOffSetX + 'px'), 'top':(this.elPosOffSetY + 'px')})
    document.body.appendChild(this.newDiv);
    this.newDiv.closer = this.removePop.bind(this);

    this.newIframe = $(document.createElement('iframe'))
                     .addClassName("UltraPopTemp")
                     .setStyle({'position':'absolute', 'zIndex':'19', 'border':'0', 'frameborder':'0px', 'background-color':'transparent', 'left':(this.elPosOffSetX + 'px'),
                                'top': (this.elPosOffSetY + 'px'), 'width': (this.newDiv.getWidth() + 'px'),
                                'height': (this.newDiv.getHeight() + 'px')})
                                // Removed ".writeAttribute("scrolling", "no")" due to conflict with other JS on Dealership Search Results page
                     .writeAttribute("src", "/inc/blank.html")
                     .writeAttribute("allowtransparency", "true");
    document.body.appendChild(this.newIframe);

  },

  removePop:function(){
    $A([this.newDiv, this.newIframe]).invoke("remove");
  },

  removeAllPops:function(){
    $$(".UltraPopTemp").invoke("remove");
  }
}


// UltraPop3 is same as UltraPop2, except that it is re-named to use it for CTR project.( it is not initialized using _UltraPop_init() function)
// UltraPop3 will correct an issue with relative positioned elements however it does not retain the elements listeners.
//UltraPop3 is developed so that it can be initialized whenever it is required/subjected to some condition or waiting time.
UltraPop3 = Class.create();
UltraPop3.prototype = {
  el:null,
  iframe:null,
  elPos:null,
  elPosOffSetX:null,
  elPosOffSetY:null,
  newIframe:null,
  newDiv:null,

  initialize:function(el){
    this.el = $(el);
    if(this.el.readAttribute("UltraPop3_init")) return;
    var tmp = $(el).previous('.UltraPop3Trigger');
    this.elPos = tmp.cumulativeOffset();
    this.elPosOffSetX = this.elPos[0];
    this.elPosOffSetY = this.elPos[1] + tmp.getHeight() + 5;
    this.overrideElBindings();
    this.el.setAttribute("UltraPop3_init", true);
    return this;
  },

  overrideElBindings:function(){
    this.el._show = this.el.show;
    this.el.show = this.createPopUp.bind(this);
  },

  createPopUp:function(){
    this.removeAllPops();

    this.newDiv = $(document.createElement('div'))
                  .update(this.el.innerHTML)
                  .addClassName(this.el.className + " UltraPopTemp")
                  .setStyle({'position':'absolute', 'zIndex':'20', 'left':(this.elPosOffSetX + 'px'), 'top':(this.elPosOffSetY + 'px')})
    document.body.appendChild(this.newDiv);
    this.newDiv.closer = this.removePop.bind(this);

    this.newIframe = $(document.createElement('iframe'))
                     .addClassName("UltraPopTemp")
                     .setStyle({'position':'absolute', 'zIndex':'19', 'border':'0', 'frameborder':'0px', 'background-color':'transparent', 'left':(this.elPosOffSetX + 'px'),
                                'top': (this.elPosOffSetY + 'px'), 'width': (this.newDiv.getWidth() + 'px'),
                                'height': (this.newDiv.getHeight() + 'px')})
                                // Removed ".writeAttribute("scrolling", "no")" due to conflict with other JS on Dealership Search Results page
                     .writeAttribute("src", "/inc/blank.html")
                     .writeAttribute("allowtransparency", "true");
    document.body.appendChild(this.newIframe);

  },

  removePop:function(){
    $A([this.newDiv, this.newIframe]).invoke("remove");
  },

  removeAllPops:function(){
    $$(".UltraPopTemp").invoke("remove");
  }
}

function _UltraPop3_init(){
        var ultrapop3 = $$(".UltraPop3");
        ultrapop3.each(function(pop) {
            new UltraPop3(pop);
    });
}
//End of UltraPop3 function
//Please do not include UltraPop3 initialization in  _UltraPop_init() function

function _UltraPop_init(){
    var ultrapop = $$(".UltraPop");
    var ultrapop2 = $$(".UltraPop2");

    if(ultrapop != "") {
        $(ultrapop).each(function(pop) {
            new UltraPop(pop);
        });
    };
    if(ultrapop2 != "") {
        $(ultrapop2).each(function(pop) {
            new UltraPop2(pop);
        });
    };
};

Event.observe(window, "load", _UltraPop_init);
