$().ready( function(){

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

	$('a.home').bind('mouseenter',function(){
		$('pre.main').hide();
		$('pre.hover').show();
	}).bind('mouseleave',function(){
		$('pre.hover').hide();
		$('pre.main').show();
	});

   
	$('.playlistAdd').live('click', function() {
		$.get('dodaj/'+$(this).attr('title'),function(data) {
			$('#playlist').html(data);
		});
		$(this).fadeOut('slow');
		
		return false;
	});

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

		$.get('usun/'+$(this).attr('title'));
		$(this).parent('li').fadeOut('slow');

		return false;
	});

	/* banlista */

	$('#banlistInput').keyup(function(event) {

		if (event.keyCode == '13') {
			
			$.post('addTag.html',{'tagPl': jQuery.trim($(this).val())},function() {
				location.reload(true);
			});

			return false;
		}

	})

	$('#banlistAdd').live('click', function() {

		$.post('addTag.html',{'tagPl': jQuery.trim($(this).prev('input').val())},function() {
			location.reload(true);
		});
		
		return false;
		
	});


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

		$.post('removeTag.html',{'tagId': $(this).attr('title')},function() {
			$(this).parent('li').fadeOut('slow');
			location.reload(true);
		});
		
		return false;
	});

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

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

		return false;
	});


//	$('img').noContext();

	starRaterPrepare();

	commentFormPrepare();

});

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



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 div.comment:first').hide().slideDown();
			} 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)
}
