/*
Classes
*/
$(document).ready(function(){
	modal = new prettyModal();
	
	$('a[rel^="prettyPhoto"]').prettyPhoto({
		theme: 'light_rounded'
	});
	
	$('a.pModal').live('click',function(){
		if($(this).attr('href') == '#openModal'){
			var url = $(this).attr('rel');
		}else{
			var url = $(this).attr('href');
			$(this).attr('href', '#openModal');
			$(this).attr('rel', url);
		}
		modal.openUrl(url);
	})

	$('form.ajax').live('submit',function(){
		var thisTarget = $(this).attr('name');
		var callback = $(this).attr('data-callback');
		$(this).ajaxSubmit({
			target: thisTarget,
			success: function(data){
				$('.targetAjaxForm').html(data);
				eval(callback);
			}
		});
		return false;
	})
	
	$('#searchForm').submit(function(){
		window.location = $(this).attr('action') + $('#query').val();
		return false;
	});
	
	$('.resetValue').focus(function(){
		if($(this).val() == $(this).attr('rel'))
			$(this).val('');
	}).blur(function(){
		if($(this).val() == '')
			$(this).val($(this).attr('rel'));
	});
	
//	if($('#nuvem_tags')[0]){
//		$('#nuvem_tags').randomize('a');
//	}

});


function prettyModal(){
	thisClass = this;
	this.isOpen = $('#prettyModal').is(':visible');
	
	$('body').append('<div id="prettyModalContent" style="display:none;"></div>');

	this.openUrl = function(url){
		if(thisClass.isOpen)
			$.prettyPhoto.close();
		
		$('#prettyModalContent').load(url, function(){
//			$.prettyPhoto({theme: 'light_rounded'});
			$.prettyPhoto.open('#prettyModalContent');
		});
		
		
	}
	this.openElement = function(element){
		$.prettyPhoto.open(element);
	}
	this.close = function(){
		$.prettyPhoto.close();
	}
	

}

function login(){
	window.location.reload();
}

function viewPost (blog){
	thisClass = this;
	this.blog = blog;
	
	$(document).ready(function(){
		$('a.lightbox').attr('rel', 'prettyPhoto[postGal]');
		$('a.lightbox[rel^="prettyPhoto"]').prettyPhoto();
	});
	
	//Rating post
	this.rating = function(isRated){
		if(isRated == '1'){
			$('.voto-texto').rating({readOnly:true});
			$('#ratingCounter').html('Você já avaliou este texto.');
		}else if(isRated == '2'){
			$('.voto-texto').rating({readOnly:true});
			$('#ratingCounter').html('Você não pode avaliar um texto que você escreveu.');
		}else{
			$('.voto-texto').rating({
				callback: function(value, link){
					var url = $('#ratingForm').attr('action')+value;
					$.get(url, function(data){
					    if(data == '1'){
							$('.voto-texto').rating('readOnly',true);
							$('.hover-rating').html('Sua avaliação');
							$('.hover-rating').removeClass('hover-rating');
							$('#ratingCounter').html('Obrigado por avaliar!');
					    }else{
					    	$('.voto-texto').rating();
					    	$('#ratingCounter').html(data);
					    }
					});
				},
				
				focus: function(value, link){
					var tip = $('.hover-rating'); 
					tip[0].data = tip[0].data || tip.html(); 
					tip.html(link.title || 'value: '+value); 
				},
			
				blur: function(value, link){
					var tip = $('.hover-rating'); 
					$('.hover-rating').html(tip[0].data || ''); 
				} 
			
			});
		}
	}

	this.comment = function(){
		var options = { 
			target:'#comment',
			success: thisClass.reloadForm 
		}; 
		
	    $('.f_comment').ajaxForm({
	        success: function(data, statusText, xhr, $form) { 
	    		$form.before(data);
	    		var obj = $form.prev();
	    		obj.fadeIn('normal');
	    		if(obj.hasClass('error')){
	    			window.setTimeout(function() {
	    					obj.fadeOut(1200);
	    				}, 2800);
	    		}else{
	    			$form[0].reset();
	    		}
	        }
	    });
	}
	
	this.reloadForm = function(responseText, statusText, xhr, $form){
		thisClass.comment();
	}
	
	this.openReply = function(){
		$('.openReply').click(function(){
			var obj = $(this)
			var commentId = obj.attr('rel');
			var url = obj.attr('href'); 
			$.get(url, function(data) {
				obj.parent().parent().append('<div id="replyTo'+commentId+'" class="comment_reply" style="display:none;">'+data+'</div>');
				var replyForm = $('#replyTo'+commentId);
				replyForm.slideDown(200);
				$('html, body').animate({scrollTop: replyForm.offset().top}, 700);
				obj.remove();
				thisClass.comment();
			});
			return false;
		})
	}
}

function colabore (){
	
	thisClass = this;
	this.init = function(){
		$('#publish').click(function(){
			var postId;
			postId = $(this).attr('name');
			$('#published').val('true');
			$('#editPost').attr('action', '/colabore/save/'+postId).submit();
		});
		
		$('#save-draft').click(function(){
			var postId;
			postId = $(this).attr('name');
			$('#published').val('false');
			$('#editPost').attr('action', '/colabore/save/'+postId).submit();
		});
		$('#delete').click(function(){
			var postId;
			postId = $(this).attr('name');
			if(confirm('Você deseja apagar este post? \n Esta ação é irreversível.'))
				$('#editPost').attr('action', '/colabore/delete/'+postId).submit();
		})
		$('.modalThumb').click(function(){
			var url = $(this).attr('href');
			if($('#tags').val() != '')
				var url = url+'/' + $('#tags').val();
			modal.openUrl(url);
			return false;
		});
		$('.selectThumb').live('click', function(){
			$('#selectedThumb').attr('src', '/media/images/posts/'+ $(this).attr('rel'));
			$('#thumb').val($(this).attr('title'));
			modal.close();
		});

	}
	
	thisClass.init();
}

function editAccount(){
	CKEDITOR.replace( 'bio',{
		toolbar :
		[
			[ 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink' ]
		]
	});
	CKEDITOR.replace( 'professional',{
		toolbar :
		[
			[ 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink' ]
		]
	});
	CKEDITOR.replace( 'graduation',{
		toolbar :
		[
			[ 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink' ]
		]
	});
}

function contact(){
	CKEDITOR.replace( 'contactContent',{
		toolbar :
		[
			[ 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink' ]
		]
	});
}

(function($) {

	$.fn.randomize = function(childElem) {
	  return this.each(function() {
	      var $this = $(this);
	      var elems = $this.children(childElem);

	      elems.sort(function() { return (Math.round(Math.random())-0.5); });  

	      $this.remove(childElem);  

	      for(var i=0; i < elems.length; i++)
	        $this.append(elems[i]);      

	  });    
	}
	})(jQuery);

