(function () {
  window.flash_api = {
    
    /**
     * Whether the user is logged in or not.
     * Note: This is false by default and only returns true if the user is logged in AND the sessionState widget is completely loaded. 
     * You can check that with the flash_api.isSessionSatetLoaded() function.
     */
    isLoggedIn: function () {
      return window.logged_in || false;
    },
    
   /**
    * Whether the sessionState widget is done loading.
    * This is needed to know when you can start using isLoggedIn.
    */
    isSessionStateLoaded: function () {
      return window.sessionStateLoaded;
    },
    
    /**
     * Opens the login form popup.
     */
    openLogin: function () {
      if(flash_api.isLoggedIn() !== true) {
    	popupManager.hide();
    	popupManager.show(window.login_form.widget().changeView('login').jq);
	    $('#generalPopup').addClass('hidden');
	    $('#login').css({
	      top: '0px',
	      marginLeft: '290px',
	      width: '172px',
	      height: '83px',
	      zIndex: 107
	    });
      }
    },
    
    /**
     * Opens the registration form popup.
     */
    openRegistration: function () {
      createRegistration();
    },
    
    /**
     * Returns the current (known) point balance.
     * Note: This is always 0 until the pointsHistory widget is loaded. You can check that with the flash_api.isPointsBalanceLoaded() function.
     */
    getPointBalance: function () {
      return window.pointBalance || 0;
    },
    
    /**
     * Returns whether the pointsBalance has been loaded.
     */
    isPointsBalanceLoaded: function () {
      return window.pointsBalanceLoaded;
    },
    
    /**
     * Participate in a contest with a given ID.
     * You have to manually check whether the user has enough points first.
     * If the user is not logged in, the login form popup will be shown.
     * Note: This call is asynchronous!
     * 
     * TODO: Funktion erweitern um zu erlauben, dass das Widget angezeigt 
     * werden kann. Bei Prämien oder Gewinnspielen, bei denen per Dropdown 
     * noch andere Eingaben gemacht werden müssen.
     * 
     */
    enterContest: function (itemId) {
      if(flash_api.isLoggedIn()) {
	      var $wrap = $('#manual_enter_contest_div');
	      if ($wrap.length !== 0) {
	        $wrap.widget().destroy().remove();
	      }
	      $wrap = $('<div></div>', {
	        id: 'hidden_display_item_div',
	        'class': 'hidden'
	      }).appendTo('body').widget({
	        name: 'MyItemDetailWidget_teaser_one_1',
	        startView: 'showItemDetails',
	        params: 'itemId='+itemId,
	        events: {
	          subscribedToTopic: function (e) {
	            // we wait until subscibedToTopic so we can be sure the widget_internal.js is ready to receive the mcb.enter_contest event.
	            if (e.widget === this.name && e.topic === 'mcb.enter_contest') {
	              hubClient.publish('mcb.enter_contest', {itemId: itemId});
	            }
	          },
	          outgoingChange: function (e, dst) {
	            if (dst === 'LoginWidget' ) {
	              flash_api.openLogin();
	              e.preventDefault();
	            }
	          }
	        }
	      });
	      return true;
      } else {
    	  flash_api.openLogin();
      }
    },
    
    /**
     * Participate in a contest with a given ID.
     * You have to manually check whether the user has enough points first.
     * If the user is not logged in, the login form popup will be shown.
     * Note: This call is asynchronous!
     *
     * TODO: Funktion erweitern um zu erlauben, dass das Widget angezeigt 
     * werden kann. Bei Prämien oder Gewinnspielen, bei denen per Dropdown 
     * noch andere Eingaben gemacht werden müssen.
     *
     */
     
    purchaseReward: function (itemId) {
      window.flash_api.enterContest(itemId);
    }
  }
})();
