$(window).load(function() {

	var $keredmenyMargo = Math.floor((405 - $("#albumok_keredmeny_fejlec div span.beirt").width()-24)/2);
	$("#albumok_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
*/

	$('#albumok_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();
	});
});

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' });
	}
}

ALBUM = {
	inited: false,

	open_uj_naplo: false,
	open_uj_bejegyzes: false,
	open_szerkesztes: false,

	opened: [],

	action: null,

	ActivateAsk: null,
	InactivateAsk: null,
	DeleteAsk: null,

	// init
	init: function(conf) {

		this.action = conf.action;

		this.ActivateAsk = conf.ActivateAsk;
		this.InactivateAsk = conf.InactivateAsk;
		this.DeleteAsk = conf.DeleteAsk;

		// naplo tiltas
		$("li.tiltas a").click(function() {
			ALBUM.status(ALBUM.InactivateAsk, 'albumhide', $(this).parents('div').parents('div').get(0), $(this).parents('div').parents('div').attr('name'));
			return false;
		});

		// naplo aktivalas
		$("li.engedelyezes a").click(function() {
			ALBUM.status(ALBUM.ActivateAsk, 'albumshow', $(this).parents('div').parents('div').get(0), $(this).parents('div').parents('div').attr('name'));
			return false;
		});

		// naplo torles
		$("li.torles a").click(function() {
			ALBUM.status(ALBUM.DeleteAsk, 'albumdelete', $(this).parents('div').parents('div').get(0), $(this).parents('div').parents('div').attr('name'));
			return false;
		});

		this.inited = true;
	},

	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);
		}
	},

	// actionok
	status: function(ask, action, obj, id) {
		var answer = confirm(ask)
		if (!answer){
			return;
		}

		post = {'action': action, 'id': id};
		$.ajax({
			type: 'POST',
			url: this.action,
			dataType: 'json',
			data: $.param(post),
			success: function (ret) {
				if (action == 'albumdelete') {
					// torles
					$(obj).html('');
				}
				// uzenet
				ALBUM.message(ret);
			}
		});
	}
}
