function getSubForm(pType)
{
	$('.block-03 .fields').html('<div style="text-align:center;"><img src="/img/ajax-loader.gif" alt="" /></div>');
	
	if(pType == 'imageUrl') {
		$.get('/jokes/images', {'jokeType' : 'imageUrl'}, function(data) {
			$('.block-03 .fields').html(data);
		});
	}
	else if(pType == 'imageFile') {
		$.get('/jokes/images', {'jokeType' : 'imageFile'}, function(data) {
			$('.block-03 .fields').html(data);
		});
	}
	else {
		$.get('/jokes/'+pType, function(data) {
			$('.block-03 .fields').html(data);
			textareaMaxLength();
		});
	}
}

function setActiveTab(pObj, pType)
{
	getSubForm(pType);
	$('.block-list li').removeClass('act');
	$(pObj).parent().addClass('act');
}

function microtime(get_as_float) {
    var now = new Date().getTime() / 1000;
    var s = parseInt(now, 10);
 
    return (get_as_float) ? now : ((Math.round((now - s) * 1000) / 1000) + s).toString().replace('.', '');
}

function addImageFileInput() 
{
	var lNewId = 'image-file-'+microtime(false);
	
	var lNewContainer = 
		'<div id="'+lNewId+'">'+
		'	<input type="file" name="imageFile[]" onchange="$(this).hide();$(\'#'+lNewId+'-sub\').show();$(\'#'+lNewId+'-sub .textfield\').val(this.value);" value="" class="file-field" />'+
		'	<div id="'+lNewId+'-sub" style="display:none;">'+
		'		<input type="submit" value=" " class="button button-del" onclick="$(\'#'+lNewId+'\').remove();return false;" />'+ 
		'		<input type="text" name="" class="textfield" value="" />'+
		'	</div>'+
		'</div>';
	
	$('#image-file-container').append(lNewContainer);
}

function addImageUrlInput() 
{
	var lNewId = 'image-url-'+microtime(false);
	
	var lNewContainer =
		'<div id="'+lNewId+'">'+
		'	<input type="submit" value=" " class="button button-del" onclick="$(\'#'+lNewId+'\').remove();return false;" />'+ 
		'	<input type="text" name="imageUrl[]" class="textfield" value="" />'+
		'</div>';
	
	$('#image-url-container').append(lNewContainer);
}

function showBigVideo(pObj)
{
	$(pObj).parent().parent().parent().find('.fullContent').show();
	$(pObj).parent().parent().remove();
}

function showViewMore(pObj)
{
        console.log($(pObj).parent().parent().parent().parent());
	$(pObj).parent().parent().parent().parent().find('.jokeContentBlock .shortContent').hide();
	$(pObj).parent().parent().parent().parent().find('.jokeContentBlock .fullContent').show();
	
	$(pObj).parent().find('.viewLess').show();
	$(pObj).hide();
}

function showViewLess(pObj) {
	$(pObj).parent().parent().parent().parent().find('.jokeContentBlock .shortContent').show();
	
	//$(window).scrollTop($(window).scrollTop() - $(pObj).parent().parent().parent().parent().find('.jokeContentBlock .fullContent').height());
	$("html, body").animate({scrollTop: $(window).scrollTop() - $(pObj).parent().parent().parent().parent().find('.jokeContentBlock .fullContent').height()}, 300);
	 
	$(pObj).parent().parent().parent().parent().find('.jokeContentBlock .fullContent').hide('slow', function() {});
	
	$(pObj).parent().find('.viewMore').show();
	$(pObj).hide();
}

function showViewMoreJoke(pObj, thisObj)
{   
    /*
        
	$(pObj).parent().parent().parent().parent().find('.jokeContentBlock .shortContent').hide();
	$(pObj).parent().parent().parent().parent().find('.jokeContentBlock .fullContent').show();
	
	$(pObj).parent().find('.viewLess').show();
	$(pObj).hide();
        */
       
       $("#"+pObj).find('.shortContent').hide();
       $("#"+pObj).find('.fullContent').show();
       
       $(thisObj).parent().find('.viewLess').show();
       $(thisObj).hide();
}

function showViewLessJoke(pObj, thisObj) {
        
        $("#"+pObj).find('.shortContent').hide();
        $("html, body").animate({scrollTop: $(window).scrollTop() - $("#"+pObj).find('.fullContent').height()}, 300);
        $("#"+pObj).find('.fullContent').hide('slow', function() {});
        
        
	$(thisObj).parent().find('.viewMore').show();
	$(thisObj).hide();
    
	//$(pObj).parent().parent().parent().parent().find('.jokeContentBlock .shortContent').show();
	
	//$(window).scrollTop($(window).scrollTop() - $(pObj).parent().parent().parent().parent().find('.jokeContentBlock .fullContent').height());
	//$("html, body").animate({scrollTop: $(window).scrollTop() - $(pObj).parent().parent().parent().parent().find('.jokeContentBlock .fullContent').height()}, 300);
	 
	//$(pObj).parent().parent().parent().parent().find('.jokeContentBlock .fullContent').hide('slow', function() {});
	
	//$(pObj).parent().find('.viewMore').show();
	//$(pObj).hide();
}

function voteForJoke(pObj, pJokeId) 
{
	$.getJSON('jokes/vote', {'jokeId' : pJokeId}, function(data) {
		if(data.status == 0) {
			$(pObj).find('i').removeClass('hand-cool').addClass('hand');
		}
		else {
			$(pObj).find('i').removeClass('hand').addClass('hand-cool');
		}
		
		$(pObj).find('span').html('('+data.votes+')');
	});
}

function favoriteJoke(pObj, pJokeId) 
{
	$.getJSON('jokes/favorite', {'jokeId' : pJokeId}, function(data) {
		if(data.status == 0) {
			$(pObj).removeClass('comm-14-act');
		}
		else {
			$(pObj).addClass('comm-14-act');
		}
	});
}

function registrationCheckField(pObj, pName)
{
	$(pObj).parent().removeClass('bad').removeClass('good');
	
	$.getJSON('auth/check-validity', {'name' : pName, 'value' : pObj.value, 'value2' : $('#password').val()}, function(data) {
		if(data.status == 1) {
			$(pObj).parent().removeClass('bad').addClass('good');
		}
		else {
			$(pObj).parent().removeClass('good').addClass('bad');
		}
	});
}

function showNotification(pMessage)
{
	if(pMessage) {
		$('#notification').html('<p class="notification">'+pMessage+'</p>');
	}
	
	$('#notification').delay(400).slideDown(400).delay(3000).slideUp(400);
}

function sendEmail(pSender, pGetter, pId)
{
	$.getJSON('jokes/send-email', {'sender' : pSender.val(), 'getter' : pGetter.val(), 'id' : pId}, function(data){
		
	});
	
	showNotification('Prikolas buvo nusiųstas draugui');
	
	pGetter.attr('value', '');
	
	pGetter.parent().find('.sfPopupClose').click();
}

function changeFixedBlockPosition()
{
	$("#fixedBlock").css("position", "absolute");
	
	plusHeight = 204;
	if($.browser.msie && $.browser.version.substr(0,1) < 7) {
		plusHeight = 220;
	}
	
	lHeight = $('.block-01 .bg').height() + plusHeight;
	if($(window).scrollTop() > lHeight) {
		$('#fixedBlock').css('top', $(window).scrollTop() + 'px');
	}
	else {
		$('#fixedBlock').css('top', lHeight + 'px');
	}
}

$(window).scroll(function() {
	changeFixedBlockPosition();
});

function textareaMaxLength() {
  var txts = document.getElementsByTagName('textarea')

  for(var i = 0, l = txts.length; i < l; i++) {
    if(/^[0-9]+$/.test(txts[i].getAttribute("maxlength"))) {
      txts[i].onkeyup = function() {
        var len = parseInt(this.getAttribute("maxlength"), 10);

        if(this.value.length > len) {
          this.value = this.value.substr(0, len);
          return false;
        }
      }
    }
  }
}

$(document).ready(function() {
	textareaMaxLength();
});
