jQuery(function() {
	           
  jQuery('#type' + jQuery('#AnnouncementTypeId option:selected').attr('value')).show();
  		
	// jQuery('#messages div').hide();
	jQuery('#messages input').click(function() {
		jQuery('#messages div').hide();
	});

	jQuery("#AccountMenu a").click(function() {
		var tsTimeStamp= new Date().getTime();
		var site = jQuery(this).attr('alt');
		jQuery("#AccountContent").load("/users/" + site + '/'+tsTimeStamp);
	});

  jQuery("#TextContent").keydown(function() {
			var maxlength = jQuery("#TextContent").attr('maxlength');
			var current = jQuery("#TextContent").val().length;
    	if (current > maxlength) {
        //limitField.value = limitField.value.substring(0, limitNum);
        jQuery("#TextContent").val(jQuery("#TextContent").val().substring(0, maxlength));
	    }
	    jQuery("#TextContentLength").val(maxlength - jQuery("#TextContent").val().length);
	});

	// zmiana wojewodztwa w wyszukiwarce, ladujemy miasta
	jQuery("#SearchAnnouncementRegionId").change(function() {
		jQuery.getJSON("/announcements/getTowns/"
					+ jQuery(this).val(), null, function(towns) {
					var options = '';
					for (i in towns) {
						options += '<option value="' + i + '">' + towns[i]
								+ '</option>';
					}
					jQuery("#SearchAnnouncementTownId").html(options);
				})
		});

	// zmiana wojewodztwa w formularzu anonsu, ladujemy miasta, czyscimy
	// dzielnice

	jQuery("#AnnouncementRegionId, .AnnouncementRegionId").change(
			function() {
				jQuery.getJSON("/announcements/getTowns/"
						+ jQuery(this).val(), null, function(towns) {
					var options = '';
					for (i in towns) {
						options += '<option value="' + i + '">' + towns[i]
								+ '</option>';
					}
					jQuery("#AnnouncementTownId, .AnnouncementTownId").html(
							options);
				})
				jQuery("#AnnouncementDistrictId, .AnnouncementDistrictId")
						.html("<option value='0'>lista pusta</option>");
			});                                   

	// zmiana miasta w formularzu anonsu, ladujemy dzielnice, czysimy dzielnice
	jQuery("#AnnouncementTownId, .AnnouncementTownId").change(
			function() {
				jQuery.getJSON("/announcements/getDistricts/"
						+ jQuery(this).val(), null, function(towns) {
					var options = '';
					for (i in towns) {
						options += '<option value="' + i + '">' + towns[i]
								+ '</option>';

					}
					jQuery("#AnnouncementDistrictId, .AnnouncementDistrictId").html(options);
				})
			});

	// dodwanie trescie angielskiej i niemieckiej
	jQuery("#AnnouncementAddEnglish").click(function() {
		jQuery("#AnnouncementContentEnglish").toggle();
	});

	jQuery("#AnnouncementAddGerman").click(function() {
		jQuery("#AnnouncementContentGerman").toggle();
	});

	// galeria w anonsie
//	jQuery("#gallery img").click(function() {
//		var big = jQuery(this).attr('name');
//		jQuery("#main_photo").attr('src', big+'/normal');
//		jQuery("#main_photo_link").attr('href', big+'/big');
		//jQuery.scrollTo(jQuery("#back"), 100);
//	});

	// dodaj anons do ulubionych
	jQuery("#navigation a[rel=favourite]").click(function() {
		var id = jQuery(this).attr('title');
		jQuery("#response").load("/favourites/add/" + id);
	});

	// dodaj anons do ulubionych
	jQuery("#navigation a[rel=comment]").click(function() {
		jQuery.scrollTo(jQuery("#addcomment"), 100);
	});
	
	
  
	// usun zdjecie uploadowane przy dodawaniu ogloszenia
	jQuery("a.delphoto").click(function() {
		var filename = jQuery(this).attr('alt');
		alert(filename);
		jQuery("#uploaded").load("/announcements/delphoto/" + filename);
	});

	// wylaczanie submita po wyslaniu zapytania
	jQuery("form").submit(function() {
		var top = 0;
		if ((!document.compatMode) || (document.compatMode == 'BackCompat')) {
			top = document.body.scrollTop;
		}
		else {
			top = document.documentElement.scrollTop;
		}

		document.getElementById('loadingbox').style.top = top + 150 + 'px';
		
		jQuery("input[type=submit]").each(function() {
			jQuery(this).attr("disabled", "true");
		});
		jQuery("#loadingbox").show();
	});
	
	// obsluga nawigacji w galerii
//	jQuery("#navigation a[rel=prev],#navigation a[rel=next]").click(function() {
//		var big = jQuery("#main_photo"); // aktualne zdjecie big uchwyt
//		var direction = jQuery(this).attr('rel'); // kierunek przesuniecia (prev next)
//		var gallery = new Array(); // wszystkie big zdjecia anonsu
//		var big_src = big.attr('src'); // adres zdjecia big
//		var src = ''; // zmienna tymczasowa do porownanie
//		var count = jQuery("#gallery img").length; // ilosc zdjec w galerii
//		var target = 0; // zdjecie docelowe

//		jQuery("#gallery img").each(function(i) {
//			src = jQuery(this).attr("name");
//			gallery[i] = src;
//			if ((src+'/normal') == big_src) {
//				if (direction == 'next') { // next
//					if (i == (count - 1)) { // z ostatniego do pierwszego
//						target = 0;
//					} else {
//						target = i + 1;
//					}
//				} else { // prev
//					if (i == 0) { // z pierwszego do ostatniego
//						target = count - 1;
//					} else {
//						target = i - 1;
//					}
//				}
//			}
//		});
//		big.attr('src', gallery[target]+'/normal');
//	});

		
	jQuery('[rel=tooltip]').bind('mouseover', function() {
		var theMessage = jQuery(this).attr('content');
		jQuery('<div class="tooltip">' + theMessage + '</div>').appendTo('body').fadeIn('fast');
		jQuery(this).bind('mousemove', function(e) {
			jQuery('div.tooltip').css({
				'top': e.pageY - (jQuery('div.tooltip').height() / 2) - 5,
				'left': e.pageX + 15
			});
		});
	}).bind('mouseout', function() {
		jQuery('div.tooltip').remove();
	});

	// Powiazanie zdjecia z anonsem
	// Mozliwe ustawienie miniaturki
	jQuery("div.photo a[title=relate]").click(function() {
		var tsTimeStamp= new Date().getTime();
		var link = jQuery(this);
		var photo = link.closest('div');
		jQuery.get('/photos/relate/'+jQuery("#AnnouncementId").val()+'/'+photo.find('img').attr('alt')+'/'+tsTimeStamp, function(result) {
			if (result == 2) {
				// Usuwamy poprzednie zdjecie glowne
				jQuery("div.photo").removeClass('main_photo');
				jQuery("div.related_photo a[title=main]").show();

				// Zaznczamy nowe zdjecie glowne
				photo.addClass('related_photo').addClass('main_photo');
				link.hide();
				link.text('Odłącz');
				photo.find("a[title=relate]").hide();
			}
			else if (result == 1) {
				photo.addClass('related_photo');
				link.text('Odłącz');
				photo.find("a[title=main]").show();
			}
			else {
				photo.removeClass('related_photo');
				link.text('Dołącz');
				photo.find("a[title=main]").hide();
			}
			//alert(data);
			//alert(photo.attr('class'));
			//icon.parents('.PackageOnList').children('.PackageIcon').html(data);
		});
	});
	
	// Ustawienie miniaturki sposrod zdjec powiazanych z anonsem
	jQuery("div.photo a[title=main]").click(function() {
		var tsTimeStamp= new Date().getTime();
		var link = jQuery(this);
		var photo = link.closest('div');
		jQuery.get('/photos/main/'+jQuery("#AnnouncementId").val()+'/'+photo.find('img').attr('alt')+'/'+tsTimeStamp, function(result) {
			if (result == '1') {
				// Usuwamy poprzednie zdjecie glowne
				jQuery("div.photo").removeClass('main_photo');
				jQuery("div.related_photo a[title=main]").show();
				jQuery("div.related_photo a[title=relate]").show();

				// Zaznczamy nowe zdjecie glowne
				photo.addClass('main_photo');
				link.hide();
				photo.find("a[title=relate]").hide();
			}
		});
	});
});


// po potwierdzeniu kliknieciem wysyla informacje o pelnoletnosci
function confirmAge() {
	jQuery.get("/adult");
	jQuery('#confirm_age').hide();
}

function error(text) {
	var top = 0;
	if ((!document.compatMode) || (document.compatMode == 'BackCompat')) {
		top = document.body.scrollTop;
	}
	else {
		top = document.documentElement.scrollTop;
	}
	document.getElementById('error').style.top = top + 150 + 'px';
	jQuery("#error p:last").html(text);
	//alert(text);
	jQuery("#error").show();
}

function success(text) {
	var top = 0;
	if ((!document.compatMode) || (document.compatMode == 'BackCompat')) {
		top = document.body.scrollTop;
	}
	else {
		top = document.documentElement.scrollTop;
	}

	document.getElementById('success').style.top = top + 150 + 'px';
	jQuery("#success p").html(text);
	jQuery("#success").show();
}

// koniec pracy w niedziele

/*
 * 
 * jQuery("#AnnouncementSundayStart").change(function () {
 * 
 * if (parseInt(jQuery("#AnnouncementSundayStart
 * option:selected").attr('value'))>0) {
 * 
 * jQuery("#AnnouncementSundayEnd").show();
 *  } else {
 * 
 * jQuery("#AnnouncementSundayEnd").hide();
 *  }
 * 
 * });
 * 
 */

function upload(action) {
	jQuery("#loadingbox").ajaxStart(function(){jQuery(this).show();});
	
	//.ajaxComplete(function(){jQuery(this).hide();});

	jQuery.ajaxFileUpload( {
		url : '/photos/upload/',
		secureuri : false,
		fileElementId : 'photo',
		dataType : 'json',
		success : function(data, status) {
			if (typeof (data.error) != 'undefined') {
				if (data.error != '') {
					alert(data.error);
				} else {
					alert("Zdjęcie zapisane! Możesz dodawać kolejne!");
					jQuery("#loadingbox").hide();
					if (action == 'temp') {
						loadTempGallery();
					} 
					else if (action == 'editor') {
						loadEditorGallery();
					}
				}
			}
		},
		error : function(data, status, e) {
			alert('Nieokreślony błąd serwera podczas przesyłania plików.');
		}
	});
	return false;
}

// zaladowanie galerii wczytanych zdjec przy dodawaniu anaonsu
function loadTempGallery() {
	var tsTimeStamp= new Date().getTime();
	jQuery("#photos").load("/photos/temp/"+tsTimeStamp);
}

// wlacza wszystkie input type = submit
function enableSubmit() {
	jQuery("#loadingbox").hide();
	jQuery("input[type=submit]").each(function() {
		jQuery(this).removeAttr("disabled");
	});
}

// po zalogowaniu wywolywane by przejsc do ulubionych
function loadUserFavourites() {
	var tsTimeStamp= new Date().getTime();
	jQuery("#AccountContent").load("/users/favourites/"+tsTimeStamp);
}

//zaladowanie galerii wczytanych zdjec przy dodawaniu anaonsu
function loadEditorGallery() {
	var tsTimeStamp= new Date().getTime();
	jQuery("#photos").load("/photos/editor/"+tsTimeStamp);
}


