// KVOTES
// Here's some JavaScript 4 u
// By Andy

// Props to Scott Andrew and Peter-Paul Koch for this basic cookie function
// http://www.quirksmode.org/js/cookies.html
function readCookie(name){ var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++){ var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if(c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; }

$(document).ready(function(){
	// Login forms
	_lf = "<form action=\"/login\" method=\"post\"><input class=\"loginu\" type=\"text\" name=\"username\" value=\"Username\" /> <input class=\"loginp\" type=\"password\" name=\"password\" value=\"Password\" /> <input class=\"logsub\" type=\"submit\" value=\"Log in\" /> <a class=\"backlogin\" href=\"javascript://\"><img src=\"/_img/close.png\" width=\"14\" height=\"14\" alt=\"X\" title=\"Close log in panel\" /></a></form>"
	su = "Username"; sp = "Password"
	_fls = false
	
	// Search forms
	si  = "Search for quotes..."
	sfi = "Find a site..."
	$("#top form input").focus(function(){ if($(this).val() == si)  $(this).val("")  })
	$("#top form input").blur(function() { if($(this).val() == "")  $(this).val(si)  })
	$("input.findsite").focus(function() { if($(this).val() == sfi) $(this).val("")  })
	$("input.findsite").blur(function()  { if($(this).val() == "")  $(this).val(sfi) })
	
	// Shorturl form
	$("#shorturl input").focus(function(){ this.select() })
	
	// Log in userdock link
	$("#userdock a.login").click(function(e){
		e.preventDefault()
		t1 = $(this).parents(".t")[0]
		t1.id = "udt1"
		if($("#udt2").length > 0){
			t2 = $("#udt2")[0]
		} else {
			t2 = document.createElement("div")
			t2.className = "t"
			t2.id = "udt2"
			t2.innerHTML = _lf
			t1.parentNode.appendChild(t2)
			$("#userdock a.backlogin").click(function(e){
				e.preventDefault()
				t2 = $(this).parents(".t")[0]
				$(t2).hide()
				$(t2.previousSibling).show()
			})
			$("#userdock input.loginu").focus(function(){ if($(this).val() == su) $(this).val("") })
			$("#userdock input.loginu").blur(function(){ if($(this).val() == "") $(this).val(su) })
			$("#userdock input.loginp").focus(function(){ if($(this).val() == sp) $(this).val("") })
			$("#userdock input.loginp").blur(function(){ if($(this).val() == "") $(this).val(sp) })
		}
		//console.log(t1.style.display)
		//t1.style.display = "none !important"
		//console.log(t1.style.display)
		//t2.style.display = "block"
		//console.log(t1.style.display)
		$(t1).hide()
		$(t2).show()
	})
	
	// Log in front page link
	$("#front #olduser a.login").click(function(e){
		e.preventDefault()
		$(this).parents("span").hide()
		if($(this).parents("span").siblings("span").html() == ""){
			$(this).parents("span").siblings("span").html(_lf)
			$("#front #olduser a.backlogin").click(function(e){
				e.preventDefault()
				$(this).parents("span").hide()
				$(this).parents("span").siblings("span").show()
			})
			$("#front #olduser input.loginu").focus(function(){ if($(this).val() == su) $(this).val("") })
			$("#front #olduser input.loginu").blur(function(){ if($(this).val() == "") $(this).val(su) })
			$("#front #olduser input.loginp").focus(function(){ if($(this).val() == sp) $(this).val("") })
			$("#front #olduser input.loginp").blur(function(){ if($(this).val() == "") $(this).val(sp) })
		} else {
			$(this).parents("span").siblings("span").show()
		}
	})
	
	// Quote voting
	$(".q .v a").each(function(){
		$(this).click(function(e){
		if($(this).parents(".disabled").length > 0){
			if(_loggedIn){
				alert("Sorry! You do not have privileges to vote on this quote. It may be that you aren't a member of the Kvotes site which this quote comes from.");
			} else {
				alert("Sorry! You have to be logged in to vote. Please click the \"Log in\" button.");
			}
			return false;
		}
		if($(this).parents(".h").children(".site").length > 0){ su = $(this).parents(".h").children(".site")[0].href.substr(18) }
		if(_site){ su = _site.u }
		if(su){
			e.preventDefault()
			id = parseInt($(this).parents(".q").attr("id").substr(1))
			v  = 0
			vt = this.className.toLowerCase()
			if(vt == "up")  { v = 1  }
			if(vt == "down"){ v = -1 }
			if(v){
				$("#q" + id + " .v span").html("<a class=\"load\" href=\"javascript://\"></a>")
				$.get("/vote.php", { ajax: true, q: id, v: v, site: su }, function(data){
					$("#q" + id + " .v strong").html(data)
					$("#q" + id + " .v span").html("<a class=\"voted" + vt + "\"></a>")
				})
			}
		}})
	})
	
	// Quote +fav'ing
	$(".q .plusfav a").each(function(){
		$(this).click(function(e){
		if($(this).parents(".h").children(".site").length > 0){ su = $(this).parents(".h").children(".site")[0].href.substr(18) }
		if(_site){ su = _site.u }
		if(su){
			e.preventDefault()
			ct = $(this).html()
			id = parseInt($(this).parents(".q").attr("id").substr(1))
			el = this
			if(ct != "Undo?"){
				$(this.parentNode).addClass("u")
				$.get("/favourite.php", { q: id, ajax: true }, function(data){
					$(el.parentNode).html(data)
				})
			} else {
				if(confirm("Are you sure you want to remove this from your favourites?")){
					window.location.href = el.href
				}
			}
		}})
	})
	
	// Quote deleting
	$(".q .ctrls a.deletequote").each(function(){
		$(this).click(function(e){
		if($(this).parents(".h").children(".site").length > 0){ su = $(this).parents(".h").children(".site")[0].href.substr(18) }
		if(_site){ su = _site.u }
		if(su){
			e.preventDefault()
			id = parseInt($(this).parents(".q").attr("id").substr(1))
			el = this
			if(confirm("Are you sure you want to delete this quote?")){
				$.get("/ajax.php", { a: "delete", q: id, site: su, sure: "yes" }, function(data){
					box = $(el).parents(".q")[0]
					box.parentNode.removeChild(box)
				})
			}
		}})
	})
	
	// Quote privacy switching
	$(".q .ctrls a.prquote").each(function(){
		$(this).click(function(e){
		if($(this).parents(".h").children(".site").length > 0){ su = $(this).parents(".h").children(".site")[0].href.substr(18) }
		if(_site){ su = _site.u }
		if(su){
			e.preventDefault()
			id = parseInt($(this).parents(".q").attr("id").substr(1))
			el = this
			$.get("/ajax.php", { a: "switchpr", q: id, site: su }, function(data){
				$(el).html(data)
				if(data == "Public") { el.className = "prquote pu"; el.title = "Make private" }
				if(data == "Private"){ el.className = "prquote pr"; el.title = "Make public"  }
			})
		}})
	})
	
	// Quote comment show/hide
	$(".q a.showcomment").each(function(){
		$(this).click(function(e){
			e.preventDefault()
			$(this).blur()
			c = $(this.parentNode).siblings(".c")[0]
			if($(this).hasClass("hc")){ // Currently shown
				c.style.display = "none"
				$(this).removeClass("hc")
				$(this).html($(this).html().replace("Hide", "Show"))
			} else { // Currently hidden
				c.style.display = "block"
				$(this).addClass("hc")
				$(this).html($(this).html().replace("Show", "Hide"))
			}
		})
	})
	
	// Comment reply
	$(".comment a.reply").each(function(){
		$(this).click(function(e){
			if(_loggedIn){
				c = $(this).parents(".comment")[0]
				cid  = parseInt(c.id.replace("comment-", ""))
				if($("#reply-" + cid).length <= 0){
					if(c.className.match(/cl([0-9])/)){ level = (parseInt(c.className.match(/cl([0-9])/)[1]) + 1) }
					else { level = 1 }
					r    = document.createElement("form")
					r.id = "reply-" + cid
					r.method    = "post"
					r.className = "reply postcomment cl" + level
					r.innerHTML = "<input type=\"hidden\" name=\"replyto\" value=\"" + cid + "\" /><a class=\"close\" href=\"javascript://\"><img src=\"/_img/close.png\" width=\"14\" height=\"14\" alt=\"X\" title=\"Close reply window\" /></a><h3>Reply to above comment</h3><p>Logged in as <strong><a href=\"/id/" + _user + "\">" + _user + "</a></strong>.</p><textarea name=\"body\"></textarea><p class=\"buttons\"><span><input type=\"checkbox\" name=\"alertme\" id=\"r" + cid + "-alertme\" /> <label for=\"r" + cid + "-alertme\">Send me an e-mail if someone replies to my comment</label></span><input type=\"submit\" value=\"Post reply\" /></p>"
					$(c).after(r)
					$("#reply-" + cid + " a.close").click(function(e){
						e.preventDefault()
						if(!$("#reply-" + cid + " textarea").val() || ($("#reply-" + cid + " textarea").val() && confirm("Are you sure you want to close the reply window? You will lose everything you've typed!"))){
							$("#reply-"+ cid).remove()
						}
					})
				}
			} else {
				alert("Howdy! You have to be logged in to post a reply. Please click the \"Log in\" button in the upper right corner. Thanks!")
			}
		})
	})
	
	// SMenu
	_smh = false
	$("#top #switch").mouseover(function(e){
		_smh = true
		if($("#top #site").length > 0){ $("#top #smenu")[0].style.left = ($("#top #site")[0].offsetLeft) + "px" }
		if($("#top #tip").length > 0){ $.get("/ajax.php", { a:"notnew" }); $("#tip").hide() }
		$("#top #smenu").show()
	})
	$("#top #switch").mouseout (function(e){ _smh = false; setTimeout(function(){ if(!_smh){ $("#top #smenu").hide() } }, 1500) })
	$("#top #smenu").mouseover (function(e){ _smh = true  })
	$("#top #smenu").mouseout  (function(e){ _smh = false; setTimeout(function(){ if(!_smh){ $("#top #smenu").hide() } }, 1500) })
	$("#top #switch").click    (function(e){ e.preventDefault(); this.blur() })
	
	// The tip
	if(readCookie("kvnew") == "yes" && _tip){
		tip  = document.createElement("div")
		tip.id = "tip"
		tip.innerHTML = "<div><a class=\"close\" href=\"javascript://\"><img src=\"/_img/close.png\" width=\"14\" height=\"14\" alt=\"X\" title=\"Close log in panel\" /></a> <strong>Tip!</strong> Mouseover the above arrow to navigate between sites.</div>"
		if($("#top #switch").length > 0){ tip.style.left = (($("#top #switch")[0].offsetLeft)-19) + "px" }
		$("#top #smenu").after(tip)
		$("#tip a.close").click(function(e){
			e.preventDefault()
			$.get("/ajax.php", { a:"notnew" })
			$("#tip").hide()
		})
	}
	
	// SiteURL lowercase-ifying input
	$("input.siteurl").change(function(){ $(this).val($(this).val().toLowerCase()) })
})
