$().ready(function() {
	// napok max. számának összehangolása a kiválasztott hónappal
	setDaysOfMonth();
	/*
	if(!jQuery().vCenter) {
		console.log('novcenter')
		//$.getScript("/js/jquery.vcenter.js");
		$('<script type="text/javascript" src="/js/jquery.vcenter.js"></script>').appendTo('head');

	} else {
				console.log('vcenter')
	}
	*/
	$().ajaxStart(function() {
		//$('div[id^=grafikon]').fadeTo("slow", 0.33);
		$('<div id="loading_ajax" style="position: absolute; top: 500px; left: 500px; width: 300px; height: 200px;"><div style="z-index: 10000;"><img src="/pic/ajax-loader.gif" alt="betöltés" /></div></div>').appendTo($('.content_side'));
	});
	$().ajaxStop(function() {
		$('#loading_ajax').remove();
		//$('div[id^=grafikon]').fadeTo("slow", 1.00);
	});
});	// EOR

function setDaysOfMonth() {
	var settings={
		yearFromID: '#selectYearFrom',
		monthFromID: '#selectMonthFrom',
		dayFromID: '#selectDayFrom',
		yearToID: '#selectYearTo',
		monthToID: '#selectMonthTo',
		dayToID: '#selectDayTo'
	}
	var settings2={
		yearFromID: '#selectYearFrom2',
		monthFromID: '#selectMonthFrom2',
		dayFromID: '#selectDayFrom2',
		yearToID: '#selectYearTo2',
		monthToID: '#selectMonthTo2',
		dayToID: '#selectDayTo2'
	}
	//settings=$.extend(settings,options || {});
	if ($(settings.yearFromID).length) {
		$(settings.yearFromID+','+settings.monthFromID).bind('change', function() {
			alignDaysToMonth(settings.yearFromID,settings.monthFromID,settings.dayFromID,true);
		});
	}
	if ($(settings.yearToID).length) {
		$(settings.yearToID+','+settings.monthToID).bind('change', function() {
			alignDaysToMonth(settings.yearToID,settings.monthToID,settings.dayToID,true);
		});
	}
	if ($(settings2.yearFromID).length) {
		$(settings2.yearFromID+','+settings2.monthFromID).bind('change', function() {
			alignDaysToMonth(settings2.yearFromID,settings2.monthFromID,settings2.dayFromID,true);
		});
	}
	if ($(settings2.yearToID).length) {
		$(settings2.yearToID+','+settings2.monthToID).bind('change', function() {
			alignDaysToMonth(settings2.yearToID,settings2.monthToID,settings2.dayToID,true);
		});
	}
}

/**
 * @description hozzáigazítja a napok számát a kiválasztott hónaphoz, ha mindkettő külön legördülőben van
 * @param year,month,day (select-field IDs)
 * @author zizi@fabrik.hu
 */
function alignDaysToMonth(fieldIDYear,fieldIDMonth,fieldIDDay,startDummy) {
	var selectedMonth=$(fieldIDMonth).val();
	var maxDays;
	switch (selectedMonth) {
		case '4':
		case '6':
		case '9':
		case '11':
			maxDays=30; break;
		case '2':
			var selectedYear=parseInt($(fieldIDYear).val());
			maxDays=((selectedYear%4==0 && selectedYear%400!=0)? 29: 28); break;	// szökőév
		default:
			maxDays=31;
	}
	var arr=new Array();
	if (startDummy) {
		arr.push('<option value=""></option>');
	}
	for(var i=1; i<=maxDays; i++) {
		arr.push('<option value="'+i+'">'+i+'</option>');
	}
	$(fieldIDDay).html(arr.toString());	
}	// EOF alignDaysToMonth

/**
 * @description grafikon betöltés
 */
function loadProfileGraph(container,params) {
	$.ajax({
		type: 'post',
		url: "/.scripts/ajaxContent/ajaxGrafikon.php",
		data: params,
		dataType: 'html',
		success: function(responseHTML) {
			$('#'+container).html(responseHTML);
			if (params.match(/size=big/)) {		// elvileg mindig igaz :-)
				$('#'+container+'_legend').html('').html($('table#'+container+'_legend_table'));
				$('#'+container+'_legend_table').addClass('tablesorter').tablesorter();
				if (container=='grafikon_also') {
					$('#grafikon_also_legend_table th.header:nth(1)').trigger('click');	// default sort: dátum szerint
				}
			}
		}
	});		
}	// EOF loadProfileGraph

/**
 * @description grafikon lekérdezések beállítása profil és saját grafikon oldalakra
 */
function setProfileGraphs(user_id) {
	// 7 és 30 napos linkek
	$('a[id^="grafikon_"]').bind('click',function(e) {
		e.preventDefault();
		var clicked_link=$(this).attr('id');
		var x=clicked_link.split('_');
		var selected_link=x[2];
		if (x[1]=='felso') {
			loadProfileGraph('grafikon_felso','todo=profil_scatter&size=big&user_id='+user_id+'&filter='+selected_link);
		} else {
			loadProfileGraph('grafikon_also','todo=profil_line_user_datum_szer&size=big&user_id='+user_id+'&filter='+selected_link+'&'+$('#formGrafikonMasokAlso').serialize());
		}
	});

	$('#formGrafikonMasokFelso').submit(function(e) {
		e.preventDefault();
		loadProfileGraph('grafikon_felso','todo=profil_scatter&size=big&user_id='+user_id+'&filter=datum_felso&'+$(this).serialize());
	});
	$('#formGrafikonMasokAlso').submit(function(e) {
		e.preventDefault();
		loadProfileGraph('grafikon_also','todo=profil_line_user_datum_szer&size=big&user_id='+user_id+'&filter=datum_also&'+$(this).serialize());
	});
}	// EOF setProfileGraphs

	
/**
 * @description set Lightbox gallery
 */
function setLightboxGallery(dummyLayerID) {
	var dummyLayer=((dummyLayerID)? dummyLayerID: '#galeria_lista');
	// 		<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/MzJe0b0qxms&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/MzJe0b0qxms&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>
	$('a[rel^="gallery_"]').bind('click', function(e) {
		e.preventDefault();
		var gallery_id=$(this).attr('rel');
		$.ajax({
			type: 'post',
			url: "/.scripts/ajaxContent/entudatomHandler.php",
			data: 'todo=gallery_pictures&gallery_id='+gallery_id,
			dataType: 'html',
			success: function(responseHTML) {
				if ($('div#response').size()==0) {
					$('<div id="response" style="display: none"></div>').appendTo(dummyLayerID);
				}
				$('#response').html(responseHTML);
				$('a[rel*="lightbox"]','#response').lightBox( {
					imageLoading:	'/pic/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
					imageBtnPrev:	'/pic/lightbox-btn-elozo.gif',			// (string) Path and the name of the prev button image
					imageBtnNext:	'/pic/lightbox-btn-kovetkezo.gif',			// (string) Path and the name of the next button image
					imageBtnClose:	'/pic/lightbox-btn-bezar.gif',		// (string) Path and the name of the close btn
					imageBlank:		'/pic/lightbox-blank.gif',			// (string) Path and the name of a blank image (one pixel)
					txtImage:		'',	// (string) Specify text "Image"
					txtOf:			'/'
				} );
				$('a[rel*="lightbox"]:first','#response').trigger('click');
			}
		});
	});
	$('a[rel^="video_"]').bind('click', function(e) {
		e.preventDefault();
		var video_id=$(this).attr('rel');
		$.ajax({
			type: 'post',
			url: "/.scripts/ajaxContent/entudatomHandler.php",
			data: 'todo=gallery_video&video_id='+video_id,
			dataType: 'html',
			success: function(responseHTML) {
				if ($('div#response_video').size()==0) {
					//$('<div id="response_video" style="display: none"></div>').appendTo(dummyLayerID);
					$('<div id="response_video" style="display: block;margin-left:100px"></div>').insertBefore(dummyLayerID);
				}
				$('#response_video').html(responseHTML);
				window.location.href='#top';
				/*
				$('a[rel*="video"]','#response_video').lightBox( {
					imageLoading:	'/pic/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
					imageBtnPrev:	'/pic/lightbox-btn-elozo.gif',			// (string) Path and the name of the prev button image
					imageBtnNext:	'/pic/lightbox-btn-kovetkezo.gif',			// (string) Path and the name of the next button image
					imageBtnClose:	'/pic/lightbox-btn-bezar.gif',		// (string) Path and the name of the close btn
					imageBlank:		'/pic/lightbox-blank.gif',			// (string) Path and the name of a blank image (one pixel)
					txtImage:		'',	// (string) Specify text "Image"
					txtOf:			'/'
				} );
				$('a[rel*="video"]:first','#response_video').trigger('click');
				*/
			}
		});
	});
}	// EOF setLightboxGallery

/**
 * @description error message box
 */
function showErrorMessageBox(idParent,aErrorMessages) {
	var box='<div class="bgr_top"><div class="bgr"></div></div>';
	box+='<div class="bgr_hiba"><div class="bgr"></div></div>';
	box+='<div class="bgr_all"><div class="bgr">';
	//box+='<div class="bezar"><a href="javascript:void(0)">Bezár</a></div>';
	box+='<div class="msg_cont">';
	box+='<h4>Hiba!</h4>';
	box+='<ul>';
	for (var i=0;i < aErrorMessages.length;i++) {
		box+='<li>'+aErrorMessages[i]+'</li>';
	}
	box+='</ul>';
	box+='</div>';
	box+='</div></div>';
	box+='<div class="bgr_bot"><div class="bgr"></div></div>';
	$(idParent).empty().append(box).removeClass().addClass('errorMessageBox').show();
}	// EOF showErrorMessageBox

/**
 * @description check numeric values on blur and show errmesbox, if invalid
 */
function checkIsNumeric(element,aErrorMessages) {
	var ret=$(element).val().match(/^\d+$/);
	if (!ret) {
		showErrorMessageBox('#messageBox',aErrorMessages);
	} else {
		$('#messageBox').hide();;
	}
	return ret;
}	// EOF checkIsNumeric

/**
 * @description get cursor position
 * 
 */
function getCursorPosition(e) {
	e = e || window.event;
	var cursor = {x:0, y:0};
	if (e.pageX || e.pageY) {
		cursor.x = e.pageX;
		cursor.y = e.pageY;
	} else {
		cursor.x = e.clientX +
		(document.documentElement.scrollLeft ||
		document.body.scrollLeft) -
		document.documentElement.clientLeft;
		cursor.y = e.clientY +
		(document.documentElement.scrollTop ||
		document.body.scrollTop) -
		document.documentElement.clientTop;
	}
	return cursor;
}	// EOF getPosition

/**
 * @description reset form fields jQuery extension
 */
$.fn.extend({ reset: function() {
	return this.each(function() {
		$(this).is('form') && this.reset();
	})
} });

/**
 * @description show todo layer
 * @param array
 * @author zizi@fabrik.hu 
 * 
 */
function initTodoScreen() {
/*
	var layer='<div id="showTodo" style="border: 1px solid red; width: 500px; height: 150px; position: absolute; top: 0; right: 0; background-color: grey">'+arr.join('<br>')+'</div>';
	$('#container').after(layer);
	$('#showTodo').click(function() {
		$(this).hide();
	});
*/
	var sender='zizi';
	
	var todoLayer='<div id="todo_list_container" style="border: 1px solid red; padding: 5px; width: 500px; height: 350px; overflow: auto; position: absolute; top: 5px; right: 5px; background-color: grey; z-index: 1000">';
	todoLayer+='<div style="font-size: 12px; font-weight: bold">Todo list</div>';
	todoLayer+='<form name="formTodo" id="formTodo" method="post" action="#">';
	todoLayer+='<strong>feladó: </strong><span style="margin-left: 9px"><input type="text" name="sender" id="sender" value=""></span>';
	todoLayer+='<span style="margin-left: 17px"><strong>címzett: </strong></span>';
	todoLayer+='<span style="margin-left: 10px"><input type="text" name="responsible" id="responsible" value=""></span>';
	todoLayer+='<span id="close_todo_list" style="margin-left: 15px">BEZÁR</span><br />';
	todoLayer+='<strong>url: </strong><span style="margin-left: 28px"><input type="text" name="url" id="url" value="'+window.location.href+'" style="width: 360px"></span>';
	todoLayer+='<span id="zoom" style="margin-left: 15px">NAGYÍT</span><br />';
	todoLayer+='<textarea name="todo_text" id="todo_text" style="width: 409px; height: 40px;"></textarea>';
	todoLayer+='<input type="button" name="button_todo" id="button_todo" value="mehet" style="margin-left: 8px; margin-bottom: 5px;" />';
	todoLayer+='szűrés listázáshoz: <input type="text" name="szures" id="szures" value="" style="margin-left: 8px; margin-bottom: 5px;" />';
	todoLayer+='<input type="button" name="button_todo_list" id="button_todo_list" value="lista" style="margin-left: 8px" />';
	todoLayer+='</form>';
	todoLayer+='<div id="todo_list"></div>';
	todoLayer+='</div>';
	$('#container').after(todoLayer);
	$('#close_todo_list').click(function() {
		$('#todo_list_container').hide();
	});
	$('#zoom').click(function() {
		$('#todo_list_container').css('height','700px');
	});

	
	$('#todo_list_container').draggable();
	// current content
	$.ajax({
		type: 'get',
		url: "/.scripts/ajaxContent/ajaxEditTodo.php",
		data: 'doit=todo',
		dataType: 'html',
		success: afterTodo
	});
	// toggling container - ez itt nincs
	$("#toggle_todo_list")
		.html('Open todo window')
		.toggle(
			function() {
				$('#todo_list_container').show();
				$(this).html('Close todo window');
				return false;
			},
			function() {
				$('#todo_list_container').hide();
				$(this).html('Open todo window');
				return false;
			});
	// insert todo
	$('#button_todo').click(function() {
		if ($('#todo_text').val()!='') {
			$.ajax({
				type: 'get',
				url: "/.scripts/ajaxContent/ajaxEditTodo.php",
				data: 'doit=todo&insert=yes&'+$('#formTodo').serialize(),
				dataType: 'html',
				success: afterTodo
			});
			$('#todo_text').val('');
		}
	});
	// list todo
	$('#button_todo_list').click(function() {
		$.ajax({
			type: 'get',
			url: "/.scripts/ajaxContent/ajaxEditTodo.php",
			data: 'doit=todo&list=yes&'+$('#formTodo').serialize(),
			dataType: 'html',
			success: afterTodo
		});
	});
}	// EOF initTodoScreen

function afterTodo(responseHtml) {
	$('#todo_list').html(responseHtml);
	$('#todo_list tr:odd').css('background-color','#e8e7e6');
	$('#todo_list tr:even').css('background-color','#fefc55');
	$(".link_todo_delete").click(function(e) {
		e.preventDefault();
		var clicked_id=$(this).attr('id');
		var clicked=clicked_id.replace('link_todo_delete_','');
		$.ajax({
			type: 'get',
			url: "/.scripts/ajaxContent/ajaxEditTodo.php",
			data: 'doit=todo&delete='+clicked,
			dataType: 'html',
			success: afterTodo
		});
	});
}	// EOF afterTodo

$().ready(function() {
	// Hehe.
	if ( window.addEventListener ) {
		//var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
		var kkeys = [], konami = "38,40,37,39,84,79,68,79";	// up,down,left,right,todo
		window.addEventListener("keydown", function(e){
			kkeys.push( e.keyCode );
			if ( kkeys.toString().indexOf( konami ) >= 0 ) {
				//window.location = "http://ejohn.org/apps/hero/";
				initTodoScreen();
				kkeys=[];
			}
		}, true);
	}
	
	// todo
	//initTodoScreen();

}); 	// EOR


