$(document).ready(function(){
  wrKey.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/','',''));
    }
   }
});

});

/* 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(){
    /*Cufon.replace('.render, h1:not(.noRender), h2:not(.noRender), h3:not(.noRender)', {
      hover: true
  });*/
}

/**
 * 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]  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
