var BASE_URL = 'http://test.gourmedia.ch/';

$(function() {
	
	/*
	 * wyślij wiadomość popoup launcher
	 */
	try {
		$("a.mailer").click( function(){
			
			var maupa = $(this).attr('href').split("?");
			
			if(maupa[1])
				var subject = maupa[1].split("=");
			else
				var subject = new Array('', '');
				
			var mailparts = maupa[0].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: "365px",
					photo: false,
					arrowKey: false,
					title: "",
					controls: false,
					iframe: true,
					href: "/popup/mail/ln/" + lang + "/email/" + mailparts[1] + "/subject/" + subject[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();
		$('ul.navigation ul').hide();
		
		/* 
		 * Elementy poziomu 1 zawierające childs zmieniamy URL / blokujemy click F.
		 */
		
		$('ul.navigation>li').each(function(index) {
			if ($(this).find("ul>li").size() > 0) {
				//alert(index + ': ' + $(this).html() + ' :: '+ $(this).find("ul>li").size());
				
				$(this).find(">a").attr("href", "#");
				$(this).find(">a").addClass("nolinkover");
				$(this).find(">a").click(function(){
					return false;
				});
			}
		});
		
    }
    catch(e){};    
	
});
