/* 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(){
		void(window.open($(this).attr('href'),'',''));
		return false;
	});
}

/**
 * Call this function when you want to render text, for example headlines, with Cufon
 */
function render(){
    Cufon.replace('.render');
}

/**
 * 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;
}
