var advertClient = {
	url: CalcunConfig.basePath + 'Plugins/AdvertClient/index.php',
	lights : [],
	stars : 1,
	
	newAdvert: function() {
		var params = {
			action: 'Plugins/AdvertClient/index.php?view=client&form=newAdvert',
			content: 'html',
			width: 600,
			height: 400,
			title: 'Új hirdetés létrehozása',
			callback: function() {
				$('#expires').datepicker({
					dateFormat: 'yy.mm.dd.',
					minDate: '0'
				});
				$('#expires').keypress(function(){return false;});
				$('#newAdvertForm').validate();
				$('#newAdvertClose').click(function() {
					advertClient.lights['newAdvert'].close();	
				});
			}
		};
		this.lights['newAdvert'] = new CalcunLightbox(params);
		this.lights['newAdvert'].show();	
	},
	
	newAdvertLoad: function() {
		if($('#advertCategory').val() != '' && $('#advertType').val() != '') {
			$('#newAdvertForm_submit').empty().append('<b>Kérjük várjon...</b>');
			$('#newAdvertForm').unbind('submit').submit();
		} else {
			jcError('please_choose_a_type_and_a_category', 'information');
		}
		return true;	
	},
	
	editAdvert: function(advertId) {
		var params = {
			action: 'Plugins/AdvertClient/index.php?view=client&form=editAdvert&advertId=' + advertId,
			content: 'html',
			width: 600,
			height: 400,
			title: 'Hirdetés módosítása',
			callback: function() {
				$('#expires').datepicker({
					dateFormat: 'yy.mm.dd.',
					minDate: '0'
				});
				$('#expires').keypress(function(){return false;});
				$('#editAdvertForm').validate();
				$('#editAdvertClose').click(function() {
					advertClient.lights['editAdvert'].close();	
				});
			}
		};
		this.lights['editAdvert'] = new CalcunLightbox(params);
		this.lights['editAdvert'].show();	
	},
	
	editAdvertLoad: function() {
		$('#editAdvertForm_submit').empty().append('<b>Kérjük várjon...</b>');
		$('#editAdvertForm').unbind('submit').submit();
		return true;	
	},
	
	loadAdvancedSearch: function() {
		if(!CalcunConfig.trim($('#advert-search-advanced').text())) {
			$.ajax({
				url: advertClient.url,
				data: {
					view: 'client',
					form: 'advancedSearch'
				}, success: function(data) {
					$('#advert-search-advanced').hide().empty().append(data);
					advertClient.attachAutocomplete();
					advertClient.showAdvancedSearch();
					advertClient.attachHints();
				}
			});
		} else {
			advertClient.showAdvancedSearch();
		}
	},
	
	showAdvancedSearch: function() {
		$('#advert-search-advanced').slideDown('normal',
			function() {
				$('#advert-advanced-open').hide();
				$('#advert-advanced-close').show();
		});
	},
	
	attachHints: function() {
		$('#hirdetes_kereso_form .hint-need').each(function() {
			$(this).focus(function() {
				$('.hint').hide();
				$('#hint_' + $(this).attr('name')).show();
			});
			$(this).blur(function() {
				$('.hint').hide();
				$('#hint_base').show();	
			});
		});
	},
	
	attachAutocomplete: function() {
		$('#dateFromSearch, #dateToSearch').datepicker({
			dateFormat: 'yy.mm.dd.'
		});
		$('#usernameSearch').autocomplete({source: CalcunConfig.basePath + 'Plugins/AdvertServer/index.php?view=autocomplete&type=user'});
		$('#citySearch').autocomplete({source: CalcunConfig.basePath + 'Plugins/AdvertServer/index.php?view=autocomplete&type=city'});
	},
	
	makeConnection: function(advertId) {
		this.lights['makeConnection'] = new CalcunLightbox({
			action: 'Plugins/AdvertClient/index.php?view=client&form=makeConnection&advertId=' + advertId,
			content: 'html',
			width: 600,
			height: 400, 
			title: 'Kapcsolatfelvétel', 
			callback: function() {
				var send = function() {
					$('#makeConnectionForm').unbind('submit');
					$('#makeConnectionForm').submit();
				};
				
				$('#makeConnectionForm').bind('submit', function() {
					if(!CalcunConfig.trim($('#message').val())) {
						jcError('message_empty', 'alert');
					} else {
						if($('#message-char-count').hasClass('message-char-toolong')) {
							jcConfirm('message_will_be_cutted', 'message_too_long', function(r) {
								if(r) {
									send();
								}
							});
						} else {
							send();
						}
					}
					return false;
				});
				
				
				var maxLength = $('#message').attr('maxlength');
				$('#message').keyup(function(e) {
					var value = $(this).val().length;
					var remaining = maxLength - value;
					$('#message-char-count').empty().text(remaining);
					if(remaining < 1) {
						$('#message-char-count').addClass('message-char-toolong');
					} else {
						$('#message-char-count').removeClass('message-char-toolong');
					}
				});
			}
		});
		this.lights['makeConnection'].show();
	},
	
	cancelMakeConnection: function() {
		this.lights['makeConnection'].close();
	},
	
	disconnect: function(element) {
		jcConfirm('really_disconnect', 'confirm', function(r, c) {
			if(r) {
				if(c) {
					$(element).attr('href', $(element).attr('href') + '&messagesToo=true');
				}
				document.location.href = $(element).attr('href');
			}
		}, 'delete_messages_too');
		
		return false;	
	},
	
	restoreAdvert: function(advertId) {
		jcConfirm('really_restore_advert', 'confirm', function(r, c) {
			if(r) {
				var url = advertClient.url + '?view=settings&action=restore&advertId=' + advertId;
				if(c) {
					url += '&messagesToo=true';
				}
				document.location.href = url;
			}
		}, 'restore_messages_too');
		
		return false;	
	},
	
	deleteAdvert: function(element) {
		jcConfirm('really_delete', 'confirm', function(r, c) {
			if(r) {
				if($(element).attr('action')) {
					if(c) {
						$(element).attr('action', $(element).attr('action') + '&messagesToo=true');
					}
					$(element).unbind('submit').submit();
				} else if($(element).attr('href')) {
					if(c) {
						$(element).attr('href', $(element).attr('href') + '&messagesToo=true');
					}
					document.location.href = $(element).attr('href');
				}
			}
		}, 'delete_messages_too');
		
		return false;
	},
	
	deleteAdvertPermanently: function(element) {
		jcConfirm('really_delete_permanently', 'confirm', function(r, c) {
			if(r) {
				if($(element).attr('action')) {
					$(element).unbind('submit').submit();
				} else if($(element).attr('href')) {
					document.location.href = $(element).attr('href');
				}
			}
		});
		
		return false;
	},
	
	showRating: function(advertId, userId) {
		this.lights['showRating'] = new CalcunLightbox({
			action: 'Plugins/AdvertClient/index.php?view=client&form=showRating&advertId=' + advertId + '&userId=' + userId,
			content: 'html',
			width: 600,
			height: 400, 
			title: 'Értékelés', 
			callback: function() {
				var send = function() {
					$('#ratingForm').unbind('submit');
					$('#ratingForm').submit();
				};
				
				$('#ratingForm').bind('submit', function() {
					if(!CalcunConfig.trim($('#rateText').val())) {
						jcError('message_empty', 'alert');
					} else {
						if($('#message-char-count').hasClass('message-char-toolong')) {
							jcConfirm('message_will_be_cutted', 'message_too_long', function(r) {
								if(r) {
									send();
								}
							});
						} else {
							send();
						}
					}
					return false;
				});
				
				
				var maxLength = $('#rateText').attr('maxlength');
				$('#rateText').keyup(function(e) {
					var value = $(this).val().length;
					var remaining = maxLength - value;
					$('#message-char-count').empty().text(remaining);
					if(remaining < 1) {
						$('#message-char-count').addClass('message-char-toolong');
					} else {
						$('#message-char-count').removeClass('message-char-toolong');
					}
				});				
			}
		});
		this.lights['showRating'].show();		
	},
	
	cancelRating: function() {
		this.lights['showRating'].close();
	},
	
	changeMedium: function(img) {
		var url = img.src;
		url = url.replace("_small_", "_medium_");
		document.getElementById('imgMedium').src = url;
	},
	
	showExtraForm: function() {
		if($('#advertStatus').val() == '1') {
			$('#messagesToo').show();
		} else {
			$('#messagesToo').hide();
		}
	},
	
	star_h: function(n) {
		$('#star').attr('class', 'star_' + n);
	},
	
	star: function(n) {
		$('#rating').val(n);
		this.stars = n;
	},
	
	star_o: function() {
		$('#star').attr('class', 'star_' + this.stars)
	}
	
};

$(document).ready(function() {
	$('#advert-advanced-open').click(function() {
		advertClient.loadAdvancedSearch();
		return false;	
	});
	
	$('#advert-advanced-close').click(function() {
		$('#advert-search-advanced').slideUp('normal', function() {
			$('#advert-advanced-close').hide();
			$('#advert-advanced-open').show();
			$(this).hide();
			
		});
		return false;
	});
	advertClient.attachAutocomplete();
	advertClient.attachHints();
	
});
