/*jslint browser: true */
/*extern Drupal, $, jQuery, Popups */

Drupal.theme.prototype.popupLoading = function() {
  var loading = '<div id="popups-loading">';
  loading += '<img src="'+ Drupal.settings.basePath + 'sites/all/modules/custom/sonybmg/popup/loading.gif" />';
  loading += '</div>';
  return loading;
};

Drupal.theme.prototype.popupButton = function(title, id) {
  return '<li><a class="button" id="'+ id +'">' + title + '</a></li>';
};

Drupal.theme.prototype.popupDialog = function(popupId, title, body, buttons) {
  var template = Drupal.theme('popupTemplate', popupId);
  var popups = template.replace('%title', title).replace('%body', body);

  var themedButtons = '';
  // FIXME: this might not actually work and it's unclear what it should do.
  if (!buttons && !$(body).is('form') && ($('form', $(body)).size() == 0)) {
    buttons = {
      'popup_return_to_site': {title: Drupal.t('Continue')}
    };
  }

  if (buttons) {
    jQuery.each(buttons, function (id, button) {
      themedButtons += Drupal.theme('popupButton', button.title, id);
    });
  }
  popups = popups.replace('%buttons', themedButtons);

  // Go ahead and convert this to a jQuery object even though that's really the
  // caller's job. Then find the first form id in the body and add that as a
  // class to make themeing a dream.
  popups = $(popups);
  var id = $('form', popups).attr('id');
  if (id) {
    popups.addClass('popup-' + id);
  }

  return popups;
};

Drupal.theme.prototype.popupTemplate = function(popupId) {
  var template;
  template = '<div id="'+ popupId + '" class="popups-box">';
  template += '  <div class="popups-title">';
  template += '    <div class="popups-close"><a href="#">' + Drupal.t('Close') + '</a></div>';
  template += '  </div>';
  template += '  <div class="popups-body"><div class="title">%title</div>%body</div>';
  template += '  <div class="popups-buttons"><ul>%buttons</ul></div>';
  template += '  <div class="popups-footer"></div>';
  template += '</div>';
  return template;
};

/**
 * We need to resize the popups-container as well as the popups if it scrolls
 */
Drupal.behaviors.resizePopupsContainer = function() {
  var popup = Popups.activePopup();
  if (popup) {
    var $popupsContainer = $('#' + popup.id + ' .popups-body');
    if ($popupsContainer.length) {
      var popupHeight = $popupsContainer.height();
      var windowHeight = $(window).height();
      if (popupHeight > (0.9 * windowHeight) ) { // Must fit in 90% of window.
        // we make this slightly smaller than popups so that it fits inside
        popupHeight = 0.85 * windowHeight;
        $popupsContainer.height(popupHeight);
      }
      $popupsContainer.height($popupsContainer.height() + 10);
      // needs an extra 20px as the bottom dropshadow looks cutoff
      var $popup = popup.$popup();
      $popup.height($popup.height() + 40);
    }
  }
};

Popups.Popup.prototype.open = function(title, body, buttons, width){
  $('embed, object').css('visibility', 'hidden');
  return Popups.open(this, title, body, buttons, width);
};

Popups.Popup.prototype.removePopup = function() {
  $('embed, object').css('visibility', 'visible');
  Popups.removePopup(this);
};

/**
 * Remove everything.
 */
Popups.Popup.prototype.close = function() {
  return Popups.close(this);
};

Drupal.behaviors.sonyPopups = function(context) {
  $(context).parents('.popups-box').find('#popup_return_to_site:not(.sony-popups-processed)')
    .addClass('sony-popups-processed')
    .click(function() {
      location.reload();
    }
  );

  // Add bgIframe support for IE6
  $(context).find('.popups-box').bgIframe();
};

/**
 * Hijack popups overlay functions so we can act on 'em.
 */
Popups._addOverlay = Popups.addOverlay;
Popups.addOverlay = function() {
  $('embed, object').css('visibility', 'hidden');
  Popups._addOverlay();
};

Popups._removeOverlay = Popups.removeOverlay;
Popups.removeOverlay = function() {
  $('embed, object').css('visibility', 'visible');
  Popups._removeOverlay();
};

/* Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: 2007-07-21 18:45:56 -0500 (Sat, 21 Jul 2007) $
 * $Rev: 2447 $
 *
 * Version 2.1.1
 */
(function($){$.fn.bgIframe=$.fn.bgiframe=function(s){if($.browser.msie&&/6.0/.test(navigator.userAgent)){s=$.extend({top:'auto',left:'auto',width:'auto',height:'auto',opacity:true,src:'javascript:false;'},s||{});var prop=function(n){return n&&n.constructor==Number?n+'px':n;},html='<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+'style="display:block;position:absolute;z-index:-1;'+(s.opacity!==false?'filter:Alpha(Opacity=\'0\');':'')+'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+'"/>';return this.each(function(){if($('> iframe.bgiframe',this).length==0)this.insertBefore(document.createElement(html),this.firstChild);});}return this;};})(jQuery);

