var loader = new Image();
loader.src = '/_img/ajax-loader.gif';

$(document).ready(function(){
	
	$('.secondary_link').click(function() {		 
		id = $(this).attr('rel');
		a = $('.subcategory_' + id);		
		
		if ($(a).size() == 0) {
			return true;
		} else {
			if ($(a).hasClass('hide') == true) {
				for (i=1; i<=4; i++) {
					if ($(this).hasClass('level_' + i) == true) {
						for (j=i+1; j<=4; j++) {
							$('a.level_' + j).addClass('hide');
						}
					}
				}							
				$(a).removeClass('hide');
			} else {
				for (i=1; i<=4; i++) {
					if ($(this).hasClass('level_' + i) == true) {
						for (j=i+1; j<=4; j++) {
							$('a.level_' + j).addClass('hide');
						}
					}
				}
			}
			/* problem bo to nie divy i trzeba by rodzicow tez chowac
			else {
				$(a).addClass('hide');
			}
			*/
			return false;
		}		
	});

	$('input.clear_on_click').focus(function() {		 
		$(this).val('');
	});
	
	$('img.thumb').click(function() {		 
		id = $(this).attr('rel');
		id = Number(id);
		current_image = id;
		id = image_array[id];		
		$('.description').hide();
		$('#description_' + id).show();
		$('#gallery_main').hide();		
		var img = new Image();
        $(img).load(function () {
        	$('#gallery_main_image .editable').html('');
            $('#gallery_main_image .editable').append(this);
            temp = $('#gallery_main_image .editable').html();
            temp = '<a href="/d_image/' + id + '/w/800/">'+temp+'</a>';
            $('#gallery_main_image .editable').html(temp);
            $('#gallery_main_image .editable img').css('border', '0');
            $('#gallery_main_image a').lightBox();                        
            $('#gallery_main').fadeIn();
        }).error(function () {
           // error
        }).attr('src', '/d_image/' + id + '/w/583/h/437/');
        
	});
	
	$('#btn_newsletter').click(function() {		
		if (is_valid_mail($('#newsletter_address').val())) {				 
			$('#newsletter').animate(
				{
			    	opacity: 0.25
			    },
			    500,
			    function() {
			    	$.ajax({
						async: false,
						url: "/ajax.php",
						data: "action=add_mail&mail=" + $('#newsletter_address').val(),		
						cache: false,
						success: function(html){
							if (html != 1) {
								alert(html);								
							} else {
								alert(TXT_MAIL_ADD);									
							}	
							$('#newsletter_address').val(TXT_MAIL)						
							$('#newsletter').animate(
								{
							    	opacity: 1
							    },
							    500
							);
						}
					});
			    }
			);
		} else {
			alert(TXT_WRONG_MAIL_ALERT);
		}
	});
	
	$('a.accept_user').click(function() {
		id = $(this).attr('rel');		 
		if (ask_user("Czy na pewno chcesz zaakceptować wybranego użytkownika?")) {
			$('#option_user_' + id).html(loader);
			$.ajax({
				async: false,
				url: "/ajax.php",
				data: "action=accept_user&id=" + id,		
				cache: false,
				success: function(html){
					$('#td_user_' + id).fadeOut('medium');
				}
			});			
		}        
	});
	$('a.deny_user').click(function() {
		id = $(this).attr('rel');		 
		if (ask_user("Czy na pewno chcesz odrzucić wybranego użytkownika?")) {
			$('#option_user_' + id).html(loader);
			$.ajax({
				async: false,
				url: "/ajax.php",
				data: "action=deny_user&id=" + id,		
				cache: false,
				success: function(html){
					$('#td_user_' + id).fadeOut('medium');
				}
			});			
		}        
	});
	
	
	$('a.accept_news').click(function() {
		id = $(this).attr('rel');		 
		if (ask_user("Czy na pewno chcesz zaakceptować wybranego newsa?")) {
			$('#option_news_' + id).html(loader);
			$.ajax({
				async: false,
				url: "/ajax.php",
				data: "action=accept_news&id=" + id,		
				cache: false,
				success: function(html){
					$('#td_news_' + id).fadeOut('medium');
				}
			});			
		}        
	});
	$('a.deny_news').click(function() {
		id = $(this).attr('rel');		 
		if (ask_user("Czy na pewno chcesz odrzucić wybranego newsa?")) {
			$('#option_news_' + id).html(loader);
			$.ajax({
				async: false,
				url: "/ajax.php",
				data: "action=deny_news&id=" + id,		
				cache: false,
				success: function(html){
					$('#td_news_' + id).fadeOut('medium');
				}
			});			
		}        
	});
	
	$('a.accept_card').click(function() {
		id = $(this).attr('rel');		 
		if (ask_user("Czy na pewno chcesz zaakceptować wybraną wizytówka i ustawić ją jako domyślną?")) {
			$('#option_card_' + id).html(loader);
			$.ajax({
				async: false,
				url: "/ajax.php",
				data: "action=accept_card&id=" + id,		
				cache: false,
				success: function(html){
					$('#td_card_' + id).fadeOut('medium');
				}
			});			
		}        
	});
	$('a.deny_card').click(function() {
		id = $(this).attr('rel');		 
		if (ask_user("Czy na pewno chcesz odrzucić wybraną wizytówkę?")) {
			$('#option_card_' + id).html(loader);
			$.ajax({
				async: false,
				url: "/ajax.php",
				data: "action=deny_card&id=" + id,		
				cache: false,
				success: function(html){
					$('#td_card_' + id).fadeOut('medium');
				}
			});			
		}        
	});
	
	$('a.accept_offer').click(function() {
		id = $(this).attr('rel');		 
		if (ask_user("Czy na pewno chcesz zaakceptować wybraną ofertę?")) {
			$('#option_offer_' + id).html(loader);
			$.ajax({
				async: false,
				url: "/ajax.php",
				data: "action=accept_offer&id=" + id,		
				cache: false,
				success: function(html){
					$('#td_offer_' + id).fadeOut('medium');
				}
			});			
		}        
	});
	$('a.deny_offer').click(function() {
		id = $(this).attr('rel');		 
		if (ask_user("Czy na pewno chcesz odrzucić wybraną ofertę?")) {
			$('#option_card_' + id).html(loader);
			$.ajax({
				async: false,
				url: "/ajax.php",
				data: "action=deny_offer&id=" + id,		
				cache: false,
				success: function(html){					
					$('#td_offer_' + id).fadeOut('medium');
				}
			});			
		}        
	});
	
	$("img.prev_image").live("click", function() {
		
		if (current_image > 0) {
			current_image = current_image - 1;			
		} else {
			current_image = image_array.length - 2;			
		}
		
		id = image_array[current_image];
		$('.description').hide();
		$('#description_' + id).show();
		$('#gallery_main').hide();		
		var img = new Image();
        $(img).load(function () {
        	$('#gallery_main_image .editable').html('');
            $('#gallery_main_image .editable').append(this);
            temp = $('#gallery_main_image .editable').html();
            temp = '<a href="/d_image/' + id + '/w/800/">'+temp+'</a>';
            $('#gallery_main_image .editable').html(temp);
            $('#gallery_main_image .editable img').css('border', '0');
            $('#gallery_main_image a').lightBox();                        
            $('#gallery_main').fadeIn();
        }).error(function () {
           // error
        }).attr('src', '/d_image/' + id + '/w/583/h/437/');				
	});
	$("img.next_image").live("click", function() {
		if (current_image < image_array.length - 2) {
			current_image = current_image + 1;			
		} else {
			current_image = 0;			
		}
		
		id = image_array[current_image];
		$('.description').hide();
		$('#description_' + id).show();
		$('#gallery_main').hide();		
		var img = new Image();
        $(img).load(function () {
        	$('#gallery_main_image .editable').html('');
            $('#gallery_main_image .editable').append(this);
            temp = $('#gallery_main_image .editable').html();
            temp = '<a href="/d_image/' + id + '/w/800/">'+temp+'</a>';
            $('#gallery_main_image .editable').html(temp);
            $('#gallery_main_image .editable img').css('border', '0');
            $('#gallery_main_image a').lightBox();                        
            $('#gallery_main').fadeIn();
        }).error(function () {
           // error
        }).attr('src', '/d_image/' + id + '/w/583/h/437/');
	});
	
	$('.sort_1, .sort_2, .sort_3, .sort_4').change(function() {
		var url = $('input#url').val();
		var s_1 =  $('.sort_1').val();
		var s_2 =  $('.sort_2').val();
		var s_3 =  $('.sort_3').val();
		var s_4 =  $('.sort_4').val();
		if (!s_4) {
			s_4 = 0;
		}
		var ss = s_1 + '_' + s_2 + '_' + s_3 + '_' + s_4
		document.location.href = url + 'sort/1/' + ss + '/';		
	});
	
	$('#btn_remove_newsletter').click(function() {
		my_mail = $('input#mail').val();
		if (my_mail.length == 0) {
			alert('Proszę podać swój adres e-mail.');
		} else {
			$('input#mail').val('Proszę czekać');
			$.ajax({
				async: false,
				url: "/ajax.php",
				data: "action=remove_newsletter&mail=" + my_mail,		
				cache: false,
				success: function(html){
					$('input#mail').val('');
					alert('Twój adres został usunięty.');
				}
			});	
		}		
	});
	
});

function ask_user(question) {
    var windowResult = confirm(question);

    if (windowResult == true) {
        return true;
    } else {
        return false;
    }
}


function check_password_recovery_form() {	
	mail = $('#mail').val();
	
	msg = "";
	
	
	if (mail.length == 0) {
		msg += "Proszę podać adres e-mail\n";
	}
	
	
	if (msg.length != 0) {
		alert(msg);
		return false;
	} else {
		return true;
	}
}

function check_password_change() {
	password = $('#new_1').val();
	re_password = $('#new_2').val();
	
	msg = "";	
	
	if (password.length == 0 || re_password.length == 0) {
		msg += "Proszę podać hasło i powtórzyć w celu weryfikacji\n";
	}
	if (password != re_password) {
		msg += "Podane hasła nie są takie same\n";
	}
	
	if (msg.length != 0) {
		alert(msg);
		return false;
	} else {
		return true;
	}
}

function check_password_recovery_2_form() {	
	password = $('#password').val();
	re_password = $('#re_password').val();
	
	msg = "";	
	
	if (password.length == 0 || re_password.length == 0) {
		msg += "Proszę podać hasło i powtórzyć w celu weryfikacji\n";
	}
	if (password != re_password) {
		msg += "Podane hasła nie są takie same\n";
	}
	
	if (msg.length != 0) {
		alert(msg);
		return false;
	} else {
		return true;
	}
	
}


function check_add_terrain_form() {
	description = $('#description').val();
	terrain_area = $('#terrain_area').val();
	terrain_price = $('#terrain_price').val();	
	
	msg = "";
	
	if (description.length == 0) {
		msg += "Proszę podać opis\n";
	}
	if (terrain_area == 0) {
		msg += "Proszę podać powierzchnię\n";
	}
	if (terrain_price == 0) {
		msg += "Proszę podać cenę\n";
	}
	
	
	if (msg.length != 0) {
		alert(msg);
		return false;
	} else {
		return true;
	}
}

function check_add_firm_form() {
	firm_name = $('#firm_name').val();
	firm_type = $('#firm_type').val();
	firm_area = $('#firm_area').val();
	firm_add_person = $('#firm_add_person').val();
	firm_add_mail = $('#firm_add_mail').val();
	
	msg = "";
	
	if (firm_name.length == 0) {
		msg += "Proszę podać nazwę firmy\n";
	}
	if (firm_type == 0) {
		msg += "Proszę podać branżę\n";
	}
	if (firm_area == 0) {
		msg += "Proszę podać obszar\n";
	}
	if (firm_add_person.length == 0) {
		msg += "Proszę podać osobę kontaktową\n";
	}
	if (firm_add_mail.length == 0) {
		msg += "Proszę podać kontaktowy adres e-mail\n";
	}
	
	if (msg.length != 0) {
		alert(msg);
		return false;
	} else {
		return true;
	}
}

function check_add_card_form() {
	firm_description = $('#firm_description').val();
	firm_offer = $('#firm_offer').val();	
	firm_type = $('#firm_type').val();
	firm_area = $('#firm_area').val();
	shortcut = $('#shortcut').val();
	
	
	msg = "";
	
	if (firm_description.length == 0) {
		msg += "Proszę podać zakres działalności\n";
	}
	if (shortcut.length == 0) {
		msg += "Proszę podać skrócony zakres działalności (wstęp))\n";
	}
	if (firm_type == 0) {
		msg += "Proszę podać branżę\n";
	}
	if (firm_area == 0) {
		msg += "Proszę podać obszar\n";
	}
	if (firm_offer.length == 0) {
		msg += "Proszę podać ofertę\n";
	}
	
	
	if (msg.length != 0) {
		alert(msg);
		return false;
	} else {
		return true;
	}
}

function check_register_form() {
	name = $('#name').val();
	surname = $('#surname').val();	
	login = $('#login').val();
	mail = $('#mail').val();
	password = $('#password').val();
	re_password = $('#re_password').val();
	
	msg = "";
	
	if (name.length == 0) {
		msg += "Proszę podać imię\n";
	}	
	if (surname.length == 0) {
		msg += "Proszę podać nazwisko\n";
	}
	if (login.length == 0) {
		msg += "Proszę podać login\n";
	}
	if (mail.length == 0) {
		msg += "Proszę podać adres e-mail\n";
	}
	if (password.length == 0 || re_password.length == 0) {
		msg += "Proszę podać hasło i powtórzyć w celu weryfikacji\n";
	}
	if (password != re_password) {
		msg += "Podane hasła nie są takie same\n";
	}
	if (!$('#regulation').attr('checked')) {
		msg += "Proszę zaakceptować regulamin\n";
	}
	
	if (msg.length != 0) {
		alert(msg);
		return false;
	} else {
		return true;
	}
}

function check_edit_data_form() {
	name = $('#name').val();
	surname = $('#surname').val();	
	login = $('#login').val();
	mail = $('#mail').val();
	password = $('#password').val();
	re_password = $('#re_password').val();
	
	msg = "";
	
	if (name.length == 0) {
		msg += "Proszę podać imię\n";
	}	
	if (surname.length == 0) {
		msg += "Proszę podać nazwisko\n";
	}
	if (login.length == 0) {
		msg += "Proszę podać login\n";
	}
	if (mail.length == 0) {
		msg += "Proszę podać adres e-mail\n";
	}
	if (password.length == 0 || re_password.length == 0) {
		msg += "Proszę podać hasło i powtórzyć w celu weryfikacji\n";
	}
	if (password != re_password) {
		msg += "Podane hasła nie są takie same\n";
	}	
	
	if (msg.length != 0) {
		alert(msg);
		return false;
	} else {
		return true;
	}
	
	
}

function check_add_news_form() {
	name = $('#name').val();
	shortcut = $('#shortcut').val();
	description = $('#description').val();
	
	msg = "";
	
	if (name.length == 0) {
		msg += "Proszę podać temat artykułu\n";
	}
	if (shortcut.length == 0) {
		msg += "Proszę podać wstęp do artykułu\n";
	}
	if (description.length == 0) {
		msg += "Proszę podać treść artykułu\n";
	}
	
	if (msg.length != 0) {
		alert(msg);
		return false;
	} else {
		return true;
	}
}	

function is_valid_mail(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);
}

function limitText(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	}
}

function check_firm_form() {
	v_name = $('#name').val();
	v_mail = $('#mail').val();
	v_phone = $('#phone').val();
	v_message = $('#message').val();
	
	if (v_name.length == 0 || v_mail.length == 0 || v_phone.length == 0 || v_message == 0) {
		alert('Proszę uzupełnić wszystkie pola');
		return false;				
	} else {
		return true;
	}
}

