$(document).ready(function(){
	var sliderConfig = {
		loadImage: "/m/lib/po/slider/1.0/load-yellow.gif",
		color: "#222",
		closeColor: "#000",
		leftArrowImage: "/m/lib/po/slider/1.0/arrow-left-yellow-blue.png",
		rightArrowImage: "/m/lib/po/slider/1.0/arrow-right-yellow-blue.png"		 
	};
	
	var currentBox =0;
	var boxes = $(".attachmentsBox .attachmentBox");
	
	$(".nextAttachment").click(function(e) {
		e.preventDefault();
		boxes.eq(currentBox).hide();
		currentBox = currentBox +1;
		boxes.eq(currentBox).show();
	});
	
	$(".prevAttachment").click(function(e) {
		e.preventDefault();
		boxes.eq(currentBox).hide();
		currentBox = Math.max(0,currentBox - 1);		
		boxes.eq(currentBox).show();
	});
	
	boxes.find("td, .attachmentTitle").hover(
		function(){
			boxes.eq(currentBox).find(".zoom").show();
		},
		function(){
			boxes.eq(currentBox).find(".zoom").hide();
		}
	);
	
	$(".yesThumbs .falseBlock").find("td, a.title").hover(
		function(){
			$(this).parents(".file").find(".zoom").show();
		},
		function(){
			$(this).parents(".file").find(".zoom").hide();
		}
	);
	
	var mySlider = new Slider($(".attachmentsBox .attachmentBox"),$(".attachmentBox td, .attachmentBox td a, .attachmentTitle"),sliderConfig);
	var mySecondSlider = new Slider($(".yesThumbs .falseBlock:not(.mp4File)"),$(".falseBlock td a, .zoom, .title"),sliderConfig);
	
	$(".categoryHub").click(function(e){
		e.preventDefault();
		var me = $(this);
		me.parent("li").siblings().find(".categories:not(.selectedDescendant)").hide();
		me.siblings(".categories").toggle();
	});
	
	var msg = new Message(sliderConfig);
	var msgConfig;	
	
	$(".yesThumbs .falseBlock:not(.mp4File) .download a").click(function(e){
		e.preventDefault();
		msgConfig = {
			html: "Uwaga! Prawa autorskie do nadesłanych zdjęć należą do firmy Xella Polska Sp. z o.o. i nie są ograniczone prawami osób trzecich. Xella Polska zezwala na publikację zdjęcia pod warunkiem podpisania go: <strong>SILKA, YTONG</strong>.",
			badButton: "Anuluj",
			goodButton: "Pobierz plik",
			goodUrl: $(this).attr("href")
		};
		msg.message(msgConfig);
	});
	
	$(".trueBlock a, .trueBlock.noThumb").click(function(e){
		e.preventDefault();
		msgConfig = {
			html: "<strong>Uwaga!</strong><br/>Ten plik został zablokowany. Aby go pobrać musisz być zarejestrowany w Biurze Prasowym.",
			badButton: "Anuluj",
			goodButton: "Zrejestruj się",
			goodUrl: "/pl/register"
		};
		msg.message(msgConfig);
	});
	
	/* LOGOWANIE */
	/* zapamiętaj mnie */
	var me, log;
	function checkRemember(){
		log = $("#autoLogin").prop("checked");
		$("#autoLogVal").prop("value",log);
	}
	
	$("#autoLogin").click(checkRemember);	
	checkRemember();
	
	/* teksty w inputach */
	$('.passFake').show();
	$('.passTrue').hide();
 
	$('.passFake').focus(function() {
		$('.passFake').hide();
		$('.passTrue').show().focus();
	});
	$('.passTrue').blur(function() {
		if($('.passTrue').val() === '') {
			$('.passFake').show();
			$('.passTrue').hide();
		}
	});  
 
	$('.defaultVals').each(function() {
		var default_value = this.value;
		$(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value === '') {
				this.value = default_value;
			}
		});
	});
	
	var that;
	$(".PressRelease .release p a").each(function(){
		that = $(this);
		if (that.attr("href").indexOf("media.xella.pl")===-1) {
			that.attr("target","_blank");
		}
	});
	
});

function runValidation() {
	$(".log-no #PressRegisterForm").attr("action","/jRegister.PressRegister.1198.po");
	
	jQuery.validator.addMethod("phone", function(phone_number, element) {
		phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 6 && phone_number.match(/^[0-9|+|(|)| ]*?$/);
	}, "Podaj numer telefonu w formacie +48xxxxxxxxx");

	jQuery.validator.addClassRules({
		requiredField: {
			required: true
		},
		mailField: {
			email: true
		},
		passField: {
			minlength: 5
		},
		matchField: {
			equalTo: "#passwordInput"
		},
		phoneField: {
			phone: true
		}
	});
	
	function checkAgree() {
		var agree = $(".agree input").prop("checked");
		if (agree===false) {
			$(".agree").addClass("badAgree");
		} else {
			$(".agree").removeClass("badAgree");
		}
	}
	
	$(".agree input").click(checkAgree);
	
	$("#PressRegisterForm").validate({
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1 ? '<strong>Liczba błędów: '+errors+'.</strong> <br/>Zaznaczone pole zostało niepoprawnie wypełnione<br/><br/>' : '<strong>Liczba błędów: '+errors+'.</strong> <br/>Zaznaczone pola zostały niepoprawnie wypełnione<br/><br/>';
				$("#summary").html(message);
				$("#summary").show();
				$(".error").addClass("badChild");
				checkAgree();
			} else {
				$("#summary").hide();
			}
		},
		errorPlacement: function(error, element) {
			error.appendTo("#summary");
		},
		messages: {
			first_name: "Podaj swoje imię",
			surname: "Podaj swoje nazwisko",
			mail: "Podaj poprawny email",
			password: "Podaj hasło (min. 5 znaków)",
			password2: "Potwierdź hasło",
			agree: "Wyrażenie zgody jest wymagane do rejestracji"
		}
		
	});
}

