var lang = new Object();
var zmieniajTopy = true;

/*function kalendarz(miesiac, rok) {
	$.ajax({url: $("base").attr("href")+"kalendarz/box.html",
        type: "POST",
        data: {
			miesiac: miesiac,
			rok: rok
		},
        error: function() {
			alert('Wystąpił błąd podczas przeładowania kalendarza');
		},
        success: function(data) {
			$("#calendarBox").replaceWith(data);
        }
    });
}*/
/* zmieniarka skrótów aktualności */
function newsChanges(szybkosc) {
	$("#content .aktualnosciBox .shortNews .news").stop(true, false).animate({opacity: 0}, szybkosc, function() { $(this).removeClass('active').css({display: 'none'}); });
	if($("#content .aktualnosciBox .shortNews .active").next('.news').length == 0) { $("#content .aktualnosciBox .shortNews .news:first").stop(true, false).css({display: 'block'}).animate({opacity: 1}, szybkosc).addClass('active');
	} else { $("#content .aktualnosciBox .shortNews .active").next('.news').stop(true, false).css({display: 'block'}).animate({opacity: 1}, szybkosc).addClass('active'); }

}
/* zmieniarka skrótów aktualności w pasku bocznym */
function newsChanges2(szybkosc) {
	$("#subContainer .aktualnosci .news").stop(true, false).animate({opacity: 0}, szybkosc, function() { $(this).removeClass('active').css({display: 'none'}); });
	if($("#subContainer .aktualnosci .active").next('.news').length == 0) { $("#subContainer .aktualnosci .news:first").stop(true, false).css({display: 'block'}).animate({opacity: 1}, szybkosc).addClass('active');
	} else { $("#subContainer .aktualnosci .active").next('.news').stop(true, false).css({display: 'block'}).animate({opacity: 1}, szybkosc).addClass('active'); }

}

/* zmieniarka topów */
function nextTop(szybkosc) {
	//$('.playMovie').fadeOut();
	$nextImgActive = $("#top .zdjecia .img.active").next().length == 0 ? $("#top .zdjecia .img:first") : $("#top .zdjecia .img.active").next();
	$("#top .zdjecia .img").each(function() {
		if($(this).css('opacity') != 0){
			$(this).stop(true, false).animate({opacity: 0}, szybkosc, function() { $(this).removeClass('active'); });
		}
	});
	$nextImgActive.stop(true, false).animate({opacity: 1}, szybkosc).addClass('active');
	
	$nextDotActive = $("#top .nawigacja .dots .dot.active").next().length == 0 ? $("#top .nawigacja .dots .dot:first") : $("#top .nawigacja .dots .dot.active").next();
	$("#top .nawigacja .dots .dot").stop(true, false).animate({opacity: 0.25}, szybkosc, function() { $(this).removeClass('active'); });
	$nextDotActive.stop(true, false).animate({opacity: 1}, szybkosc).addClass('active');
	
	var active_no = $nextImgActive.attr('id').replace('topImg','');
	
	$("#top .playMovie").stop(true, false).fadeOut();
	if( !$("#top .zdjecia .img#topImg"+active_no+" .yt").is(":visible") ) $("#playMovie"+active_no).stop(true, false).fadeIn();

	$("#top .ytcontrol").stop(true, false).fadeOut();
	if( $("#top .zdjecia .img#topImg"+active_no+" .yt").is(":visible") )$("#ytcontrol"+active_no).stop(true, false).fadeIn();
}
function prevTop(szybkosc) {
	//$('.playMovie').fadeOut();
	$prevImgActive = $("#top .zdjecia .img.active").prev().length == 0 ? $("#top .zdjecia .img:last") : $("#top .zdjecia .img.active").prev();
	$("#top .zdjecia .img").stop(true, false).animate({opacity: 0}, szybkosc, function() { $(this).removeClass('active'); });	
	$prevImgActive.stop(true, false).animate({opacity: 1}, szybkosc).addClass('active'); 
	
	$prevDotActive = $("#top .nawigacja .dots .dot.active").prev().length == 0 ? $("#top .nawigacja .dots .dot:last") : $("#top .nawigacja .dots .dot.active").prev();
	$("#top .nawigacja .dots .dot").stop(true, false).animate({opacity: 0.25}, szybkosc, function() { $(this).removeClass('active'); });
	$prevDotActive.stop(true, false).animate({opacity: 1}, szybkosc).addClass('active');
	
	var active_no = $prevImgActive.attr('id').replace('topImg','');
	
	$("#top .playMovie").stop(true, false).fadeOut();
	if( !$("#top .zdjecia .img#topImg"+active_no+" .yt").is(":visible") ) $("#playMovie"+active_no).stop(true, false).fadeIn();

	$("#top .ytcontrol").stop(true, false).fadeOut();
	if( $("#top .zdjecia .img#topImg"+active_no+" .yt").is(":visible") )$("#ytcontrol"+active_no).stop(true, false).fadeIn();
}
function showTop(szybkosc, top) {
	//$('.playMovie').fadeOut();
	$clickImgActive = $($("#top .zdjecia .img").get(top-1));
	$("#top .zdjecia .img").stop(true, false).animate({opacity: 0}, szybkosc, function() { $(this).removeClass('active'); });	
	$clickImgActive.stop(true, false).animate({opacity: 1}, szybkosc).addClass('active');

	$clickDotActive = $($("#top .nawigacja .dots .dot").get(top-1));
	$("#top .nawigacja .dots .dot").stop(true, false).animate({opacity: 0.25}, szybkosc, function() { $(this).removeClass('active'); });	
	$clickDotActive.stop(true, false).animate({opacity: 1}, szybkosc).addClass('active');
	
	var active_no = $clickImgActive.attr('id').replace('topImg','');
	
	$("#top .playMovie").stop(true, false).fadeOut();
	if( !$("#top .zdjecia .img#topImg"+active_no+" .yt").is(":visible") ) $("#playMovie"+active_no).stop(true, false).fadeIn();

	$("#top .ytcontrol").stop(true, false).fadeOut();
	if( $("#top .zdjecia .img#topImg"+active_no+" .yt").is(":visible") )$("#ytcontrol"+active_no).stop(true, false).fadeIn();
}
function sprawdzaczWysokosciowy() {
	var wysokoscTresci = $("#mainContainer").height();
	var wysokoscPanelu = $("#subContainer").height();
	if(wysokoscPanelu>wysokoscTresci) {
		$("#subContainer .aktualnosci").hide();
	}
}

function topInterval(){
	if($('.topy .img').length>1){
		topInterObj=setInterval( function(){
			$next = $('.topy .img.active').next().is('.img')
				? $('.topy .img.active').next()
				: $('.topy .img:first');
			$('.topy .img.active').stop(true,true).fadeOut(500, function(){
				$(this).removeClass('active')
				$next.stop(true,true).fadeIn(500, function(){
					$next.addClass('active')
				});
			});
		}, 7500 );
	}
}

$(document).ready(function() {
	
	topInterval();
	
	sprawdzaczWysokosciowy();
	$("a[rel^='prettyPhoto']").prettyPhoto({
		overlay_gallery: true,
		opacity: 0.90,
		social_tools: false		
	});
	
	$('#subContainer .menu a').click(function() {
		if($(this).hasClass('rodzic')){
			if($(this).parent().find('.p2').css('display')=='block') {
				$('#subContainer .menu .p2').slideUp();
			} else {
				$('#subContainer .menu .p2').slideUp();
				$(this).parent().find('.p2').slideDown();				
			}
			return false;
		}
	});
	
	$("#subContainer .menu li a").each(function() {
		if($(this).attr("href") == window.location.href) {
			var ul = $(this).parents("ul");
			if(ul.hasClass("p2")) {
				setTimeout(function() { ul.prev().trigger("click"); }, 350);
			}
		}
	});

	// pobieranie tlumaczen dla js
	$.ajax({
		async : false,
		url: $("base").attr("href")+$("head").attr("lang")+"/tools/js_translate.html",
		global: false,
		type: "POST",
		dataType: "json",
		success: function(data){
			for(i in data)
			{
				lang[i] = data[i];
			}
		}
	});
	
	/* zdjecia + promocje + oferty*/
	$("#content a").hover(function() {
		var klasa = $(this).find("img:last").attr('class');
		if(klasa!="ramka") {
			$(this).find("img:last").stop(true, false).animate({opacity: 0.6});
		}
	}, function() {
		$(this).find("img:last").stop(true, false).animate({opacity: 1});
	});
	
	/* sButton */
	$(".sButton").hover(function() {
		$(this).stop(true, false).animate({opacity: 0.6});
	}, function() {
		$(this).stop(true, false).animate({opacity: 1});
	});
	
	
	$(".kontaktFormOpen").prettyPhoto({
		custom_markup: '<div id="kontaktFormContainer" style="padding: 10px; font-size: 12px; line-height: 18px;"></div>',
		changepicturecallback: function() {
			$.ajax({
				url: $("base").attr("href")+$("head").attr("lang")+"/tools/kontakt.html",
				dataType: "json",
				success: function(data) {
					if(data != null && data.html) {
						$("#kontaktFormContainer").html(data.html);
						Cufon.replace("#kontaktFormContainer .headline, #kontaktFormContainer .button");
						resizePrettyPhoto("kontaktFormContainer");
					} else {
						$("#kontaktFormContainer")
							.css({'padding-top':'30px','text-align':'center'})
							.html(lang.formLoadError);
					}
				},
				error: function() {
					$("#kontaktFormContainer")
						.css({'padding-top':'30px','text-align':'center'})
						.html(lang.formLoadError);
					}
			});
		},
		social_tools: ''
	});
	
	// kotwice
	$("a").each(function() {
		var href = $(this).attr("href");
		if(href !== undefined && href.match(/^#.+$/gi)) {
			$(this).attr("href", window.location.href+$(this).attr("href"));
		}
	});

	/* rozwijanie menu */
	$('.menu ul.p1 li').hover(
		function () {
			$(this).find('ul.p2').stop(true, true).slideDown(250);
			$(this).addClass('active');
		},
		function () {
			$(this).find('ul.p2').delay(250).stop(true, true).fadeOut(100);
			$('.menu li').removeClass('active');
	});
	/* rozwijanie menu */
	$('.menu ul.p2 li').hover(
		function () {
			$(this).find('ul.p3').stop(true, true).show();
			$(this).addClass('active');
		},
		function () {
			$(this).find('ul.p3').delay(250).stop(true, true).hide();
			$('.menu li').removeClass('active');
	});
});

function resizePrettyPhoto(id) {	
	$(".pp_content").animate({'height':$("#"+id).outerHeight(true)});	
}

function kontaktFormSubmit() {

	$("#kontaktFormMain .ajaxError").empty().remove();
	$.ajax({
		url: $("base").attr("href")+$("head").attr("lang")+"/tools/kontakt.html",
		type: "POST",
		data: $("#kontaktForm").serialize(),
		dataType: "json",
		success: function(data) {
			if(typeof(data.status) !== "undefined") {
				if(data.status == 1) {
					$("#kontaktFormMain").html(data.msg);
					Cufon.replace("#kontaktFormContainer .button");
				} else {
					$("#kontaktFormMain").append('<div class="ajaxError" style="margin-top: 15px;">'+data.msg+'</div>')
				}
			} else {
				$("#kontaktFormMain").html(data.html);
				Cufon.replace("#kontaktFormContainer .button");
			}
			resizePrettyPhoto("kontaktFormContainer");
		},
		error: function() {
			//hide_info();
			//komunikat(hotstore_lang.formSendError, hotstore_lang.formSendErrorTxt, 'javascript:hide_info();', false, false);
		}
	});
	
}

function kontaktFormReset() {
	$("#kontaktForm input, #kontaktForm textarea").val("");
}
