/*
 * General JS-file for vemringde.se, 
 * it relies heavily on jQuery 1.4.2
 * 
 * @author: Carl-Fredrik Herö, carlfredrik.hero@gmail.com
 */


jQuery(document).ready(function() {
	
	//Insert TweetMeme Retweet & Facebook Like button,
	//without slow page load lag caused from the <iframe> tag.
	// source: http://wp.xhezairi.com/add-tweetmeme-rt-and-fb-like-button-no-slow-page-loads.html
	if (jQuery('#fb_btn').length && jQuery('#tw_btn').length) {
	  var fb_code = "";
	  var tw_code = "";
	  var fr_code = "";
	  
	  var title = encodeURIComponent('läser '+document.title);

	  tw_code += '<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://platform.twitter.com/widgets/tweet_button.html?via=vemringde&amp;text='+title+'&amp;count=vertical&amp;related=carlfredrikhero" style="width:55px; height:62px;"></iframe>';

	  fb_code += "<iframe src=\"http://www.facebook.com/plugins/like.php?href=" + escape(document.URL) +"&amp;show_faces=false&amp;action=like&amp;layout=box_count\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden;width:100%;height:65px;\" allowTransparency=\"true\" >";

	  jQuery('#fb_btn').html(fb_code);
	  jQuery('#tw_btn').html(tw_code);
	}
	
	// fixed sidebar
	// http://jqueryfordesigners.com/fixed-floating-elements/
	if(jQuery('#scrollfix').length){
		var top = jQuery('#scrollfix').offset().top - parseFloat(jQuery('#scrollfix').css('marginTop').replace(/auto/,0));
		  jQuery(window).scroll(function (event) {
		    // what the y position of the scroll is
		    var y = jQuery(this).scrollTop() +42;
		  
		    // whether that's below the form
		    if (y >= top) {
		      // if so, ad the fixed class
		      jQuery('#scrollfix').addClass('fixed').offset({top: y});
		    } else {
		      // otherwise remove it
		      jQuery('#scrollfix').removeClass('fixed').offset({top: top});
		    }
		  });
	}
	
});

