/**
 * @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);           								
					}
					if(parseInt(result.chat) == 1)
					{
						LoadChat(result.username, true);
					}
				}
				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) {}*/
	
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-11125952-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
	
}

function LoadFooter()
{
	 var year = "2009,2010";
	 
	 var htmlResult = "<div id=\"footer\"> \
  <p>&copy; " + year + " Sombrenuit.org </p><br /> \
  <p>Best viewed on <a href='http://www.mozilla.com/?from=sfx&amp;uid=0&amp;t=317'><img src='http://sfx-images.mozilla.org/affiliates/Buttons/firefox3/FF3b80x15_square.gif' alt='Spread Firefox Affiliate Button' border='0' /></a></p> \
</div> \
<div style=\"font-size: 0.8em; text-align: center; margin-top: 1.0em; margin-bottom: 1.0em;\">  \
&copy; " + year + " Sombrenuit. All rights reserved. All trademarks are property of their respective owners in the US and other countries. </div>";

	$("#idFooter").html(htmlResult);
}

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

function LoadSM()
{
	soundManager.url = '/lib/jquery/smswf/'; // directory where SM2 .SWFs live
	
	// Note that SoundManager will determine and append the appropriate .SWF file to the URL,
	// eg. /path/to/sm2-flash-movies/soundmanager2.swf automatically.
	
	// Beta-ish HTML5 audio support (force-enabled for iPad), flash-free sound for Safari + Chrome. Enable if you want to try it!
	// soundManager.useHTML5Audio = true;
	
	// do this to skip flash block handling for now. See the flashblock demo when you want to start getting fancy.
	soundManager.useFlashBlock = false;
	
	// disable debug mode after development/testing..
	// soundManager.debugMode = false;
	
	soundManager.flashVersion = 9; // optional: shiny features (default = 8)
	
	// If you prefer to have the library wait for window.onload() before calling soundManager.onload()/onerror() methods, you can modify SM2's "waitForWindowLoad" property:
	soundManager.waitForWindowLoad = true;
	
	soundManager.onready(function(oStatus) 
	{
		// oStatus: object containing "success" parameter
		if (oStatus.success)
		{
			soundManager.createSound(
			{
				id: 'chatPop',
				url: '/sounds/pop.mp3',
				//autoLoad: true,
				//autoPlay: false,
				onload: function() {
				//alert('The sound '+this.sID+' loaded!');
				},
				//volume: 50
			});			
		} 
		else 
		{
			//alert("Problem on sound manager");
		}
	});
}



