var BASE_URL = 'http://www.askobudownictwo.hostorama.pl/';

$(function() {
	
	$("a.tooltip").tipTip();
	
	/* Bookmark */
	
	try{
		$(".dodajdowybranych").click(function(){
			
			var flatno = $(this).attr("flatno");
			
			var deltxt = "usuń z wybranych mieszkań";
			var marktxt = "dodaj do wybranych mieszkań";
			
			//alert(flatno);
			
			$.get("/ajaxserver/bookmarkflat/flatid/"+flatno, function(number_of_bookmarked_lokals){
				$("#bookmarked-now").html(number_of_bookmarked_lokals)
					
				if(number_of_bookmarked_lokals > 0)
					$("#bookmarks-sidebar").addClass('active');
				else
					$("#bookmarks-sidebar").removeClass('active');
					
				//alert("Data Loaded: " + number_of_bookmarked_lokals);
			});
			
			if(eval('lokal_'+flatno+'_is_bookmarked') == 1)
			{
				eval('lokal_'+flatno+'_is_bookmarked = "0";');
				$(this).removeClass('bookmarked');
				$(this).html(marktxt);
				$(this).attr("title", marktxt);
				if(now_in_lokal_bookmarks == 1) {
					$("#flatty-"+flatno).hide('slow');
				}
			}
			else
			{
				$(this).addClass('bookmarked');
				eval('lokal_'+flatno+'_is_bookmarked = "1";');
				$(this).html(deltxt);
				$(this).attr("title", deltxt);	
			}	
			
			$("a.tooltip").tipTip();
	    });
	} catch(e){};

	
	
	/*
	 * wyślij wiadomość popoup launcher
	 */
	try {
		$("a.mailer").click( function(){
			var mailparts = $(this).attr('href').split(":");
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
            if (reg.test(mailparts[1]) == false) {
				alert('Invalid Email Address');
				return false;
			}
			else {
				$(this).colorbox({
					width: "400px",
					height: "360px",
					photo: false,
					arrowKey: false,
					title: "",
					controls: false,
					iframe: true,
					href: "/popup/mail/ln/" + lang + "/email/" + mailparts[1]
				});
			}
			//return false;
		});
	} 
	catch (e) {
	};
	
	
	/*
	 * more toggler zwiń/rozwiń 
	 */
	try {
		$("a.moretoggler").click( function(){
			var parts = $(this).attr('href').split("#");
			$("#"+parts[1]).toggle('slow');
			if($(this).hasClass('moretoggleractive'))
				$(this).removeClass('moretoggleractive');
			else
				$(this).addClass('moretoggleractive');
			return false;
		});
	} 
	catch (e) {
	};
	
	
	/*
	 * home notices
	 */
	try {
	
		$('.notice .description').hide();
		/*
		$('.pointer').click(
			function () {
				$(this).parent().find('.description').show(300);
			}
		);
		$('.close').click(
			function () {
				$(this).parent().parent().find('.description').hide(250);
			}
		);*/
		
		$('.pointer').mouseover(
			function () {
				var obj = $(this).parent().find('.description');
				//obj.clearQueue();
				obj.show(400);
			}
		);
		$('.pointer').mouseout(
			function () {
				var obj = $(this).parent().find('.description');
				//obj.clearQueue();
				obj.hide(250);
			}
		);
		
		
		
	} catch (e) { 
		// do nothing.
	};
	
	/* 
	 * NEWSLETTER ajax handler 
	 */
	try {
		$("#newsletter-subscribe-button").click(function() {
			
			var newsletter_email = $("input#newsletter_email").val();
			var newsletter_lang = $("input#newsletter_lang").val();
			
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			
			if (newsletter_email == "" || newsletter_email == $("input#newsletter_email").attr('placeholder') ) {
				alert($("#newsletter-mail-error").html());
				$("input#newsletter_email").focus();
				return false;
			}
			else{
				if(!emailReg.test(newsletter_email)){
					alert($("#newsletter-mail-error").html());
					$("input#newsletter_email").focus();
					return false;
				}
				
				var dataString = 'email=' + newsletter_email + '&lang=' + newsletter_lang;
				
				$.ajax({
				    type : "POST",
					url : "/ajaxserver/newslettersubscribe",
					data : dataString,
					success : function(data) {
						var response = eval('(' + data + ')');
						
						if(response.status == 'error'){
							alert(response.message);
							return false;
						}
						else{
							$('#newsletter-form').hide('slow');
							$('#newsletter-form-thankyou').show('slow');
						}
					}
				});
			}

			return false;
		});
	}
	catch(e){};
	
	
	
	/*
	 * Labelka wewnątrz input / textarea np. [wpisz imię   ] => klikamy i znika 
	 * Robimy to następująco <input type="text" name="imie" placeholder="wpisz imię">  
	 */
	try {
		$(':input[placeholder]').each(function() {
			var $this = $(this);
			if ($this.val() === '') {
				$this.val($this.attr('placeholder'));
			}
			$this.focus(function() {
				if ($this.val() === $this.attr('placeholder')) {
					$this.val('');
				}
			});
			$this.blur(function() {
				if ($this.val() === '') {
					$this.val($this.attr('placeholder'));
				}
			});
		});
	}
	catch(e){};
	
	/* 
	 * szukajka 
	 */
	try {
		$('#phrase').focus(function(){
			if ( $(this).val() == "wpisz szukaną frazę" || $(this).val() == "enter phrase" || $(this).val() == "введите искали фразу") 
				$(this).val('');
		});
	} 
	catch (e) {};
		
	/* 
	 * newsletter
	 */
	try {
		$('#email').focus(function() {
			if($(this).val() == "podaj swój e-mail")
				$(this).val('');
		});
    }
    catch(e){};	

	/* 
	 * top nav
	 */
    try {
    	$(".navigation").droppy();
    }
    catch(e){};    
	
});
