var Eo = function() {
	var offerCount = 1;
	var offerDelay = 5000;
	var offerCurrentItem = 1; 
	var offerEffect = "drop";
	var tempId = 0;

	return {
		init: function() {
			$("#eo-error").dialog({
				bgiframe: true,
				resizable: false,
				title: 'Ajax hiba',
				height: 30,
				width: 300,
				show: 'scale',
				hide: 'scale',
				modal: true,
				autoOpen: false,
				overlay: {
					backgroundColor: '#000',
					opacity: 0.5
				}
			});
			$('#eo-error').ajaxError(function(event, request, settings, exception){
   			$(this).html("Hiba a következő oldal megnyitásakor: <b>" + settings.url + "</b><br/>Hibakód: <b>" + request.status + "</b>");
 				$(this).dialog('open');
 				var t=setTimeout("if ($('#eo-error').dialog('isOpen')) {$('#eo-error').dialog('close');}",3000);
			});		
//			$.get('/ajax/fake.php', {},
//				function(data) {
//					var targetdiv = "#prevmesdata" + id + "_" + mes_id;
//					$(targetdiv).html(data);
//			});
		},
		initClub: function() {
			$("#club-dialog").dialog({
				bgiframe: true,
				resizable: false,
				height:330,
				width: 400,
				modal: true,
				autoOpen: false,
				overlay: {
					backgroundColor: '#000',
					opacity: 0.5
				},
				buttons: {
					'Mégsem': function() {
						$(this).dialog('close');
					},
					'Meghívom': function() {
						if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('#email').val())) {
							var url= '/ajax/send_clubinvite_process.php';
							$.post(url, $('#clubinvite').serialize(),
								function(data) {
									$("#club-dialog").dialog('close');
							});
						} else {
							alert("Érvénytelen a címzett e-mail cím formátuma! Javítsd ki légy szíves!")
							return (false)
						}
					}
				}
			});
		},
		initTTDialog: function() {
			$('#tt-dialog').dialog('open');
			$('#tt_sport_id').val($('#sport_id').val());
		},		
		fillTrainingFromTemplate: function() {
			var params = {
				template_id: $("#tte_id").val()
			}
			$.getJSON('/ajax/get_template.php', params,
				function(data) {
					$("#tte-name").html(data.name);
					$("#tte-id").val(data.template_id);
					$("#sport_id").val(data.sport_id);
					$("#sport_id").change();
					$("#training_type_id").val(data.training_type_id);
					$("#hour").val(data.time.substring(0,2));
					$("#min").val(data.time.substring(3,5));
					$("#thour").val(data.training_time.substring(0,2));
					$("#tmin").val(data.training_time.substring(3,5));
					$("#tsec").val(data.training_time.substring(6,8));
					$("#tte-data").show("slow");
					$("#tte-dialog").dialog('close');
			});
		},		
		saveTrainingType: function() {
			if ($('#tt_sport_id').val() == "") {
				alert("Válassz ki egy sportot!");
				return;	
			}
			if ($('#tt_name').val() == "") {
				alert("Add meg az edzéstípus nevét");
				return;	
			}
			var url= '/ajax/save_training_type.php';
			$.post(url, $('#tt-form').serialize(),
				function(data) {
					$('#sport_id').val(data.tt[0].sport_id);
					setTrainingType($('#sport_id').val());
					var tthtml = '<option value="' + data.tt[0].Id + '">' + data.tt[0].name + '</option>';
					$(tthtml).appendTo('#training_type_id');
					$('#training_type_id').val(data.tt[0].Id);
					var ttstr = "{'name': '" + data.tt[0].name + "', 'sport_id':" + data.tt[0].sport_id + ", 'id':" + data.tt[0].Id + "}";
					var ttobj = eval('(' + ttstr + ')');
					tt.push(ttobj);
					$("#tt-dialog").dialog('close');
			},'json');
		},		
		setTrainingMode: function() {
			if ($('#show_advanced_fields:checked').val() == "T") {
				$('#advanced-descr').hide('slow', function() {
					$('#advanced-fields').show('slow');
				});
			} else {
				$('#advanced-fields').hide('slow', function() {
					$('#advanced-descr').show('slow');
				});
			}
		},		
		submitTrainingList: function() {
			if ($('#is_training:checked').val() == "verseny") {
				var itc = "verseny";	
			} else {
				var itc = "edzes";				
			}
			if ($('#favorite:checked').val() == "kedvencek") {
				var fav = "kedvencek";	
			} else {
				var fav = "";				
			}
			var surl = '/edzeslista/' + $('#date').val() + '/' + $('#sport_id').val() + '/' + itc + '/' + fav;
			document.location = surl;
			return true;
		},		
		clubInvite: function() {
			$('#club-dialog').dialog('open');
		},		
		deleteMsg: function(msgtext, msgid) {
			Eo.tempId = "#" + msgid;
			$('#delete-msg-title').html(msgtext);
			$('#dialog').dialog('open');
		},		
		getPreviousTopicMessage: function(mes_id, topic_id, id) {
			var params = {
				mes_id: mes_id,
				topic_id: topic_id,
				id: id
			}
			$.get('/ajax/get_prev_mes.php', params,
				function(data) {
					var targetdiv = "#prevmesdata" + id + "_" + mes_id;
					$(targetdiv).html(data);
			});
		},
		getCalorie: function(targetdiv) {
			var params = {
				date: $("#date").val(),
				sport_id: $("#sport_id").val(),
				distance: $("#distance").val()
			}
			$.get('/ajax/get_calorie.php', params,
				function(data) {
					$(targetdiv).val(data);
			});
		},
		getRewriteUrl: function(text, targetdiv) {
			var params = {
				text: text
			}
			$.get('/ajax/get_rewrite_url.php', params,
				function(data) {
					$(targetdiv).val(data);
			});
		},
		getCalendar: function(year, month, targetdiv) {
			var params = {
				year: year,
				month: month,
				targetdiv: targetdiv
			}
			$.get('/ajax/get_calendar.php', params,
				function(data) {
					$(targetdiv).html(data);
			});
		},
		getPulse: function(date, avg_pulse, targetdiv) {
			var params = {
				date: date,
				avg_pulse: avg_pulse
			}
			$.get('/ajax/get_pulse.php', params,
				function(data) {
					$(targetdiv).html(data);		
			});
		},
		startOffer: function(oc) {
			offerCount = oc;
			setTimeout('Eo.replaceOffer()', offerDelay);
		},
		replaceOffer: function() {
			var divid = '#offer-' + offerCurrentItem;
			var options = {direction: "left"};
			$(divid).hide(offerEffect,options,1000,Eo.replaceOfferCB);
		},
		replaceOfferCB: function() {
			offerCurrentItem++;
			if (offerCurrentItem > offerCount) {
				offerCurrentItem = 1;	
			}
			var divid = '#offer-' + offerCurrentItem;
			var options = {direction: "right"};
			$(divid).show(offerEffect,options,1000);
			setTimeout('Eo.replaceOffer()', offerDelay);
		},
		setPTFavorite: function(content_id, type, act, adddiv, removediv) {
			var mainpage = 0;
			if (act == "deletemain") {
				act = "delete";
				mainpage = 1;
			}
			$(adddiv).hide();
			$('fav-loading').show();
			var params = {
				content_id: content_id,
				type: type,
				action: act
			}
//			var params = "content_id=" + content_id + "&type=" + type + "&action=" + act;	
			new Ajax.Request('/ajax/process_favorite.php',   
				{
					method:'get', 
					parameters: params,
					onSuccess: function(transport){
						var response = transport.responseText || "no response text";
						var respjson = response.evalJSON();
						if (respjson.error_code == "no-error") {
							$('fav-loading').hide();
							if (mainpage == 0) {
								$(removediv).show();
							} else {
								$(removediv).hide();
							}
						} else {
							$('fav-error').innerHTML += 'Hiba a művelet során: ' + respjson.error_code;
						}	
			     	}
			     	,
			     	onFailure: function(){ 
						$('fav-error').innerHTML += "Hiba a művelet során: Ajax hiba";
			     	}   
		     });
		},
		setFavorite: function(content_id, type, act, adddiv, removediv) {
			var mainpage = 0;
			if (act == "deletemain") {
				act = "delete";
				mainpage = 1;
			}
			$("#" + adddiv).hide();
			$('#fav-loading').show();
			var params = {
				content_id: content_id,
				type: type,
				action: act
			}
			$.getJSON('/ajax/process_favorite.php', params,    
				function(data)		{
					if (data.error_code == "no-error") {
						$('#fav-loading').hide();
						if (mainpage == 0) {
							$("#" + removediv).show();
						} else {
							$("#" + removediv).hide();
						}
					} else {
						$('#fav-error').html("Hiba a művelet során: " + respjson.error_code);
					}	
	      });
		},
		changeFavorite: function(favclass, favid) {
			$(".fav-item").css("display", "none");
			$(favclass).css("display", "block");
			$("#menu1").css("backgroundPosition", "0px 0px");
			$("#menu2").css("backgroundPosition", "-37px 0px");
			$("#menu3").css("backgroundPosition", "-74px 0px");
			$("#menu4").css("backgroundPosition", "-107px 0px");
			$("#menu5").css("backgroundPosition", "-143px 0px");
			$("#menu6").css("backgroundPosition", "-184px 0px");
			if (favid == "#menu1") {
				$(favid).css("backgroundPosition", "0px -33px");
			 	return;	
			}
			if (favid == "#menu2") {
				$(favid).css("backgroundPosition", "-37px -33px");
			 	return;	
			}
			if (favid == "#menu3") {
				$(favid).css("backgroundPosition", "-74px -33px");
			 	return;	
			}
			if (favid == "#menu4") {
				$(favid).css("backgroundPosition", "-107px -33px");
			 	return;	
			}
			if (favid == "#menu5") {
				$(favid).css("backgroundPosition", "-143px -33px");
			 	return;	
			}
			if (favid == "#menu6") {
				$(favid).css("backgroundPosition", "-184px -33px");
			 	return;	
			}
		},
		initFavorite: function() {
			favpe = $('.fav-PE');
			if (favpe.length > 0) {
				favpe.css("display","block");
				$('#menu1').css("backgroundPosition", "0px -33px");
				return;	
			}
			favpe = $('.fav-TR');
			if (i == favpe.length-1) {
				favpe.css("display","block");
				$('#menu2').css("backgroundPosition", "0px -33px");
			}
			favpe = $('.fav-BL');
			if (i == favpe.length-1) {
				favpe.css("display","block");
				$('#menu3').css("backgroundPosition", "0px -33px");
			}
			favpe = $('.fav-TO');
			if (i == favpe.length-1) {
				favpe.css("display","block");
				$('#menu4').css("backgroundPosition", "0px -33px");
			}
			favpe = $('.fav-AL');
			if (i == favpe.length-1) {
				favpe.css("display","block");
				$('#menu5').css("backgroundPosition", "0px -33px");
			}
			favpe = $('.fav-AR');
			if (i == favpe.length-1) {
				favpe.css("display","block");
				$('#menu6').css("backgroundPosition", "0px -33px");
			}
		},
		deleteTT: function(tt_id) {
			var url = "/index.php?page_id=15&amp;page_type=T&amp;action=delete&amp;id="+tt_id;
			var answer = confirm ("Biztos törölni akarod ezt az edzéstípust?");
			if (answer) window.location = url;
		},
		adClick: function(event, adlink) {
		if (event.button==2)
		  {
		      alert("You clicked the right mouse button!")
		  }
		else
		  {
		  alert("You clicked the left mouse button!")
		  }			
			var params = {
				adurl: adlink.href
			}
			location.href=adlink.href;

			alert(adlink.href);

		}
	}	
}();
