

var upload_i=1;
var upload_intv;

function upload_filechange(){
	$('.normalUploader').fadeOut();
	$('.flashUploader').fadeIn();
	$.ajaxFileUpload({
		url:'feltoltes-ajaxfile',
		secureuri:false,
		fileElementId:'fileToUpload',
		dataType: 'json'
	});

	upload_intv=setInterval(function(){		upload_i+=(280-upload_i)/20;
		if(upload_i>278) upload_i=278;
		$('.percent').text( Math.round(upload_i/280*100) + ' %' );		$('.progressLine').width(upload_i);
	}, 500 );
}


function upload_callback(data){	clearInterval(upload_intv);	if(data.ok){		$('.percent').text( '100 %' );
		$('.progressLine').width(280);
		$('#feltolt_pic .picture img').attr('src', data.file);
		$('input[name=fn]').val(data.fn);
		$('#feltolt_pic').fadeIn();
	}else{		$('.flashUploader').fadeOut();
		$('.normalUploader').fadeIn();
		alert("Hiba történt a feltöltés közben: " + data.msg );
	}
}



function kezdolap_hsz_focus(th){	if(!$(th).parent().hasClass("cmInactive")) return;
	$(th).height(32).val('');
	$(th).parent().removeClass("cmInactive").addClass("cmActive");
}
function kezdolap_hsz_blur(th){
	if($(th).val()!='') return;
	if(!$(th).parent().hasClass("cmActive")) return;
	$(th).height(16).val('Szólj hozzá!');
	$(th).parent().removeClass("cmActive").addClass("cmInactive");
}

function kezdolap_hsz_keyup(th){	var c=$(th).val().length;
	$(th).parent().parent().find(".commentTime span").text(c);
	if(c>300) $(th).val( $(th).val().substr(0,300) );
}


function post_tetszik(id){	$.get(BASE+'post-ajaxtetszik', {id:id}, post_tetszikajax);
	$('#post_'+id+' .articlelinks span').text("Ez tetszik neked");
}

function post_nemtetszik(id){
	$.get(BASE+'post-ajaxnemtetszik', {id:id}, post_tetszikajax);
	$('#post_'+id+' .articlelinks span').text("Ez nem tetszik neked");
}

function post_hsz(id){	$('#post_'+id+' textarea').get(0).focus();
}

function post_tetszikajax(d){	if(d=="OK") return;
	alert(d);
}

function feltolt_keyup(th){
	var c=$(th).val().length;
	$(".info2 span").text(c);
	if(c>1000) $(th).val( $(th).val().substr(0,1000) );
}


function hsz_torol(id){	if(!confirm("Biztos törlöd a hozzászólást?")) return;
	$.get(BASE+'post-ajaxhsztorol', {id:id}, hsz_torol_ajax);
}
function hsz_torol_ajax(d){	if(d.match(/^[0-9]+$/)){		$('#hsz_'+d).fadeOut();
	}else{		alert(d);
	}
}


// CÍMKÉK feltöltésnél
var mouse={x:0, y:0}
var signer={x:0, y:0}

function img_mousemove(h){	mouse.x=h.pageX;
	mouse.y=h.pageY;
}
$(function(){	$('a.picture img').mousemove(img_mousemove);
});
function img_click(){	var o=$('#upload_img').offset();
	var x=mouse.x-o.left-50;
	var y=mouse.y-o.top-50;
	if(x<=0) x=1; if(y<=0) y=1;
	if(x+100>$('#upload_img').width()) x=$('#upload_img').width()-100;
	if(y+100>$('#upload_img').height()) y=$('#upload_img').height()-100;
	x=Math.round(x); y=Math.round(y);
	signer.x=x;
	signer.y=y;
	$('#img_signer .signerComment').show();
	$('#img_signer .signerCommentText').hide();	$('#img_signer').css('left', (x-12)+'px').css('top', y+'px').show();
	$('#img_signer input[name=message]').get(0).focus();}
function img_signer_save(){	var msg=$('#img_signer input[name=message]').val();
	if(msg.length>20){		alert("Túl hosszú címke!");
		return;
	}
	if(msg.length<3){		alert("Túl rövid címke!");
		return;
	}
	if(msg.match(/,/)){		alert("A címke nem tartalmazhat vesszőt!");
		return;
	}
	if($('#cimkek a').size()>0) $('#cimkek').append(', ');
	$('#cimkek').append('<a />').find('a:last').attr({'rel':signer.x+'x'+signer.y, href:'#',onclick:'return false'}).hover(cimke_hover1, cimke_hover2).text(msg);
	$('#cimkek').append(' <a />').find('a:last').attr({href:'#'}).click(cimke_torol).text('(töröl)');
	$('#img_signer input[name=message]').val('');
	$('#img_signer').hide();
}

function cimke_hover1(){	var k=$(this).attr('rel').split('x');
  $('#img_signer .signerComment').hide();
	$('#img_signer .signerCommentText').text( $(this).text() ).show();
	$('#img_signer').css('left', (k[0]-12)+'px').css('top', k[1]+'px').show();
}
function cimke_hover2(){
	$('#img_signer').hide();
}
function cimke_torol(){	var $a=$(this).prev();
	$(this).remove();
	$a.remove();
	return false;
}

function feltoltes_submit(th){	var cimke=[]
	var cimke_info=[]
	$('#cimkek a[rel]').each(function(){		cimke.push($(this).text());
		cimke_info.push($(this).attr('rel'));
	});
	$('input[name=cimke]').val( cimke.join(',') );
	$('input[name=cimke_info]').val( cimke_info.join(',') );
	$('input[type=file]').remove();
}



$(function(){	if($('#uj_post_feltoltes').size()>0) return;
	$('div[id^=post_cimke_] > a').hover(kezdolap_cimke_hover1, kezdolap_cimke_hover2);
	$('.signer:not(.new)').each(function(){		$(this).css('opacity', 0).css('cursor', 'pointer');
		$('img.floatleft', this).css('opacity', 0).css('cursor', 'pointer');
		$('.signerCommentText', this).hide();
		$(this).hover(kezdolap_keret_hover1, kezdolap_keret_hover2);
		$(this).show();
	});
});
function kezdolap_cimke_hover1(){	var id='#'+$(this).attr('rel');
	//$(id).find('.signerCommentText').show();
	$(id).css('opacity', 1);
	$(id).find('img.floatleft').css('opacity', 1);
}
function kezdolap_cimke_hover2(){	var id='#'+$(this).attr('rel');
	$(id).css('opacity', 0);
	$(id).find('img.floatleft').css('opacity', 0);
	//$(id).find('.signerCommentText').hide();
}
function kezdolap_keret_hover1(){	$('.signerCommentText', this).show();
	$(this).css('opacity', 1);
}
function kezdolap_keret_hover2(){
	$('.signerCommentText', this).hide();
	$(this).css('opacity', 0);
}



function uj_cimke(pid){	$('div.signer[id^=cimke_'+pid+'_]').hide();
	var x=$('#post_'+pid+' a.picture img').width() - 200;
	var y=$('#post_'+pid+' a.picture img').height() - 200;
	x=Math.round(x); y=Math.round(y);
	$('#new_signer_'+pid).css('left', x+'px').css('top', y+'px').show();
	$('#post_'+pid+' a.picture').removeAttr('href');
	$('#post_'+pid+' a.picture img').bind('click', kezdolap_img_click).css('cursor', 'crosshair');
	$('div.signer.new:visible input[name=message]').get(0).focus();
	$('div.signer.new:visible input[name=coord]').val(x+'x'+y);
}
function uj_cimke_megse(pid){
	$('div.signer[id^=cimke_'+pid+'_]').show();
	$('#new_signer_'+pid).hide();
	$('#post_'+pid+' a.picture').attr('href', $('#post_'+pid+' a.picture').attr('rel') );
	$('#post_'+pid+' a.picture img').unbind('click').css('cursor', 'pointer');
}
function kezdolap_img_click(){
	var o=$(this).offset();
	var x=mouse.x-o.left-50;
	var y=mouse.y-o.top-50;
	if(x<=0) x=1; if(y<=0) y=1;
	if(x+100>$(this).width()) x=$(this).width()-100;
	if(y+100>$(this).height()) y=$(this).height()-100;
	x=Math.round(x); y=Math.round(y);
	signer.x=x;
	signer.y=y;
	$('div.signer.new:visible').css('left', (x-12)+'px').css('top', y+'px').show();
	$('div.signer.new:visible input[name=message]').get(0).focus();
	$('div.signer.new:visible input[name=coord]').val(x+'x'+y);
}

function check_kezdolap_ujcimke(th){	var msg=$('input[name=message]', th).val();
	if(msg.length>20){
		alert("Túl hosszú címke!");
		return false;
	}
	if(msg.length<3){
		alert("Túl rövid címke!");
		return false;
	}
	if(msg.match(/,/)){
		alert("A címke nem tartalmazhat vesszőt!");
		return false;
	}
}


// PANEL
$(window).resize(function(){
	var l=($(window).width()/2 - 350);
	if(l<0) l=0;	$('.panel').css('left', l + 'px');
});
$(function(){	var l=($(window).width()/2 - 350);
	if(l<0) l=0;
	$('.panel').css('left', l + 'px');
});
function panel_center(){	$('.panel').css('top', ($(window).scrollTop()+50)+'px');
}
function panel_show(){	var url="";
	var rel=$(this).attr('rel');
	var fn="";
	if(rel!=""){		url=rel.split('|')[0];
		if(rel.split('|').length>1) fn=rel.split('|')[1];
	}
	if(url=="")	url=$(this).attr('href');
	if(url=="-") url=location.href;	$('.panel .middleMiddle').text('Betöltés...');	$('.panel').fadeIn();	panel_center();
	if(fn!=""){
		$('.panel .middleMiddle').load( BASE + url + "/ajax", function(){ eval(fn); } );
	}else{		$('.panel .middleMiddle').load( BASE + url + "/ajax" );
	}
	return false;
}
$(function(){	$('a.showpanel').click(panel_show);
});



// cimkék középre igazítása
function cimke_center(){	$('.signer').each(function(){		var w=$(this).width();		var w2=$('.signerCommentText', this).width();
		$('.signerCommentText', this).css('margin-left', ( (w-w2)/2 - 8 )+'px');
	});
}
$(cimke_center);



/*
	18-as kérdés
*/
var _18=$.cookie("tartalom18");
if(_18!=0 && _18!=1){	// kérdés:
 	$(function(){		$('#tartalom18_a').trigger('click');
	});
}

function settartalom18(ok){	$.cookie("tartalom18", ok, {expires: 1} );
}