$(function(){

    $('form').each(function(){
    	$(this).bValidator({lang: 'ar'});
    });



	
    // fancybox
    $('.fancybox').each(function(){
        options = {
            'overlayOpacity'	:	0.2,
            'transitionIn'		:	'none',
            'transitionOut'		:	'none',

            onComplete : function(){
                $('input.focus, textarea.focus').focus();
            }
        };

        if ($(this).attr('width')) options.width = parseInt( $(this).attr('width') );
        if ($(this).attr('height')) options.height = parseInt( $(this).attr('height') );

        $(this).fancybox(options);
    });
	


	// time ago
	$(".timeago").timeago();
	//$('.timeago').live(function() { $(this).liveUpdate('.timeago').focus(); });
	




	$('a.scroll').each(function(){
		$(this).click(function(){
			target = $(this).attr('href');
			
			if (target == '#') return false;
			
			options = {easing: 'easeOutExpo'};
			$.scrollTo(target, 1000, options);
			return false;
		});
	});




	start_title_inside();








	$('#navigate a').click(function(){
        // detect current top post
		var scrollTop = $(window).scrollTop() + $('#jokes').offset().top;
		var current_post = false


		$('.joke').each(function(){
			top1 = $(this).offset().top;
			bottom = $(this).offset().top + $(this).outerHeight();

			if ( (top1 <= scrollTop) && (bottom >= scrollTop) ) {
				current_post = this;
			}
		});

		options = {easing: 'swing'};

		if (! $(current_post).length) {
			target = $('.joke:last');
			top1 = $(target).offset().top - $('#header').height() - 30;
			$.scrollTo(top1, options);
		}
		else
		if ($(this).attr('id') == 'navigate-up') {
			target = $(current_post).prev();
			if (target.length) {
				top1 = $(target).offset().top - $('#header').height() - 30;
    			$.scrollTo(top1, options);
			}
		}
		else {
			target = $(current_post).next();
			if (target.length) {
				if ($(target)[0] == $('.joke:last')[0]) {
					$('#more').click();
				}
				else {
					top1 = $(target).offset().top - $('#header').height() - 30;
    				$.scrollTo(top1, options);
 				}
			}
			else {
				$('#more').click();
			}
		}

		return false;
	});







	$('#more').click(function()
	{
		page_no = $(this).attr('page-no');
		page_no++;

		$(this).attr('page-no', page_no);
		$('#page-no').val(page_no);

		location.hash = '!' + page_no;

		$.post ('?ajax=more&page-no=' + page_no, function(result){
			if (! result) {

			}
			else {
				target = $('.joke:last');

				$('#jokes').append (result);

				target = $(target).next();

				if (target.length) {
					options = {easing: 'swing'};
					top1 = $(target).offset().top - $('#header').height() - 30;
	    			$.scrollTo(top1, 1500, options);
				}

				start_title_inside();
			}
		})


		return false;
	});






	// check page hash, then rediect to it
	page_no = location.hash;
	page_no = page_no.replace('#!', '');
	if (is_int(page_no)) {
		$('#page-no').val(page_no);
		$('#more').attr('page-no', parseInt($('#page-no').val()) - 1 ).click();
	}








	$('#paging-form').submit(function()
	{
		if (! $(this).data('bValidator').isValid()) return false;

		$('#more').attr('page-no', parseInt($('#page-no').val()) - 1 ).click();

		return false;
	});






	$('input').focus(function(){
		$(this).addClass('focus-input');
	}).blur(function(){
		$(this).removeClass('focus-input');
	});


	$(document).keyup(function (e) {
		if ($('input.focus-input:visible').length) return false;
		
		if(e.which == 107) $('#navigate-down').click();
		if(e.which == 109) $('#navigate-up').click();
	});







	$('.add-comment form').live('submit', function(){
		if (! logged) {
			fancybox('#popup-register');
			return false;
		}

	 	// validate
	 	body = $(this).find('[name="body"]').val();
	 	body = $.trim(body);
		if (! body) return false;

		// send comment
		me = this;
		target = $(this).attr('target');
		
		return ajax_submit_form (this, false, function(result)
		{
			result = $.parseJSON(result);

			$(target).append(result.html);
			$(me).parents('.joke').find('.comments-count span').text(result.comments);

			$(me)[0].reset();
		});
	});





	
	
	$('.like-link').live ('click', function(){
		if (! logged) {
			fancybox('#popup-register');
			return false;
		}
		 
		me = this;
		return ajax_call(this, function(result){
			$(me).addClass('liked');
			$(me).find('.likes-count').text(result);
		})
	});







	$('.ajax-form').each(function(){
		$(this).submit(function()
		{
			if (! $(this).data('bValidator').isValid()) return false;
			me = this;

			return ajax_submit_form (this, false, function(result){
//				alert(result);
				result = $.parseJSON(result);

				inner_message ($(me).parent().parent().find('.result-msg'), result.message, result.error ? 'error-msg' : 'notify-msg');
				if (! result.error) {
					//setTimeout('$.fancybox.close ()', 3000);
					$(me)[0].reset();
				}
			});
		});
	});





	$('.ajax-upload-form').each(function(){
		$(this).submit(function()
		{
			if (! $(this).data('bValidator').isValid()) return false;
			me = this;

			$('#popup-iframe').load(function(){
				result = get_iframe_body('#popup-iframe');
				if (! result) return;
				result = $.parseJSON(result);

				inner_message ($(me).parent().parent().find('.result-msg'), result.message, result.error ? 'error-msg' : 'notify-msg');
				if (! result.error) {
					setTimeout('$.fancybox.close ()', 3000);
					if (! $(me).hasClass('disable-reset-on-success')) $(me)[0].reset();

					if($(me).attr('id') == 'profile-form') setTimeout('location.href = location.href;', 2000);
				}
			});
		});
	});







	$('#login-form').submit(function()
	{
		if (! $(this).data('bValidator').isValid()) return false;
		me = this;

		return ajax_submit_form (this, false, function(result){
			result = $.parseJSON(result);

			inner_message ($(me).parent().find('.result-msg'), result.message, result.error ? 'error-msg' : 'notify-msg');
			if (! result.error) setTimeout('$.fancybox.close (); location.href = location.href;', 3000);
		});
	});






	$('#add-post-form').submit(function()
	{
		error = false;

		if (! $(this).data('bValidator').isValid()) return false;

		data = $(this).serializeObject();

		if ( (data.kind == 'text') && (! data.body) ) error = 'أدخل المحتوى';
		if ( (data.kind == 'photo') && (! $(this).find('[name="photo"]').val()) ) error = 'حدد ملف الصورة';
		if ( (data.kind == 'video') && (! data.video) ) error = 'أدخل عنوان على اليوتيوب';

		if (error) {
			inner_message ($(this).parent().find('.result-msg'), error, 'error-msg');
			return false;
		}
	});




















});
