var PageAsk = Class.create( {}, {

  init: function () {
    
    $('#TopQuestionsContainer').topqrotator({getQuestionsAction: this._serverData.aActions.sGetTopQuestionsAjax});
    this.iMaxMainWords = 20;
    this.iMaxDetailsWords = 50;

    this.iMinMainTextareaHeight = 50;
    this.iMinDetailsTextareaHeight = 12;

    this.oSubmitButton = $('#' + this._serverData.aDom.sSubmitButtonId);

    this.oSubmitButton.click(this.getHandler(this, 'loadAnonymousDialog'));
   
    this.initTextarias();
    this.initDomObjects();
    
  },//This is a last punched card of init function

  postInit:function() { },//This is a last punched card of postInit function

  loadAnonymousDialog: function() {

    this.inputUserId = $('#' + this._serverData.aDom.sInputIdUserId);

    if(!this._serverData.bUserIsLogin)
    {
      var data = {};
      data[this._serverData.aActions.sLoadAnonymousDialog] = true;
      data['maintext'] = this.oQuestionInput.val();
      data['details']  = this.oDetailsInput.val();

      $.post(this._serverData.sRequestUrl, data, this.getHandler(this.onLoadAnonymousDialog));
    }else
    {
      this.AddAsk();
    }

  },//This is a last punched card of loadAnonymousDialog function

  onLoadAnonymousDialog: function(mResponse) {

    var mResponse = eval('('+mResponse+')');

    if(mResponse.anonymousDialog) {

      $('body').prepend(mResponse.anonymousDialog.html);

      var oControlsManager = ControlsManager.getInstance();

      oControlsManager.addAjaxControl(mResponse.anonymousDialog.data, this.getHandler(this, 'onAddAnonymousDialogControl'));

    }

  },//This is a last punched card of onLoadAnonymousDialog function


  onAddAnonymousDialogControl: function () {

    this._controls.anonymousDialog.addEventListener('SubmitTextEvent', this.getHandler(this, 'onSubmitTextEvent'));
    this._controls.anonymousDialog.addEventListener('CloseDialogEvent', this.getHandler(this, 'onCloseDialogEvent'));
    
    this._controls.anonymousDialog.setMainText(this.oMainWordsCounter.trim(this.oQuestionInput.val()));
    this._controls.anonymousDialog.setDescription(this.oDetailsWordsCounter.trim( this.oDetailsInput.val()));

    this._controls.anonymousDialog.showDialog();

  },//This is a last punched card of onAddAnonymousDialogControl function


  onSubmitTextEvent: function (sMainText, sDetailsText, sDialogType/*, sCaptchaId, sCaptchaValue*/) {

    this.oQuestionInput.val(sMainText);

    this.oDetailsInput.val(sDetailsText);
    
    this.AddAsk();


  },//This is a last punched card of onSubmitTextEvent function


  onCloseDialogEvent: function (sMainText, sDetailsText, sDialogType) {

    if(sMainText.length)
      this.oQuestionInput.val(sMainText);

    if(sDetailsText.lebgth)
      this.oDetailsInput.val(sDetailsText);
    
        //crutch
    this._controls.anonymousDialog.oDialogBlock.remove();

  },//This is a last punched card of onCloseDialogEvent function



  initDomObjects: function()
  {
   /* this.oQuestionWordCounterBlock = $('#'+this._serverData.aDom.sQuestionWordCounterBlockId);
    this.oQuestionWordCounterBlock.click(function(){$('#'+this._serverData.aDom.sQuestionInpId).focus();});
  

    this.oDetailsWordCounterBlock = $('#'+this._serverData.aDom.sDetailsTextareaCounterId);
    this.oDetailsWordCounterBlock.click(function(){this.oDetailsInput.focus();});*/
    this.oErrorBlock = $('#' + this._serverData.aDom.sErrorMessBlockId);
  },




  initTextarias: function()
  {
    //Set words counter
    this.oMainWordsCounter = new wordClass(this._serverData.aDom.sQuestionInpId, null, this._serverData.aDom.sQuestionWordCounterBlockId, this.iMaxMainWords);
    //this.oMainWordsCounter.calculateWords();

    this.oDetailsWordsCounter = new wordClass(this._serverData.aDom.sDetailsInpId, null, this._serverData.aDom.sDetailsTextareaCounterId, this.iMaxDetailsWords);
    //this.oDetailsWordsCounter.calculateWords();

    this.oQuestionInput = $('#'+this._serverData.aDom.sQuestionInpId);

    //Set autogrow
    this.oQuestionInput.autogrow({minHeight: this.iMinMainTextareaHeight});

    this.oQuestionInput.focus(this.getHandler(this, 'QuestionFocus'));
    this.oQuestionInput.blur(this.getHandler(this, 'QuestionBlur'));

    this.AskText = this.oQuestionInput.val();

    this.oDetailsInput = $('#'+this._serverData.aDom.sDetailsInpId);
    
    this.oDetailsInput.autogrow({minHeight: this.iMinMainTextareaHeight});

    this.oDetailsInput.focus(this.getHandler(this, 'DetailsFocus'));
    this.oDetailsInput.blur(this.getHandler(this, 'DetailsBlur'));
    
    this.AddText =  this.oDetailsInput.val();
  },


  QuestionFocus: function()
  {
    if(this.oQuestionInput.val()==this.AskText)
    this.oQuestionInput.val("");
  },

  QuestionBlur:function()
  {
    if(this.oQuestionInput.val()=='')
    this.oQuestionInput.val(this.AskText);
  },

  DetailsFocus: function()
  {
    if(this.oDetailsInput.val()==this.AddText)
    this.oDetailsInput.val("");
  },

  DetailsBlur: function()
  {
    if(this.oDetailsInput.val()=='')
    this.oDetailsInput.val(this.AddText);
  },
  
  
  AddAsk: function()
  {
      if(this.checkUserText())
        $('#'+this._serverData.aDom.sAskFormId).submit();
  },

  checkUserText: function () {

    var bResult = true;

    var sMessage = "";
    //this.oMainWordsCounter.calculateWords();
    //this.oDetailsWordsCounter.calculateWords();
    var sMainText = this.oMainWordsCounter.trim(this.oQuestionInput.val());

    if(sMainText == this.AskText || sMainText.length == 0) {
      this.showError("Enter ask please!");
      //alert("Enter ask please!");
      bResult = false;

    } else if( this.oMainWordsCounter.countWords > this.iMaxMainWords ) {
      this.showError("Too many words in question! Please delete some words!");
      //Alert("Too many words in question! Please delete some words!");
      bResult = false;

    } else if(this.strip_tags(sMainText) != sMainText) {
      this.showError("Oh, sorry, Snippets doesn't allow profanity or html.");
      //alert("Oh, sorry, Snippets doesn't allow profanity or html.");
      bResult = false;

    }



      var sDetailsText = this.oDetailsWordsCounter.trim( this.oDetailsInput.val() );
      
      if(sDetailsText == this.AddText)
        this.oDetailsInput.val("");
      else if( this.oDetailsWordsCounter.countWords > this.iMaxDetailsWords ) {
        this.showError("Too many words in details! Please delete some words!");
        //alert("Too many words in details! Please delete some words!");
        bResult = false;

      } else if(this.strip_tags(sDetailsText) != sDetailsText) {
        this.showError("Oh, sorry, Snippets doesn't allow profanity or html.");  
      
          //alert("Oh, sorry, Snippets doesn't allow profanity or html.");
        bResult = false;
      }
    if(bResult)
      this.hideError();
    return bResult;

  },//This is a last punched card of checkUserText function

  //Helpers

  strip_tags: function(str, allowed_tags) {

    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';

    var replacer = function(search, replace, str) {
      return str.split(search).join(replace);
    };

    // Build allowes tags associative array
    if (allowed_tags) {
      allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
    }

    str += '';

    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);

    // Go through all HTML tags
    for (key in matches) {

      if (isNaN(key)) {
        // IE7 Hack
        continue;
      }

      // Save HTML tag
      html = matches[key].toString();

      // Is tag not in allowed list? Remove from str!
      allowed = false;

      // Go through all allowed tags
      for (k in allowed_array) {
        // Init
        allowed_tag = allowed_array[k];
        i = -1;

        if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
        if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
        if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}

        // Determine
        if (i == 0) {
          allowed = true;
          break;
        }
      }

      if (!allowed) {
        str = replacer(html, "", str); // Custom replace. No regexing
      }
    }

    return str;

  },//This is a last punched card of strip_tags function
  
   showError: function (sErrorText) {
  
    this.hideError();
    
    this.oErrorBlock.text(sErrorText);
    this.oErrorBlock.attr('class', 'ErrorBlock');
    this.oErrorBlock.show('normal');
  
  },//This is a last punched card of showError function 
  
  hideError: function () {
  
    if( this.oErrorBlock.css('display') != 'none' ) {
    
      this.oErrorBlock.text('');
      
      this.oErrorBlock.hide();
      
    }
  
  }//This is a last punched card of hideError function 

}, [Page] );