window.defaultAlertFuntion = window.alert;

window.defaultAlert = function(message, fn){
	defaultAlertFuntion(message);
	if(fn != undefined){
		eval(fn);
	}
}
window.alert = function(message, fn){
	$.modal(str_replace(message, "\n", '<br/>'), {'onClose':fn});
	$('#simplemodal-container').css('height', ($('#simplemodal-data').height()) + 'px').css('width', ($('#simplemodal-data').width()) + 'px');
}

var Pc365 = {
	User: new Object(),
	Article: new Object(),
	Blog: new Object(),
	Comment: new Object(),
	Topic: new Object(),
	Favorite: new Object()
}


Pc365.FeatureRoller = function (){
	var active = $('ul.features').attr('active');
	if(active == undefined)active = 5;
	active++;
	if(active == 6)active = 1;
	$('ul.features').attr('active', active);
	var li = $('ul.features li').eq(active-1);
	var html = li.find('div.text').eq(0).html();
	$('.featureimage').fadeOut(function(){
		$('.featureimage').css('background-image','url('+li.attr('image')+')');
		$('.featuretext').html(html);
		$('.featureimage').fadeIn(function(){$('.featurebox').css('background-color','#000000');});
	});
	setTimeout('Pc365.FeatureRoller()', 6000);
}

/** User **/
// Login
Pc365.User.Login = function (){
	$.post(
		'/@/action/login.php',
		{login_email:$('#login_email').val(), login_password:$('#login_password').val(), autologin:$('#autologin').is(':checked')},
		function(html){
			switch(html){
				case 'OK':
					document.location.reload();
					break;
				case 'NO_SUCH_USER':
					alert('Nincs ilyen felasználó, ellenőrizd az e-mail címet és a jelszót!');
					break;
				case 'NEW':
					document.location.href = '/profilom';
					break;
				case 'INACTIVE':
					alert('Deaktivált (bannolt?) felhasználó! Sajnáljuk!');
					break;
				default:
					alert('Valami hiba lehet: '.html);
			}
		}
	);
}
// Register
Pc365.User.Register = function (){
	$.post(
		'/@/action/register.php?r=1',
		{email:$('#register_email').val()},
		function(html){
			switch(html){
				case 'OK':
					alert('Köszönjük regisztrációt, e-mailben értesítünk a további teendőidről!');
					closeRegisterBox();
					break;
				case 'WRONG_EMAIL':
					alert('Hibás e-mail cím!');
					break;
				case 'ALREADY_REGISTERED':
					alert('Ez az e-mail cím már regisztrálva van a rendszerünkben!');
					break;
				default:
					alert('Valami hiba lehet: '.html);
			}
		}
	);
}
Pc365.User.ProfileZipPress = function (zip){
	$.post('/@/get/zipCheck.php', {zip:zip}, function(html){$('#zipcode').html(html)});
}
Pc365.User.ProfileNickPress = function(nick){
	$.post('/@/nickCheck.php', {nick:nick}, function(ret){
		if(ret != '*'){
			$('#user_url').css('color', 'inherit');
			$('#user_url').html(ret);
		}else{
			$('#user_url').html('A választott user név már foglalt!');
			$('#user_url').css('color', 'red');
		}
	});
}
/** Article **/
Pc365.Article.Search = function(){
	var szoveg = $('#text').val();
	if(szoveg.length < 4){ alert('A keresendő szöveg nem lehet rövidebb négy karakternél!'); return false; }
	document.location.href = '/archivum/kereses/' + szoveg;//+ '/' + $('#category').val();
}
Pc365.Article.SubmitVote = function(voteId){
	var vote = $("input[@name=vote_" + voteId + "]:checked").val();
	if(vote == undefined || vote < 0){
		alert('És akkor mire is szavazol? Kattints!');
		return;
	}
	$.post('/@/action/vote.php',
		{voteId:voteId, vote:vote},
		function(html){
			document.location.reload();
		}
	);
}
/** Topic **/
Pc365.Topic.Search = function(){
	var text = $('#topicSearchString').val();
	if(text.length >= 3)document.location.href='/forum/kereses/' + text;
	else alert('A keresendő kifejezés rövidebb, mint három katakter!');
}

Pc365.Topic.ShowNewForm = function(){
	$('#newTopicDiv').slideDown('slow');
}

Pc365.Topic.Create = function (){
	var name = $('#newTopicName').val();
	var category = $('#newTopicCategory').val();
	var description = $('#newTopicDescription').val();
	
	if(name == ''){alert('Elfelejtetted megadni a téma címét'); return;}
	if(category == 0){alert('Elfelejtetted megadni a téma kategóriáját'); return;}
	
	$.post('/@/action/newtopic.php',
		{name:name, category:category, description:description},
		function(html){
			var res = html.split(';');
			if(res[0] == 'ok'){
				alert('A fórumtéma létrejött, szólj te hozzá elsőként!');
				document.location.href='/forum/' + res[1];
			}else{
				switch(html){
					case 'datePara':
						alert('Óránként csak egy témát lehet lértehozni!' + "\n" + 'Próbáld késöbb!');
						break;
					case 'namePara':
						alert('Már létezik ilyen nevű téma!');
						break;
					case 'nameLengthPara':
						alert('A téma neve minimum 3 betű, de max 30 betű legyen!');
						break;
					case 'error':
						alert('Valami hiba történt, szólj kérlek egy moderátornak!');
						break;
					default:
						alert('Valami hiba lehet: '.html);
				}
			}
		}
	);
}


/** Comment **/
Pc365.Comment.Add = function (){
	var commentBody = $('#addCommentBody').val();
	var replyId = $('#addCommentReplyId').val();
	var objectClass = $('#commentItemClass').val();
	var objectId = $('#commentItemId').val();
	
	if(commentBody.length == 0){alert('Üres hozzászólást ne küldj!');return;}
	
	$.post(
		'/@/action/addComment.php',
		{commentBody:commentBody, objectClass:objectClass, objectId:objectId, replyId:replyId},
		function(html){
			var res = html.split(';');
			if(res[0] == 'ok'){
				document.location.reload();
				$('#addCommentBody').val('');
			}else if(res[0] == 'datePara'){
				alert('Ne kapkodj, percenként csak egy hozzászólás lehetséges!');
			}else{
				alert('Valami hiba történt... !');
			}
		}
	)
}

Pc365.Comment.Reply = function(commentId, nick_name){
	document.location.href="#writeComment";
	$('#replyText').html('Válasz ' + nick_name + ' üzenetére <a href="#a" onclick="Pc365.Comment.RemoveReply();"><img align="absmiddle" src="/img/delete.png"></a>');
	$('#addCommentReplyId').val(commentId);
	$('#addCommentBody').focus();	
}

Pc365.Comment.RemoveReply = function(){
	$('#replyText').html('');
	$('#addCommentReplyId').val('');
	$('#addCommentBody').focus();
}

Pc365.Comment.ShowReplied = function (commentId, obj){
	$.post(
		'/@/get/comment.php',
		{commentId:commentId, objectClass:$('#commentItemClass').val(), objectId:$('#commentItemId').val()},
		function(html){
			$(obj).parent().html(html);
		}
	);
}

Pc365.Comment.Delete = function(commentId){
	if(confirm('Biztosan törlöd?')){
		$.post('/@/action/deleteComment.php',
			{commentId:commentId, objectClass:$('#commentItemClass').val(), objectId:$('#commentItemId').val()},
			function(html){
				window.defaultAlert('A hozzászólást töröltük!', 'document.location.reload();');
			}
		);
	}
}

Pc365.Comment.ShowModerateForm = function(commentId){
	var objectClass = $('#commentItemClass').val();
	var objectId = $('#commentItemId').val();
	
	$.post('/@/get/moderateform.php',
		{commentId:commentId, objectClass:objectClass, objectId:objectId},
		function(html){
			$.modal(html);
			$('#commentModerateText').markItUp(mySettings);
		}
	);
}

Pc365.Comment.Moderate = function(commentId){
	var text = $('#commentModerateText').val();
	var objectClass = $('#commentItemClass').val();
	var objectId = $('#commentItemId').val();
	
	$.post('/@/action/moderateComment.php',
		{commentId:commentId, objectClass:objectClass, objectId:objectId, text:text},
		function(html){
			if(text){
				defaultAlert('A hozzászólást moderáltuk!', 'document.location.reload();');
			}else{
				defaultAlert('A hozzászólást visszaállítottuk!', 'document.location.reload();');
			}
		}
	);
}

/** Favorite **/
Pc365.Favorite.Add = function (id, entityClass){
	$.post(
		'/@/action/favorite.php',
		{entityId:id, entityClass:entityClass, method:'add'},
		function(html){
			document.location.reload();
		}
	);
}

Pc365.Favorite.Remove = function (id, entityClass){
	$.post(
		'/@/action/favorite.php',
		{entityId:id, entityClass:entityClass, method:'remove'},
		function(html){
			document.location.reload();
		}
	);
}

/** Blog **/
Pc365.Blog.ShowNewForm = function (blogPostId){
	$('#blogTitle').val('');
	$('#blogText').val('');
	$('#status').attr('checked', '');
	$('#itemId').val('');
	$('#editBlog').slideDown('slow');
}

Pc365.Blog.ShowEditForm = function(blogPostId){
	$('#blogTitle').val($('#currentBlogPostTitle').val());
	$('#blogText').val($('#currentBlogPostBody').val());
	if($('#currentBlogPostStatus').val() == 'visible')status = 'checked'; else status = '';
	$('#status').attr('checked', status);
	$('#itemId').val(blogPostId);
	$('#editBlog').slideDown('slow');
}

Pc365.Blog.Delete = function (blogPostId){
	if(confirm('Valóban törölni szeretnéd?')){
		$.post(
			'/@/action/deleteblogpost.php',
			{blogPostId:blogPostId},
			function(html){
				var res = html.split(';');
				if(res[0] == 'ok'){
					document.location.href = '/blog/' + res[1];
				}
			}
		);
	}
}

Pc365.Blog.Submit = function (){
	var title = $('#blogTitle').val();
	var body = $('#blogText').val();
	var itemId = $('#itemId').val();
	if($('#status').attr('checked') == true) status = 'visible';else status = 'hidden';
	
	if(title.length == 0){alert('Nem adtál meg címet a blogbejegyzésnek!');return;}
	if(body.length == 0){alert('Szöveg nélküli blogbejegyzés? Nemá!');return;}
	
	
	$.post(
		'/@/action/blogpost.php?',
		{title:title, body:body, objectId:itemId, status:status},
		function(html){
			var res = html.split(';');
			if(res[0] == 'ok'){
				document.location.href = res[1];
				$('#comment').val('');
			}else if(res[0] == 'datePara'){
				alert('Ne kapkodj, tíz percenként egy blogbejegyzés engedélyezett!');
			}else{
				alert('Nem sikerült létrehozni a blogbejegyzést... valami hiba van!');
			}
		}
	)
}

Pc365.Blog.GetPostsByDate = function (userId, date){
	$.post(
		'/@/get/getblogpostsbydate.php',
		{userId:userId, date:date},
		function(html){
			var res = html.split('##@#@##');
			var content = '<ul>';
			for(var i=0;i<res.length;i++){
				content += '<li>' + res[i] + '</li>';
			}
			content += '</ul>';
			$('#open_' + userId + '_' + date).html('-');
			$('#' + userId + '_' + date).html(content);
		}
	);
}

Pc365.IwiwShare = function() {
    u = location.href;
    t = document.title;
    window.open("http://iwiw.hu/pages/share/share.jsp?u=" + encodeURIComponent(u) + "&t=" + encodeURIComponent(t), "iwiwshare", "toolbar=0,status=0,location=1, width=650,height=600,scrollbars=1");
    return false;
}