/* Author: Diego Caponera

*/

BLOG = {
	
	common: {
		
		settings: {
			basePath : mwBasepath
		},
		
		init: function() {

			var $blog = $('#main');
			
			// masonry setup
			if($('.single', $blog).length == 0){

				$blog.imagesLoaded( function(){
					$blog.masonry({
						itemSelector: '.post',
			       		columnWidth: 260,
						isAnimated: true
			    	});
			
					BLOG.common.postBackground($('.post'));
			
			   	});


			    $blog.infinitescroll({
					navSelector  : '#page-nav',
					nextSelector : '#page-nav .next',
					itemSelector : '.post',
					loading: {
						finishedMsg: 'No more pages to load.',
						img: BLOG.common.settings.basePath + '/web/img/loading.gif'
					}
				},

				function( newElements ) {

					var $newElems = $( newElements ).css({ opacity: 0 });
			        
			        $newElems.imagesLoaded(function(){
			        
						$newElems.animate({ opacity: 1 });
						$blog.masonry( 'appended', $newElems, true ); 
						
						BLOG.common.postBackground($newElems);
						
			        });
				});

			}
			
			// Bind forms to controllers
			$(document).on('submit', 'form[data-controller]', function(){		

				return BASE.exec(
					$(this).attr('data-controller'),
					$(this).attr('data-action'),
					this
				);

			});	
			
			// Bind buttons	to controllers
			$(document).on('click', 'button[data-controller]', function(){		

				return BASE.exec(
					$(this).attr('data-controller'),
					$(this).attr('data-action'),
					this
				);

			});	
			
			// FancyBox startup
			$("#main .lightbox").fancybox({
				
			});
		
		},
		
		postBackground: function(elements){
		
			$.each(elements, function(){
				
				$(this).css('background-color', 'rgba(188, 208, 228, '+ ( 5 + Math.random() * 10) / 17 +')');
				
			});
			
		},
		
		search: function(form) {
			
			if($(form.key).val().length < 3){
				alert('At least 3 chars, please!');
			}
			
			return $(form.key).val().length >= 3;
			
		},
		
		highlight: function(elements) {
			
			$(elements).highlight($(elements).attr('data-key') );
			
		},
		
		updateCaptcha: function(element) {

			$.post(
				BLOG.common.settings.basePath + 'captcha/seed',
				{},
				function(data){
					
					var form = $('#'+$(element).attr('data-form'));
					$('[name="seed"]', form).val(data.seed);
					$('.captcha-image', form).attr('src', BLOG.common.settings.basePath+'captcha/image/'+data.seed);
					
				}
			);
			
		},
		
		scrollSharer: function(element) {
			
			var top = $(element).offset().top - parseFloat($(element).css('margin-top').replace(/auto/, 0));
			
		    $(window).scroll(function (event) {
		    
		  		// what the y position of the scroll is
				var y = $(this).scrollTop();

				// whether that's below the form
				if (y >= top) {
		        	// if so, ad the fixed class
		        	$(element).addClass('fixed');

				} else {
		        	// otherwise remove it
		        	$(element).removeClass('fixed');
		
				}
		    });		
			
		},
		
		snippets: function(element) {
			
			$.each($('pre.snippet', element), function(){
			
				$(this).snippet(
					$(this).attr('data-language'),
					{
						style		: 'golden',
						transparent	: false,
						showNum		: false,
						clipboard	: BLOG.common.settings.basePath + "web/ZeroClipboard.swf"
					}
				);
				
			});
			
		}

	},
	
	contact: {
		init: function() {

		},
		
		submit: function(form) {
			
			$.post(
				$(form).attr('action'), 
				{
					id_post	: $(form.id_post).val(),
					name	: $(form.name).val(),
					email	: $(form.email).val(),					
					body	: $(form.body).val(),	
					token	: $(form.token).val(),
					captcha	: $(form.captcha).val(),
					seed	: $(form.seed).val(),
					mode	: 'ajax'
				},
				function(data){

					if(data.status == 'OK'){

						$('input[type=submit]', form).attr('disabled', 'disabled').val(data.message);
						
					}else if(data.status == 'ERROR'){
						
						$('input[type=submit]', form).val(data.message);
						
						setTimeout(function(){
							
							$('input[type=submit]', form).val('Write Me!');
							
						}, 3000);
						
					}
				
				}
			);
			
			return false;
			
		}		
	},
	
	comment: {
		init: function() {

		},
		
		submit: function(form) {

			$.post(
				$(form).attr('action'), 
				{
					id_post	: $(form.id_post).val(),
					name	: $(form.name).val(),
					email	: $(form.email).val(),					
					url		: $(form.url).val(),
					body	: $(form.body).val(),
					notify	: $(form.notify).attr('checked') == 'checked' ? 1 : 0,	
					token	: $(form.token).val(),
					captcha	: $(form.captcha).val(),
					seed	: $(form.seed).val(),					
					mode	: 'ajax'
				},
				function(data){
					
					if(data.status == 'OK'){
						
						var commentList = $('.comment-list');
						
						if(commentList.length == 0){
							
							$('.post-comments h2').after($('<ul></ul>').addClass('comment-list'));
							
						}

						$('input[type=submit]', form).attr('disabled', 'disabled').val(data.message);
						$('.comment-none').hide();
						$('.comment-list').append(data.comment).find('li:last-child').effect("highlight", {}, 3000);
						
					}else if(data.status == 'ERROR'){
						
						$('input[type=submit]', form).val(data.message);
						
						setTimeout(function(){
							
							$('input[type=submit]', form).val('Write Comment!');
							
						}, 3000);
						
					}
				
				}
			);

			return false;
			
		}
	},
	
	stack: {
		
		init: function(element){
			
			$('#iconlist li').on('hover', function(){
			
				$('.tab', element)
					.hide()
					.parent().find($('a', this).attr('href'))
					.show();
				
				$('#iconlist li').removeClass('selected');
				$(this).addClass('selected');
				
			});
			
			$('#iconlist li a').on('click', function(){return false});
			
		}
		
	}
	
};

BASE = {
	
	exec: function(controller, action, elements) {

    	var ns = BLOG, 
			action = ( action === undefined ) ? "init" : action;

		if ( controller !== "" && ns[controller] && typeof ns[controller][action] == "function" ) {
      		return ns[controller][action](elements);
		}

	},

	init: function() {

		// Run common setup
    	BASE.exec("common");

		// Run startup actions [if any]
		$.each($('[data-startup]'), function(){

			BASE.exec(
				$(this).attr('data-controller'),
				$(this).attr('data-action'),
				this
			);
			
		});

  	}	
	
}

$(document).ready(BASE.init);
