$(document).ready(function() {
	
	// Wstawia losowy top na strone
	
	var tops = new Array(
		'bully',
		'gta',
		'manhunt',
		'mc',
		'warriors',
		'rdr'
	);
	
	var rand = Math.round(Math.random() * 5);
	
	switch (rand) {
		case 4: var randAdd = '<img class="randomTopLeft" style="left: -55px;" src="/layout/img/random_top_warriors_left.png" />'; break;
		default: var randAdd = '';
	}
	
	$('div#top > div.randomTop').append('<img src="/layout/img/random_top_' + tops[rand] + '.png" />' + randAdd);
	$('div#top > div.randomTop > img').fadeIn();
	
	// Usuwa ostatni separator z glownej czesci strony
	
	var comments = $('div#main > div.comment').length;
	var commentForm = $('div#commentForm').length;
	
	if (comments == 0 && commentForm == 0) {
		$('div#main > div.box > img:last').remove();
	} else if (comments == 0 && commentForm == 1) {
		$('div#commentForm').css('paddingTop', '5px');
	}
	
	// Usuwa dolny margines spod ostatniego komentarza
	
	$('div#main > div.comment:last').css('marginBottom', 0);
	
	// Przygotowuje, dodaje i ukrywa tlo underline do pierwszego znaku #family li
	
	$('ul#family > li').each(function() {
		var member = $(this).find('a > img').attr('alt').replace(/ /g, '').toLowerCase();
		$(this).css('background', 'transparent url(/layout/img/rockstar_family_footer_underline_' + member + '.png) no-repeat 5px -1px');
	});
	
	// Odslania tlo underline do pierwszego znaku #family li po najechaniu kursorem myszy
	
	$('ul#family > li').hover(function() {
		var member = $(this).find('a > img').attr('alt').replace(/ /g, '').toLowerCase();
		$(this).css('backgroundPosition', '5px 8px');
	}, function() {
		$(this).css('backgroundPosition', '5px -1px');
	});
	
	// Odslanianie i ukrywanie nawigacji formularza komentarza
	
	$('div#commentForm > form > div > textarea').focus(function() {
		$('div#commentForm > form > div.controls').fadeIn();
	});
	
	$('div#commentForm > form > div > textarea').blur(function() {
		$('div#commentForm > form > div.controls').fadeOut();
	});
	
	// Licznik znakow pozostalych do napisania komentarza
	
	var commentMaxLimit = $('div#commentForm > form > div.controls > input[name="charsLeft"]').val();
	
	$('div#commentForm > form > div > textarea').keyup(function() {
		var current = jQuery.trim($(this).val().replace(/\r\n/g, '\n')).length;
		var counter = commentMaxLimit - current;
		var color = (counter > 10) ? '#8cbd7a' : ((counter > 0) ? '#f3b52f' : '#dc7671');
		$('div#commentForm > form > div.controls > input[name="charsLeft"]').val(Math.abs(counter)).css('color', color);
		
		if (counter >= 0) {
			$('div#commentWarning').fadeOut();
		} else {
			$('div#commentWarning').fadeIn();
		}
	});
	
	// Automatyczne odswiezanie licznika
	
	$('div#commentForm > form > div > textarea').focus(function() {
		window.setInterval("$('div#commentForm > form > div > textarea').keyup();", 1000);
	});
	
	// Sprawdzanie pustych pol w formularzach
	
	$('form').submit(function() {
		var empty = 0;
		
		$(this).find(':text')
			.add($(this).find(':password'))
			.add($(this).find('textarea'))
		.each(function() {
			
			if (jQuery.trim($(this).val()) == '') {
				++empty;
			}
		});
		
		if (empty > 0) {
			alert('Należy wypełnić wszystkie pola.');
			return false;
		}
	});
	
	// Formularz logowania
	
	function getClientAreaSize()
	{
		// Zwraca szerokosc i wysokosc obszaru roboczego klienta
		
		if (window.ActiveXObject && !window.XMLHttpRequest) { // <= IE6
			return new Array(document.body.clientWidth + 10, document.body.clientHeight + 10);
		} else if (window.ActiveXObject && window.XMLHttpRequest) { // IE7
			return new Array(document.documentElement.clientWidth, document.documentElement.clientHeight);
		} else { // Inne niz IE
			return new Array(self.innerWidth, self.innerHeight);
		}
	}
	
	var clientAreaSize = getClientAreaSize();
	var marginLeftRight = Math.floor((clientAreaSize[0] - 466) / 2);
	var marginTopBottom = Math.floor((clientAreaSize[1] - 326) / 2);
	
	$('div#netbar > div.navbar > div > a.in').click(function() {
		$('div#login > div.window').css('margin', marginTopBottom + 'px ' + marginLeftRight + 'px');
		var login = $('div#login').width(clientAreaSize[0] + '%').height(clientAreaSize[1] + '%');
		window.ActiveXObject ? login.show() : login.fadeIn('fast');
		$('div#login > div.window > div > form input[name="login"]').focus();
	});
	
	// Wysylka formularza logowania
	
	$('div#login > div.window > div > form input.submit').click(function() {
		$('div#login > div.window > div > form').submit();
	});
	
	// Hiperlacze wylogowania
	
	$('div#netbar > div.navbar > div > a.out').live('click', function() {
		
		if (confirm('Na pewno chcesz się teraz wylogować?')) {
			document.location.href = $(this).attr('href');
		} else {
			return false;
		}
	});
	
	// Ukrycie formularza logowania
	
	$('div#login > div.window > div > a.cancel').click(function() {
		window.ActiveXObject ? $('div#login').hide() : $('div#login').fadeOut('fast');
	});
	
	// Otwarcie hiperlacza w nowym oknie
	
	var protocol = document.location.protocol + '//';
	var host = document.location.host;
	var baseDomain = !window.baseDomain
		? host.substring(host.lastIndexOf('.', host.lastIndexOf('.') - 1) + 1)
		: window.baseDomain;
	
	var outgoingLinks = $('div#main > div.box > div.content a[href]')
		.not('a[href=""]')
		.not('a[href^="/"]')
		.not('a[href^="#"]')
		.not('a[href^="' + protocol + baseDomain + '"]')
		.not('a[href^="' + protocol + 'www.' + baseDomain + '"]')
		.not('a.targetSelf');
	
	var subdomain = new RegExp('\.' + baseDomain.replace(/\./g, '\\.') + '$', 'i');
	
	$(outgoingLinks).each(function() {
		var uriHost = $(this).attr('href').match(/^https?\:\/\/((?:[a-z\d\-]+\.)+[a-z]{2,6})(?:\/.*)?$/i);
		
		if (uriHost != null && subdomain.test(uriHost[1])) {
			outgoingLinks = $(outgoingLinks).not($(this));
		}
	});
	
	$(outgoingLinks)
		.add('a[href^="/out/"]')
		.add('*.targetNew a')
		.add('a.targetNew')
		.not('a.targetSelf')
	  .not($('div#main > div.box > div.content')
	    .find('a[href$=".jpg"], a[href$=".png"]'))
	.click(function() {
		window.open($(this).attr('href'));
		return false;
	});
	
	// Usuniecie zbednych tagow br spomiedzy elementow
	
	$('div#main > div.box > div.content ul > br')
		.add('div#main > div.box > div.content ol > br')
		.add('div#main > div.box > div.content blockquote br')
		/*.add('div#main > div.box > div.content table > br')
		.add('div#main > div.box > div.content table > tbody > br')
		.add('div#main > div.box > div.content table > tbody > tr > br')*/
	.remove();
	
	// Usuniecie zbednych tagow br zza elementow blokowych
	
	$('div#main > div.box > div.content img.alignCenter')
			.parents('div#main > div.box > div.content *')
		.add('div#main > div.box > div.content img.alignCenter')
		.add('div#main > div.box > div.content div')
		.add('div#main > div.box > div.content blockquote')
		.add('div#main > div.box > div.content ul')
		.add('div#main > div.box > div.content ol')
		.add('div#main > div.box > div.content table')
	.next('br').remove();
	
	// Usuniecie obramowania hiperlaczy zawierajacych obraz
	
	$('a').each(function() {
		
		if ($(this).text() == '' || $(this).children().length > 0) {
			$(this).css('background', 'none');
		}
	});
	
	// Usuniecie gornego marginesu dla kolejnych stopek newsa
	
	$('div#main > div.box > div.content').each(function() {
		$(this).find('div.footer:gt(0)').css('marginTop', 0);
	});
	
	// Wyroznia co drugi wiersz tabeli infoBox
	
	$('table.infoBox > tbody > tr:odd').addClass('ndRow');
	
	// Fancybox
	
	$('div#main > div.box > div.content').each(function()
	{
	  $(this)
	    .find('a[href$=".jpg"], a[href$=".png"]')
	    .attr('rel', 'fancybox-' + Math.random().toString().replace('.', ''))
	    .fancybox({
	      padding: 1,
	      cyclic: true,
	      centerOnScroll: true,
	      overlayOpacity: 0.7,
	      overlayColor: '#000',
	      titlePosition: 'over',
	      transitionIn: 'elastic',
	      transitionOut: 'elastic',
	      onComplete: function()
	      {
	        $('div#fancybox-wrap').hover(function()
          {
            $('div#fancybox-title').slideDown(200);
          }, function()
          {
            $('div#fancybox-title').slideUp(200);
          });
	      }
	    });
	});
	
	$('a#fancybox-close').removeAttr('style'); // Close button fix
  
  $('div#fancybox-outer').css({
    backgroundColor: '#000',
    border: '1px solid #f3b52f'
  });
  
	// Pokaz wszystkie przyciski partnerow
	
	$('div#menu > div.affiliates > div.showAll > a').click(function()
	{
	  $(this).parent().hide();
	  $('div#menu > div.affiliates > div.collapse').fadeIn();
	});
	
});
