/// ENTITY POST FORM FUNCTIONS
$(document).ready(function(){
    $('body').live('click',function(){
        checkCloseForm();
    });
});
function loadFormPart(part, obj){
	$('#entity_form_menu li.current').removeClass('current');
	$('a.current').removeClass('current');
	$(obj).addClass('current');
	$(obj).parent().parent().parent().addClass('current');
	if(part != 'text'){
		if(part == 'flash'){
			$("#formpart_flash li:first").addClass('current');
		}
		if(part == 'photo'){
			$("#formpart_photo li:first").addClass('current');
		}
		$('#formpart').css('display','none');
		$('#formpart').html($('#formpart_' + part).html());
		$('#formpart').show();
		$('#entity_cancel_attach').show();
//		$('#entity_form_menu').hide();
	} else {
		$('#formpart').hide();
		$('#formpart').html('');
	}
	return false;
}

function loadImagePart(part, obj){
	$('#imagepart').html($('#imagepart_'+part).html());
	$('#formpart .info-line .tabs .current').removeClass('current');
	$(obj).parents('li').addClass('current');
	$(obj).blur();
	return false;
}

function loadFlashPart(part, obj){
	$('#flashpart').html($('#flashpart_'+part).html());
	$('#formpart .info-line .tabs .current').removeClass('current');
	$(obj).parents('li').addClass('current');
	$(obj).blur();
	return false;
}

function showResponse(data){
	showMessage($('msg',data).text());
	if($('error',data).text() != true){
                $('#sticker_counter_sidebar').html(parseInt($('#sticker_counter_sidebar').html())+1);
                $('#file_audio').val('');
                $('#file_image').val('');
		$('#newEntityForm').clearForm();
		deleteFlickrAttach();
		deleteVimeoAttach();
		deleteYoutubeAttach();
		$('#entityForm_group').val('0');
                $('.holder .bit-box').remove();
		// Set default post button:
		but = $('#post');
		publicPostStatus(but);
		// ----

		if($('.li-index').length){
			href = $('.li-index a.ajax').attr('href');
		} else {
			alert('unknown li!');
		}
		$('#newEntityForm input[type=submit]').attr('disabled', false);
		$('#ajax-content .new-record').show();
		$('#ajax-content .new-record .group-date').after($('html',data).text());
		window.form_inside = false;
		checkCloseForm();
		avatarDDinit();
//		tb_init('#'+$($('.new-record .record')[0]).attr('id')+' a.thickbox');
//		imgLoader = new Image();// preload image
//		imgLoader.src = tb_pathToImage;
		webSnapr.init();
		$('#symbol_limit_text').html($('#symbol_limit_real').html());
                $('#formpart_buttons input[type=submit]').attr('disabled', false);
	}else{
            $('#formpart_buttons input[type=submit]').attr('disabled', false);
        }
        $('#entity_text').attr('form_h','');
}

/**
*  Set Attach Flick, YouTube and Vimeo
*/

function setAttach(obj){
	window.form_inside = true;
	img_url = $(obj).children('.attach_src').html();
	img_thumb = $(obj).children('.attach_thumb').html();
	$('#flickr_attach .flickr_image').html(img_thumb);
	$('#flickr_attach input').val(img_url);
	$('#modalwindow2').hide();
	$('#flickr_attach').show();
}

function setVimeoAttach(obj){
	window.form_inside = true;
	img_url = $(obj).children('.attach_src').html();
	img_thumb = $(obj).children('.attach_thumb').html();
	$('#vimeo_attach .vimeo_image').html(img_thumb);
	$('#vimeo_attach input').val(img_url);
	$('#modalwindow2').hide();
	$('#vimeo_attach').show();
}

function setYoutubeAttach(obj){
	window.form_inside = true;
	img_url = $(obj).children('.attach_src').html();
	img_thumb = $(obj).children('.attach_thumb').html();
	$('#youtube_attach .youtube_image').html(img_thumb);
	$('#youtube_attach input').val(img_url);
	$('#modalwindow2').hide();
	$('#youtube_attach').show();
}

/**
* delete Attach (Flicker, YouTube, Vimeo)
*/
function deleteFlickrAttach() {
	$('#flickr_attach input').val('');
	$('#flickr_attach').hide();
	return false;
}

function deleteYoutubeAttach() {
	$('#youtube_attach input').val('');
	$('#youtube_attach').hide();
	return false;
}

function deleteVimeoAttach() {
	$('#vimeo_attach input').val('');
	$('#vimeo_attach').hide();
	return false;
}

/**
*
*/
function showFullForm(){
	if (!$('#entity_text').data('isOpen')){
//		var textarea = $('#entity_text');
//		var size = 8;
//		var symbols = parseInt(textarea.val().length);
//		var width = parseInt(textarea.width());
//		var symbols_per_row = width/size;
//		var rows = Math.ceil(symbols/symbols_per_row);
//		if(rows > 1){
//		    height = 34 + (rows - 1) * parseInt(textarea.css('font-size'));
//		} else {
//			height = 32;
//		}
		$('#entity_text').data('isOpen',true);
		

                form_h = $('#entity_text').attr('form_h');
                if (form_h) {
                    $('#entity_text').css('height', form_h+'px');
                }else{
                       $('#entity_text').css('height', '32px');
                }
		$('#formpart_buttons').show();
		$('#entity_form_menu').show();
		$('#entity_form_private').show();
		$('#entity_label').hide();
		if($('#entity_text').attr('default') == 'true'){
			$('#entity_text').val('');
			$('#entity_text').attr('default', false);
		}
		if($('#entity_text').val() == $('#entity_text_default').html()){
			$('#entity_text').val('');
			$('#entity_text').attr('default', false);
		}
		setTimeout('focusEntityTextarea', 500);

            if (!$('.holder').length && $("#postTags").length) {
                $("#postTags").fcbkcomplete({
                json_url: "/load/autocompleteTags",
                cache: false,
                filter_case: false,
                filter_hide: true,
                firstselected: true,
                filter_selected: true,
                newel: true
              });
            }
	}
}

function focusEntityTextarea(){
	$('#entity_text').focus();
}

function validatePostForm(formData, jqForm, options){
	error = false;
	text = $('#entity_text').val();
        var reg = /\w/;

        if(!text){
		showMessage($('#entity_empty_error').html());
		error = true;
	} else if (parseInt(text.length) > parseInt($('#symbol_limit_real').html()) && reg.test(text) ){
		showMessage($('#entity_limit_error').html());
		error = true;
	}
	if(error == true){
		return false;
	} else {
                if(text && reg.test(text)){
                    $('#formpart_buttons input[type=submit]').attr('disabled', true);
                }
		return true;
	}
}

function cancelFormAttach(){
	$('#formpart').fadeOut();
	$('#formpart').html('');
	$('#entity_cancel_attach').hide();
	$('.b-ef-entity-buttons a.current').removeClass('current');
//	$('#entity_form_menu').show();
	return false;
}

function checkCloseForm(){
	if($('.b-new-entity-form').length){
		if(window.form_inside != true){
			if ($('#entity_text').data('isOpen')){

                            if (navigator.platform.toLowerCase()=='macintel') {
                             var height = '12px';
                            }else{
                             var height = '16px';
                            };
                            $('#entity_text').data('isOpen',false);
                            $('#entity_text').css('height',height);
                            $('#formpart_buttons').hide();
                            $('#entity_form_menu').hide();
                            $('#entity_form_private').hide();
                            if(!$('#entity_text').val().length){
                                    $('#entity_text').val($('#entity_text_default').html());
                                    $('#entity_text').attr('default', true);
                            }
			}
		}
	}
}

/* from view */
            $(document).ready(function(){
            	var options = {
            		beforeSubmit: validatePostForm,
            		url: window.postUrl,
            		success: showResponse,
            		dataType:  'xml'
            	};
            	$('#newEntityForm').ajaxForm(options);
            });

            $(function(){
	var $tooltipBox = $('<div id="tooltip-01"><div></div><i></i></div>'),
		$tooltipText = $('div',$tooltipBox);

	$tooltipBox.appendTo($('body'));

	$('.b-ef-entity-buttons a').hover(
		function(){
			var $link = $(this),
				offset = $link.offset(),
				ttText = $link.attr('tooltip');

			if (ttText)	{
				$tooltipText.text(ttText);
				$tooltipBox.css({
					top:  offset.top - 23,
					left: offset.left
				}).show();
			}
		},
		function(){
			$tooltipBox.hide();
		}
	);
});


$(function(){
	$('.b-select').each(function(){
		var $selBox = $(this),
			$selBtn = $('.b-select-value',$selBox),
			$selInput = $('input',$selBox),
			$selPopup = $('.b-select-popup',$selBox);

		$selBtn.hover(
			function(){ $selPopup.show(); },
			function(e){
				var target = $(e.relatedTarget);
				if (!(target.parents('.b-select-popup').length || target.is('.b-select-popup'))){
					$selPopup.hide();
				}
			}
		)
		$selPopup.hover(
			function(){},
			function(e){
				var target = $(e.relatedTarget);
				if (!(target.parents('.b-select').length || target.is('.b-select'))){
					$selPopup.hide();
				}
			}
		)
		$('a',$selPopup).click(function(){
			var $link = $(this);
			$selBtn.html($link.html());
			$selInput.val($link.attr('value'));
			$selPopup.hide();
			return false;
		})
	});

});

$(function(){
    displaylimit("entity_text",window.limitSymbolInPost, "symbol_limit_text");
    displaylimit("quote_text",window.limitQuotePost, "symbol_limit_2");
})
