$(document).ready(function(){
  wrSetup.init();
  wrKey.init();
  viewAdjustment.init();
  wrUI.init();

  //Go to WorkRoom
  $(window).keypress(function(ev) {
    if(ev.which == 119){
      if(wrKey.value.ctrl && wrKey.value.alt){
        ev.preventDefault();
        void(window.open('/workroom/','',''));
      }
     }
  });

});
/* General setup [start] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
var wrSetup = {
  value: {

  },

  init: function(){
    this.initOnce();
  },

  initOnce: function(){
    if(this.value.initOnce == 1){
      return false;
    }

    $("a.blank").live('click', function(ev){
      ev.preventDefault();
      void(window.open($(this).attr('href'),'',''));
    });

    /* Makes all "/ladda-hem"-files open in a new window (ladda hem == download) */
    $('a[href*="/ladda-hem/"]').click(function(ev){
      if($(this).hasClass('noBlank')){
        // Do nothing
      } else {
        ev.preventDefault();
        void(window.open($(this).attr('href'),'',''));
      }
    });

    this.value.initOnce = 1;
    return true;
  }
}
/* General setup [end]   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/

/* Scroll [start] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
/**
 * wrScroll takes care of the most scrolls necessary in WorkRoom
 */
var wrScroll = {
    /**
     * Where do you want to scroll?
     * This scrolls the whole page, the most common scroll-type
     *
     * Todo: x isn't implemented in logic yet, do that when needed
     */
    scrollTo: function(x, y, forceDuration){
        if(forceDuration){
            var duration = forceDuration;
        } else {
            var duration = scrollDuration;
        }

        $.scrollTo({top:y, left:0}, duration);
    },

    /**
     * Calculate how long the scroll should take
     * Takes care of if the scroll would take to long by normal calculation
     * @param from
     * @param to
     */
    calculateDuration: function(from, to){
        //How far will we be scrolling? Find out difference between point A and point B
        if(from > to){
            var delta = Math.round(from - to);
        } else if(from < to){
            var delta = to - from;
        } else {
            //Seems to be equal, but better be sure and return default value
            return scrollDuration;
        }

        if(delta < scrollDuration){
            //Difference is less than default duration, fallback to default value
            return scrollDuration;
        } else {
            if(delta > 3*scrollDuration){
                //Difference is larger than three times of default value, lower it to that
                return 3*scrollDuration;
            } else {
                //Difference is in between allowed values, return it
                return delta;
            }
        }
    },

    getBlockNicePosition: function(id){

        if($(".blockIndex:first").attr('id') == id){
            return 0;
        } else {
            //return $("#" + id).position().top + firstHeightMargin + fullHeightMargin/2;
            return $("#" + id).position().top + (fullHeightMargin - firstHeightMargin);
        }
    }
}

/* Scroll  [end]  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
	Makes all links with class 'blank' open in a new window
*/
function bindLinksBlank(){
	$("a.blank").unbind('click');
	$("a.blank").click( function(ev){
    ev.preventDefault();
		void(window.open($(this).attr('href'),'',''));
	});
}

/**
 * Call this function when you want to render text, for example headlines, with Cufon
 */
function render(object){
  if(!object){
    Cufon.replace('.render');
  } else {
    Cufon.replace(object);
  }
}

/**
 * Takes 'str' and tries to remove from beginning of string what's in 'removeBefore', then tries to do the same from the
 * end of the string with 'removeAfter'.
 * @param str
 * @param removeBefore
 * @param removeAfter
 *
 * @Todo Make more smart with actual test that it is from beginning and from end of string...
 */
function filterString(str, removeBefore, removeAfter){
	if(removeBefore){
		str = str.replace(removeBefore, '');
	}

	if(removeAfter){
		str = str.replace(removeAfter, '');
	}

	return str;
}

/**
 * Validates an e-mail if it follows the correct pattern
 * @param email
 */
function validateEmail(email){
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   } else {
     return true;
   }
}

/* Keystroke-handling [start] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/**
 * Container for sound-functionality
 */
var wrKey = {
  value: {
    ctrl: 0,
    alt: 0,
    shift: 0
  },

  init: function(){
    wrKey.initOnce();
  },

  initOnce: function(){
    if(wrKey.value.initOnce == 1){
      return false;
    }
    //Bind for keydown
    $(document).keydown(function(ev) {
      if (ev.keyCode == '17') {
         wrKey.value.ctrl = 1;
       } else if (ev.keyCode == '18') {
         wrKey.value.alt = 1;
       } else if (ev.keyCode == '16') {
         wrKey.value.shift = 1;
       }
    });

    //Bind for key up
    $(document).keyup(function(ev) {
      if (ev.keyCode == '17') {
         wrKey.value.ctrl = 0;
       } else if (ev.keyCode == '18') {
         wrKey.value.alt = 0;
       } else if (ev.keyCode == '16') {
         wrKey.value.shift = 0;
       }
    });
  }
};

/* Keystroke-handling  [end]  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* WR User Interface [start] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/

var wrUI = {
  value: {

  },

  /**
   * WorkRoom User Interface, initializing function
   * Everything in this function needs to be re-init-safe.
   */
  init: function(){
    wrUI.initOnce();
    $(".buttonset").buttonset();
    wrButton.init();
    // $(".wrAutoSuggest").chosen();
    $("button.previous").button({ icons: { primary: "ui-icon-carat-1-w" } });
    $("button.next").button({ icons: { secondary: "ui-icon-carat-1-e" } });

    //$('table.tableHover').tableHover({colClass: 'colHover', headCols: true, footCols: true, clickClass: 'click'});

    //wrTip.init();
  },

  initOnce: function(){
    if(wrUI.value.initOnce == 1){
      return false;
    }

    $("button.noAction").live('click', function(ev){
      ev.preventDefault();
      ev.preventExtensions();
      return false;
    });

    wrUI.value.initOnce = 1;
    return true;
  }
};

/* WR User Interface  [end]  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/

/* Buttons [start] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
var wrButton = {
  init: function(){
    $("button.wrButton").button();

    $(".wrButton-edit, .wrButton_edit").button({
        icons: {
            primary: "ui-icon-pencil"
        },
        text: false
    });

    $(".wrButton-add").button({
        icons: {
            primary: "ui-icon-plusthick"
        },
        text: false
    });

    $(".wrButton-delete, .wrButton_delete").button({
        icons: {
            primary: "ui-icon-circle-close"
        },
        text: false
    });

    $(".wrButton-active").button({
        icons: {
            primary: "ui-icon-power"
        },
        text: false
    });

    $(".wrButton-approve").button({
        icons: {
            primary: "ui-icon-check"
        },
        text: false
    });

    $(".wrButton-reject").button({
        icons: {
            primary: "ui-icon-close"
        },
        text: false
    });

    $(".wrButton-profile").button({
        icons: {
            primary: "ui-icon-contact"
        },
        text: false
    });
  }
};
/* Buttons  [end]  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/

/* Param-link [start] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
var wrParam = {
  value: {

  },

  // Holds the wrParams for current device - the params are loaded through wrRequest.php, can be "get"-params from the URL
  param: {

  },

  init: function(){
    wrParam.initOnce();
  },

  initOnce: function(){
    if(wrParam.value.initOnce){
      return false;
    }

    this.loadAutoParams();

    wrParam.value.initOnce = 1;
  },

  /**
   * Fetches the autoloaded wrParam-values
   * These values are most likely set in the wrRequest.php-file
   * Most commonly used is the params set in URL, for example
   * http://www.engstream.se/workroom/user/param:userID=123
   *
   * Reach a param with JavaScript by wrParam.param.userID where "userID" is the actual param-key
   */
  loadAutoParams: function(){
    $('input[type="hidden"][id^="wrValueParam_"]').each(function(){
      paramKey = wrFunction.filterString($(this).attr('id'), 'wrValueParam_');
      paramValue = $(this).val();
      eval('wrParam.param.' + paramKey + ' = "' + paramValue + '";');
    });
  },

  getParam: function(paramSource, specific){
    switch(typeof(paramSource)){
      case 'object':
        if(typeof(paramSource.attr('href')) !== 'undefined'){
          //Might be a link
          var paramsRaw = wrFunction.filterString(paramSource.attr('href'), 'param:').split(",");
        } else if(typeof(paramSource.val()) !== 'undefined'){
          //Might be a button
          var paramsRaw = wrFunction.filterString(paramSource.val(), 'param:').split(",");
        }
      break;
      case 'string':
        var paramsRaw = wrFunction.filterString(paramSource, 'param:').split(",");
      break;
      default:
        return false;
      break;
    }

    var params = new Array();
    var i;
    for(i in paramsRaw){
      thisParam = paramsRaw[i].split("=");
      params[thisParam[0]] = thisParam[1];
    }
    if(specific){
      return params[specific];
    } else {
      return params;
    }
  }
};
/* Param-link  [end]  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* Date-handling [start] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/

var wrDate = {
  value: {

  },

  init: function(){
    wrDate.initOnce();
  },

  initOnce: function(){
    if(wrDate.value.initOnce == 1){
      return false;
    }

    //Setup the quicklinks for datepickers and timearrows
    $('a[id*="_quickLink_"]').live('click', function(ev){
      ev.preventDefault();

      $this = $(this);
      thisID = $this.attr('id');
      datepickerID = thisID.substr(0, thisID.lastIndexOf('_',thisID.lastIndexOf('_')-1));

      switch(thisID.substr(thisID.lastIndexOf('_') + 1)){
        case 'weekAgo':
          $("#" + datepickerID).datepicker("setDate", wrDate.makeDate(false, 'date', 0, 0, -7));
        break;
        case 'yesterday':
          $("#" + datepickerID).datepicker("setDate", wrDate.makeDate(false, 'date', 0, 0, -1));
        break;
        case 'today':
          $("#" + datepickerID).datepicker("setDate", wrDate.makeDate(false, 'date'));
        break;
        case 'tomorrow':
          $("#" + datepickerID).datepicker("setDate", wrDate.makeDate(false, 'date', 0, 0, 1));
        break;
        case 'now':
          $("#" + datepickerID + 'Hour').val(wrDate.makeDate(false, 'getHours'));
          $("#" + datepickerID + 'Minute').val(wrDate.makeDate(false, 'getMinutes'));

          wrDate.semiValidateTime($("#" + datepickerID + 'Hour'));
          wrDate.semiValidateTime($("#" + datepickerID + 'Minute'));
        break;
      }
    });

    //When writing some value in the hour-inputs, semi-validate it (autocorrect)
    $(".publishTime input").live('blur', function(){
      $this = $(this);
      wrDate.semiValidateTime($this);
    });

    wrDate.value.initOnce = 1;
    return true;
  },

  semiValidateTime: function(timeObj){
    if(timeObj.attr('id').indexOf('Minute')){
      if(timeObj.val().length == 1){
        if(timeObj.val().match(/^[0-9]{1}/)){
          timeObj.val('0' + timeObj.val());
        } else {
          timeObj.val('00');
        }
      } else if(timeObj.val().length == 0){
        timeObj.val('00');
      } else if(timeObj.val().length > 2){
       timeObj.val('00');
      } else if(timeObj.val().length == 2){
        if(!timeObj.val().match(/^([0-5]{1}[0-9]{1})/)){
          timeObj.val('00');
        }
      }
    } else {
      if(timeObj.val().length == 1){
        if(timeObj.val().match(/^[0-9]{1}/)){
          timeObj.val('0' + timeObj.val());
        } else {
          timeObj.val('00');
        }
      } else if(timeObj.val().length == 0){
        timeObj.val('00');
      } else if(timeObj.val().length > 2){
       timeObj.val('00');
      } else if(timeObj.val().length == 2){
        if(!timeObj.val().match(/^(0[0-9]{1}|1[0-9]{1}|2[0-3]{1})/)){
          timeObj.val('00');
        }
      }
    }
  },

  getDatepickerDate: function(datepickerID){
    var day = $("#" + datepickerID).datepicker('getDate').getDate();
    if(day < 10){
        day = "0" + day;
    }
    var month = $("#" + datepickerID).datepicker('getDate').getMonth() + 1;
    if(month < 10){
        month = "0" + month;
    }
    var year = $("#" + datepickerID).datepicker('getDate').getFullYear();

    return year + '-' + month + '-' + day;
  },

  initDatePicker: function(object, changeControl){
    object.datepicker({
        dateFormat: "yy-mm-dd",
        dayNames: ['Söndag', 'Måndag', 'Tisdag', 'Onsdag', 'Torsdag', 'Fredag', 'Lördag'],
        dayNamesMin: ['Sö', 'Må', 'Ti', 'On', 'To', 'Fr', 'Lö'],
        dayNamesShort: ['Sön', 'Mån', 'Tis', 'Ons', 'Tors', 'Fre', 'Lör'],
        monthNames: ['Januari','Februari','Mars','April','Maj','Juni','Juli','Augusti','September','Oktober','November','December'],
        monthNamesShort: ['Jan','Feb','Mar','Apr','Maj','Jun','Jul','Aug','Sep','Okt','Nov','Dec'],
        firstDay: 1
    });

    if(changeControl){
      object.datepicker( "option", "changeMonth", true );
      object.datepicker( "option", "changeYear", true );
    }
  },

  glueTimeArrows: function(){
    //Logic for up and down-arrows for setting time
    $("a[class^='publishTime']").live('click', function(ev){
      ev.preventDefault();
      var identifier = $(this).attr('id');
      if(identifier.indexOf('Hour') > 0){
        var hour = true;
      } else {
        var hour = false;
      }
      if(identifier.indexOf('Minute') > 0){
        var minute = true;
      } else {
        var minute = false;
      }
      if(identifier.indexOf('Up') > 0){
        var direction = 'up';
      } else {
        var direction = 'down';
      }

      if(hour){
        var limit = 24;
      } else if(minute){
        var limit = 60;
      }

      if(direction == 'up'){
        var tmpID = wrFunction.filterString(identifier, 'Up');
        var tmp = eval($("#" + tmpID).val());
            tmp++;
            if(tmp >= limit){
                $("#" + tmpID).val('00');
            } else if(tmp <= 9){
                $("#" + tmpID).val('0' + tmp);
            } else {
                $("#" + tmpID).val(tmp);
            }
      } else {
        var tmpID = wrFunction.filterString(identifier, 'Down');
        var tmp = eval($("#" + tmpID).val());
            tmp--;
            if(tmp < 0){
                $("#" + tmpID).val(limit - 1);
            } else if(tmp <= 9){
                $("#" + tmpID).val('0' + tmp);
            } else {
                $("#" + tmpID).val(tmp);
            }
      }
    });
  },

  getTimestamp: function(){
    return (new Date()).getTime();
  },

  getToday: function(){
    var d = new Date();
    var year = d.getFullYear();
    var month = new String(d.getMonth() + 1);
    if(month.length < 2){
      month = '0' + month;
    }
    var day = new String(d.getDate());
    if(day.length < 2){
      day = '0' + day;
    }
    return year + '-' + month + '-' + day;
  },

  getTomorrow: function(){
    var d = new Date();
    var year = d.getFullYear();
    var month = new String(d.getMonth() + 1);
    if(month.length < 2){
      month = '0' + month;
    }
    var day = new String(d.getDate()+1);
    if(day.length < 2){
      day = '0' + day;
    }
    return year + '-' + month + '-' + day;
  },

  dateDiff: function(dateTime_1, dateTime_2, noABS){
    date1 = wrDate.makeDate(dateTime_1);
    date2 = wrDate.makeDate(dateTime_2);
    try {
      if(noABS){
        return Math.round(((date2.getTime() - date1.getTime())/(1000*60*60*24)))+1;
      } else {
        return Math.abs(Math.round(((date2.getTime() - date1.getTime())/(1000*60*60*24)))+1);
      }
    } catch(errno){
      return false;
    }
  },

  makeDate: function(dateTime, returnAs, addYear, addMonth, addDay, addHour, addMinute, addSecond){
    if(!dateTime){
      rightNow = new Date();
      dateTime = rightNow.getFullYear() + '-' + wrFunction.pad(rightNow.getMonth()) + '-' + wrFunction.pad(rightNow.getDate()) + ' ' + wrFunction.pad(rightNow.getHours()) + ':' + wrFunction.pad(rightNow.getMinutes()) + ':' + wrFunction.pad(rightNow.getSeconds());
    }

    if(!addYear){
      addYear = 0;
    }
    if(!addMonth){
      addMonth = 0;
    }
    if(!addDay){
      addDay = 0;
    }
    if(!addHour){
      addHour = 0;
    }
    if(!addMinute){
      addMinute = 0;
    }
    if(!addSecond){
      addSecond = 0;
    }

    if(dateTime.match(/^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01]) (0[0-9]|1[0-9]|2[0-3]):([0-5]{1}[0-9]{1}):([0-5]{1}[0-9]{1})$/)){
      //As example: 2011-07-02 09:47:01
      year = parseInt(dateTime.substr(0,4), 10) + addYear;
      month = wrFunction.pad(parseInt(dateTime.substr(5,2), 10) - 1 + addMonth);
      day = wrFunction.pad(parseInt(dateTime.substr(8,2), 10) + addDay);
      hour = wrFunction.pad(parseInt(dateTime.substr(11,2), 10) + addHour);
      minute = wrFunction.pad(parseInt(dateTime.substr(14,2), 10) + addMinute);
      second = wrFunction.pad(parseInt(dateTime.substr(17,2), 10) + addSecond);
      var makeDate = new Date(year,month,day,hour,minute,second);
    } else if(dateTime.match(/^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01]) (0[0-9]|1[0-9]|2[0-3]):([0-5]{1}[0-9]{1})$/)){
      //As example: 2011-07-02 09:47
      year = parseInt(dateTime.substr(0,4), 10) + addYear;
      month = wrFunction.pad(parseInt(dateTime.substr(5,2), 10) - 1 + addMonth);
      day = wrFunction.pad(parseInt(dateTime.substr(8,2), 10) + addDay);
      hour = wrFunction.pad(parseInt(dateTime.substr(11,2), 10) + addHour);
      minute = wrFunction.pad(parseInt(dateTime.substr(14,2), 10) + addMinute);
      var makeDate = new Date(year,month,day,hour,minute,0);
    } else if(dateTime.match(/^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/)){
      //As example: 2011-07-02
      year = parseInt(dateTime.substr(0,4), 10) + addYear;
      month = wrFunction.pad(parseInt(dateTime.substr(5,2), 10) - 1 + addMonth);
      day = wrFunction.pad(parseInt(dateTime.substr(8,2), 10) + addDay);
      var makeDate = new Date(year,month,day,0,0,0);
    } else {
      return false;
    }

    if(!returnAs){
      returnAs = 'object';
    }

    switch(returnAs){
      case 'getDate':
        return makeDate.getDate();
        break;
      case 'getDay':
        return makeDate.getDay();
        break;
      case 'getFullYear':
        return makeDate.getFullYear();
        break;
      case 'getHours':
        return makeDate.getHours();
        break;
      case 'getMilliseconds':
        return makeDate.getMilliseconds();
        break;
      case 'getMinutes':
        return makeDate.getMinutes();
        break;
      case 'getMonth':
        return makeDate.getMonth();
        break;
      case 'getSeconds':
        return makeDate.getSeconds();
        break;
      case 'getTime':
        return makeDate.getTime();
        break;
      case 'date':
        var year = makeDate.getFullYear();
        var month = new String(makeDate.getMonth() + 1);
        if(month.length < 2){
          month = '0' + month;
        }
        var day = new String(makeDate.getDate());
        if(day.length < 2){
          day = '0' + day;
        }
        return year + '-' + month + '-' + day;
        break;
      case 'datetime':
        var year = makeDate.getFullYear();
        var month = new String(makeDate.getMonth() + 1);
        if(month.length < 2){
          month = '0' + month;
        }
        var day = new String(makeDate.getDate());
        if(day.length < 2){
          day = '0' + day;
        }
        var hour = new String(makeDate.getHours);
        if(hour.length < 2){
          hour = '0' + day;
        }
        var minute = new String(makeDate.getMinutes());
        if(minute.length < 2){
          minute = '0' + minute;
        }
        var second = new String(makeDate.getSeconds());
        if(second.length < 2){
          second = '0' + second;
        }
        return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
        break;
      case 'object':
      default:
        return makeDate;
        break;
    }
  },

  processDate: function(dateStr){
    var datePattern = /^(\d{4})(-)(\d{2})(-)(\d{2})$/;
    var matchArray = dateStr.match(datePattern); // Is the format correct?

    if (matchArray == null) {
      return '0000-00-00';
    } else {
      return dateStr;
    }
  }
};

/* Date-handling  [end]  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/

/* wrGrid [start] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

var viewAdjustment = {
  value: {

  },

  init: function(){
    viewAdjustment.initOnce();
    //viewAdjustment.grid15();
    viewAdjustment.adjustGrid();
  },

  initOnce: function(){
    if(viewAdjustment.value.initOnce == 1){
      return false;
    }

    // When resizing the window, re-adjust
    $(window).resize(function(){
      viewAdjustment.adjustGrid();
    });

    viewAdjustment.value.initOnce = 1;
    return true;
  },

  adjustGrid: function(){
    $("#story").each(function(){
      tmpContent = $(this).width();
      $(this).find('.content').css({
        width: tmpContent - 44 + 'px'
      });
    });

    $("#story").each(function(){
      tmpGrid100 = $(this).width();

      // Grid 10%
      tmpGrid10 = Math.floor((tmpGrid100-180)/10);
      $(this).find('.wrGrid_10').css({
        width: tmpGrid10 + 'px'
      });
      $(this).find('.wrGrid_10 input[type="text"], .wrGrid_10 input[type="password"], .wrGrid_10 textarea').css({
        width: tmpGrid10 - 20 + 'px'
      });

      // Grid 20%
      tmpGrid20 = Math.floor((tmpGrid100-80)/5);
      $(this).find('.wrGrid_20').css({
        width: tmpGrid20 + 'px'
      });
      $(this).find('.wrGrid_20 input[type="text"], .wrGrid_20 input[type="password"], .wrGrid_20 textarea').css({
        width: tmpGrid20 - 20 + 'px'
      });

      // Grid 25%
      tmpGrid25 = Math.floor((tmpGrid100-60)/4);
      $(this).find('.wrGrid_25').css({
        width: tmpGrid25 + 'px'
      });
      $(this).find('.wrGrid_25 input[type="text"], .wrGrid_25 input[type="password"], .wrGrid_25 textarea').css({
        width: tmpGrid25 - 20 + 'px'
      });

      // Grid 33%
      tmpGrid33 = Math.floor((tmpGrid100-40)/3);
      $(this).find('.wrGrid_33').css({
        width: tmpGrid33 + 'px'
      });
      $(this).find('.wrGrid_33 input[type="text"], .wrGrid_33 input[type="password"], .wrGrid_33 textarea').css({
        width: tmpGrid33 - 20 + 'px'
      });

      // Grid 40%
      tmpGrid40 = Math.floor((tmpGrid100-40)/2.5);
      $(this).find('.wrGrid_40').css({
        width: tmpGrid40 + 'px'
      });
      $(this).find('.wrGrid_40 input[type="text"], .wrGrid_40 input[type="password"], .wrGrid_40 textarea').css({
        width: tmpGrid40 - 20 + 'px'
      });

      // Grid 50%
      tmpGrid50 = Math.floor((tmpGrid100-20)/2);
      $(this).find('.wrGrid_50').css({
        width: tmpGrid50 + 'px'
      });
      $(this).find('.wrGrid_50 input[type="text"], .wrGrid_50 input[type="password"], .wrGrid_50 textarea').css({
        width: tmpGrid50 - 20 + 'px'
      });
      // transform autosuggestion
      $(this).find('.wrGrid_50 .chzn-container').css({
        width: tmpGrid50 - 3 + 'px'
      });
      $(this).find('.wrGrid_50 .chzn-drop').css({
        width: tmpGrid50 - 5 + 'px'
      });
      $(this).find('.wrGrid_50 .chzn-search input').css({
        width: tmpGrid50 - 40 + 'px'
      });

      // Grid 67%
      tmpGrid67 = Math.floor(((tmpGrid100-40)/3)*2);
      $(this).find('.wrGrid_67').css({
        width: tmpGrid67 + 'px'
      });
      $(this).find('.wrGrid_67 input[type="text"], .wrGrid_67 input[type="password"], .wrGrid_67 textarea').css({
        width: tmpGrid67 - 20 + 'px'
      });

      // Grid 75%
      tmpGrid75 = tmpGrid25*3+40;
      $(this).find('.wrGrid_75').css({
        width: tmpGrid75 + 'px'
      });
      $(this).find('.wrGrid_75 input[type="text"], .wrGrid_75 input[type="password"], .wrGrid_75 textarea').css({
        width: tmpGrid75 - 20 + 'px'
      });

      // Grid 100%
      $(this).find('.wrGrid_100').css({
        width: tmpGrid100 + 'px'
      });
      $(this).find('.wrGrid_100 input[type="text"], .wrGrid_100 input[type="password"], .wrGrid_100 textarea').css({
        width: tmpGrid100 - 20 + 'px'
      });
      // transform autosuggestion
      $(this).find('.wrGrid_100 .chzn-container').css({
        width: tmpGrid100 - 3 + 'px'
      });
      $(this).find('.wrGrid_100 .chzn-drop').css({
        width: tmpGrid100 - 5 + 'px'
      });
    });
  }
};

/* wrGrid  [end]  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
