Event.observe(window,'load',function(){
	manageLinks();
})

var an = new Array();

function setMenuLinks(href) {
	var an = document.getElementsByTagName('a');
	for (var i=0; i < an.length; i++) {
		if (an[i].className && an[i].className.match('menu_')) {
			an[i].className = 'menu_a';
		}
	}
	var parts = href.split('/');
	if (!parts[1]) $('m_0').className = 'menu_a_act';
	else if (parts[1] == 'cikkek' && $('m_' + parts[2])) $('m_' + parts[2]).className = 'menu_a_act';
	else if ($('m_' + parts[1])) $('m_' + parts[1]).className = 'menu_a_act';
}

function manageLinks() {
	var as = document.getElementsByTagName('a');
	for (var i=0; i < as.length; i++) {
		an.push(as[i]);
	}
	for (var i=0; i < an.length; i++) {
		Event.observe(an[i],'click',function(e){
			var obj = Event.element(e);
			if (obj.src) obj = obj.parentNode;
			var bhref = obj.href;
			var target = (obj.target != '') ? true : false;
			if (!obj.className.match('link') && !bhref.match('.mp3') && !bhref.match('mailto') && !target) {
				Event.stop(e);
				href = bhref.replace('http://' + sn, '');
				setMenuLinks(href);
				//$('player_link').href = bhref;
				var hash = href.replace(p,'');
				hash = (hash == '') ? 'fooldal' : hash;
				window.location.hash = hash;
				$('head_loader').style.display = 'block';
				$('main').scrollTo();
				var ajaxreq =	new Ajax.Request (
									bhref,
									{ 
										method: 'post',
										parameters: 'ajax=true',
										onComplete: setContent,
										onFailure: requestFail
									}
								);
			}
		});
	}
}

function sendMfrm(link) {
	//alert(link);
	var ajaxreq =	new Ajax.Request (
						link,
						{ 
							method: 'post',
							parameters: 'ajax=true&' + $('mfrm').serialize(),
							onComplete: setContent,
							onFailure: requestFail
						}
					);
}

function sendBfrm(link) {
	var ajaxreq =	new Ajax.Request (
						link,
						{ 
							method: 'post',
							parameters: 'ajax=true&' + $('bfrm').serialize(),
							onComplete: setContent,
							onFailure: requestFail
						}
					);
}

function sendComment(link) {
	var action = $('commentfrm').action;
	var ajaxreq =	new Ajax.Request (
						action,
						{ 
							method: 'post',
							parameters: 'ajax=true&' + $('commentfrm').serialize(),
							onComplete: setContent,
							onFailure: requestFail
						}
					);
}

function sendLikeData() {
	var action = $('likefrm').action;
	var ajaxreq =	new Ajax.Request (
						action,
						{ 
							method: 'post',
							parameters: 'ajax=true&' + $('likefrm').serialize(),
							onComplete: setContent,
							onFailure: requestFail
						}
					);
}

function sendRegType(num) {
	var action = $('regtypefrm' + num).action;
	var ajaxreq =	new Ajax.Request (
						action,
						{ 
							method: 'post',
							parameters: 'ajax=true&' + $('regtypefrm' + num).serialize(),
							onComplete: setContent,
							onFailure: requestFail
						}
					);
}


function delProfImage() {
	var action = $('delpic').action;
	var ajaxreq =	new Ajax.Request (
						action,
						{ 
							method: 'post',
							parameters: 'ajax=true&' + $('delpic').serialize(),
							onComplete: setContent,
							onFailure: requestFail
						}
					);
}

function delProfLike() {
	var action = $('dellikeband').action;
	var ajaxreq =	new Ajax.Request (
						action,
						{ 
							method: 'post',
							parameters: 'ajax=true&' + $('dellikeband').serialize(),
							onComplete: setContent,
							onFailure: requestFail
						}
					);
}

function delBandImage() {
	var action = $('delbpic').action;
	var ajaxreq =	new Ajax.Request (
						action,
						{ 
							method: 'post',
							parameters: 'ajax=true&' + $('delbpic').serialize(),
							onComplete: setContent,
							onFailure: requestFail
						}
					);
}

function delProfMusic(num) {
	var action = $('delbmusic').action;
	$('delbmusic').elements['bandMusicNum'].value = num;
	var ajaxreq =	new Ajax.Request (
						action,
						{ 
							method: 'post',
							parameters: 'ajax=true&' + $('delbmusic').serialize(),
							onComplete: setContent,
							onFailure: requestFail
						}
					);
}

function delConcert(id) {
	var action = $('delconcert').action;
	$('delconcert').elements['concertid'].value = id;
	var ajaxreq =	new Ajax.Request (
						action,
						{ 
							method: 'post',
							parameters: 'ajax=true&' + $('delconcert').serialize(),
							onComplete: setContent,
							onFailure: requestFail
						}
					);
}

function requestFail(r) {
	alert(r.status);
}

function setContent(r) {
	//alert(r.responseText);
	$('head_loader').style.display = 'none';
	$('right').innerHTML = r.responseText;
	
	if ($('page_title')) document.title = $('page_title').innerHTML;
	else document.title = t;
	for (var i=0; i < an.length; i++) {
		$(an[i]).stopObserving();
	}
	an.clear();
	manageLinks();
}


function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

var wwidth = f_clientWidth();
var wheight = f_clientHeight();

var img = new Image();

function plotImage(i) {
	var dimp = $('popup').getDimensions();
	var l = Math.ceil((wwidth - dimp.width) / 2);
	var t = Math.ceil((wheight - dimp.height) / 2);
	$('popup').setStyle({left : l + 'px', top : t + 'px'});
	$('overlay').style.display = 'block';
	$('popup').style.display = 'block';
	img.src = i;
	if (img.width > 0) putImage(i); else waitForImage(i);
}

function waitForImage(i) {
	if (img.width > 0) putImage(i); else setTimeout('waitForImage(\'' + i + '\')',500);
}

function putImage(i) {
	var w = img.width;
	var h = img.height;
	if (w > wwidth - 100) {
		var tw = wwidth - 100;
		var th = Math.ceil(tw/w*h);
	} else {
		var tw = w;
		var th = h;
	}
	if (th > wheight - 50) {
		var oh = wheight - 50;
		var ow = Math.ceil(oh/th*tw);
		th = oh; tw = ow;
	}
	$('popup').setStyle({width : tw + 'px', height : (th + 20) + 'px'});
	var l = Math.ceil((wwidth - tw) / 2);
	var t = Math.ceil((wheight - th - 20) / 2);
	$('popup').setStyle({left : l + 'px', top : t + 'px'});
	$('popup_img').setStyle({width : tw + 'px', height : th + 'px'});
	$('popup_img').innerHTML = '<img src="' + i + '" style="width : ' + tw + 'px; height : ' + th + 'px; margin : 0px; padding : 0px; cursor : pointer" onclick="closePopup()">';
}

function plotVideo(code) {
	$('popup_img').innerHTML = $('code_' + code).innerHTML;
	var dims = $w($('dim_' + code).innerHTML);
	if (parseInt(dims[0]) > 0 && parseInt(dims[1]) > 0) {
		var dh = parseInt(dims[1]) + 20;
		$('popup_img').setStyle({width : dims[0] + 'px', height : dims[1] + 'px'});
		$('popup').setStyle({width : dims[0] + 'px', height : dh + 'px' });
	}
	var dimp = $('popup').getDimensions();
	var l = Math.ceil((wwidth - dimp.width) / 2);
	var t = Math.ceil((wheight - dimp.height) / 2);
	$('popup').setStyle({left : l + 'px', top : t + 'px'});
	$('overlay').style.display = 'block';
	$('popup').style.display = 'block';
}

function closePopup() {
	$('overlay').style.display = 'none';
	$('popup').style.display = 'none';
	$('popup').setStyle({width : '200px', height : '220px'});
	$('popup_img').innerHTML = '';
	$('popup_img').setStyle({width : '200px', height : '200px'});
	img = new Image();
}

//Cookie függvények
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return decodeURI(c.substring(nameEQ.length,c.length));
	}
	return null;
}


function deleteCookie(name) {
	createCookie(name,"",-1);
}

function issetCookie(name) {
	if (readCookie(name) != null) return true; else return false;
}

function emailCheck(s) {
	var reg = /^[0-9a-z\.-]+@([0-9a-z-]+\.)+[a-z]{2,4}$/i;
	if(!reg.test(s)) {
		return false;
	};
	return true;
}

function numControl(obj) {
	var nums = '0123456789';
	var v = obj.value;
	var ret = '';
	for (var i = 0; i < v.length; i++) {
		if (nums.indexOf(v.charAt(i)) >= 0) {
			ret += v.charAt(i);
		}
	}
	obj.value = ret;
	return true;
}

function checkRegData() {
	if ($('regfrm').elements['userEmail'].value == '') {
		alert('Nem adtad meg az e-mail címed!');
		$('regfrm').elements['userEmail'].focus();
		return false;
	}
	if (!emailCheck($('regfrm').elements['userEmail'].value)) {
		alert('Hibás az e-mail cím!');
		$('regfrm').elements['userEmail'].focus();
		return false;
	}
	if ($('regfrm').elements['userUserName'].value == '') {
		alert('Nem adtál meg nevet!');
		$('regfrm').elements['userUserName'].focus();
		return false;
	}
	if ($('regfrm').elements['userUserName'].value.length < 5) {
		alert('Túl rövid nevet adtál meg!');
		$('regfrm').elements['userUserName'].focus();
		return false;
	}
	if ($('regfrm').elements['userPassword'].value == '') {
		alert('Nem adtál meg jelszót!');
		$('regfrm').elements['userPassword'].focus();
		return false;
	}
	if ($('regfrm').elements['userPassword'].value != $('regfrm').elements['userPassword1'].value) {
		alert('Nem egyezik a két jelszó!');
		$('regfrm').elements['userPassword'].focus();
		return false;
	}
	if (!$('regfrm').elements['accept'].checked) {
		alert('A felhasználási feltételeket el kell fogadnod!');
		$('regfrm').elements['accept'].focus();
		return false;
	}
	$('regfrm').submit();
}

function checkProfData() {
	if ($('regfrm').elements['userPassword'].value != $('regfrm').elements['userPassword1'].value) {
		alert('Nem egyezik a két jelszó!');
		$('regfrm').elements['userPassword'].focus();
		return false;
	}
	$('regfrm').submit();
}

function checkBandData() {
	if ($('bandfrm').elements['bandContactName'].value == '') {
		alert('Nem adtad meg a kapcsolattartó nevét!');
		$('bandfrm').elements['bandContactName'].focus();
		return false;
	}
	if ($('bandfrm').elements['bandContactEmail'].value == '') {
		alert('Nem adtad meg a kapcsolattartó e-mail címét!');
		$('bandfrm').elements['bandContactEmail'].focus();
		return false;
	}
	if (!emailCheck($('bandfrm').elements['bandContactEmail'].value)) {
		alert('Hibás az e-mail cím!');
		$('bandfrm').elements['bandContactEmail'].focus();
		return false;
	}
	/*if ($('bandfrm').elements['bandContactPhone'].value == '') {
		alert('Nem adtad meg a kapcsolattartó telefonszámát!');
		$('bandfrm').elements['bandContactPhone'].focus();
		return false;
	}*/
	if ($('bandfrm').elements['bandName'].value == '') {
		alert('Nem adtad meg a zenekar nevét!');
		$('bandfrm').elements['bandName'].focus();
		return false;
	}
	$('bandfrm').submit();
}

