jQuery(document).ready(function(){ 
	
	jQuery('a.more-link').live('click', function(e){
	    e.preventDefault();
	    
	    jQuery(this).addClass('loading');
	    	    
	    target = jQuery(this).parent();
	    the_id = target.parent().attr('id');
	    header = target.parent().parent().find('#' + the_id + ' .head');
	    	    
	    jQuery.ajax({
	        type: "GET",
	        url: jQuery(this).attr('href'),
	        dataType: "html",
	        success: function(out){
	        	
	            result = jQuery(out).find('.content');
	            target.replaceWith(result.fadeIn(500));
	            header.removeClass('visual');
	            target.parent().removeClass('loading');
	            
	            if (jQuery.browser.opera) {
                    var reference = 'html';
                }else{
                    var reference = 'html,body';
                }
	            
        		jQuery(reference).animate({
        			scrollTop: jQuery('#' + the_id).offset().top - 50
        		}, 300);
	            
	        }
	    });
	});
	
	jQuery('#pagination').addClass('ajax');
	jQuery('#pagination .previous').remove();
	jQuery('#pagination .next a').text('▼');
	
	jQuery('#pagination.ajax.archive .next a').live('click', function(e){
	    e.preventDefault();
	    jQuery(this).text('').parent().addClass('loading');
	    jQuery.ajax({
	        type: "GET",
	        url: jQuery(this).attr('href'),
	        dataType: "html",
	        success: function(out){
	        	
	            result = jQuery(out).find('.loop');
	            nextlink = jQuery(out).find('#pagination .next a').attr('href');
	            jQuery('#pagination').before(result.fadeIn(200));
	            jQuery('#pagination .next a').text('▼').parent().removeClass('loading');
	            if (nextlink != undefined) {
	                jQuery('#pagination .next a').attr('href', nextlink);
	            } else {
	                jQuery('#pagination').remove();
	            }
	            
	            twttr.widgets.load();
	            gapi.plusone.go();
	   	        }
	    });
	});
	
	jQuery('#videoloader #pagination.ajax.custom .next a').live('click', function(e){
	    e.preventDefault();	 	           
	    jQuery(this).text('').parent().addClass('loading');
	    jQuery.ajax({
	    	cache: false,
	        type: "GET",
	        url: jQuery(this).attr('href') ,
	        dataType: "html",
	        success: function(out){
	        	
	            result = jQuery(out).find('.loop.videos');
	            jQuery('#videoloader #pagination.custom').before(result.fadeIn(200));
	            jQuery('#videoloader #pagination.custom .next a').text('▼').parent().removeClass('loading');
	            nextlink = jQuery(out).find('#pagination.custom .next a').attr('href');
	            if (nextlink != undefined) {
	                jQuery('#videoloader #pagination.custom .next a').attr('href', nextlink);
	            } else {
	                jQuery('#videoloader #pagination.custom').remove();
	            }
	   	     }
	    });
	});
	
	jQuery('#mediaPlayer li').click(function() {
	
		var embed = jQuery(this).attr('data-url');
		var title = jQuery(this).attr('title');
		var source = jQuery(this).attr('data-source');	
			
		jQuery('#mediaPlayer .stage iframe').attr('src', embed + '&autoplay=1');
		jQuery('#mediaPlayer h2 a').text( title ).attr('href', source);		
	
	});
		
	jQuery(function () {
	
		var pagination = jQuery('#pagination.archive');
				
		jQuery(window).scroll(function(){
						
		        if  ( jQuery(window).scrollTop() == jQuery(document).height() - jQuery(window).height() && pagination ){
		        		          		
		        	if( ! touched )	{
		        	
		        		var touched = true;          	
		        		          										
	    			    jQuery('#pagination.archive .next a').text('').parent().addClass('loading');
	    			    jQuery.ajax({
	    			    
	    			        type: "GET",
	    			        url: jQuery('#pagination.archive .next a').attr('href'),
	    			        dataType: "html",
	    			        success: function(out){    			        		
	    			        	
		    			        	result = jQuery(out).find('.loop');
		    			            nextlink = jQuery(out).find('#pagination.archive .next a').attr('href');
		    			            jQuery('#pagination.archive').before(result.fadeIn(200));
		    			            jQuery('#pagination.archive .next a').text('▼').parent().removeClass('loading');
		    			            
		    			            if (nextlink != undefined) {
		    			                jQuery('#pagination.archive .next a').attr('href', nextlink);
		    			            } else {
		    			                jQuery('#pagination.archive').remove();
		    			            }
		    			            
		    			            twttr.widgets.load();
		    			            gapi.plusone.go();
		    			            		    			            				        			            									
									var touched = null;							
	    			        }
	    			    });
    			    
    			   	}
		        
		        }
		});
	});
		
});
