

function debug(obj)
{
	if(debug_mode && typeof console != 'undefined' && typeof console.log != 'undefined')
		console.log(obj);
}



$(document).ready(function()
{
	$('.hintField')
	.each(function()
	{
		if($(this).attr('value') == '')
		{
			$(this).val($(this).attr('title'))
				.addClass('emptyField');
		}
	})
	.focus(function()
	{
		if($(this).val() == $(this).attr('title'))
		{
			$(this).val('')
				.removeClass('emptyField');
			if($(this).hasClass('passwordField') && $(this).attr('type') == 'text')
			{
				new_obj = $(this).clone();
				new_obj.attr('type', 'password');
				$(this).after(new_obj).remove();
				new_obj.focus();
			}
		}
	}).focusout(function()
	{
		if($(this).val() == '')
		{
			$(this).val($(this).attr('title'))
				.addClass('emptyField');
			if($(this).hasClass('passwordField') && $(this).attr('type') == 'password')
			{
				new_obj = $(this).clone();
				new_obj.attr('type', 'text');
				$(this).after(new_obj).remove();
			}
		}
	}).change(function()
	{
		$(this).removeClass('emptyField');
	})
	
	$('TEXTAREA.enterSubmit').keydown(function(e)
	{
		if(e.keyCode == 13 && !e.shiftKey)
		{
			$(this).parents('FORM').submit();
			return false;
		}
	});
	$('TEXTAREA.autoGrow').keydown(function(e)
	{
		if(e.keyCode == 13 && e.shiftKey)
		{
			ileLinii = $(this).val().split(/\n/).length;
			ileLinii++;
			$(this).height(ileLinii*$(this).data('lineHeight'));
		}
		
	}).keyup(function(e)
	{
		ileLinii = $(this).val().split(/\n/).length;
		$(this).height(ileLinii*$(this).data('lineHeight'));
	}).each(function()
	{
		// zapisz oryginalny rozmiar (jednalinia)
		$(this).css('height', $(this).css('lineHeight'));
		$(this).data('lineHeight', $(this).height());
	});
	
	$('.fullParentWidth').each(function()
	{
		$(this).width($(this).parent().width());
	});
	
	$('.yt_preview').click(function()
	{
		if($(this).data('player') == true)
			return false;
		
		var yt_id = $(this).attr('id').replace(/^yt_/, '');
		
		
		$(this).children().remove();
		$(this).append($('<div style="text-align: center;"><object width="500" height="405" style="margin-left: auto; margin-right: auto;">'+
'<param name="movie" value="http://www.youtube.com/v/'+yt_id+'?version=3&amp;hl=pl_PL"></param>'+
'<param name="allowFullScreen" value="true"></param>'+
'<param name="allowscriptaccess" value="always"></param>'+
'<embed src="http://www.youtube.com/v/'+yt_id+'?version=3&amp;hl=pl_PL&autoplay=1&rel=0" '+
'	type="application/x-shockwave-flash" '+
'	width="500" '+
'	height="405" '+
'	allowscriptaccess="always"'+ 
'	allowfullscreen="true">'+
'</embed>'+
'</object></div>'));
		
		$(this).data('player', true);
		
		return false;
		
	}).each(function()
	{
		var yt_id = $(this).attr('id').replace(/^yt_/, '');
		
		$(this).css('position', 'relative')
				.css('width', '500')
				.css('height', '405')
				.css('margin', 'auto')
				.append($('<img style="position: absolute; top: 50%; left: 50%;width: 128px; height: 128px; margin-top: -64px; margin-left: -64px;" src="/gfx/player_play.png" />'));
/*		if(IS_MOBILE)
			$(this).attr('href', 'vnd.youtube:bZfPTK-J8yQ?vndapp=youtube_mobile&vndclient=mv-google&vndel=watch');
*/
	});


	// obsluga stronicowania
	$('.stronicowanie > .pages').each(function()
	{
		$(this).scrollTo($(this).children('a.active'), 0);
//		$(this).scrollTo('-='+($(this).width()/2-$(this).children('a.active').width())+'px', 0); 
	});
	
	
	
	if(user_adult == 0)
	{
		// chowa materialy dla doroslych i pojawia się komunikat, ze to dla doroslych ;)
		$('DIV.for_adult').each(function()
		{
			$(this).hide();
			info = $('#for_adult_info').clone().attr('id', 'for_adult_'+$(this).attr('id')).css('display', 'block').data('art_id', $(this).attr('id'));
			$(this).after(info);
		});
	}
	
	
	$('IMG.fastzoom')
		.mouseover(function() {
			var n_img = $(this).clone();
			pos = $(this).offset();
			$(n_img)
				.css('position', 'absolute')
				.css('left', pos.left)
				.css('right', pos.right);
			$(this).before(n_img);
			
			
			$(n_img)
				.data('origHeight', $(this).height())
				.data('origWidth', $(this).width())
				.addClass('removeOnEscape')
				.mouseout(function() {
					// hover out
					$(this).animate({
						height: $(this).data('origHeight'),
						width: $(this).data('origWidth'),
						left: "+="+$(this).data('moveLeft'),
						top: "+="+$(this).data('moveTop')
						}, 100, function()
						{
							$(this).remove();
						});
				
				});
			var opt = {};
			if($(this).attr('zoom-width'))
				opt.width = $(this).attr('zoom-width');
			if($(this).attr('zoom-height'))
				opt.height = $(this).attr('zoom-height');
			if(typeof opt.height == 'undefined')
			{
				// musimy policzyc jaka medzie wysokosc z proporcji
				opt.height = Math.floor(opt.width*$(this).height()/$(this).width());
			}
			$(n_img)
				.data('moveLeft', Math.floor((opt.width-$(this).width())/2))
				.data('moveTop', Math.floor((opt.height-$(this).height())/2));
			opt.left = "-="+$(n_img).data('moveLeft');
			opt.top = "-="+$(n_img).data('moveTop');
			$(n_img).animate(opt, 100);
		});
		
});


function show_for_adult(btn)
{
	for_adult_div = $(btn).parent();
	
	art_id = $(for_adult_div).data('art_id');
	
	for_adult_div.slideUp('fast', function() { 	$('#'+$(this).data('art_id')).slideDown(); });
}

function json(fn, args, callback)
{
	f = fn;
	if(fn.indexOf('/') == -1)
		f = module+'/'+f;
	var settings = {
		data: args,
		success: callback,
		error: callback,
		type: 'POST',
		dataType: 'json'
	};
	$.ajax('/'+f+'.json', settings);
}


function vote(typ_materialu, id_materialu, glos, callback)
{
	if(typeof user_login == 'undefined')
	{
		alert('Tylko zalogowani użytkownicy mogą głosować! Zaloguj się!');
		return ;
	}
	json('glosowanie/glosuj', {'typ_materialu': typ_materialu, 'id_materialu': id_materialu, 'glos': glos}, callback);
}






