$().ready( function(){

	if($.cookie('pelnoletniosc')) {
		showCSPNONK();
	}

	$('.sortmode').live('click', function() {

		$.get('setSort/'+$(this).attr('title'),function() {
				location.reload(true);
			});

		return false;
	});

	$('#search_adv').click(function(){

		$('#filtrbox').slideUp();
		$('#searchbox').slideDown();

		return false;

	});

	$('.filter').click(function(){

		$('#searchbox').slideUp();
		$('#filtrbox').slideDown();
		$('html, body').animate({scrollTop:0})

		return false;

	});

	$('#filtrboxAll').click(function(){
		$('#filtrbox input[type=checkbox]').attr('checked', 'checked');
		return false;
	});

	$('#filtrboxClear').click(function(){
		$('#filtrbox input[type=checkbox]').attr('checked', false);
		return false;
	});


	$('#filtrboxClose').click(function(){
		$('#filtrbox').slideUp();
		return false;
	});

	$('#filtrboxSave').click(function(){

		var tags  = new Array;
		
		$("#filtrbox input:checked").each(function() {
			tags.push($(this).attr('id'));
		});

		$.post('setTags.html',{'tags[]': tags},function() {
			location.reload(true);
		});

		return false;
		
	});


	starRaterPrepare();

	commentFormPrepare();
	
	setInterval('blink()', 500);
	
});

function blink(color){
	if($('.blink').text() == 'SEX OFERTY')
		$('.blink').html('&nbsp;');
	else
		$('.blink').text('SEX OFERTY');
}

function starRaterPrepare(){
	if($('input[type=radio].starRater').length === 0) return;

	$('input[type=radio].starRater').rating({
		focus: function(){
			$('.starsLight').hide()
			},
		blur: function(){
			$('.starsLight').show()
			},
		callback: function(){
			$('input[type=radio].starRater').rating('disable')
			var vote = $(this).val()
			var movieId = $('input[name=movieId]').val()
			$.post('dodaj-glos.html', {
				movieId:movieId,
				vote:vote
			}, function(data){
				if(data.valid == 1){
					$('.starsLight').remove();
					$('.rating').after('<span class="error">Głos został dodany.</span>')
					$('.rating').prev('h3').html('Ocena: '+data.rate.votesAvg+' ('+data.rate.votesTotal+' głosów)')
				} else {
					$('input[type=radio].starRater').rating('enable')
					$('.starsLight').show()
					$('.rating').after('<span class="error">'+data.msg+'</span>')
				}
				hideAfterTimeout('span.error')
			}, 'json')
		}
	})
	
}

function hideAfterTimeout(selector, timeout){
	if(typeof timeout === 'undefined') timeout = 1000
	setTimeout(function(){ $(selector).fadeOut('normal', function(){$(this).remove()})}, timeout)
}


function commentFormPrepare(){

	
	if($('form[name=commentForm]').length === 0) return;

	$('#captchaImage').hide();
	$('form[name=commentForm]').hide();

	$("#commentButton").bind('click', function(){

		$('form[name=commentForm]').slideToggle('normal', function(){
			if($(this).css('display')!='none'){
				reloadCaptcha()
			}
		});
	})

	var isSaving = false;
	var commentSaved = false;

	if(isSaving) return;
	if(commentSaved) {
		$('textarea[name=commentBody]').after('<label class="error">Twój komentarz został już zapisany</label>')
		hideAfterTimeout('label.error', 3000)
	}

	$('input[name=submitComment]').click(function(){

		isSaving = true;
		var movieId = $('input[name=movieId]').val();
		var commentBody = $('textarea[name=commentBody]').val();
		var commentSign = $('input[name=commentSign]').val();
		var commentVericode = $('input[name=commentVericode]').val();

		$.post('dodaj-komentarz.html', {commentVericode:commentVericode, commentBody:commentBody, commentSign:commentSign, movieId:movieId}, function(data){
			if(data.valid == 1){
				commentSaved = true
				$('textarea[name=commentBody]').val('')
				$('input[name=commentSign]').val('')
				$('input[name=commentVericode]').val('')
				$('#comments').prepend(data.html)
				$('#comments .comment:first').hide().slideDown();
				$('form[name=commentForm]').slideUp();
			} else {
				$('input[name=commentVericode]').after('<label class="error">'+data.msg+'</label>')
				hideAfterTimeout('label.error', 3000)
			}
			isSaving = false;
		}, 'json')
		return false;
	})
}

function reloadCaptcha(){

	var imgParent = $('#captchaImage').parent();
	var width = $('#captchaImage').width();
	var height = $('#captchaImage').height();

	$('#captchaImage').remove();
	var newImage = new Image(width, height);
		newImage.src=$('base').attr('href')+'captcha/show/'+new Date().getTime()+'.png';
		newImage.id="captchaImage";
	imgParent.prepend(newImage);

	// for our beloved IE6 :)
//	setTimeout(function(){ imgParent.prepend(newImage) }, 0)
}

// obsługa cspnonka
function showCSPNONK(){
	if(1){
		$('#CSPNONK_main').floating({targetX:'left', targetY:'bottom'});
	}
	else{
		$('#CSPNONK_main').remove();
	}
}

