$(document).ready(function() {


	$('h1').corner("top 8px")
	$('.rounded').corner('8px')
	$('.roundedTop').corner('top 8px')
	$('#userBar').corner('bevel bottom')


		/*
		$(".content").hide();//hide tab stuff
		var content_show = $('.active').attr("title");
		$("#"+content_show).show();

		//clicked
		$("a.tab").click(function () {
			$(".active").removeClass("active");// switch all tabs off			
			$(this).addClass("active");// switch this tab on
			$(".content").hide();// hide tab content
			var content_show = $(this).attr("title");//get the content id
			$("#"+content_show).show();//show the content
		});

//
*/



	$('.expandLink').html('<img src="'+siteURL+'public/expand.png" alt="" title="Expand"> ');
	$('.expand').hide();
		$('.expandLink').click(function() {
			$(this).parent().next().slideToggle();
		});


	$(".notify").hover(
		function () {
			$(this).next('span').text($(this).next('span').attr('title'));
		}, function () {
			$(this).next('span').text(' ');
		}
	);


//Colour Rows
	$('.row').filter(':odd').addClass('row-one').removeClass('row').end();


//registration form
	if ($("#searchFormField").length > 0) {
		$("#searchFormField").keypress(function() {
			$("#searchResults").load('/user/search/'+$("#searchFormField").val(), null, function(){
				$("#searchResultList li").click(function() {
					var username = $(this).text();

					if (confirm('Are you sure that '+username+' is the user who referred you?')) { 
						$('#searchFormField').val(username);
						$('#searchFormField').val($(this).text());
						$('#resultId').val(this.id);
						$("#searchResults").empty();
						$('#searchFormField').attr('readonly', true);
					}

				});
			});
		});
	}


//Toggle Email Subscription Alerts
	if ($("a.toggle-email-alerts").length > 0) {
		$("a.toggle-email-alerts").click(function() {
			var id = this.id;
			$("#"+id).text('....working....');
			$.ajax({
				url: siteURL+'subscription/change-alert/'+id,
				type: 'GET',
				timeout: 1000,
				dataType: 'text', 
				error: function() {alert('Failed to Update.');},
				success: function(text) {$("#"+id).text(text);}
			})
		});
	}


//Poll Voting
	if ($(".submit_vote").length > 0) {
		$(".submit_vote").click(function() {
			var id = this.id;
			$.ajax({
				url: siteURL+'poll/ajax_vote/'+id,
				type: 'POST',
				timeout: 1000,
				dataType: 'html', 
				data: {pollValue: $('input[name=option_'+id+']:checked').val()}, 
				error: function() {alert('Could not vote on poll.');},
				success: function(html) {
					$('#poll_results_'+id).fadeOut('slow', function(){
        				$('#poll_results_'+id).html(html);
						$('#poll_results_'+id).fadeIn("slow");}
  					); 
				}
			})
		});
	}


//Expand/Collapse Div

/*if ($('span.expandLink')) {
		$("div.expand").hide();
		$("span.expandLink").click(function() {
			$("div.expand").slideToggle('normal');
		});
	}*/


	//if ( $('#threadView') ) {
	ArmPostFunctions();

		function ArmPostFunctions() {


			//Quoting A Post
				if ($("a.quote-click").length > 0) {
					$("a.quote-click").click(function() {
						var id = this.id;
						$.ajax({
						url: siteURL+'post/ajax_quote/'+id,
						type: 'GET',
						timeout: 1000,
						dataType: 'html', 
						error: function() {alert('Post could not be quoted.');},
						success: function(text) {var text = $("#postTextArea").val()+text+"\n\n"; $("#postTextArea").val(text); $.scrollTo($('a#last'), 2000);}
						})
					});
				}


			//Delete Post
				if ($("a.DeletePost").length > 0) {
					$("a.DeletePost").click(function() {

						var id = this.id.split('_')[1];

						if (confirm('Are you sure you want to delete this?')) {
							$.ajax({
							url: siteURL+'post/ajax_delete/'+id,
							type: 'GET',
							timeout: 1000,
							dataType: 'html', 
							error: function() {alert('Post WAS NOT deleted, please try again in 5 seconds.');},
							success: function(text) {
								$('#row_post_'+id).remove(); 
								$('div.row-one').removeClass('row-one').addClass('row'); 
								$('div.row').filter(':odd').addClass('row-one').removeClass('row').end();}
							});
						}
					});
				}
		
		
			//Add A Post
				if ($("#addPost").length > 0) {
					$("#addPost").click(function() {

						$("#addPost").attr("disabled", "true");
						$("#addPost").text('Working...');

						$.ajax({
							url: siteURL+'post/ajax_add/'+$("#threadId").val()+'/'+$("#pageId").val(),
							type: 'POST',
							timeout: 1000,
							dataType: 'html', 
							data: {post_content: $("#postTextArea").val()},
							error: function(html) {
								$("#addPost").removeAttr('disabled');
								$("#addPost").text('Add Post');
								alert('Post could not be added, please try again in 5 seconds.');	
							},
							success: function(html) {
								$('#newPost').append(html); 
								$("#postTextArea").val(' ');
								ArmPostFunctions();
								$("#addPost").removeAttr('disabled');
								$("#addPost").text('Add Post');
								$('#addPost').attr('disabled', 'disabled'); 
								setTimeout(function(){ $('#addPost').removeAttr('disabled') } , 10000);
							}
						})
					});
				}
	
	
			//Vote On A Post
				if ($("a.post-vote").length > 0) {
					$("a.post-vote").click(function() {
		
						var id = this.id;
						var post = id.split('_');
						var postId = post[1];
	
						if (post[0] == 'up') {
							var URL = siteURL+'post/vote-up/'+postId;
						} else {
							var URL = siteURL+'post/vote-down/'+postId;
						}
	
						$.ajax({
						url: URL,
						type: 'GET',
						timeout: 1000,
						dataType: 'html', 
						error: function() {alert('Vote failed.');},
						success: function(html) {$('span#voteid_'+postId).html(html);}
						});
					});
				}
		
		
			//Edit Post
				if ($("a.clickToEdit").length > 0) {
					$("a.clickToEdit").click(function() {
						var postId = this.id.split('_')[1];
						$.ajax({
							url: siteURL+'post/ajax_raw/'+postId,
							type: 'GET',
							timeout: 1000,
							dataType: 'html', 
							error: function() {alert('Error: Please try again in 5 seconds.');},
							success: function(html) {$('#thepost_'+postId).html(html);
								$("#cancelPost").click(function() {
									$('#thepost_'+postId).load(siteURL+'post/ajax_standard/'+$('#post_id').val());
								});
								$("#fullEditPost").click(function() {
									window.location = siteURL+'post/edit/'+$('#post_id').val();
								 });
								$("#updatePost").click(function() {
									$.ajax({
										url: siteURL+'post/ajax_update/'+$('#post_id').val(),
										type: 'POST',
										timeout: 1000,
										dataType: 'html', 
										data: {postContent: $('#quotedPost').val()},
										error: function() {alert('Error: Please try again in 5 seconds.');},
										success: function(html) {$('#thepost_'+postId).html(html);}
										});
									return false;
								});}
						});
				
					});
				}
		}
	//}


//Vote On A Profile
	if ($("a.profile-vote").length > 0) {
		$("a.profile-vote").click(function() {

			var id = this.id;
			var profile = id.split('_');
			var profileId = profile[1];

			if (profile[0] == 'up') {
				var URL = siteURL+'user/vote-up/'+profileId;
			} else {
				var URL = siteURL+'user/vote-down/'+profileId;
			}

			$.ajax({
			url: URL,
			type: 'GET',
			timeout: 1000,
			dataType: 'html', 
			error: function() {alert('Vote failed.');},
			success: function(html) {$('span#voteid_'+profileId).html(html);}
			});
		});
	}

	if ($(".smileyClick").length > 0) {
		$(".smileyClick").click(function() {
			var text = $("#postTextArea").val()+this.id;
			$("#postTextArea").val(text);
		});
	}


});


//OLD STUFF
	function CheckAll() {

		for (var i=0; i < document.posting.elements.length; i++) {
			var e = document.posting.elements[i];

				if ((e.name != 'allbox') && (e.type=='checkbox')) {
					e.checked = document.posting.allbox.checked;
				}
		}

	}




	/*function insert(c, Fname) {

		if (tagWrap(c)) {
			return false
		}

		var spec=';'+special.join(';')+';';

		if (spec.indexOf(';'+c+';')!=-1) {
			doInsert(end[c]?special[c][1]:special[c][0],Fname);
		} else {
			doInsert("["+(end[c]?"/":"")+c+"]",Fname);
			Fname.post.blur();
		}

		Fname.post.focus();
		end[c]=end[c]?false:true;
		return false;
	}
	function tagWrap(tag) {

		var a = getHTMLOfSelection();  

		if (a != "" && tag != "*") {
			document.selection.createRange().text = "["+tag+"]" + a + "[/"+tag+"]";
			return true;
		} else {
			return false;
		}
	}


	function getHTMLOfSelection () {

		var range;

		if (document.selection && document.selection.createRange) {
			range = document.selection.createRange();
			return range.htmlText;
		} else if (window.getSelection) {
			var selection = window.getSelection();

				if (selection.rangeCount > 0) {
					range = selection.getRangeAt(0);
					var clonedSelection = range.cloneContents();
					var div = document.createElement('div');
					div.appendChild(clonedSelection);
					return div.innerHTML;
				} else {
					return null;
				}
		} else {
			return null;
		}
	}*/
