jQuery(function($) {
	$.neobizzShowAddDialogBool = false;
	$.neobizzShowAddDialog = function(){
		if(	$.neobizzShowAddDialogBool)return;
		$.neobizzShowAddDialogBool = true;
		if($('#popupBoxNeobizz').length==0){
			$.ajax({
				  url: PUBLIC_URL + 'neobizz/ajax/getfeedaddform',
				  success: function(data) {
					$('body').append(data);
					$('#popupBoxNeobizz').show();
					
//					$.fixIEpoziton();
					$.getTextContentNeobizz('ad');
					$('#adContentTextArea').focus();					
					$('#categoryId').change($.updateForm);					
					$('#cityId').change($.updateForm);
					
					$('#neobizzPrice').focus(function(){$.deleteInputDefaultText($(this));});
					$('#neobizzPrice').blur(function(){$.setInputDefaultText($(this));});
					$('#neobizzPhone').focus(function(){$.deleteInputDefaultText($(this));});
					$('#neobizzPhone').blur(function(){$.setInputDefaultText($(this));});
					$.neobizzShowAddDialogBool = false;
				  }
				});
			return;
		}
		$.getTextContentNeobizz('ad');		
		$('#popupBoxNeobizz').show();
		$.neobizzShowAddDialogBool = false;		
	};

	$.neobizzShowEventDialogBool = false;
	$.neobizzShowEventDialog = function(){
		if($.neobizzShowEventDialogBool)return;
		$.neobizzShowEventDialogBool = true;
		if($('#popupBoxNeobizzEvent').length==0){
			$.ajax({
				  url: PUBLIC_URL + 'neobizz/ajax/getfeedeventform',
				  success: function(data) {
					$('body').append(data);
					$('#popupBoxNeobizzEvent').show();
					
//					$.fixIEpoziton();
					$.getTextContentNeobizz('event');
					$('#adContentTextArea').focus();					

					$('#neobizzPhoneEvent').focus(function(){$.deleteInputDefaultText($(this));});
					$('#neobizzPhoneEvent').blur(function(){$.setInputDefaultText($(this));});
					$.neobizzShowEventDialogBool = false;
				  }
				});
			return;
		}
		$.getTextContentNeobizz('event');		
		$('#popupBoxNeobizzEvent').show();
		$.neobizzShowEventDialogBool = false;		
	};

	$.neobizzShowServiceDialogBool = false;
	$.neobizzShowServiceDialog = function(){
		if($.neobizzShowServiceDialogBool)return;
		$.neobizzShowServiceDialogBool = true;					
		if($('#popupBoxNeobizzService').length==0){
			$.ajax({
				  url: PUBLIC_URL + 'neobizz/ajax/getfeedserviceform',
				  success: function(data) {
					$('body').append(data);
					$('#popupBoxNeobizzService').show();
					
//					$.fixIEpoziton();
					$.getTextContentNeobizz('service');
					$('#adContentTextArea').focus();					
					
					$('#neobizzServicePrice').focus(function(){$.deleteInputDefaultText($(this));});
					$('#neobizzServicePrice').blur(function(){$.setInputDefaultText($(this));});
					$('#neobizzServicePhone').focus(function(){$.deleteInputDefaultText($(this));});
					$('#neobizzServicePhone').blur(function(){$.setInputDefaultText($(this));});
					$.neobizzShowServiceDialogBool = false;					
				  }
				});
			return;
		}
		$.getTextContentNeobizz('service');		
		$('#popupBoxNeobizzService').show();
		$.neobizzShowServiceDialogBool = false;		
	};

	$.neobizzShowAutoDialogBool = false;
	$.neobizzShowAutoDialog = function(){
		if($.neobizzShowAutoDialogBool)return;
		$.neobizzShowAutoDialogBool = true;					
		if($('#popupBoxNeobizzAuto').length==0){
			$.ajax({
				  url: PUBLIC_URL + 'neobizz/ajax/getfeedautoform',
				  success: function(data) {
					$('body').append(data);
					$('#popupBoxNeobizzAuto').show();
					
//					$.fixIEpoziton();
					$.getTextContentNeobizz('auto');
					$('#autoContentTextArea').focus();					
					
					$('#categoryIdauto').change(function(){$.updateGeneralForm('auto');});					
					$('#neobizzAutoPrice').focus(function(){$.deleteInputDefaultText($(this));});
					$('#neobizzAutoPrice').blur(function(){$.setInputDefaultText($(this));});
					$('#neobizzAutoPhone').focus(function(){$.deleteInputDefaultText($(this));});
					$('#neobizzAutoPhone').blur(function(){$.setInputDefaultText($(this));});
					$.neobizzShowAutoDialogBool = false;					
				  }
				});
			return;
		}
		$.getTextContentNeobizz('auto');		
		$('#popupBoxNeobizzAuto').show();
		$.neobizzShowAutoDialogBool = false;		
	};

	$.neobizzShowImobDialogBool = false;
	$.neobizzShowImobDialog = function(){
		if($.neobizzShowImobDialogBool)return;
		$.neobizzShowImobDialogBool = true;					
		if($('#popupBoxNeobizzImob').length==0){
			$.ajax({
				  url: PUBLIC_URL + 'neobizz/ajax/getfeedimobform',
				  success: function(data) {
					$('body').append(data);
					$('#popupBoxNeobizzImob').show();
					
//					$.fixIEpoziton();
					$.getTextContentNeobizz('imob');
					$('#autoContentTextArea').focus();					
					
					$('#categoryIdimob').change(function(){$.updateGeneralForm('imob');});
					$('#cityIdimob').change(function(){$.updateGeneralForm('imob');});					
					$('#neobizzImobPrice').focus(function(){$.deleteInputDefaultText($(this));});
					$('#neobizzImobPrice').blur(function(){$.setInputDefaultText($(this));});
					$('#neobizzImobPhone').focus(function(){$.deleteInputDefaultText($(this));});
					$('#neobizzImobPhone').blur(function(){$.setInputDefaultText($(this));});
					$.neobizzShowImobDialogBool = false;					
				  }
				});
			return;
		}
		$.getTextContentNeobizz('imob');		
		$('#popupBoxNeobizzImob').show();
		$.neobizzShowImobDialogBool = false;		
	};

	$.neobizzShowDiverseDialogBool = false;
	$.neobizzShowDiverseDialog = function(){
		if($.neobizzShowDiverseDialogBool)return;
		$.neobizzShowDiverseDialogBool = true;					
		if($('#popupBoxNeobizzDiverse').length==0){
			$.ajax({
				  url: PUBLIC_URL + 'neobizz/ajax/getfeeddiverseform',
				  success: function(data) {
					$('body').append(data);
					$('#popupBoxNeobizzDiverse').show();
					
//					$.fixIEpoziton();
					$.getTextContentNeobizz('diverse');
					
					$('#neobizzDiversePrice').focus(function(){$.deleteInputDefaultText($(this));});
					$('#neobizzDiversePrice').blur(function(){$.setInputDefaultText($(this));});
					$('#neobizzDiversePhone').focus(function(){$.deleteInputDefaultText($(this));});
					$('#neobizzDiversePhone').blur(function(){$.setInputDefaultText($(this));});
					$.neobizzShowDiverseDialogBool = false;					
				  }
				});
			return;
		}
		$.getTextContentNeobizz('diverse');		
		$('#popupBoxNeobizzDiverse').show();
		$.neobizzShowDiverseDialogBool = false;		
	};

	$.neobizzShowMatrimonialDialogBool = false;
	$.neobizzShowMatrimonialDialog = function(){
		if($.neobizzShowMatrimonialDialogBool)return;
		$.neobizzShowMatrimonialDialogBool = true;					
		if($('#popupBoxNeobizzMatrimonial').length==0){
			$.ajax({
				  url: PUBLIC_URL + 'neobizz/ajax/getfeedmatrimonialform',
				  success: function(data) {
					$('body').append(data);
					$('#popupBoxNeobizzMatrimonial').show();
					
//					$.fixIEpoziton();
					$.getTextContentNeobizz('matrimonial');
					
					$('#neobizzMatrimonialPhone').focus(function(){$.deleteInputDefaultText($(this));});
					$('#neobizzMatrimonialPhone').blur(function(){$.setInputDefaultText($(this));});
					$.neobizzShowMatrimonialDialogBool = false;					
				  }
				});
			return;
		}
		$.getTextContentNeobizz('matrimonial');		
		$('#popupBoxNeobizzMatrimonial').show();
		$.neobizzShowMatrimonialDialogBool = false;		
	};

	$.neobizzShowJobDialogBool = false;
	$.neobizzShowJobDialog = function(){
		if($.neobizzShowJobDialogBool)return;
		$.neobizzShowJobDialogBool = true;					
		if($('#popupBoxNeobizzJob').length==0){
			$.ajax({
				  url: PUBLIC_URL + 'neobizz/ajax/getfeedjobform',
				  success: function(data) {
					$('body').append(data);
					$('#popupBoxNeobizzJob').show();
					
//					$.fixIEpoziton();
					$.getTextContentNeobizz('job');
					
					$('#neobizzJobPhone').focus(function(){$.deleteInputDefaultText($(this));});
					$('#neobizzJobPhone').blur(function(){$.setInputDefaultText($(this));});
					$.neobizzShowJobDialogBool = false;					
				  }
				});
			return;
		}
		$.getTextContentNeobizz('job');		
		$('#popupBoxNeobizzJob').show();
		$.neobizzShowJobDialogBool = false;		
	};
	
	$.getTextContentNeobizz = function(prefix){
		if($('#share-box_composer').length==0)return;
		var text = $('#share-box_composer').val();
		var def_text = $('#share-box_composer').attr('defaulttext');
		if(text == def_text)return;
		$('#'+prefix+'ContentTextArea').html(text);
		$('#'+prefix+'ContentTextArea').val(text);
		$('#share-box_composer').val(def_text);
		$('#share-box_composer').html(def_text);				
		$('#uploader').ready(function() {
			$('#uploader').css('height','100px');
		});
		$('#uploader').css('height','100px');
		$('#'+prefix+'ContentTextArea').focus();
	};

	$.resetFeedTextNeobizz = function(){
		if($('#share-box_composer').length==0)return;
		var text = $('#share-box_composer').val();
		var def_text = $('#share-box_composer').attr('defaulttext');
		if(text == def_text)return;
		$('#share-box_composer').val(def_text);
	};
	
	$.fixIEpoziton = function(){
		if (!(jQuery.browser.msie && parseInt(jQuery.browser.version)<=6))return; 
		$('#popupBoxNeobizz').css('position','absolute').css('top','500px');
//		var poz = $('#userStatus').offset().left-90;
//		$('#popupBoxNeobizz').css('position','absolute').css('top','200px').css('left',poz+'px');		
	};
	$.updateForm = function(){
		$.updateGeneralForm('');
	};

	$.updateGeneralForm = function(specsId){
		var catId = $('#categoryId'+specsId).val();
		var cityId = $('#cityId'+specsId).val();
		$.ajax({
			  type: 'POST',			
			  url: PUBLIC_URL +  'neobizz/ajax/getspecsfeed',
			  data: jQuery.param({catId : catId, cityId : cityId}),
			  dataType: 'json',
			  success: function(data) {
					if(data.result=="success") {
						$('#neobizz'+specsId+'_specs').html(data.html);
					}
			 }
		});		
	};
	
	$.deleteInputDefaultText = function(param){
		if(param.attr('data')!=param.val())return;
		param.val('');
	};

	$.setInputDefaultText = function(param){
		if(param.val()!="")return;
		param.val(param.attr('data'));		
	};
	
	$('#close-bizz').click( function(){
		$('#bizz-container').hide();
		jQuery.mediaType = 'text';
		jQuery('#general-error').hide();
		$('#bizz-button').css("background-position","0 0px");
	});

	$.getPositionY = function(){
		var fixPoint;
		fixPoint = $('#userStatus');
		if(fixPoint) return fixPoint.offset().left-90;
		return 100;
	};
	$.sendNeobizz = function(promoRedirect){
		$.sendNeobizzData(jQuery('#add_ad_form').serialize(),function(){$('#popupBoxNeobizz').remove();}, promoRedirect);
	};
	$.sendNeobizzEvent = function(){
		$.sendNeobizzData(jQuery('#add_event_form').serialize(),function(){$('#popupBoxNeobizzEvent').remove();});
	};
	$.sendNeobizzService = function(){
		$.sendNeobizzData(jQuery('#add_service_form').serialize(),function(){$('#popupBoxNeobizzService').remove();});
	};
	$.sendNeobizzAuto = function(){
		$.sendNeobizzData(jQuery('#add_auto_form').serialize(),function(){$('#popupBoxNeobizzAuto').remove();});
	};
	$.sendNeobizzImob = function(){
		$.sendNeobizzData(jQuery('#add_imob_form').serialize(),function(){$('#popupBoxNeobizzImob').remove();});
	};
	$.sendNeobizzDiverse = function(){
		$.sendNeobizzData(jQuery('#add_diverse_form').serialize(),function(){$('#popupBoxNeobizzDiverse').remove();});
	};
	$.sendNeobizzMatrimonial = function(){
		$.sendNeobizzData(jQuery('#add_matrimonial_form').serialize(),function(){$('#popupBoxNeobizzMatrimonial').remove();});
	};
	$.sendNeobizzJob = function(){
		$.sendNeobizzData(jQuery('#add_job_form').serialize(),function(){$('#popupBoxNeobizzJob').remove();});
	};
	
	$.sendNeobizzDataBool = false;
	$.sendNeobizzData = function(dataPost,closeFunc, promoRedirect){
		 dataPost += '&channel=' + $.channelId;
//		if($.sendNeobizzDataBool)return;
		$.sendNeobizzDataBool = true;		
		$.ajax({
			  type: 'POST',			
			  url: PUBLIC_URL + 'neobizz/ajax/feedaddad',
			  data: dataPost,
			  dataType: 'json',
			  success: function(data) {
					$.sendNeobizzDataBool = false;		
			
					if(data.result=="success"){
						$.resetFeedTextNeobizz();
						$('#feeds-container').prepend(data.html);
						closeFunc();
						if($.attachEventFeed)$.attachEventFeed($('#event_'+data.eventId));
						$('body').append(data.promotion_html);
                                                if (promoRedirect && data.promoLink) {
                                                    document.location = data.promoLink;
                                                }
                                                if (data.showPromo)
                                                    $.showPromo();
						return;						
					}
					if(data.result == "error"){
						systemMessenger.set(data.message, 0);
						systemMessenger.display();
						return;
					}
					if(data.result == "redirect"){
						document.location = data.location;
						return;
					}
					
			  }
			});		
	};

	$.getNeobizzEditFormBool = false;
	$.getNeobizzEditForm = function(param){
		if($.getNeobizzEditFormBool)return;
		$.getNeobizzEditFormBool = true;
		$.ajax({
			  type: 'POST',			
			  url: PUBLIC_URL + 'neobizz/ajax/editform',
			  data: jQuery.param(param),
			  dataType: 'json',			  
			  success: function(data) {
				if(data.result=="success"){
					$('body').append(data.html);
					$.showNeobizzEdit();
					
					$('#categoryId').change($.updateForm);					
					$('#cityId').change($.updateForm);
				}
				$.getNeobizzEditFormBool = false;				
			  }
		});
	};

	$.showNeobizzEdit = function(){
		$('#popupBoxNeobizzEdit').show();
		$('#adContentTextArea').focus();					
		$('#categoryId').change($.updateForm);					
		$('#cityId').change($.updateForm);
		
		$('#neobizzPrice').focus(function(){$.deleteInputDefaultText($(this));});
		$('#neobizzPrice').blur(function(){$.setInputDefaultText($(this));});
		$('#neobizzPhone').focus(function(){$.deleteInputDefaultText($(this));});
		$('#neobizzPhone').blur(function(){$.setInputDefaultText($(this));});		
	};

	$.modifyNeobizzBool = false;
	$.modifyNeobizz = function(){
		if($.modifyNeobizzBool)return;
		$.modifyNeobizzBool = true;
		$.ajax({
			  type: 'POST',			
			  url: PUBLIC_URL + 'neobizz/ajax/update-my-ad',
			  data: jQuery('#edit_neobizz_form').serialize(),
			  dataType: 'json',
			  success: function(data) {
					$.modifyNeobizzBool = false;
					if(data.type==1){
						window.location.reload();
						$('#popupBoxNeobizzEdit').remove();
						return;						
					}
					systemMessenger.set(data.message, data.type);
					systemMessenger.display();
					return;
			  }
			});				
	};
	
	$.getPromo = function(param){
		$.ajax({
			  type: 'POST',			
			  url: PUBLIC_URL + 'neobizz/ajax/getpromo',
			  data: jQuery.param(param),
			  success: function(data) {
				if(data.result=="success"){
					$('body').append(data.html);
					$.showPromo();
				}
			  }
		});
	};
	$.neobizzSelectPromo = false;
	$.showPromo = function(){
		$.neobizzSelectPromo = false;
		$('#popupBoxNeobizzPromo').show();
		$('.neobizz_select_promo').click(function(){
			if($.neobizzSelectPromo)return;
			$.neobizzSelectPromo = true;			
			var data = $(this).attr('data');
			data = eval('('+data+')');
			$.ajax({
				  type: 'POST',			
				  url: PUBLIC_URL + 'neobizz/ajax/promo',
				  data: jQuery.param(data),
				  dataType: 'json',
				  success: function(data) {
						if(data.result=="success"){
							$('#popupBoxNeobizzPromo').remove();
							$('body').append(data.html);
							if(data.code==1){
								$.walletAtachControl();
								return;
							}
							$('#popupBoxWalletPay').show();
						}
						if(data.result == "error"){
							systemMessenger.set(data.message, 0);
							systemMessenger.display();
							return;
						}						
				  }
				});			
		});
		$('#neobizz_exit_select_link').click(function(){
			$('#popupBoxNeobizzPromo').remove();			
		});

		
	};	
});