$(window).load(function() {

	var $keredmenyMargo = Math.floor((405 - $("#blog_keredmeny_fejlec div span.beirt").width()-24)/2);
	$("#blog_keredmeny_fejlec div span.kulcsszo").css("margin-left", $keredmenyMargo + "px");

/*
 405, mert a teljes szelesseg 510, d ebbol amr kivontam a span.kulcsszo szelesseget, az ugyis fixen 105
 -24, mert a width() nem a paddingokkal szamolt szelesseget adja vissza, a padding mindig 24px
*/

	$('#blog_fooldal_fejlec .lepesek a').bind("mouseenter mouseleave", function() {
		$(this).parent("div.lepesek").toggleClass('on');
	});
	
	$("a.folytat").click(function(){
		$(this).parent("p").find("span.pont3").hide();
		$(this).parent("p").find("span.folytatas").show();
		$(this).hide();
		return false;
	});
	
	$("a.archivum_ev").click(function(){
		$(this).parent("li").toggleClass("nyitva");
		return false;
	});
	
	$(".tipus ul li *").click(function(){
		$(this).parent("li").addClass("kivalasztott").siblings("li").removeClass();
	});

	/* verseny: mi ez hover */
	$("#verseny-miez").mouseover(function(){
		$("#verseny-miez_layer").show();
	});
	$("#verseny-miez_layer").mouseleave(function(){
		$(this).hide();
	});

	// nyeremeny kep valtasa
	$('#parfumcsomag').mouseover(function() {
		$('#parfumcsomag').addClass('open');
	});

	$('#parfumcsomag').mouseout(function() {
		$('#parfumcsomag').removeClass();
	});


	/* a versenyrol bovebben kinyit / bezar */
	$("#versenyrol-bovebben-gomb").click(function() {
		$("#versenyrol-bovebben").slideToggle("slow");
		return false;
	});
});

STARVOTE = {
	inited: false,

	w: null,
	h: null,
	wfix: null,

	action: null,

	enabled: false,

	// init
	init: function(conf) {
		this.w = conf.w;
		this.h = conf.h;
		this.wfix = conf.wfix;

		this.action = conf.action;

		this.votecount = conf.votecount;

		$enabled = this.enabled = conf.enabled;

		if (this.enabled) {
			$(".star-rating li a").each(function() {
				$info = $(this).parents('.stars').find(".star-info");
				$(this).mouseover(function() {
					$info.html($(this).attr('rel'));
					$info.show();
					// ahanyadik * X (szelesseg) - Y (wtf?)
					$info.css('margin-left', ($(this).html() * STARVOTE.w) - STARVOTE.wfix-5);
				});
				$(this).mouseout(function() {
					$info.hide();
				});
				$(this).click(function() {
					// ajax mentes (tipus, id, szavazat)
					STARVOTE.vote($(this).parents('.stars').attr('name'), $(this).attr('href').replace('#', ''), $(this).html(), $(this).parents('.stars'));
				});
			});
		}
		this.inited = true;
	},

	// szavazas kuldese
	vote: function(type, id, vote, stars) {
		if (!this.inited || !this.enabled) {
			return;
		}
		post = {'type': type, 'id': id, 'vote': vote};
		$.ajax({
			type: 'POST',
			url: this.action,
			dataType: 'json',
			data: $.param(post),
			success: function (ret) {
				if (ret.status == 'ok') {
					stars.find(STARVOTE.votecount).html(ret.cnt);
					stars.find('.current-rating').width(ret.current + 'px');
				}
				STARVOTE.message(ret);
			}
		});
	},

	message: function(ret) {
		$('#growl_message_' + ret.status).jGrowl(ret.message, { header: ret.header, life: 3000, theme: 'babaszoba_' + ret.status, speed: 'slow' });
	}
}

BLOG = {
	inited: false,

	open_uj_naplo: false,
	open_uj_bejegyzes: false,
	open_szerkesztes: false,

	opened: [],

	action: null,

	BlogActivateAsk: null,
	BlogInactivateAsk: null,
	BlogDeleteAsk: null,
	PostDeleteAsk: null,

	// init
	init: function(conf) {

		this.action = conf.action;

		this.BlogActivateAsk = conf.BlogActivateAsk;
		this.BlogInactivateAsk = conf.BlogInactivateAsk;
		this.BlogDeleteAsk = conf.BlogDeleteAsk;
		this.PostDeleteAsk = conf.PostDeleteAsk;

		// uj naplo gomb
		$("a.blog_gomb_ujnaplo").click(function(){
			BLOG.open_uj_naplo = true;
			$("a.blog_gomb_ujnaplo").hide();
			$("#blog_ujnaplo").show();
			return false;
		});

		// naplo tiltas
		$("li.tiltas a").click(function() {
			$host = $(this).parents('div').parents('div').attr('name');
			BLOG.status(BLOG.BlogInactivateAsk, $host, 'bloghide', $(this).parents('div').parents('div').get(0));
			return false;
		});

		// naplo aktivalas
		$("li.aktivalas a").click(function() {
			$host = $(this).parents('div').parents('div').attr('name');
			BLOG.status(BLOG.BlogActivateAsk, $host, 'blogshow', $(this).parents('div').parents('div').get(0));
			return false;
		});

		// naplo torles
		$("li.torles a").click(function() {
			$host = $(this).parents('div').parents('div').attr('name');
			BLOG.status(BLOG.BlogDeleteAsk, $host, 'blogdelete', $(this).parents('div').parents('div').get(0));
			return false;
		});

		// post torles
		$("div.blog_bejegyzes a.torles").click(function() {
			BLOG.status(BLOG.PostDeleteAsk, null, 'postdelete', $(this).parents('div').parents('div').get(0), $(this).attr('href').match("([0-9]+)#?.*$")[1]);
			return false;
		});

		this.inited = true;
		this.closeAll();
	},

	closeAll: function() {
		if (this.opened.length) {
			for (i = 0; i < this.opened.length; i++) {
				this.opened[i].hide();
			}
			this.opened = [];
		}
	},

	message: function(ret) {
		$('#growl_message_' + ret.status).jGrowl(ret.message, { header: ret.header, life: 3000, theme: 'babaszoba_' + ret.status, speed: 'slow' });
		// ha linkre el kell dobni
		if (ret.href) {
			setTimeout("document.location.href = '" + ret.href + "';", 3000);
		}
	},

	// blog / post actionok
	status: function(ask, host, action, obj, id) {
		var answer = confirm(ask)
		if (!answer){
			return;
		}

		post = {'host': host, 'action': action, 'id': id};
		$.ajax({
			type: 'POST',
			url: this.action,
			dataType: 'json',
			data: $.param(post),
			success: function (ret) {
				if (action == 'blogdelete') {
					// blog torlese
					$(obj).html('');
				}
				// uzenet
				BLOG.message(ret);
			}
		});
	}
}

// kep beszuro kitolto
function kepTagInsert(pos) {
	$('.picid_' + pos + ' a.codeinsert').click(function () {
		$('#message').insertAtCaret('<kep id="' + pos + '" align="' + $('.picid_' + pos + ' select').val() + '" />');
		return false;
	});
}

