/**
 * Autouzupełnianie listy użytkowników
 */
jQuery().ready(
		function() {
			jQuery("input[name=login]").autocomplete(
					'modules/ajax/users/user_list.php', {
						width : 250
					});
			jQuery('#contentDeeper a[href $= .jpg]').lightBox();
			jQuery('#contentDeeper a[href $= .png]').lightBox();
			jQuery('#contentDeeper a[href $= .gif]').lightBox();
			jQuery('#contentDeeper a[href $= .bmp]').lightBox();
			jQuery('#gallery a[href $= .jpg]').lightBox();
			jQuery('#gallery a[href $= .png]').lightBox();
			jQuery('#gallery a[href $= .gif]').lightBox();
			jQuery('#gallery a[href $= .bmp]').lightBox();
		});

/**
 * Autouzupełnianie listy miejscowości
 */
function cityList(fieldName) {
	jQuery("input[name=" + fieldName + "]").autocomplete(
			'modules/ajax/expeditions/city_list.php', {
				width : 250
			});
}

/**
 * dodaj zdjęcie
 */
function addPhoto(type, photoNumber, photosLimit) {
	jQuery('#photo' + photoNumber).load(
			'modules/ajax/photos/photo_add.php?type=' + type + '&photoNumber='
					+ photoNumber + '&photosLimit=' + photosLimit);
	if (photoNumber < photosLimit) {
		jQuery('#addPhotoLink').load(
				'modules/ajax/photos/photo_add_link.php?type=' + type
						+ '&photoNumber=' + photoNumber + '&photosLimit='
						+ photosLimit);
	} else {
		jQuery('#addPhotoLink').html('');
	}
	return false;
}

/**
 * Dodaj zdjęcie
 */
function addAdvertPhoto(type, photoNumber) {
    jQuery('#photo'+photoNumber).load('modules/ajax/photos/photo_add.php?type='+type+'&photoNumber='+photoNumber);
    jQuery('#addPhotoLink').load('modules/ajax/photos/photo_add_link.php?type='+type+'&photoNumber='+photoNumber);
    return false;
}

/**
 * Ładowanie listy regionów
 */
function provinceList(number, countryId) {
	jQuery('#province' + number)
			.load(
					'modules/ajax/expeditions/province_list.php?countryId=' + countryId);
	return false;
}

/**
 * zapomniałem hasła
 */
function sendNewPassword() {
	jQuery()
			.ready(
					function() {
						jQuery('div#remaind_password')
								.load(
										'modules/ajax/users/user_new_password.php?email=' + jQuery(
												"input#email").val());
					});
	return false;
}

/**
 * newsletter
 */
function newsletterSubscribeCategorySearch() {
	jQuery().ready(
			function() {
				jQuery('div#newsletter_category').load(
						'modules/ajax/newsletter/newsletter_category.php?subscriberEmail='
								+ jQuery("input#subscriberEmailCategorySearch").val()
								+ '&action='
								+ jQuery("input#newsletterActionCategorySearch").val()
								+ '&category='
								+ jQuery("input#newsletterCategorySearch").val());
			});
	return false;
}

function newsletterSubscribeCategoryOffer() {
	jQuery().ready(
			function() {
				jQuery('div#newsletter_category').load(
						'modules/ajax/newsletter/newsletter_category.php?subscriberEmail='
								+ jQuery("input#subscriberEmailCategoryOffer").val()
								+ '&action='
								+ jQuery("input#newsletterActionCategoryOffer").val()
								+ '&category='
								+ jQuery("input#newsletterCategoryOffer").val());
			});
	return false;
} 

function newsletterSubscribe() {
	jQuery().ready(
			function() {
				jQuery('div#newsletterID').load(
						'modules/ajax/newsletter/newsletter.php?subscriberEmail='
								+ jQuery("input#subscriberEmail").val()
								+ '&action='
								+ jQuery("input#newsletterAction").val());
			});
	return false;
}

/**
 * advicefriend
 */

function adviceFriendMethod() {
	jQuery().ready(
			function() {
				jQuery.ajax( {
					type : "POST",
					url : 'modules/ajax/advicefriend/advicefriend.php?',
					data : jQuery("#polec").serialize(true),
					success : function(msg) {
						jQuery("#polec").html(msg);
					}
				});
			});
	return false;
}

/**
 * rejestracja
 */
function registerForm() {
	jQuery().ready(function(e) {
		jQuery.ajax( {
			type : "POST",
			url : "modules/ajax/users/user_registration.php",
			data : jQuery("#registerForm").serialize(true),
			success : function(msg) {
				jQuery("#registration").html(msg);
				jQuery().scrollTo("#registration");
			}
		});

	});
}

/**
 * edycja użytkownika
 */
function userEditForm() {
	jQuery().ready(function(e) {
		jQuery.ajax( {
			type : "POST",
			url : "modules/ajax/users/user_edit.php",
			data : jQuery("#userEditForm").serialize(true),
			success : function(msg) {
				jQuery("#useredit").html(msg);
				jQuery().scrollTo("#useredit");
			}
		});

	});
}

/**
 * dodawanie 
 */
function advertNewForm() {
	jQuery().ready(function(e) {
		jQuery.ajax( {
			type : "POST",
			url : "modules/ajax/adverts/advert_new.php",
			data : jQuery("#advertNewForm").serialize(true),
			success : function(msg) {
				jQuery("#advertnew").html(msg);
				jQuery().scrollTo("#advertnew");
			}
		});

	});
}

/**
 * edycja 
 */
function advertEditForm() {
	jQuery().ready(function(e) {
		jQuery.ajax( {
			type : "POST",
			url : "modules/ajax/adverts/advert_my_edit.php",
			data : jQuery("#advertEditForm").serialize(true),
			success : function(msg) {
				jQuery("#advertedit").html(msg);
				jQuery().scrollTo("#advertedit");
			}
		});

	});
}

/**
 * oceny
 */
jQuery().ready(function() {
	jQuery('#noteForm').submit(function(e) {
		e.preventDefault();
		jQuery.ajax( {
			url : "modules/ajax/notes/note.php",
			data : jQuery(this).serialize(),
			success : function(msg) {
				jQuery("#note_box").html(msg);
			}
		});
		return false;
	});
});

/**
 * Rezerwacje
 */
jQuery('#reservationForm').live('submit', function(e) {
	e.preventDefault();
	jQuery.ajax( {
		url : "modules/ajax/flats/reservations.php",
		data : jQuery(this).serialize(),
		success : function(msg) {
			jQuery("#reservations").html(msg);
		}
	});
	return false;
});
/**
 * Usuń rezerwację
 */
function delReservation(flatId, reservationId) {
	jQuery('div#reservations').load(
			"modules/ajax/flats/reservations.php?action=delete&flatId="
					+ flatId + "&reservationId=" + reservationId);
}

/**
 * zakładki na stronie głównej - wyszukiwarki i galeria
 */
jQuery().ready(function() {
	jQuery("div#search_bookmarks div").click(function() {
		jQuery("#col_r div#search1").removeClass("zakladki2_on");
		jQuery("#col_r div#search2").removeClass("zakladki2_on");
		jQuery("#col_r div#search1").addClass("zakladki2_off");
		jQuery("#col_r div#search2").addClass("zakladki2_off");
		jQuery(this).addClass("zakladki2_on");
		jQuery(this).removeClass("zakladki2_off");
		var id = jQuery(this).attr("id");
		jQuery("#col_r div#search1_content").hide();
		jQuery("#col_r div#search2_content").hide();
		jQuery("#col_r div#" + id + "_content").show();
		jQuery("#szukaj_box_top").removeClass("szukaj_box_top_search1");
		jQuery("#szukaj_box_top").removeClass("szukaj_box_top_search2");
		jQuery("#szukaj_box_top").addClass("szukaj_box_top_" + id);

		jQuery("div#search_bookmarks").removeClass("zakladki2_search1");
		jQuery("div#search_bookmarks").removeClass("zakladki2_search2");
		jQuery("div#search_bookmarks").addClass("zakladki2_" + id);
	});
	jQuery("div#gallery_bookmarks div").click(function() {
		jQuery("div#gallery1").removeClass("zakladki1_on");
		jQuery("div#gallery2").removeClass("zakladki1_on");
		jQuery("div#gallery1").addClass("zakladki1_off");
		jQuery("div#gallery2").addClass("zakladki1_off");
		jQuery(this).addClass("zakladki1_on");
		jQuery(this).removeClass("zakladki1_off");
		var id = jQuery(this).attr("id");
		jQuery("div#gallery1_content").hide();
		jQuery("div#gallery2_content").hide();
		jQuery("div#" + id + "_content").show();

		jQuery("div#gallery_bookmarks").removeClass("zakladki1_gallery1");
		jQuery("div#gallery_bookmarks").removeClass("zakladki1_gallery2");
		jQuery("div#gallery_bookmarks").addClass("zakladki1_" + id);
	});

	jQuery("div#rotator div.list div").click(function() {
		var id = jQuery(this).attr("id");

		if (id != "news1") {
			jQuery("div#news1").addClass("item");
			jQuery("div#news1").removeClass("item_a");
		}
		if (id != "news2") {
			jQuery("div#news2").addClass("item");
			jQuery("div#news2").removeClass("item_a");
		}
		if (id != "news3") {
			jQuery("div#news3").addClass("item");
			jQuery("div#news3").removeClass("item_a");
		}

		jQuery(this).removeClass("item");
		jQuery(this).addClass("item_a");

		jQuery("a#news1_content").hide();
		jQuery("a#news2_content").hide();
		jQuery("a#news3_content").hide();

		jQuery("a#" + id + "_content").show();

	});
});

/**
 * Limity w polach tekstowych textarea
 */
var photoDescriptionLimit = 255;
var flatPhotoDescriptionLimit = 160;
var articleTitleLimit = 160;
var articleShortContentLimit = 255;
var userDescriptionLimit = 256;
var relationTitleLimit = 160;
var relationDescriptionLimit = 256;
var clanDescriptionLimit = 500;
jQuery(document).ready(
		function() {
			/**
			 * Zdjęcia
			 */
			jQuery("form[name=photoForm] textarea[name=description]").keyup(
					function() {
						var count_chars = jQuery(this).val().length;
						if (count_chars > photoDescriptionLimit) {
							var new_value = jQuery(this).val().substring(0,
									photoDescriptionLimit);
							jQuery(this).val(new_value)
						}
						jQuery("#photoDescriptionLeft").html(
								"" + photoDescriptionLimit
										- jQuery(this).val().length + "");
					});

			/**
			 * Klany
			 */
			jQuery("form[name=clanForm] textarea[name=description]").keyup(
					function() {
						var count_chars = jQuery(this).val().length;
						if (count_chars > clanDescriptionLimit) {
							var new_value = jQuery(this).val().substring(0,
									clanDescriptionLimit);
							jQuery(this).val(new_value)
						}
						jQuery("#clanDescriptionLeft").html(
								"" + clanDescriptionLimit
										- jQuery(this).val().length + "");
					});

			/**
			 * Rejestracja
			 */
			jQuery("textarea[name=about_me]").keyup(
					function() {
						var count_chars = jQuery(this).val().length;
						if (count_chars > userDescriptionLimit) {
							var new_value = jQuery(this).val().substring(0,
									userDescriptionLimit);
							jQuery(this).val(new_value)
						}
						jQuery("#userDescriptionLeft").html(
								"" + userDescriptionLimit
										- jQuery(this).val().length + "");
					});

			/**
			 * Relacja
			 */
			jQuery("form[name=relationForm] textarea[name=short_content]")
					.keyup(
							function() {
								var count_chars = jQuery(this).val().length;
								if (count_chars > relationDescriptionLimit) {
									var new_value = jQuery(this).val()
											.substring(0,
													relationDescriptionLimit);
									jQuery(this).val(new_value)
								}
								jQuery("#relationDescriptionLeft").html(
										"" + relationDescriptionLimit
												- jQuery(this).val().length
												+ "");
							});
			jQuery("form[name=relationForm] input[name=title]")
			.keyup(
					function() {
						var count_chars = jQuery(this).val().length;
						if (count_chars > relationTitleLimit) {
							var new_value = jQuery(this).val()
									.substring(0,
											relationTitleLimit);
							jQuery(this).val(new_value)
						}
						jQuery("#relationTitleLeft").html(
								"" + relationTitleLimit
										- jQuery(this).val().length
										+ "");
					});

			/**
			 * Artykuły
			 */
			jQuery("form[name=articleForm] textarea[name=short_content]")
					.keyup(
							function() {
								var count_chars = jQuery(this).val().length;
								if (count_chars > articleShortContentLimit) {
									var new_value = jQuery(this).val()
											.substring(0,
													articleShortContentLimit);
									jQuery(this).val(new_value)
								}
								jQuery("#articleShortContent").html(
										"" + articleShortContentLimit
												- jQuery(this).val().length
												+ "");
							});
			jQuery("form[name=articleForm] input[name=title]")
			.keyup(
					function() {
						var count_chars = jQuery(this).val().length;
						if (count_chars > articleTitleLimit) {
							var new_value = jQuery(this).val()
									.substring(0,
											articleTitleLimit);
							jQuery(this).val(new_value)
						}
						jQuery("#articleTitleLeft").html(
								"" + articleTitleLimit
										- jQuery(this).val().length
										+ "");
					});
			
			/**
			 * Zdjęcia dla mieszkań, relacji
			 */
			jQuery("form[name=flatForm] textarea[name=photo1_description], form[name=relationForm] textarea[name=photo1_description], form[name=articleForm] textarea[name=photo1_description]").keyup(
					function() {
						var count_chars = jQuery(this).val().length;
						if (count_chars > flatPhotoDescriptionLimit) {
							var new_value = jQuery(this).val().substring(0,
									flatPhotoDescriptionLimit);
							jQuery(this).val(new_value)
						}
						jQuery("#photoDescriptionLeft1").html(
								"" + flatPhotoDescriptionLimit
										- jQuery(this).val().length + "");
					});
			jQuery("form[name=flatForm] textarea[name=photo2_description], form[name=relationForm] textarea[name=photo2_description], form[name=articleForm] textarea[name=photo2_description]").keyup(
					function() {
						var count_chars = jQuery(this).val().length;
						if (count_chars > flatPhotoDescriptionLimit) {
							var new_value = jQuery(this).val().substring(0,
									flatPhotoDescriptionLimit);
							jQuery(this).val(new_value)
						}
						jQuery("#photoDescriptionLeft2").html(
								"" + flatPhotoDescriptionLimit
										- jQuery(this).val().length + "");
					});
			jQuery("form[name=flatForm] textarea[name=photo3_description], form[name=relationForm] textarea[name=photo3_description], form[name=articleForm] textarea[name=photo3_description]").keyup(
					function() {
						var count_chars = jQuery(this).val().length;
						if (count_chars > flatPhotoDescriptionLimit) {
							var new_value = jQuery(this).val().substring(0,
									flatPhotoDescriptionLimit);
							jQuery(this).val(new_value)
						}
						jQuery("#photoDescriptionLeft3").html(
								"" + flatPhotoDescriptionLimit
										- jQuery(this).val().length + "");
					});
			jQuery("form[name=flatForm] textarea[name=photo4_description], form[name=relationForm] textarea[name=photo4_description], form[name=articleForm] textarea[name=photo4_description]").keyup(
					function() {
						var count_chars = jQuery(this).val().length;
						if (count_chars > flatPhotoDescriptionLimit) {
							var new_value = jQuery(this).val().substring(0,
									flatPhotoDescriptionLimit);
							jQuery(this).val(new_value)
						}
						jQuery("#photoDescriptionLeft4").html(
								"" + flatPhotoDescriptionLimit
										- jQuery(this).val().length + "");
					});
			jQuery("form[name=flatForm] textarea[name=photo5_description], form[name=relationForm] textarea[name=photo5_description], form[name=articleForm] textarea[name=photo5_description]").keyup(
					function() {
						var count_chars = jQuery(this).val().length;
						if (count_chars > flatPhotoDescriptionLimit) {
							var new_value = jQuery(this).val().substring(0,
									flatPhotoDescriptionLimit);
							jQuery(this).val(new_value)
						}
						jQuery("#photoDescriptionLeft5").html(
								"" + flatPhotoDescriptionLimit
										- jQuery(this).val().length + "");
					});

		});

/**
 * Planowanie trasy podróży przy dodawaniu wyprawy
 */
jQuery().ready(function() {
	jQuery("div#planRouteLink").click(function() {
		jQuery("div#planRoute").show();
	});
});

function popUp(URL) {
	var width=900;
	var height=600;
	id = "popup";
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height+",left = "+(screen.width-width)/2+",top = "+(screen.height-height)/2+"');");
}
