﻿/* DialogImage */
DialogImage = function()
{
  /* private members */
  var _dialog = null;
    
  /* private functions */
  
  /* dashboard dialog object */
  return {
      create: function() {
        if(!_dialog){ // lazy initialize the dialog and only create it once
            _dialog = new YAHOO.ext.BasicDialog('dialogImage', { 
                    modal:true,
                    width:700,
                    height:500,
                    shadow:true,
                    resizable: true,
                    proxyDrag: true,
                    animateTarget:'pnl-app-header',
                    autoTabs: true,
                    fixedcenter: true,
                    sizeControls: function()
                    {
                        switch (this._mode)
                        {
                            case 0:
                                var extElement = getEl('shownImage');
                                if (extElement != null)
                                {
                                    extElement.setHeight(_dialog.size.height - 110);
                                    if (extElement.getWidth() > _dialog.size.width - 50)
                                        extElement.setHeight(_dialog.size.height - 124);
                                }
                                break;
                        }
                    }
            });
            _dialog.addKeyListener(27, _dialog.hide, _dialog);
            _dialog.addButton(AjaxCalls.Translate('general.close').value, _dialog.hide, _dialog);
            _dialog.addButton(AjaxCalls.Translate('general.next').value, this.showNextImage, _dialog);
            _dialog.addButton(AjaxCalls.Translate('general.previous').value, this.showPreviousImage, _dialog);
            _dialog.el.setStyle('visibility','hidden');
            _dialog.on('resize', _dialog.sizeControls);
            // set event handlers
            var tabs = _dialog.getTabs();
            if (tabs)
                tabs.onTabChange.subscribe(this.tabSelected, this);
            var addButton = getEl('cmdSubmitComment');
            if (addButton)
                addButton.addListener('click', this.addCommentButtonClicked);
            var submitSalesButton = getEl('cmdSubmitSales');
            if (submitSalesButton)
                submitSalesButton.addListener('click', this.submitSalesButtonClicked);
        }         
      },
      
      showPreviousImage: function()
      {
        var data = AjaxCalls.GetPreviousImageData(_dialog._section, _dialog._category, _dialog._image, _dialog._salesMode).value;
        _dialog._section = data.SectionIndex;
        _dialog._category = data.CategoryIndex;
        _dialog._image = data.ImageIndex;
        _dialog._mode = 0;
        _dialog._title = data.Title;
        _dialog._price = data.Price;
        var tabs = _dialog.getTabs();
        if (tabs)
        {
            var tab = tabs.getTab('dlg-image-sales');
            if (tab)
            {
                if (_dialog._price != '')
                    tab.enable();
                else
                    tab.disable();
            }
            tab = tabs.getTab('dlg-image-comments');
            if (tab && tab.isActive())
            {
                DialogImage.updateImageCommentsTab();
            }
        }
        DialogImage.updateImageTab();
        _dialog.sizeControls();
        var txtTitle = _dialog._title;
        if (_dialog._price != '')
            txtTitle += ' - ' + _dialog._price;
        getEl('dialogTitle').update(txtTitle);
      },
      
      showNextImage: function()
      {
        var data = AjaxCalls.GetNextImageData(_dialog._section, _dialog._category, _dialog._image, _dialog._salesMode).value;
        _dialog._section = data.SectionIndex;
        _dialog._category = data.CategoryIndex;
        _dialog._image = data.ImageIndex;
        _dialog._mode = 0;
        _dialog._title = data.Title;
        _dialog._price = data.Price;
        var tabs = _dialog.getTabs();
        if (tabs)
        {
            var tab = tabs.getTab('dlg-image-sales');
            if (tab)
            {
                if (_dialog._price != '')
                    tab.enable();
                else
                    tab.disable();
            }
            tab = tabs.getTab('dlg-image-comments');
            if (tab && tab.isActive())
            {
                DialogImage.updateImageCommentsTab();
            }
        }
        DialogImage.updateImageTab();
        _dialog.sizeControls();
        var txtTitle = _dialog._title;
        if (_dialog._price != '')
            txtTitle += ' - ' + _dialog._price;
        getEl('dialogTitle').update(txtTitle);
      },
      
      addCommentButtonClicked: function ()
      {
        AjaxCalls.AddComment(_dialog._section, _dialog._category, _dialog._image, getEl('txtAuthor').dom.value, getEl('txtComment').dom.value).value;
        var tabs = _dialog.getTabs();
        if (tabs)
        {
            var tab = tabs.getTab('dlg-image-comments');
            if (tab)
                tab.activate();
        }
      },
      
      submitSalesButtonClicked: function ()
      {
        var response = AjaxCalls.SendSalesEmail(getEl('txtEmail').dom.value, getEl('txtName').dom.value, getEl('txtSubject').dom.value, getEl('txtBody').dom.value).value;
        getEl('lblMessage').update(response);
      },
      
      tabSelected: function (panel, selectedItem)
      {
        var tab = -1;
        switch (selectedItem.id)
        {
            case 'dlg-image':
                tab = 0;
                break;
            case 'dlg-image-comments':
                tab = 1;
                DialogImage.updateImageCommentsTab();
                break;
            case 'dlg-image-comments-add':
                tab = 2;
                DialogImage.updateImageCommentsAddTab();
                break;
            case 'dlg-image-sales':
                tab = 3;
                DialogImage.updateImageSalesTab();
                break;
        }
        _dialog._mode = tab;
        _dialog.sizeControls();
      },
      
      updateImageTab: function()
      {
        var tabs = _dialog.getTabs();
        if (tabs)
        {
            var tabImage = tabs.getTab('dlg-image');
            if (tabImage)
                tabImage.setContent('<img id="shownImage" src="ShowImageBinary.aspx?section=' + _dialog._section + '&category=' + _dialog._category + '&image=' + _dialog._image + '&mode=' + _dialog._salesMode + '" />', false);
        }
      },
      
      updateImageCommentsTab: function()
      {
        var tabs = _dialog.getTabs();
        if (tabs)
        {
            var tab = tabs.getTab('dlg-image-comments');
            if (tab)
            {
                var comments = AjaxCalls.GetComments(_dialog._section, _dialog._category, _dialog._image).value;
                var answer = '';
                for (var index = 0; index < comments.length; index++)
                {
                    answer += '<div class="comment">';
                    answer += '<div class="author">' + comments[index].Author + '</div>';
                    answer += '<div class="date">' + comments[index].DateString + '</div>';
                    answer += '<div class="text">' + comments[index].Text + '</div>';
                    answer += '</div>';
                }
                tab.bodyEl.update('<div class="comments">' + answer + '</div>');
            }
        }
      },
      
      updateImageCommentsAddTab: function()
      {
        var tabs = _dialog.getTabs();
        if (tabs)
        {
            getEl('txtSubject').dom.value = '';
            getEl('txtName').dom.value = '';
            getEl('txtEmail').dom.value = '';
            getEl('txtBody').dom.value = '';
        }
      },
      
      updateImageSalesTab: function()
      {
        var tabs = _dialog.getTabs();
        if (tabs)
        {
            getEl('txtAuthor').dom.value = '';
            getEl('txtComment').dom.value = '';
        }
      },
      
      show: function(animateTarget, image, section, category, salesMode, title, price) {
          if (!_dialog) {
              this.create();
          }
          if (_dialog.el.isVisible())
          {
            _dialog.hide();
            if (animateTarget != _dialog.animateTarget)
                _dialog.show(animateTarget);
          }
          else
          {
            _dialog._image = image;
            _dialog._section = section;
            _dialog._category = category;
            _dialog._salesMode = salesMode;
            _dialog._mode = 0;
            _dialog._title = title;
            _dialog._price = price;
            _dialog.show(animateTarget);
            var tabs = _dialog.getTabs();
            if (tabs)
            {
                var tab = tabs.getTab('dlg-image-sales');
                if (tab)
                {
                    if (_dialog._price != '')
                        tab.enable();
                    else
                        tab.disable();
                }
            }
            DialogImage.updateImageTab();
            _dialog.sizeControls();
            var txtTitle = _dialog._title;
            if (_dialog._price != '')
                txtTitle += ' - ' + _dialog._price;
            getEl('dialogTitle').update(txtTitle);
          }
      },          
      
      hide: function() 
      {
            if (_dialog && _dialog.el.isVisible())
                _dialog.hide();
      }
      
  }
}();




/* DialogImage */
DialogTranslation = function()
{
  /* private members */
  var _dialog = null;
    
  /* private functions */
  
  /* dashboard dialog object */
  return {
      create: function() {
        if(!_dialog){ // lazy initialize the dialog and only create it once
            _dialog = new YAHOO.ext.BasicDialog('dialogTranslation', { 
                    modal:true,
                    width:700,
                    height:500,
                    shadow:true,
                    resizable: false,
                    proxyDrag: true,
                    animateTarget:'pnl-app-header',
                    autoTabs: false,
                    fixedcenter: true
            });
            _dialog.addKeyListener(27, _dialog.hide, _dialog);
            _dialog.addButton(AjaxCalls.Translate('submit').value, this.confirm, _dialog);
            _dialog.addButton(AjaxCalls.Translate('general.close').value, _dialog.hide, _dialog);
            _dialog.el.setStyle('visibility','hidden');
        }         
      },
  
      confirm: function()
      {
        var translation = getEl('txtTranslation').dom.value;
        var response = AjaxCalls.UpdateTranslation(_dialog._language, _dialog._keyword, translation).value;
        _dialog.hide();
      },
  
      show: function(animateTarget, text, keyword, language) {
          if (!_dialog) {
              this.create();
          }
          if (_dialog.el.isVisible())
          {
            _dialog.hide();
            if (animateTarget != _dialog.animateTarget)
                _dialog.show(animateTarget);
          }
          else
          {
            _dialog._text = text;
            _dialog._keyword = keyword;
            _dialog._language = language;
            getEl('txtTranslation').dom.value = _dialog._text;
            _dialog.show(animateTarget);
          }
      },          
      
      hide: function() 
      {
            if (_dialog && _dialog.el.isVisible())
                _dialog.hide();
      }
      
  }
}();
