/* Author: Michel Stomp
   Other Author:
*/

$(document).ready(function() { 
	
	// infinitescroll() is called on the element that surrounds 
	// the items you will be loading more of
	// IE7 style fix 
	if ($.browser.msie  && parseInt($.browser.version) == 7 || $.browser.msie  && parseInt($.browser.version) == 8) {
	} else {
		if($('.pagination a.next-page').length){
			$('.pagination').hide();
			$('#posts').infinitescroll({
				navSelector  : ".pagination",
				nextSelector : ".pagination a.next-page", 
				itemSelector : ".post",
				animate      : true,
				errorCallback: function(){
					$('.lazy-load-copy').hide();
				}
			});
		}
	}
	
	$('.footer-menu a').bind('click', function(e){
		var url = $(this).attr('href');
		if(url.indexOf("mailto:") > -1)return;
		loadPage(url);
		e.preventDefault();
	});
});

function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
return str;
}

// load info page
function loadPage(url){
	
	id = url.split('/').join('');
	
	// check if page is already opened and do nothing
	if($("#posts").find("#page-" + id).length > 0){
		$("#posts #page-" + id).remove();
	}
	
	// load page content
	$.ajax({
		url: url,
		context: document.body,
		success: function(html){
			// strip loaded content
			var s = html.substr(html.indexOf("<article"));
			s = s.substr(0, s.indexOf("</article") + 11);
			s = s.replace('<article ', '<div id="page-' + id + '" ');
			s = s.replace('</article>', '</div>');
			$("#posts").prepend(s); // add loaded content to top of container
			
			var a = $("#posts").find("div:first");
			
			// add close button
			addCloseButton(a);
			
			var h = a.height(); // get height of container
			a.height(0); // force height to 0
			
			// animate height
			a.animate({ 
			    height: h
			  }, 250, 'linear', function(){
				
			});
		}
	});
}

// add close button to info page
function addCloseButton(article){
	// add the close button
	article.prepend('<a href="#"><div class="btn-close"><img src="http://www.bastianmusic.com/tumblr/assets/img/btn-close.png"></div></a>'); // add the button to the container
	
	var btn = article.find('.btn-close:first');
	
	// bind the close-button to close the page
	btn.bind('click', function(e){
		e.preventDefault();
		
		// animate height to 0 and remove the article
		article.animate({
		    height: 0
		  }, 250, 'linear', function() {
		    article.remove();
		});
	});
}


















