﻿if (Object.isUndefined(Nmma)) { var Nmma = { } };
if (Object.isUndefined(Nmma.Shared)) { Nmma.Shared = { } };
if (Object.isUndefined(Nmma.Shared.Shows)) { Nmma.Shared.Shows = { } };

Nmma.Shared.Shows.ProcessingBackgroundLayer = Class.create({
  initialize: function(options) {
    var options = options ? options : {};
    this.backgroundLayer = new Element('div', { 'id': options.id ? options.id : '_backgroundLayer' + new Date().getTime(), 'class': 'shows-processingBackgroundLayer' });
    var pageDimensions = Nmma.Utilities.getPageSize();
	  this.backgroundLayer.setStyle({ 'position': 'fixed', 'top': 0, 'left': 0, 'width': pageDimensions.pageWidth + 'px', 'height': pageDimensions.pageHeight + 'px', 'zIndex': 999999 });
    this.backgroundLayer.addClassName('shows-processingBackgroundLayer');
    this.backgroundLayer.setStyle({ 'background': '#CCC url(/lib/img/icons/shows/bigloader.gif) no-repeat center center', 'opacity': 0.10, '-moz-opacity': 0.10, 'filter': 'alpha(opacity=10)' });
    document.body.insertBefore(this.backgroundLayer, document.body.firstChild);
  },
  hide: function() {
	  this.backgroundLayer.hide();
	  this.backgroundLayer.remove();
  }
});

Nmma.Shared.Shows.Generic = {
  view: function(args) {
    var target = args.Target ? args.Target : '';
    if (args.WindowScrollTop) window.scroll(0, 0);
		$(target).update(Nmma.Shared.Shows.Generic.getLoader(args.LoaderHeight ? args.LoaderHeight : ''));
		$(target).show();
    Nmma.Ajax.request(args.Url ? args.Url : '', { 'parameters': { 'arguments': args.Arguments ? args.Arguments : {} },
		  onSuccess: function(response) { if (response.isSuccess()) { $(target).update(response.responseText); } else { alert('Error'); } },
			onFailure: function(response) { alert('Failure'); }
    });
  },
  redirect: function(url) { document.location.href = url; },  
  alternate: function(c) { var b = true; $(c).immediateDescendants().each(function(e) {if (b) $(e).addClassName('alternate'); else $(e).removeClassName('alternate'); b = !b; }); },
  getLoader: function(padding) { return '<div style="text-align:center;"><div class="nmma-loader" style="margin:auto;' + (padding ? 'padding:' + padding + 'px;"' : '"') + '></div></div>'; },
  clear: function(id) { if ($(id)) $(id).update(''); },
  hide: function(id) { if ($(id)) $(id).hide(); },
  show: function(id) { if ($(id)) $(id).show(); },
  focus: function(args) { }
};

Nmma.Shared.Shows.Show = {
  clear: function(id) {
    if ($(id)) $(id).update(''); 
  },
  hide: function(id) {
    if ($(id)) $(id).hide();
  },
  show: function(id) {
    if ($(id)) $(id).show();
  },
  oncountrychanged: function(args) { Nmma.Shared.Shows.Generic.view({ 'Url': Nmma.Forms.BaseUri(), 'Arguments': { 'ActionType': 'countrychanged', 'Id': args.Id, 'ControlHash' : args.ControlHash, 'StateElement': args.StateElement ? args.StateElement : 'State', 'PostalCodeElement': args.PostalCodeElement ? args.PostalCodeElement : 'PostalCode' }, 'Target': args.Target ? args.Target : 'divState' }); },    
  getLoader: function(padding) { return '<div style="text-align:center;"><div class="products-loader" style="margin:auto;' + (padding ? 'padding:' + padding + 'px;"' : '"') + '></div></div>'; }   
};

Nmma.Shared.Shows.Products = {
  getUrl: function() {
		return '/shared/pages/ajaxhandlers/shows/products.aspx';
  },
  getLoader: function(padding) {
		return '<div class="products-loader"' + (padding ? ' style="' + padding + 'px;" ' : '') + '></div>';
  },
  onclick: function(o) {
    o.toggleClassName('collapsed');    
    o.toggleClassName('expanded');    
    o.next().visible() ? o.next().hide() : o.next().show();
  }
};

Nmma.Shared.Shows.Participant = {
  getUrl: function() {
		return '/shared/pages/ajaxhandlers/shows/participant.aspx';
  },
  getLoader: function(padding) {
		return '<div class="products-loader"' + (padding ? ' style="' + padding + 'px;" ' : '') + '></div>';
  },
  view: function(id) {
    var popup = new Nmma.Dialog({ 'width': 600, 'cssClass': 'exhibitor', 'draggable': true, 'showButtons': false });
    popup.setContent('<div class="exhibitor_loader">&nbsp;</div>');
    popup.show();
    Nmma.Ajax.request(Nmma.Shared.Shows.Participant.getUrl(), {
      parameters: { 'arguments': { 'ActionType': 'view', 'Id': id } },
		  onSuccess: function(response) { 
		    if (response.isSuccess()) {
          popup.loadRequestSuccess(response);     
          popup.center();
		    }
		    else { popup.loadRequestFailure(response); }
		  },
		  onFailure: function(response) { popup.loadRequestFailure(response); }
  	});  
  }  
};

Nmma.Shared.Shows.Contest = {
  save: function(form, targetErrors) {
    var target = 'divContests';  
    var backgroundLayer = new Nmma.Shared.Shows.ProcessingBackgroundLayer();
    Nmma.Shared.Shows.Show.hide(targetErrors);
    Nmma.Shared.Shows.Show.clear(targetErrors);
    Nmma.Ajax.request(Nmma.Forms.BaseUri(), {
      parameters: { 'form': form, 'arguments': { 'ActionType': 'save' } },
		  onSuccess: function(response) {
        backgroundLayer.hide();
		    if (response.isSuccess()) { $(target).update(response.responseText); }
		    else {
		      $(targetErrors).update(response.getErrorsList());
		      $(targetErrors).show();
		    }
		  },
			onFailure: function(response) { backgroundLayer.hide(); alert('An error occured while executing your request'); }
    });
  },  
  viewRules: function viewRules(id, controlHash) {
    var popup = new Nmma.Dialog({ 'width': 745, 'cssClass': 'exhibitor', 'draggable': true, 'showButtons': false });
    popup.setContent('<div class="exhibitor_loader">&nbsp;</div>');
    popup.show();
    Nmma.Ajax.request(Nmma.Forms.BaseUri(), {
      parameters: { 'arguments': { 'ActionType': 'viewrules', 'Id': id, 'ControlHash': controlHash.toString() } },
		  onSuccess: function(response) { 
		    if (response.isSuccess()) { 
          popup.loadRequestSuccess(response);     
          popup.center();
		    }
		    else { popup.loadRequestFailure(response); }
		  },
		  onFailure: function(response) { popup.loadRequestFailure(response); }
  	});
  }  
};

Nmma.Shared.Shows.Newsletter = {
  subscribe: function(form, targetErrors) {
    var target = 'divNewsletter';  
    var backgroundLayer = new Nmma.Shared.Shows.ProcessingBackgroundLayer();
    Nmma.Shared.Shows.Show.hide(targetErrors);
    Nmma.Shared.Shows.Show.clear(targetErrors);
    Nmma.Ajax.request(Nmma.Forms.BaseUri(), {
      parameters: { 'form': form, 'arguments': { 'ActionType': 'save' } },
		  onSuccess: function(response) {
        backgroundLayer.hide();
		    if (response.isSuccess()) { $(target).update(response.responseText); }
		    else {
		      $(targetErrors).update(response.getErrorsList());
		      $(targetErrors).show();
		    }
		  },
			onFailure: function(response) { backgroundLayer.hide(); alert('An error occured while executing your request'); }
    });
  }
};

Nmma.Shared.Shows.Seminar = {
  getUrl: function() {
		return '/shared/pages/ajaxhandlers/shows/seminar.aspx';
  },
  viewseminar: function(id) {
    var popup = new Nmma.Dialog({ 'width': 745, 'cssClass': 'exhibitor', 'draggable': true, 'showButtons': false });
    popup.setContent(Nmma.Shared.Shows.Show.getLoader());
    popup.show();
    Nmma.Ajax.request(Nmma.Shared.Shows.Seminar.getUrl(), {
      parameters: { 'arguments': { 'ActionType': 'viewseminar', 'Id': id } },
		  onSuccess: function(response) { 
		    if (response.isSuccess()) { popup.loadRequestSuccess(response); popup.center(); }
		    else { popup.loadRequestFailure(response); }
		  },
		  onFailure: function(response) { popup.loadRequestFailure(response); }
  	});
  },
  viewspeaker: function(id) {
    var popup = new Nmma.Dialog({ 'width': 745, 'cssClass': 'exhibitor', 'draggable': true, 'showButtons': false });
    popup.setContent(Nmma.Shared.Shows.Show.getLoader());
    popup.show();
    Nmma.Ajax.request(Nmma.Shared.Shows.Seminar.getUrl(), {
      parameters: { 'arguments': { 'ActionType': 'viewspeaker', 'Id': id } },
		  onSuccess: function(response) { 
		    if (response.isSuccess()) { popup.loadRequestSuccess(response); popup.center(); }
		    else { popup.loadRequestFailure(response); }
		  },
		  onFailure: function(response) { popup.loadRequestFailure(response); }
  	});
  },
  toggle: function(e) {
    $(e).toggleClassName('expanded');
    $(e).toggleClassName('collapsed'); 
    $(e).up().next('table', 0).toggle();    
  }
};

Nmma.Shared.Shows.Request = {
  submit: function(form, targetErrors) {
    var target = 'divRequest';
    var backgroundLayer = new Nmma.ProcessingBackgroundLayer();
    if ($(targetErrors)) { $(targetErrors).update(''); $(targetErrors).hide(); }
    Nmma.Ajax.request(Nmma.Forms.BaseUri(), {
      parameters: { 'form': form, 'arguments': { 'ActionType': 'submit' } },
		  onSuccess: function(response) {
        backgroundLayer.hide();
		    if (response.isSuccess()) {
		      switch (response.getArguments().CAPTCHA)
		      {
		        case 'empty':
              $('Captcha').focus();		        
		          break;
		        case 'refresh':
              $('Captcha').value = '';
              Nmma.Shared.Captcha.load();
              $('Captcha').focus();
		          break;
		        default:
              window.scroll(0, 0);
              $(target).update(response.responseText);              
		          break;
		      }
		    }
		    else { window.scroll(0, 0); $(targetErrors).update(response.getErrorsList()); $(targetErrors).show(); }
		  },		  
			onFailure: function(response) { backgroundLayer.hide(); alert('An error occured while executing your request'); }
    });
  }
};

Nmma.Shared.Shows.SeniorityPoints= {
  sort: function(args) {
    if (args.Element.hasClassName('asc')) {
      args.Element.removeClassName('asc'); 
      args.Element.addClassName('desc');
      $('SortingOrder').value = '2';
    }
    else {
      args.Element.removeClassName('desc'); 
      args.Element.addClassName('asc');        
      $('SortingOrder').value = '1';
    }
    $('SortingColumn').value = args.Id;
    $A(args.Element.up().childElements()).each( function(e) { if (e.id != args.Element.id) { e.removeClassName('asc'); e.removeClassName('desc'); }});    
    var backgroundLayer = new Nmma.Shared.Shows.ProcessingBackgroundLayer();
    Nmma.Ajax.request(Nmma.Forms.BaseUri(), { parameters: { 'form': args.Form, 'arguments': { 'ActionType': 'sort' } },
		  onSuccess: function(response) {
        backgroundLayer.hide();
		    if (response.isSuccess()) {
		      $(args.Target).update(response.responseText); 
		    }
		    else { alert(response.getErrorsList()); document.location.href = Nmma.Forms.BaseUri(); }
		  },
			onFailure: function(response) { backgroundLayer.hide(); alert('Failure: '); document.location.href = Nmma.Forms.BaseUri(); }
    });
  }
};

