jQuery.noConflict();
jQuery(document).ready(function() {
	/*// Search stuf to be implemented laters
	jQuery('#SearchForm_SearchForm_Search').attr('value', 'Search');
	jQuery('#SearchForm_SearchForm_Search').bind('click', function() {
		if (jQuery('#SearchForm_SearchForm_Search').attr('value') == "Search" || jQuery('#SearchForm_SearchForm_Search').attr('value') == "Search must be longer") {
			jQuery('#SearchForm_SearchForm_Search').attr('value', '');
		}
	});
	jQuery('#SearchForm_SearchForm').bind('submit', function(){
		var searchString = jQuery('#SearchForm_SearchForm_Search').attr('value');
		if (searchString.length < 2 || searchString == "Search" || searchString == "Search must be longer") {
			jQuery('#SearchForm_SearchForm_Search').attr('value', "Search must be longer");
			return false;
		}
		return true;
	});*/
	jQuery('#Sidebar').stickySide(20, 154);
	
	if (typeof(hljs) != 'undefined'){
		hljs.initHighlightingOnLoad();
	}
});

jQuery(window).load(function() {
	jQuery('#TopContainer h1').textMaxWidth(610, 15);
});
/*
function isScrolledIntoView(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
      && (elemBottom <= docViewBottom) &&  (elemTop >= docViewTop) );
}*/

jQuery.fn.stickySide = function(paddingTop, startAt) {
	var obj = this;
	var initialTop = (jQuery(this).offset()) ? jQuery(this).offset().top : startAt;
	initialTop -= paddingTop;
	jQuery(window).scroll(function() {
		var docViewTop = jQuery(window).scrollTop();
		if (docViewTop > initialTop) {
			if (jQuery(obj).css('position') == 'absolute') {
				jQuery(obj).css('position', 'fixed');
				jQuery(obj).css('top',  paddingTop + 'px');
			}
		} else if (jQuery(obj).css('position') == 'fixed' ){
			jQuery(obj).css('position', 'absolute');
			jQuery(obj).css('top', '0px');
		}
	});
}
jQuery.fn.stickySide2 = function(paddingTop, startAt) {
	this.each(function(){
		var obj = jQuery(this);
		var initialTop = (obj.offset()) ? obj.offset().top : startAt;
		initialTop -= paddingTop;
		
		jQuery(window).scroll(function() {
			var docViewTop = jQuery(window).scrollTop();
			if (docViewTop > initialTop && obj.css('position') == 'relative') {
				obj.css('position', 'fixed');
				obj.css('top', paddingTop + 'px');
			} else if (docViewTop < initialTop && obj.css('position') == 'fixed') {
				obj.css('position', 'relative');
				obj.css('top', 'auto');
			}
		});
	});
}

// assumes maxWidth in px, and font-size in px
jQuery.fn.textMaxWidth = function(maxWidth, minFontSize) {
	this.each(function(){
		var width = jQuery(this).width();
		var fontSize = minFontSize + 1;
	
		while (width > maxWidth && fontSize > minFontSize) {
			fontSize = parseInt(jQuery(this).css('font-size'));
			fontSize -= 1;
			jQuery(this).css('font-size', fontSize + 'px');
			width = jQuery(this).width();
		}
	});
}
