/**
 * @author Edo
 */

/*$.postJSON = fuction(url, data, callback) 
{
	$.post(url, data, callback, "json");
};*/

function CheckLogin()
{
	try
	{
		$.getJSON(baseLink + "loginCheck.php" + callbackAttrS, 
		function(JSON)
		{
			/*if(status != "success")
			{
				return false;
			}*/
					
			$.each(JSON.results, 
			function(i, result)
			{
				if(parseInt(result.login) == 1)
				{
					$("#menuright").html("<li><a href=\"blog.html?u=" + result.username + "\">My Page</a></li><li><a href=\"account.html?u=" + result.username + "\">" + result.username + "</a>&nbsp;<a href=\"logout.html\">Logout</a>");
					if($("#idLogin"))
						$("#idLogin").css("display", "none");
					//News Part
					var newsTitle = $("#iAmNews");
					if(newsTitle.length > 0)
					{
						if(result.pri > 1)
						{
							var submitNews = $(".submitNews");
							if(submitNews.length > 0)
								submitNews.html("<form id=\"postform\" method=\"get\" action=\"post.html\"><div><input name=\"type\" type=\"hidden\" value=\"0\"/><input name=\"NewPost\" type=\"submit\" value=\"New Post\"/></div></form>");
							var submitNewsComment = $(".submitNewsComment");						
							if(submitNewsComment.length > 0)
							{
								jQuery.each(submitNewsComment, function(index, value) 
								{
									var arr = value.id.split("_");
									$("#" + value.id).html("<a href=\"post.html?type=1&nid=" + arr[arr.length - 1] + "\">Add Comment</a>&nbsp;");
								});
							}
							var newsEdit = $(".editNews");
							if(newsEdit.length > 0)
							{
								jQuery.each(newsEdit, function(index, value)
								{
									var arr = value.id.split("_");
									$("#" + value.id).html("<a href=\"post.html?type=0&edit=1&nid=" + arr[arr.length - 1] + "\"><img src=\"images/edit.gif\"/ class=\"editImg\"></a>");
								});
							}							
							var newsDelete = $(".deleteNews");
							if(newsDelete.length > 0)
							{
								jQuery.each(newsDelete, function(index, value)
								{
									var arr = value.id.split("_");
									$("#" + value.id).html("<a href=\"\"><img src=\"images/delete.png\"/ class=\"deleteImg\" onclick=\"return DeleteNewsPost(" + arr[arr.length - 1] + ");\"></a>");
								});
							}							
						}
						else if(result.pri > 0)
						{
							var submitNewsComment = $(".submitNewsComment");						
							if(submitNewsComment.length > 0)
							{
								jQuery.each(submitNewsComment, function(index, value) 
								{
									var arr = value.id.split("_");
									$("#" + value.id).html("<a href=\"post.html?type=1&nid=" + arr[arr.length - 1] + "\">Add Comment</a>&nbsp;");
								});
							}
						}					
					}
					//Blog Part
					var blogTitle = $("#iAmBlog");
					if(blogTitle.length > 0)
					{
						var username = $.jqURL.get("u");
						if(username == undefined)
							location.href = "blog.html?u=" + result.username;
						var submitBlog = $(".submitBlog");
						if(submitBlog.length > 0)
							submitBlog.html("<form id=\"postform\" method=\"get\" action=\"post.html\"><div><input name=\"type\" type=\"hidden\" value=\"2\"/><input name=\"NewPost\" type=\"submit\" value=\"New Post\"/></div></form>");
						var submitBlogComment = $(".submitBlogComment");						
						if(submitBlogComment.length > 0)
						{
							jQuery.each(submitBlogComment, function(index, value) 
							{
								var arr = value.id.split("_");
								$("#" + value.id).html("<a href=\"post.html?type=3&u=" + $.jqURL.get("u") + "&pid=" + arr[arr.length - 1] + "\">Add Comment</a>&nbsp;");
							});
						}
						var username = $.jqURL.get("u");
						if(username != undefined)
						{			
							if(result.username.toLowerCase() == username.toLowerCase())
							{
								var blogEdit = $(".editBlog");
								if(blogEdit.length > 0)
								{
									jQuery.each(blogEdit, function(index, value)
									{
										var arr = value.id.split("_");
										$("#" + value.id).html("<a href=\"post.html?type=2&edit=1&pid=" + arr[arr.length - 1] + "\"><img src=\"images/edit.gif\"/ class=\"editImg\"></a>");
									});
								}
								var blogDelete = $(".deleteBlog");
								if(blogDelete.length > 0)
								{
									jQuery.each(blogDelete, function(index, value)
									{
										var arr = value.id.split("_");
										$("#" + value.id).html("<a href=\"\"><img src=\"images/delete.png\"/ class=\"deleteImg\" onclick=\"return DeleteBlogPost(" + arr[arr.length - 1] + ");\"></a>");
									});
								}
							}
						}							
					}	
					//Account Info Part
					var accountInfoInput = $(".editAccountInput");		
					if(accountInfoInput.length > 0)
					{
						jQuery.each(accountInfoInput, function(index, value) 
						{
							$("#" + value.id).removeAttr("readonly");
						});
					}
					var accountPost = $("#accountPost");
					if(accountPost)
					{
						var accountPostHtml = "<input type=\"submit\" name=\"Submit\" value=\"Submit\" class=\"button\" />";
						accountPostHtml += "<input type=\"reset\" value=\"Reset\" class=\"button\" onclick=\"confirm_reset();\"/>";
						
						accountPost.html(accountPostHtml);           								
					}
				}
				else
				{
					$("#menuright").html("<li><a href=\"login.html\">Login</a></li>");
					$("#passwordCheck").attr("value", "js");
					//Post
					var postCheck = $("#idPostErrorDiv");
					if(postCheck.length > 0)
					{
						location.href = "index.html";
					}
					
					//Captcha
					if(parseInt(result.captcha) > 2)
					{
						$("#idCaptcha").css("display", "");
						$("#idCaptchaReturn").css("display", "");
						$("#idCaptchaImg").attr("src", baseLink + "random.php");
					}					
				}
		  	});
		});
	}
	catch(ex)
	{	
	}
}

function LimitText(limitField, limitNum) 
{
    if(limitField.value.length > limitNum) 
	{
        limitField.value = limitField.value.substring(0, limitNum);
    } 
}

function TextWrap(textField)
{
	var colAtt = parseInt(textField.getAttribute("cols"));
	
	var text = "";
	for(var i = 0; i < textField.value.length; i++)
	{
		if(i >= colAtt)
		{
			var temp = i % colAtt;
			var charInfo = textField.value[i];
			if(temp == 0 && charInfo != "\n")
			{
				var found = 0;
				var tempText = "";
				for(var j = text.length; j != 0; j--)
				{
					if(text[j] == " ")
					{
						found = 1;
						j--;
						tempText = text.substring(0, j);
						tempText += "\n";
						j++;
						for(var k = j; k < text.length; k++)
						{
							tempText += text[k];
						}
					}
				}
				if(found == 0)
				{
					text += textField.value[i];
				}
			}
			else
				text += textField.value[i];
		}
		else
			text += textField.value[i];
	}
	
	textField.value = text;
}

function Analytics()
{
	/*var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
	try
	{
		var pageTracker = _gat._getTracker("UA-11125952-1");
		pageTracker._trackPageview();
	} catch(err) {}	*/
}

$(function() {
    $('a[href$="url="]')    // all links whose href ends in "url="
        .each(function(i, el) {
            this.href += escape(document.location.href);
        })
    ;
});


