function Infobox2(id, url, data) {
    this.id = id;
    this.placeHolderId = id + '_ib_ph';
    this.placeHolder = $('#' + this.placeHolderId);
    this.buttonsPanel = $('#' + id + '_ib_buttons');
    this.header = $('#' + id + '_ib_header');

    //  If URL is NULL then it means that box has a static content that shouldn't be loaded dynamically...
    this.url = url;
    this.data = data;

    this.loader = null;

    this.init();
}

Infobox2.prototype.init = function () {
    var oThis = this;

    $('h2', this.header).click(function () {
        oThis.toggle(); return false;
    });

    if (this.url != null) {
        this.loader = new ContentLoader(this.placeHolderId, this.url, this.data);
    }

    var hash = window.location.hash.replace("#", "");
    //  if IB was oppened before or URL contains anchor equals IB id...
    if ($.cookie(this.id + '_needShow') == '1' || hash.toLowerCase() == this.id.toLowerCase()) {
        this.show();
    }
}

Infobox2.prototype.toggle = function () {
    var oThis = this;
    var action;

    if (this.placeHolder.is(':hidden')) {
        action = 'expand';
        this.show();
    }
    else {
        action = 'collapse';
        this.hide();
    }

    //Google Analytics Tracking code for "expand" or "collapse" events.

    //Get Chemical name from the H1.
    var chemical_name = $('h1 > span').html();
    //Get csid from the permalink.
    var cs_id = $('li[class = "_quick-permalink"] > a > strong').html();
    //Get the Infobox heading.   
    var ib_heading = $('div[id = "' + oThis.id + '_ib_container"] > div > h2 > a').html();
    csEvent(ib_heading, action, chemical_name + ' | ' + cs_id);
}

Infobox2.prototype.reload = function (url, data) {
    var oThis = this;

    if (oThis.loader != null) {
        if (url != null) oThis.loader.url = url;
        oThis.loader.data = data;
        oThis.loader.reloadContent();
    }
}

Infobox2.prototype.show = function () {
    var oThis = this;
    //$(this.toggleImg).attr('src', '/images/infobox/collapse.gif');

    this.header.parent().removeClass('closed');
    this.header.parent().addClass('opened');

    this.placeHolder.slideDown('fast', function () {
        if (oThis.loader != null && !oThis.loader.loaded) {
            oThis.loader.load();
        }
    });

    //  Make Buttons panel visible...
    this.buttonsPanel.show();

    $.cookie(this.id + '_needShow', '1', { expires: 365 });
}

Infobox2.prototype.hide = function() {
    //$(this.toggleImg).attr('src', '/images/infobox/expand.gif');
    this.placeHolder.slideUp('fast');
    this.header.parent().removeClass('opened');
    this.header.parent().addClass('closed');

    //  Make Buttons panel invisible...
    this.buttonsPanel.hide();

    $.cookie(this.id + '_needShow', null);
}

