﻿/*
$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c).removeClass('hide_menu');  },
			function() { $(this).removeClass(c).addClass('hide_menu'); }
		);
	});
};

$.fn.hoverClass2 = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};


function show_submenu(rel) {
	$('.submenu2[rel='+rel+']').fadeIn();
	$('#menu ul.menu a[rel='+rel+'].menu').addClass('active');
	keep_rel = rel;
}
*/
$(function() {

	/*
	 * $('#tekst #sitemap ul>li>ul>li').css('background', 'none');
	 * $('#tekst #sitemap ul>li>ul>li>ul>li').css('background',
	 * 'transparent url(../img/li_.png) no-repeat scroll left top;
	 * !important');
	 */
	
	var ktory = 1;
	
	setInterval(function() {
		if (ktory == 1) {
			$('#patroni div.wiersz_1').fadeOut(1000, function() {
				$('#patroni div.wiersz_2').fadeIn(1000);
			});
			ktory = 2;
		} else {
		    $('#patroni div.wiersz_2').fadeOut(1000, function() {
				$('#patroni div.wiersz_1').fadeIn(1000);
			});
			ktory = 1;
		}
	}, 6000);
       /*   
   

	$('#patroni div.wiersz_1').fadeIn(1000, function() {
		setInterval(function() {
			
		})
		$(this).delay(4000).fadeOut(1000);
	});
	$('#patroni div.wiersz_2').fadeIn(1000).delay(4000).fadeOut(1000);      
	
	  */
});

function sonda_send(sel) {

	if (typeof(sel) == 'undefined') {
		sel = '#sonda_form';
	}

	$(sel).ajaxSubmit({
// target: '#sonda .in',
		success : function(t,s) {
			var a = 'dupa';
			// $('#sonda').html(t);
		
// $('#sonda').stop(true,true).fadeOut(100, function() {
// $('#sonda').html(t).fadeIn(300);
// $('#sonda').fadeIn();
			$('#sonda').html(t);
// $('#sonda').fade();
// });
			},
		
		beforeSubmit : function(formData, obj, option) {
				
			a = $('#sonda_form :radio').fieldValue();
			if (a.length == 0) {
				return false;
			}
				
// $('#sonda').fadeOut(100, function() {
// $('#sonda').html('<span>Wysyłam...</span>').fadeIn(100);
			$('#sonda').html('<span>Wysyłam...</span>');
// });
			
			return true;
		}
	});	
	
	
}
/*
 * function hide_submenu(param) { $('.submenu2').hide(false); if
 * (selected_id && param) { $('.submenu2[rel='+selected_id+']').show(); }
 * $('#menu ul.menu a').removeClass('active'); }
 */
$(function() {
	
	if (banner) {
		$('#bn').flash({
			src: banner,
		    width: 936,
		    height: banner_height, 
			wmode: 'transparent'
		});
	};
	
	if (reklamabox) {
		$('.reklamabox').flash({
			src: reklamabox,
		    width: 222,
		    height: 230,
		wmode: 'transparent'
		});
	};
	
	$('a.lightbox').lightbox({
		// help: ' \u2190 / P - previous
		// image\u00a0\u00a0\u00a0\u00a0\u2192 / N - next
		// image\u00a0\u00a0\u00a0\u00a0ESC / X - close image gallery',
		prevLinkTitle: 'poprzedni',
		nextLinkTitle: 'następny',
		prevLinkText:  '&laquo; Poprzedni',
		nextLinkText:  'Nastepny &raquo;',
		closeTitle: 'Zamknij',
		image: '',
		of: ' z '
	});
	
	/*
	 * if (document.all) { $("#wrapper #menu ul.menu
	 * li.menu").hoverClass ("sfHover"); $("#wrapper #menu ul.menu
	 * li.menu_active").hoverClass2 ("sfHover"); }
	 * 
	 * $('#menu ul.menu li.menu').hover(function(){ $('#wrapper #menu
	 * ul.menu li.menu_active ul.sub_active').css('top','-999em'); },
	 * function(){
	 * 
	 * $('#wrapper #menu ul.menu li.menu_active
	 * ul.sub_active').css('top','35px'); });
	 * 
	 * $('#menu ul.menu li.menu_active').hover(function(){ //$('#wrapper
	 * #menu ul.menu li.menu_active ul.sub_active').css('top','-999em'); },
	 * function(){ $('#wrapper #menu ul.menu li.menu_active
	 * ul.sub_active').css('top','35px'); });
	 */
	$('a.wstecz_go').click(function() {
		window.history.back();
	});
	
	$('a.drukuj_go').click(function() {
		window.print();
	});
	
	// if (jQuery.accordion != undefined) {
		$('#theMenu').accordion({
			active: 'h3.selected',
			header: 'h3.head',
			alwaysOpen: false,
			autoHeight: false
		});
		
		$('#theMenu h3.head').click(function(){

			$('.zwin').each(function(){
				$('.zwin').css('display', 'none');
				
				
			});
			
			

			if( ($(this).is(".ui-state-active")) )
			{
				$(this).children( '.zwin' ).css('display', 'block');
				
				
			}
		
		});

		$('.zwin').click(function(){
			 $('.zwin').css('display','none');
		});

		$('#user_login_form').validate({
			rules : {
				username : {
					required: true
				},
				password : {
					required: true
				}
			},
			errorElement : "td",
			errorPlacement: function(error, element) {
				// element.parent("td").next("td").next("td").addClass("error");
				error.insertAfter( element.parent("td").nextAll("td:last") );
			},
			/*
			 * success: function(label) {
			 * label.text("").parent("td").removeClass("error"); },
			 */
			messages: {
				username : "Uzupełnij to pole",
				password : "Uzupełnij to pole"
			}
		});
		
		$('#user_pass_form').validate({
			rules : {
				password : {
					required: true
				},
				new_password : {
					required: true,
					minlength: 8
				},
				new_password2 : {
					required: true,
					minlength: 8,
					equalTo : '#new_password'
					
				}
			},
			errorElement : "td",
			errorPlacement: function(error, element) {
				// element.parent("td").next("td").next("td").addClass("error");
				error.insertAfter( element.parent("td").parent("tr").children("td:last") );
			},
			/*
			 * success: function(label) {
			 * label.text("").parent("td").removeClass("error"); },
			 */
			messages: {
				password : "Uzupełnij to pole",
				new_password: {
					required : "Uzupełnij to pole",
					minlength : "Podaj minimum 8 znaków"
				},
				new_password2: {
					required : "Uzupełnij to pole",
					minlength : "Podaj minimum 8 znaków",
					equalTo : "Powtórz poprawnie hasło"
				}
			}
		});
		
		$('#user_forget_form').validate({
			rules : {
				username : {
					required: true
				},
				email : {
					required: true,
					email: true
				}
			},
			errorElement : "td",
			errorPlacement: function(error, element) {
				// element.parent("td").next("td").next("td").addClass("error");
				error.insertAfter( element.parent("td").nextAll("td:last") );
			},
			/*
			 * success: function(label) {
			 * label.text("").parent("td").removeClass("error"); },
			 */
			messages: {
				username : "Uzupełnij to pole",
				email : {
					required : "Uzupełnij to pole",
					email : "Niepoprawny e-mail"
				}
			}
		});
		
		$('#newsletter_form').validate({
			rules : {
				eml : {
					required: true,
					email: true
				}
			},
			errorElement : "label",
			errorClass : "err2",
			messages: {
				eml : {
					required : "Uzupełnij to pole",
					email : "Niepoprawny e-mail"
				}
			}
		});  
		
		
		$('#ebook_form').validate({
			rules : {
				eml : {
					required: true,
					email: true
				}
			},   
			errorPlacement: function(error, element) {
				// element.parent("td").next("td").next("td").addClass("error");
				error.insertAfter( element.parent('td').nextAll("td:last") );
			},
			errorElement : "td",
			errorClass : "err_ebook2",
			messages: {
				eml : {
					required : "Uzupełnij to pole",
					email : "Niepoprawny e-mail"
				}
			}
		});
		
		
		
		 $.validator.addMethod(
		            "regex",
		            function(value, element, regexp) {
		                var check = false;
		                var re = new RegExp(regexp);
		                return this.optional(element) || re.test(value);
		            },
		            "Please check your input."
		    );

		
		$('#reje_form').validate({
			rules : {
				username : {
					required: true
				},
				name : {
					required: true
				},
				email: {
					required: true,
					email : true
				},
				reg: {
					required: true
				},
				postcode: {
					required: false,
					regex: /^[0-9][0-9]-[0-9][0-9][0-9]$/
				}
			},
			errorElement : "td",
// //wrapper : "td",
			errorPlacement: function(error, element) {
				// element.parent("td").nextAll("td:last").addClass("error");
				if (element.attr('name') == 'postcode') {
					error.attr('colspan','3');
				}
				error.insertAfter( element.parent("td").parent().children("td:last"));
				
			},
			/*
			 * success: function(label) {
			 * label.text("").parent("td").removeClass("error"); },
			 */
			messages: {
				username : "Uzupełnij to pole",
				password : "Uzupełnij to pole",
				name : "Uzupełnij to pole",
				email : {
					required : "Uzupełnij to pole",
					email : "Niepoprawny e-mail"
				},
				reg : "Uzupełnij to pole",
				postcode: 'Niepoprawny kod pocztowy'
				
			}
		});
		
		
		$('#formularz1_form').validate({
			rules : {
				name : {
					required: true
				},
				postcode: {
					required: true,
					regex: /^[0-9][0-9]-[0-9][0-9][0-9]$/
				},
				work : {
					required: true
				},
				stand : {
					required: true
				},
				phone : {
					required: true
				},
				region: {
					required: true
				}
		},
		errorElement : "td",
// //wrapper : "td",
		errorPlacement: function(error, element) {
			// element.parent("td").nextAll("td:last").addClass("error");
			if (element.attr('name') == 'postcode') {
				error.attr('colspan','3');
			}
			error.insertAfter( element.parent("td").parent().children("td:last"));
			
		},
		/*
		 * success: function(label) {
		 * label.text("").parent("td").removeClass("error"); },
		 */
		messages: {
			name : "Uzupełnij to pole",
			postcode: {
				'regex' : 'Niepoprawny kod pocztowy',
				'required' : 'Uzupełnij to pole'
			},
			stand: 'Uzupełnij to pole',
			region: 'Uzupełnij to pole',
			work : 'Uzupełnij to pole',
			phone : 'Uzupełnij to pole'
				
		}
		});
		
		$('#user_edit_form').validate({
			rules : {
				email: {
					required: true,
					email : true
				},
				postcode: {
					regex: '^[0-9][0-9]-[0-9][0-9][0-9]$'
				}
			},
// errorElement : "tr",
// errorPlacement: function(error, element) {
//						
// var row = $(element).parent("td").parent("tr");
// var t = error.html();
// error.html('<td class="error" style="background-position: 50px 50%; width:
// 80px;"></td><td><span class="">'+t+'</span></td>');
// error.insertAfter(row);
//						
//						
// // error.insertAfter( element.parents("tr"));
//
// //element.parent("td").nextAll("td:last").addClass("error");
// //var e1 = $("<td>", {class:error});
// //var e2 = $('<td>');
// //$("#email_err_txt").append(error);
// },
			showErrors: function(errorMap, errorList) {
				$("#user_edit_form .error").remove();
				if (this.allow == false) {
					this.allow = true;
					this.form();
				} else {
					for (e in errorList) {
						var obj = errorList[e].element;
						var msg = errorList[e].message;
						var row = $(obj).parent("td").parent("tr");
						row.after('<tr class="error"><td class="error" style="background-position: 50px 50%; width: 80px;"></td><td><span class="error">'+msg+'</span></td></tr>');
					}
					this.allow = false;
				}
			},

			messages: {

				email : {
					required : "Uzupełnij to pole",
					email : "Niepoprawny e-mail"
				},
				postcode: "Niepoprawny kod pocztowy"
				
			}
		});
		
		$('#other_select').change(function() {
			load_other($(this).attr('selectedIndex'));
		});
		
});

function load_other(index) {
	// alert(index);
	if (index == 0) {
		return;
	}
	var d = other_json[index-1];
	$('#other_points').text(d['points']?d['points']:"0");
	// alert(d['points']);
	$('#other_place').text(d['place']?d['place']:"-");
	$('#other_regionplace').text(d['regionplace']?d['regionplace']:"-");
	$('#other_category').text(d['title']);
	if (d['link']) {
		// show first cert
		$('#other_cert').attr('href',d['link'].replace(/&amp;/gi,'&'));
		$('#other_cert').show();
	} else {
		$('#other_cert').hide();
	}
	if (d['regionlink']) {
		//alert(d['regionlink'].replace('&amp;','&'));
	 	$('#other_regioncert').attr('href',d['regionlink'].replace(/&amp;/gi,'&'));
		$('#other_regioncert').css('marign-top',d['link']?'5px':'65px').show();
	} else {
		$('#other_regioncert').hide();
	}
}


function search_f(itm,s)
{
// var src = document.getElementById("srce").value;
	var src = itm.val();
	 if (src==s){ 
// document.getElementById('srce').value = '';
		 itm.val('');
// document.getElementById('srce').focus();
		 itm.focus();
		 
		/* document.messages.submit(); */
     }     
}


// ankiety hack
$(function() {
	$('#test-wyniki p.errorq').append('<br /><span class="error errorlarger">Uzupełnij to pole</span>');
});


