/*
 * @access public
 * @description show/hide submit button in register form
 * @return void
 */
function setLicenseShow()
{
  if (document.register_user.agreement.checked )
  {
    $('#submi').css('display', 'block');
  } else
  {
    $('#submi').css('display', 'none');
  }
}

/*
 * @access public
 * @description show/hide submit button in register form
 * @return void
 */
function hideAgreement()
{
  $("#center_main").css('display','block');
  $("#center_hidden").css('display','none');
}

function showCopyright(selector)
{
  if($(selector).css('display') == 'none') {
    $(selector).css('display','block');
  }
  else {
    $(selector).css('display','none');
  }
}

/*
 * @access public
 * @description show/hide submit button in register form
 * @return void
 */
function loadAgreement()
{  
  $("#center_hidden").load("/static/agreement");
  $("#center_main").css('display','none');
  $("#center_hidden").css('display','block');
}

function doPopup()
{
  $('#main_popup').css("visibility", "visible");
  $('#greybg').css("visibility", "visible");
}

function removePopup()
{
  $('#main_popup').css("visibility", "hidden");
  $('#greybg').css("visibility", "hidden");
  deAttachPopup();
}

/*
 * @access public
 * @description added html of popup in DOM tree
 * @return void
 */
function attachPopup()
{
  if(isExists('#main_popup') || isExists('#greybg'))
  {
    return false;
  }
  $('body').prepend('<div class="popup" id="main_popup" name="main_popup"></div>');
  $('#wrap').prepend('<div class="greybg" id="greybg"></div>');
}

/*
 * @access public
 * @param string selector
 * @descrirption check if exists elemt with specified selector
 * @return bool
 */
function isExists(selector)
{
  return $(selector).length > 0;
}

/*
 * @access public
 * @description remove html popup from DOM document
 * @return void
 */
function deAttachPopup()
{
  $('#main_popup').remove();
  $('#greybg').remove();
}

/*
 * @access public
 * @param string selector
 * @param int max img width
 * @param max img height
 * @description Check img size for IE6(for other browser user css max-width,max-height).
 *  Images fro check select by selector
 * @return void
 */
function imgSizeCheck(accessor,width_max,height_max)
{
  if($.browser.msie && $.browser.version.charAt(0) == '6') {
    $(accessor).each(function (i) {
        this.width = (this.width > width_max) ? width_max : this.width;
        this.height = (this.height > height_max) ? height_max : this.height;
      }
    );
  }
}

/*
 * @access public
 * @param obj selectbox obj
 * @param string form selector
 * @description show/hide slectboxes with phone models by selected vendor
 * @return void
 */
function showModels(obj,selectorForm) {
  var vendor = new Array('nokia', 'sony_erricsson');
  for (var i=0; i!=vendor.length; i++) {
    var selector = '#'+vendor[i];
//    alert("Selector at " + i + " is [" + selector+ "] value is " + obj.val());
    if(obj.val() == vendor[i])
    {
//      alert("show " + selector);
      $(selector).css('display','inline');
    } else 
    {
//      alert("hiding " + selector);
      $(selector).css('display','none');
    }
    $(selector).attr('selectedIndex',0);
  };
  $(selectorForm).hide();  
}

/*
 * @access public
 * @param obj selectbox obj
 * @param string form selector
 * @description show/hide send form. Check if default value is selected
 * @return void
 */
function showSendForm(obj, selector) {
  if(obj.attr('selectedIndex') != 0) {
    $(selector).show();
  }
  else {
    $(selector).hide();
  }
}

/**
 * @access public
 * @param string form selector
 * @param string selector for laod data
 * @details submit form by formSelector with ajax request and load answer by dataSelector
 */
function sbmForm(formSelector,dataSelector) {
  var str = $(formSelector).serialize();
    $.post($(formSelector).attr('action'), str, function(data) {
      $(dataSelector).html(data);
    });
}

/**
 * @class Loader
 * @details implements ajax request logic with history. 
 *  Used links such as: /path/path#?var=1&var=2
 */
Loader = function() {
  var processSelector = '#status';

  /**
   * @acess private
   * @param string url
   * @details
   * @return 
   */
  parseUrl = function(url) {
    return url.replace('#','');
  }

  return {
    
    /**
     * @access public
     * @param string url
     * @param string selector
     * @details make ajax request by passed url and laod answer by selector
     *  Example: /video#?page=2 --> ajax call: /video?page=2
     * @return void
     */
    load : function(url,selector) {
      var url = parseUrl(url);
      if(url) {
        $(selector).load(url,'',function(){$(processSelector).hide()});
      };
    },
  
    /**
     * @access public
     * @param string selector
     * @details set process selector. This layout show on request and hide after. Wait img fro example
     * @return void
     */
    setProcessSelector : function(selector) {
      processSelector = selector;
    }
  }
}();

/**
 * @class Comments
 * @details implements some callbacks for comment admin section functionality
 *  Inject in init method of CommentsHelper.
 * @See CommentsHelper
 */
Comments = function() {

  /**
   * @acess private
   * @param object clecked object
   * @details return object with commentAttach class.
   * @return object
   */
  getCommentAttach = function(obj) {
    return  $($(obj).parents()[0]).prevAll('.commentAttach')
  }

  /**
   * @acess private
   * @param object clecked object
   * @details return objct with class text
   * @return object
   */
  getComment = function(obj) {
    return $($(obj).parents()[1]).children('.text');
  }

  return {
    
    /**
     * @access public
     * @param string server answer in json format
     * @param object jedit
     * @param object obj editable text obj
     * @details parse json and attach new values of comment text and attached file id in html
     * @return bool
     */
    afterSubmit: function(result,jedit,obj) {
      if(!result) {
        $(obj).attr('innerHTML',jedit.beforeContent);
        return false;
      }
      var data = $.parseJSON(result);
      if(!data['text'] || data['fileId'] == undefined) {
        $(obj).attr('innerHTML',jedit.beforeContent);
        return false;
      }
      $(obj).attr('innerHTML',data['text']);
      getCommentAttach(obj).attr('id',data['fileId']);
      return true;
    },
  
    /**
     * @access public
     * @param object jedit
     * @param object obj editable text obj
     * @details get all data and send them to server via ajax. Data is fileId, commentId, text
     * @return void
     */
    beforeSubmit : function(jedit,obj) {
      jedit.beforeContent = $(obj).attr('revert');
      var attachId = getCommentAttach(obj).attr('id');
      if(attachId) {
        jedit.submitdata = {fileId : attachId,json: 1};
      }
      else {
        jedit.submitdata = {json: 1};
      };
    },

    /**
     * @access public
     * @param clicked object
     * @details simulate click on text fro exec jedit listener
     * @return void
     */
    editButtonCallBack : function(obj) {
      getComment(obj).click();
    },

    /**
     * @access public
     * @param clicked object
     * @details delete comment with ajax request and than delete div with comment.
     *  Using  getComment method
     * @return void
     */
    deleteButtonCallBack : function(obj) {
      var commentId = getComment(obj).attr('id');
      $.post(deleteAction,{id:commentId},function(){
        $($('#'+commentId+'').parents()[1]).remove();
      });
    },

    /**
     * @access public
     * @param string action url for delete request
     * @return void
     */
    setDeleteAction : function(delAction) {
      deleteAction = delAction;
    }
  }
}();

