$.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
		return $(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
		this.value = '';
		else if (type == 'checkbox' || type == 'radio')
		this.checked = false;
		else if (tag == 'select')
		this.selectedIndex = -1;
	});
};

$(document).ready(function() {
	var switching = false;
	var showcomments = false;
	var i = 1;
	
	$("#image_loader").hide();
	
	switchImage(1);
	
	/*$(this).everyTime(10000, function() {
		if(switching == false) {
			listcount = $("#news_list li").size();
			i++;
			if(i <= listcount) {
				switchImage(i);
			}
			else {
				i = 1;
				switchImage(i)
			}
		}
	});*/
	
	$("#news_list li").click(function() {
		$().stopTime();
		active = $("#news_list li.active").attr("id");
		active = active.substr(5,active.length-5)
		target = $(this).attr("id");
		target = target.substr(5,target.length-5)
		
		if(switching == false && active != target) {
			switchImage(target);
		}
		return false;
	});
	
	$("#search_query").keyup(function() {
		query = $(this).val();
		if(query.length > 2) {
			$.ajax({
				type: "GET",
				url: "/dam/_scripts/_search.php",
				data:{query:query},
				dataType: "html",
				success: function(data) {
					$("#search_output").load("/dam/_inc/_search.php", "", function() {
						$(data).appendTo("#search_body");
					});
				}
			});
		}
		else {
			$("#search_output").empty();
		}
	});
	
	$("a.read_comments").click(function() {
		postid = $(this).attr("id");
		postid = postid.substr(14,postid.length-14);
		if($("#comments_"+postid).hasClass("hidden")) {
			$("#comments_"+postid).removeClass("hidden").addClass("visible");
			postvalue = $(this).html();
			$(this).text("Dölj kommentarer");
			$.ajax({
				type: "GET",
				url: "/dam/_scripts/_comments.php",
				data:{id:postid},
				dataType: "html",
				success: function(data) {
					if(data != "") {
						$(data).appendTo("#comments_"+postid+" .comment_content");
					}
					$("#comments_"+postid).slideDown('fast');
				}
			});
		}
		else {
			$("#comments_"+postid).removeClass("visible").addClass("hidden");
			$(this).html(postvalue);
			$("#comments_"+postid).slideUp('fast', function() {
				$("#comments_"+postid+" .comment_content").empty();
			});
		}
		return false;
	});
	
	$('.submit_comment').click(function() {
		postid = $(this).attr("id");
		postid = postid.substr(15,postid.length-15);
		
		author = $('#comment_name_'+postid).val();
		email = $('#comment_email'+postid).val();
		comment = $('#comment_text_'+postid).val();
		confirmed = $('#send_'+postid).val();
		$.ajax({
			type: "GET",
			url: "/dam/index.php",
			data:{action:"comment",ajax:"yes",id:postid,comment:comment,name:author,email:email,confirmed:confirmed},
			dataType: "html",
			success: function() {
				$.ajax({
					type: "GET",
					url: "/dam/_scripts/_comments.php",
					data:{id:postid},
					success: function(data) {
						$("#comments_"+postid+" .comment_content").empty();
						$(data).appendTo("#comments_"+postid+" .comment_content");
						$('#comment_form_'+postid).clearForm();
					}
				});
			}
		});
		return false;
	});

	$('textarea.expanding').autogrow({
		maxHeight: 200,
		minHeight: 100,
		lineHeight: 16
	});
	
	$('a.tooltip').click(function() {
		return false;
	});
	
	$('a.delete_blog').click(function() {
		var url = $(this).attr("href");
		jConfirm('Är du säker på att du vill ta bort bloggen?', 'Radera blogg', function(r) {
			if(r == true) {
				document.location.href = url;
			}
			else return false;
		});
		return false;
	});
	
	$('a.delete_slideshow').click(function() {
		var url = $(this).attr("href");
		jConfirm('Är du säker på att du vill ta bort bilden?', 'Radera bildspel', function(r) {
			if(r == true) {
				document.location.href = url;
			}
			else return false;
		});
		return false;
	});
	
	$('a.delete_player').click(function() {
		var url = $(this).attr("href");
		jConfirm('Är du säker på att du vill ta bort spelaren?', 'Radera spelare', function(r) {
			if(r == true) {
				document.location.href = url;
			}
			else return false;
		});
		return false;
	});
	
	$('a.delete_game').click(function() {
		var url = $(this).attr("href");
		jConfirm('Är du säker på att du vill ta bort matchen?', 'Radera match', function(r) {
			if(r == true) {
				document.location.href = url;
			}
			else return false;
		});
		return false;
	});
	
	$('a.delete_comment').click(function() {
		var url = $(this).attr("href");
		jConfirm('Är du säker på att du vill ta bort kommentaren?', 'Radera kommentar', function(r) {
			if(r == true) {
				document.location.href = url;
			}
			else return false;
		});
		return false;
	});
	$('a.delete_action').click(function() {
		var url = $(this).attr("href");
		jConfirm('Är du säker på att du vill ta bort händelsen?', 'Radera händelse', function(r) {
			if(r == true) {
				document.location.href = url;
			}
			else return false;
		});
		return false;
	});
	
	function switchImage(current) {
		postid = current;	
		$("#spotlight_content").fadeOut('fast', function() {
			switching = true;
			
			$.ajax({
				type: "GET",
				url: "/dam/_scripts/_spotlight.php",
				data:{id:postid},
				dataType: "html",
				beforeSend: function() {
					$("#image_loader").show();
				},
				success: function(data) {
					$("#news_list li.active").removeClass('active');
					$("#news_list li#spot-"+postid).addClass('active');
					$("#image_loader").hide();
					$("#spotlight_content").empty();
					$(data).appendTo("#spotlight_content");
					$("#spotlight_content").fadeIn('fast', function() {
						switching = false;
					});
				}
			});
		});
	}
});
