//****************************************** METODY *****************************************************************

//*************************************** J Q U E R Y ****************************************************************
$(document).ready(function() {
	  
	$(window).scroll(function(){
    if  ($(window).scrollTop() > $(".smartBannerIdentifier").offset().top){
      $(".banner").css("position", "fixed");
      $(".banner").css("top", "0");
    }
   	if  ($(window).scrollTop() <= $(".smartBannerIdentifier").offset().top){
      $(".banner").css("position", "relative");
      $(".banner").css("top", $(".smartBannerIdentifier").offset);
    }
    if  ($('#footer').offset().top <= ($(".banner").offset().top+$(".banner").height())){
      $(".banner").css("position", "absolute");
      $(".banner").css("top", $("#footer").offset().top-$(".banner").height()-15);
      
    }
   }); 
   
   //zalani emailu poptavky
   	$('#send-mail').click(function() {
	  	jConfirm('Opravdu si přejete odeslat tento formulář?', 'Odeslat', function(confirm) {
		    if(confirm){
				sendForm();
		 	}					       
		});
	});
   	
   	
   	addBanner("/Public/Banner/mojezatvoje.swf",240,400,"div.banner");
});

function showHide(thisClick){
var content = $(thisClick).parent().find("div.content");
	if($(content).is(":visible") ){
	  $(thisClick).addClass('plus');
	  $(content).animate({
	    opacity: 0.0,
	    height: 'toggle'
	  }, 500, function() {
	    // Animation complete.
	  });
	  
	}else{
	  	$(thisClick).removeClass('plus');
		$(content).animate({
	    opacity: 1.0,
	    height: 'toggle'
	  }, 500, function() {
	    // Animation complete.
	  });
	}
  
}

function filter(){

	var city    = $("select[name=city]").val();
	var section = $("select[name=section]").val();
	var order   = $("select[name=order]").val();
	var type    = $("input[name=type]").val();
	
	$.ajax({
		type: "POST",
		url: "/helper/get-type-advertisement",
		data: "city="+city+"&type="+type+"&order="+order+"&section="+section,
		success: function(html){	    
		    $('div.type-content').html(html);
		}

	});  

}
function sendForm(){

	$("span.animation")
        .ajaxStart(function(){
            $(this).show();
            
     })
     .ajaxComplete(function(){
            
            $(this).hide();
            
    });
    
	var name    = encodeURIComponent($("input[name=name]").val());
	var tel 	= encodeURIComponent($("input[name=tel]").val());
	var email   = $("input[name=email]").val();
	var type    = encodeURIComponent($("input[name=type]").val());
	var city    = encodeURIComponent($("input[name=city]").val());
	var text    = encodeURIComponent($("textarea[name=text]").val());
	$('#footer p.err').text('');
	$('#footer p.mess').text('');
	
	if(name != "" && email != "" && type != "" && city != ""){
		if(isValidEmailAddress(email)){
			email = encodeURIComponent(email);
			$.ajax({
				type: "POST",
				url: "/helper/send-form",
				data: "name="+name+"&tel="+tel+"&email="+email+"&type="+type+"&city="+city+"&text="+text,
				success: function(html){	
								    
					$('#footer p.mess').text('Váše nabídka byla úspěšně odeslána.');
					
					$("input[name=name]").val('');
					$("input[name=tel]").val('');
					$("input[name=email]").val('');
					$("input[name=type]").val('');
					$("input[name=city]").val('');
					$("textarea[name=text]").val('');
					
				}
		
			}); 
		
		}else $('#footer p.err').text('Vložený E-mail není ve správném tvaru.');
	}else $('#footer p.err').text('Nevyplnili jste povinné údaje.');
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

