// Top slider

$(document).ready(function() {

	$(".paging").show();
	$(".paging a:first").addClass("active");

	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;

	$(".image_reel").css({'width' : imageReelWidth});

	rotate = function(){
		var triggerID = $active.attr("rel") - 1;
		var image_reelPosition = triggerID * imageWidth;

		$(".paging a").removeClass('active');
		$active.addClass('active');


		$(".image_reel").animate({
			left: -image_reelPosition
		}, 500 );
	};

	rotateSwitch = function(){
		play = setInterval(function(){
			$active = $('.paging a.active').next();
			if ( $active.length === 0) {
				$active = $('.paging a:first');
			}
			rotate();
		}, 5000);
	};

	rotateSwitch();

	$(".image_reel a").hover(function() {
		clearInterval(play);
	}, function() {
		rotateSwitch();
	});

	$(".paging a").click(function() {
		$active = $(this);
		clearInterval(play);
		rotate();
		rotateSwitch();
		return false;
	});
});

	// Kolejno�c stron

function priorityIncrease(id){
    $.ajax({
            type: "POST",
            url: 'priority',
            data: "increase=true&id=" +id,
            success: function(){
                window.location.reload();
            }
        });
}

function priorityDecrease(id){
    $.ajax({
            type: "POST",
            url: 'priority',
            data: "increase=false&id=" +id,
            success: function(){
                window.location.reload();
            }
        });
}

function moveDown(urlU){
var str = String(urlU);
    str =  str.substring(str.indexOf("#")+1);

    $.ajax({
            type: "POST",
            url: str,
            success: function(){$("#page-grid").load(location.href+" #page-grid>*","");}
        });

    return false;
}

jQuery(document).ready(function() {
    $('.banner').children().each(
        function(){
            //access to form element via $(this)
            $(this).click(function() {
                $.ajax({
                    type: "POST",
                    url: 'banner/increment',
                    data: "id=" + this.id

                });
            });
        }
        );
    $('.banner').click(function() {
        $.ajax({
            type: "POST",
            url: 'banner/increment',
            data: "id=" + this.id

        });
    });

	// news
	newsChanger();

	// events
	eventsPage();
});

function validateNewsletter(){
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var hasError = false;
    var email = $("#Newsletter_email").val();
    if(!emailReg.test(email) || email.length < 7){
        $("#newsletterMsg").html("<p class=\"error\">Niepoprawny email!</p>");
        return false;
    }
    return true;
}

function newsChanger() {
	var $news = $('#content123 .news .new');
	if ($news.length > 1) { // news should be changed only when there are more than one news
		var $buttons = $('<div class="news-buttons"></div>'),
			i = 1,
			$new_to_display = $news.first().clone().prependTo($news.parent()).addClass('new-to-display');
		$news.each(
			function() {
				$buttons.append('<div class="news-button" id="newsbutton' + i + '">' + i + '</div>');
				i++;
			}
		);
		$('.foto', $new_to_display).wrap('<div class="foto-and-buttons-container"></div>').after($buttons);

		$('#content .news .news-button').click(
			function() {
				var $button = $(this),
					num = parseInt($(this).text()) - 1;
				if (!$button.hasClass('active')) {
					$('.foto img, .date, .title, .desc', $new_to_display).fadeOut(300,
						function() {
							$('.news-button', $buttons).removeClass('active').eq(num).addClass('active');
							$('.foto img', $new_to_display).replaceWith($('.foto img', $news.eq(num)).clone());
							$('.date', $new_to_display).replaceWith($('.date', $news.eq(num)).clone());
							$('.title', $new_to_display).replaceWith($('.title', $news.eq(num)).clone());
							$('.desc', $new_to_display).replaceWith($('.desc', $news.eq(num)).clone());
							$('.foto img, .date, .title, .desc', $new_to_display).fadeIn(300);
						}
					);
				}
			}
		).first().click();

	}
}

function eventsPage() {
	var $events = $('.events');
	if ($events.length > 0) {
		var $calendar = $('.calendar', $events),
			$calendar_inner = $('<div class="calendar-inner"></div>'),
			$curr_month = $('.header .current-month', $events),
			$curr_year = $('.header .current-year', $events),
			$first_table = $('table:first', $calendar),
			table_width = $first_table.outerWidth(),
			table_left_margin = parseInt($first_table.css('margin-left').replace('px', '')),
			table_right_margin = parseInt($first_table.css('margin-right').replace('px', '')),
			table_full_width = table_width + table_left_margin + table_right_margin;

		$events = $($events.get(0)).css('position', 'relative');

		function calendarBlock(block) {
			if (block) {
				var $blocker = $('<div id="calendar-blocker" style="position:absolute; top:0; left:0; width:100%; height:100%; background:#000;"></div>').css('opacity', 0);
				$events.append($blocker);
			} else {
				$('#calendar-blocker', $events).remove();
			}
		}

		$calendar.wrapInner($calendar_inner.css({
												'width': 1 * table_full_width,
												'margin-left': -table_full_width,
												'margin-right': -table_full_width
												})
						   );
		$calendar_inner = $('.calendar-inner', $calendar);
		$first_table.css('margin-left', table_full_width + table_left_margin);

		$('.header .prev-month', $events).click(
			function() {
				var $clicked = $(this);
				if ($clicked.hasClass('blocked')) return false;
				$clicked.addClass('blocked');
				calendarBlock(true);
				var $first = $('table:first', $calendar);
				$.ajax({
					url: $(this).attr('href'),
					data: {year: $first.data('prevyear'), month: $first.data('prevmonth')},
					success: function(data) {
						$('table:first', $calendar).css('margin-left', table_left_margin);
						$calendar_inner.prepend(data);
						$('table:first', $calendar).animate(
							{'margin-left': table_full_width + table_left_margin},
							500,
							function() {
								$('table:last', $calendar).remove();
								$curr_month.text($('table:eq(1) caption', $calendar).text());
								$curr_year.text($('table:eq(1)', $calendar).data('year'));
								$clicked.removeClass('blocked');
								calendarBlock(false);
							}
						);
					}
				});
				return false;
			}
		);

		$('.header .next-month', $events).click(
			function() {
				var $clicked = $(this);
				if ($clicked.hasClass('blocked')) return false;
				$clicked.addClass('blocked');
				calendarBlock(true);
				var $last = $('table:last', $calendar);
				$.ajax({
					url: $(this).attr('href'),
					data: {year: $last.data('nextyear'), month: $last.data('nextmonth')},
					success: function(data) {
						$calendar_inner.append(data);
						$('table:first', $calendar).animate(
							{'margin-left': table_left_margin},
							500,
							function() {
								$(this).next().css('margin-left', table_full_width + table_left_margin);
								$(this).remove();
								$curr_month.text($('table:eq(1) caption', $calendar).text());
								$curr_year.text($('table:eq(1)', $calendar).data('year'));
								$clicked.removeClass('blocked');
								calendarBlock(false);
							}
						);
					}
				});
				return false;
			}
		);

		$('table td a', $calendar).live('click',
			function() {
				$.ajax({
					url: $(this).attr('href'),
					success: function(data) {
						var $list = $('.events-list', $events),
							$newlist = $(data);
						$newlist.css('display', 'none');
						if ($list.length > 0) {
							$list.fadeOut(300,
								function() {
									$list.replaceWith($newlist);
									$newlist.fadeIn(300);
									//$('.right #event-info', $events).remove();
								}
							);
						} else {
							$calendar.after($newlist);
							$newlist.fadeIn(300);
							//$('.right #event-info', $events).remove();
						}
					}
				});
				return false;
			}
		);

		$('.events-list a', $events).live('click',
			function() {
				$.ajax({
					url: $(this).attr('href'),
					success: function(data) {
						var $info = $('.right #event-info', $events), $newinfo = $(data);
						$newinfo.css('display', 'none');
						if ($info.length > 0) {
							$info.fadeOut(300,function(){
								$info.replaceWith($newinfo);
								$newinfo.fadeIn(300);
							});
						} else {
							$('.events-list', $events).after($newinfo);
							$newinfo.fadeIn(300);
						}
					}
				});
				return false;
			}
		);

		$('.event-promoted-link').click(function() {
			var event = $(this).attr('rel');
			if (event.length) {
				$('#' + event).click();
			}
		});

		$('.goto', $events).live('click',
			function() {
				$.ajax({
					url: $(this).attr('href'),
					success: function(data) {
						var $info = $('#koncerty .right #event-info', $events), $newinfo = $(data);
						$newinfo.css('display', 'none');
						if ($info.length > 0) {
							$info.fadeOut(300,function(){
								$info.replaceWith($newinfo);
								$newinfo.fadeIn(300);
							});
						} else {
							$('#koncerty .events-list', $events).after($newinfo);
							$newinfo.fadeIn(300);
						}
					}
				});
				return false;
			}
		);

        $('#koncerty .right #event-info .img a', $events).live('click',
			function() {
				$.ajax({
					url: $(this).attr('href'),
					success: function(data) {
						var $info = $('.right #event-info', $events), $newinfo = $(data);
						$newinfo.css('display', 'none');
						if ($info.length > 0) {
							$info.fadeOut(300,function(){
								$info.replaceWith($newinfo);
								$newinfo.fadeIn(300);
							});
						} else {
							$('.events-list', $events).after($newinfo);
							$newinfo.fadeIn(300);
						}
					}
				});
				return false;
			}
		);

	}
}

/**
 * laduj stronę urlU do kontenera update.
 */
function loadPage(urlU, update){
	var str = String(urlU);
    $.ajax({
            type: 'GET',
            url: str,
            success: function(html){
				$(update).html(html);
			}
        });

    return false;
}

