Drupal.behaviors.playerOpen = function(context) {
  for (var base in Drupal.settings.playerOpen) {
    if (!$(base + '.player_open_processed').size()) {
      var element_settings = Drupal.settings.playerOpen[base];
      $(element_settings.selector).each(function(i, item) {
      	var settings = {}
        for (prop in element_settings) {
        	settings[prop] = element_settings[prop];
        }
        settings.element = this;
        settings.target_page = $(this).attr('href');
        if (settings.target_page) {
          var simpleEvent = new Drupal.simpleEvent(base, settings);
        }
      });
      $(base).addClass('player_open_processed');
    }
  }
};


Drupal.playerEvents = {};

Drupal.playerEvents.openPlayer = function(_self, options) {
  var width = options.width || 760;
  var height = options.height || 620;
  _self.utilsPopupWin = window.open(options.target_page, 'PLAYER', "width="+width+",height="+height+',scrollbars=no,resize=yes');
  if (_self.utilsPopupWin){
    _self.utilsPopupWin.focus();
    var popupCaller = {}
    popupCaller._self = _self;
    popupCaller.options = options;
    _self.utilsPopupWin.popupCaller = popupCaller;
  }
  else {
    Drupal.simpleEvents.utils.notifyError("Sorry, there was a problem opening the popup window.  Please check your popup blocker.", true);
  }
  return false;
}

Drupal.playerEvents.onBeforeSMSLightbox = function() {
  $('#mediaplayerbox').addClass('offscreen_item');
}

Drupal.playerEvents.onCloseSMSLightbox = function() {
  $('#mediaplayerbox').removeClass('offscreen_item');	
}

$(document).ready(function(){
		$(document).bind("onBeforeSMSLightbox", Drupal.playerEvents.onBeforeSMSLightbox);
	  $(document).bind("onCloseSMSLightbox", Drupal.playerEvents.onCloseSMSLightbox);
	}
);

Drupal.playerEvents.launch_sms_lightbox = function() {
  Masthead.openStationSMSLightbox();
  return false;
}

Drupal.playerEvents.launch_email_host = function(show_page) {
  var win = window.open(show_page+"?show_email=1", 'MAINWINDOW');
  if (win) {
  	win.focus();
  }
  else {
    alert('Sorry, there was a problem opening the show page.  If you have a popup blocker enabled please allow popups from this site.')
  }
}
