/**
 * 
 */
$(document).ready(function(){
	var opened=0;
	$("#newsletter-input").click(function(){
	if($("#newsletter-input").val()=="Wpisz swój adres email..."){
		$("#newsletter-input").val("");
	}
	});
	
	$("#newsletter-add").click(function(){
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var hasError=false;
		if($("#newsletter-input").val() == '') {
            $("#UserEmail").html('<span style="color:#FF0000;">Wprowadź swój adres e-mail</span>');
            hasError = true;
        }else if(!emailReg.test($("#newsletter-input").val())) {
            $("#UserEmail").html('<span style="color:#FF0000;">Wprowadź poprawny e-mail</span>');
            hasError = true;
        }
		if(hasError==false){
			$("#UserEmail").html('<span style="color:#f66c0f;">Czekaj...</span>');
			$.ajax({
				type: "POST",
				url: base+"mailing_users/add1",
				data: 'mail='+$("#newsletter-input").val(),
				success: function(msg){
					$("#UserEmail").html('');
					 $.colorbox({href:"/img/cbox-2.png",close:""});
						setTimeout("$.colorbox.close()",5000);
				}
			})
		}
		
	});
	$("a[rel='school-gallery']").lightBox(
	{
		txtImage: "Zdjęcie",
		txtOf: "z",
		imageBtnPrev: '/images/lb-prev.png',
		imageBtnNext: '/images/lb-next.png'
	}		
	);
	 $("ul.subnav1").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)  
	  
	    $("ul.topnav1").mouseover(function() { //When trigger is clicked... 
			
				$("#rollover3").attr("src",base+"img/oferta-krztalcenia-h.png");
		  
				//Following events are applied to the subnav itself (moving subnav up and down)  
				$(this).find("ul.subnav1").stop().slideDown('fast').show(); //Drop down the subnav on click  
			
	      /*  $(this).parent().hover(function() {  
	        }, function(){  
	        	
	            $(this).find("ul.subnav1").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up  
				
	        });  */
	  
	        
	        }).hover(function() {  
	            $(this).addClass("subhover"); //On hover over, add class "subhover"  
	        }, function(){
	        	$("#rollover3").attr("src",base+"img/oferta-krztalcenia.png");
	            $(this).removeClass("subhover"); //On hover out, remove class "subhover"  
	    });
		
		 $("ul.topnav1").mouseout(function(){
			$(this).find("ul.subnav1").slideUp('slow');
		 });
	  $("input[name='news-type']").click(function(){
		 switch($(this).attr("value")){
		 case 'radio-all':
			 window.location=base+'aktualnosci-i-wydarzenia';
			 break;
		 case 'radio-news':
			 window.location=base+'aktualnosci-i-wydarzenia/aktualnosci';
			 break;
		 case 'radio-events':
			 window.location=base+'aktualnosci-i-wydarzenia/wydarzenia';
			 break;
		 case 'radio-conferences':
			 window.location=base+'aktualnosci-i-wydarzenia/konferencje';
			 break;
		 case 'radio-others':
			 window.location=base+'aktualnosci-i-wydarzenia/pozostale';
			 break;
		 } 
	  });
	  $(".news article .toggle-it").click(function(){
		//$(this).parents("article").siblings().children().find('.news-txt').slideUp();
		//$(this).parents("article").children().find('.news-txt').slideDown();
		if($(this).parent().parent().find('.news-txt').css("display")=="none"){
		 $(this).parent().parent().parent().find('.news-txt').slideUp();
		 
		 $(this).parent().parent().find('.news-txt').slideDown();
		 }else{
		 $(this).parent().parent().parent().find('.news-txt').slideUp();
		 }
		 
	  });
	  
	  //bookmark
	  if(window.opera) {
			if ($("a.bookmarkhref").attr("rel") != ""){ // don't overwrite the rel attrib if already set
				$("a.bookmarkhref").attr("rel","sidebar");
			}
		}

		$("a.bookmarkhref").click(function(event){
			event.preventDefault(); // prevent the anchor tag from sending the user off to the link
			var url = this.href;
			var title = this.title;

			if (window.sidebar) { // Mozilla Firefox Bookmark
				window.sidebar.addPanel(title, url,"");
			} else if( window.external ) { // IE Favorite
				window.external.AddFavorite( url, title);
			} else if(window.opera) { // Opera 7+
				return false; // do nothing - the rel="sidebar" should do the trick
			} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
				 alert('Unfortunately, this browser does not support the requested action,'
				 + ' please bookmark this page manually.');
			}

		});
		//szukaj
		$('#search-input').bind('keypress', function(e) {
			var code = (e.keyCode ? e.keyCode : e.which);
			 if(code == 13) { //Enter keycode
			  window.location=base+"szukaj/"+$('#search-input').val();
			 }

			
		});
		$(".search-box a").click(function(){
			window.location=base+"szukaj/"+$('#search-input').val();
		});
		$("input[name='school-type']").click(function(){
			var j='';
			$.ajax({
				type: "POST",
				url: base+"schools/get_schools_json/"+$(this).val(),
				success: function(msg){
					j=jQuery.parseJSON(msg);
				
					var options = '';
				      for (var i = 0; i < j.length; i++) {
				        options += '<option value="' + j[i]['School']['id'] + '">' + j[i]['School']['title'] + '</option>';
				      }
				      $("select#schools").html(options);
				      
				}
			});
			
			
		     // alert(options);
		     // 

		});
		function rekrutacjaSubmit(){
			$.ajax({
				   type: "POST",
				   url: base+"home/rekrutacja2",
				   data: $("#signupForm").serialize(),
				   success: function(msg){
				      if(msg==1){
				    	  $.colorbox({href:"/img/cbox-1.png",close:""});
							setTimeout("$.colorbox.close()",5000);
				      }else{
				    	  $.colorbox({href:"/img/cbox-3.png",close:""});
							setTimeout("$.colorbox.close()",5000);
				      }
				   }
			});
		}
		/*$("#signupForm").submit(function(event){
			
			
			return false;
		});*/
		
		$("#signupForm").validate({
			errorLabelContainer: "#messageBox",
			groups: {
				pola: "name email phone street city school",
			},
			rules: {
				name: "required",
				email: {
					required: true,
					email: true
				},
				phone: "required",
				street: "required",
				city: "required",
				agree: "required",
				school: "required"
			
			},
			messages: {
				name: "Musisz uzupełnić wszystkie pola",
				phone: "Musisz uzupełnić wszystkie pola",
				email: "Musisz uzupełnić wszystkie pola",
				street: "Musisz uzupełnić wszystkie pola",
				city: "Musisz uzupełnić wszystkie pola",
				school: "Musisz uzupełnić wszystkie pola",
				agree: "Musisz zaakceptować warunki.",
				
					
			},
			submitHandler: function(form){
				$.ajax({
				   type: "POST",
				   url: base+"home/rekrutacja2",
				   data: $("#signupForm").serialize(),
				   success: function(msg){
				      if(msg==1){
				    	  $.colorbox({href:"/img/cbox-1.png",close:""});
							setTimeout("$.colorbox.close()",5000);
				      }else{
				    	  $.colorbox({href:"/img/cbox-3.png",close:""});
							setTimeout("$.colorbox.close()",5000);
				      }
				   }
				});
			}
			
		});
		
});

