/* open new centered window with scrolling ability */
/* USAGE: javascript:openPop('filename.html','windowname', width, height); */


function openWin(url,name,popW,popH) {
        version = navigator.appVersion;
	var w = 800, h = 600;
        if (version.toLowerCase().indexOf("mac")!=-1) {
                    popW = eval("popW-18");
        }
	if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
	}
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	window.open(url,name,'width=' + popW + ',height=' + popH + ',scrollbars=yes,toolbar=no,status=yes,resizable=no,top=' + topPos + ',left=' + leftPos);void(0);
}




/* open new centered window without scrolling ability */
/* USAGE: javascript:openPop('filename.html','windowname', width, height); */


function openPop(url,name,popW,popH) {
        version = navigator.appVersion;
	var w = 800, h = 600;

	if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
	}
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	window.open(url,name,'width=' + popW + ',height=' + popH + ',scrollbars=no,toolbar=no,status=yes,resizable=no,top=' + topPos + ',left=' + leftPos);void(0);
}


//no spam
// USAGE: javascript:doMail('user','domain.com')
function doMail(user,domain) {
	locationstring = "mailto:" + user + "@" + domain;
	window.location = locationstring;
}

function checkEmail(addy) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(addy)){
		return (true)
	} else {
		return (false)
	}
}


// code from blather.org to get rid of that silly little dotted line on click
window.onload = function(){
       if (document.getElementsByTagName) {
               var a = document.getElementsByTagName('a');
               for (var i = 0; i < a.length;  i++) {
                       a[i].onfocus = function(){this.blur()};
               }
       }
}



// YK DEFINED
	// ---------------------------------------------------------------------------
	// get post comments
	//---------------------------------------------------------------------------
	
	function showComments(theid, scrollTarg){
		if (!scrollTarg){ scrollTarg = "comments"; }
		targ = document.getElementById("comments"+theid).style["display"];
		entryTitle = document.getElementById("title"+theid).innerHTML;
		
		if (targ == "block"){
			$('#stamp'+theid).removeClass();
			$('#stamp'+theid).addClass("comment-stamp");
			$("#comments"+theid).slideUp("fast");
			$("#title"+theid).ScrollTo(800);
			openComment = "";
		} else {
			$('#stamp'+theid).addClass("comment-stamp-loading");
			// snag the new ones and upon retrieval, show the comments / form
			$.post("_lib/_funcs/retrieve.php",
				{ id: theid, thesec: "comments", thetitle: entryTitle, name: cname, email: cemail, url: cweb  },
				function(data){
					//document.getElementById("comments"+theid).innerHTML = data;
					$("#comments"+theid).html(data);
					$("#comments"+theid).slideDown("fast", function(){
						$('#stamp'+theid).addClass("comment-stamp-off");
						//$('#commenthref'+theid).css({ color: "#d0d0d0" });
						if (scrollTarg != "noscroll"){
							$('#'+scrollTarg+theid).ScrollTo(800);
						}
					});	
					
					
					openComment = theid;
				}
			)
		}
	}
	
	// ---------------------------------------------------------------------------
	// add comment to post
	//---------------------------------------------------------------------------
	
	function addComment(theid){
		var cname = document.getElementById("cName"+theid).value;
		var cemail = document.getElementById("cEmail"+theid).value;
		var cweb = document.getElementById("cWeb"+theid).value;
		var cbody = document.getElementById("cBody"+theid).value;
		var err = "";
		// error checking
		if (cbody == ""){
			err = "Enter a comment.";
		}
		if (cweb){
			if (cweb.substring(0,7) != "http://"){
				err = "Invalid web address.";
			}
		}
		if ((cemail == "") || (!checkEmail(cemail))){
			err = "Invalid email address.";
		}
		if (cname == ""){
			err = "Please enter a name.";
		}
		if (err){
			alert(err);
		} else {
			//
			// SET COOKIES
			$.cookie('yk_cname', cname, {expires: 7, path: '/', domain: '.yewknee.com', secure: false});
			$.cookie('yk_cemail', cemail, {expires: 7, path: '/', domain: '.yewknee.com', secure: false});
			$.cookie('yk_cweb', cweb, {expires: 7, path: '/', domain: '.yewknee.com', secure: false});
			//alert(cname + "\r\n" + cemail + "\r\n" + cweb + "\r\n\r\n" + cbody);
			$.post("_lib/_funcs/add.php",
				{ id: theid, thesec: "comments", name: cname, email: cemail, web: cweb, comment: cbody },
				function(data){
					if (data.substring(0,3) != "ERR"){
						countlocale = data.indexOf(':');
						commentcount = data.substring(0,countlocale);
						//alert(commentcount);
						data = data.substring((countlocale+1));
						if (commentcount == 1){
							var entryTitle = document.getElementById("title"+theid).innerHTML;
							//$('#comments'+theid).html = "<h4>"+entryTitle+"</h4>" + data;
							var data = "<h4>Comments: "+entryTitle+"</h4>" + data;
							document.getElementById("comments"+theid).innerHTML = data;
						} else {
							$('#thecommentform'+theid).before(data);
						}
						$('#commenthref'+theid).html("Comments ("+commentcount+")");
						document.getElementById("cBody"+theid).value = "";
					} else {
						alert("ERROR!" + data.substring(4));
					}
				});
			
		}
		
	}
	
	// ---------------------------------------------------------------------------
	// cancel comment submission
	//---------------------------------------------------------------------------
	
	function cancelComment(theid){
		showComments(theid);
	}
	
	// ---------------------------------------------------------------------------
	// log out user - kill cookies
	//---------------------------------------------------------------------------
	
	function logout(){
		// UNSET COOKIES
		$.cookie('yk_cname', null, {expires: -1, path: '/', domain: '.yewknee.com', secure: false});
		$.cookie('yk_cemail', null, {expires: -1, path: '/', domain: '.yewknee.com', secure: false});
		$.cookie('yk_cweb', null, {expires: -1, path: '/', domain: '.yewknee.com', secure: false});
		location.replace("/logout/");
	}
	
	// ---------------------------------------------------------------------------
	// get search term, modify, redirect to search page (filtering)
	//---------------------------------------------------------------------------
	
	function submitsearch(){
		var err = false;
		var searchTerm = document.getElementById("searchbx").value;
		
		if (!searchTerm){
			err = "Searches require a term or phrase to search for.";
		}
		
		if (!err){
			var searchTerm = searchTerm.replace(/ /g, "_");
			window.location.href = "http://www.yewknee.com/search/"+searchTerm+"/";
		} else {
			alert(err);
		}
	
	}
	
	// ---------------------------------------------------------------------------
	// sniff for <ENTER> key being pressed
	//---------------------------------------------------------------------------
	function checkEnter(e, whichForm){ 
		var characterCode; 
		
		if(e && e.which){ 
			e = e;
			characterCode = e.which; //character code is contained in NN4's which property
		} else {
			e = event;
			characterCode = e.keyCode; //character code is contained in IE's keyCode property
		}
		
		if(characterCode == 13){ 
			if (whichForm == "search"){
				submitsearch();
			}
			return false;
		} else {
			return true;
		}	
	}
	// ---------------------------------------------------------------------------
	// show / hide sidebar sections
	//---------------------------------------------------------------------------
	 function hideSidebar(targ){
  		
		var thetarg = document.getElementById(targ);
		var thetargStyle = thetarg.style['display'];

		if (thetargStyle == "block"){
			$(thetarg).slideUp("fast");
			$.cookie('yk_sec_'+targ, 'divoff', {expires: 7, path: '/', domain: 'yewknee.com', secure: false});
		} else if (thetargStyle == "none"){
			$(thetarg).slideDown("slow");
			$.cookie('yk_sec_'+targ, 'block', {expires: 7, path: '/', domain: 'yewknee.com', secure: false});
		}
	
	}
	// ---------------------------------------------------------------------------
	// send an email
	//---------------------------------------------------------------------------
	function sendMail(){
		var err = "";
		$("div#mailError").hide();
		var mailName = document.getElementById("mailName").value;
		var mailEmail = document.getElementById("mailEmail").value;
		var mailMsg = document.getElementById("mailMsg").value;
		var mailKey = document.getElementById("mailKey").value;
		// do some error checking
		if ((mailName == "your name") || (mailName == "")){
			err = "Please provide a name.<br />";
			$("#mailName").addClass("inputRegError");
		} else {
			$("#mailName").removeClass("inputRegError");
		}
		//
		if (!checkEmail(mailEmail)){
			err += "Invalid email address.<br />";
			$("#mailEmail").addClass("inputRegError");
		} else {
			$("#mailEmail").removeClass("inputRegError");
		}
		//
		if ((mailMsg == "your message") || (mailMsg == "")){
			err += "Please provide a message.<br />";
			$("#mailMsg").addClass("inputRegError");
		} else {
			$("#mailMsg").removeClass("inputRegError");
		}
		// ready to send?
		if (err){
			//$("#mailError").html("Please correct your errors.");
			//$("#mailError").fadeIn();
			//setTimeout ("fadeError()", 2000);
		} else {
			swapMailMsg();
			$("#mailStatusMsg").load("/_lib/v8.mailform.php",
				{name: mailName, email: mailEmail, message: mailMsg, key: mailKey},
				function (data){
					$("#mailMsg").val("");
				}
			);
		}
	}

	function swapMailMsg(bool){
		if (bool){
			$("#mailForm").slideDown();
			$("mailStatus").slideUp();
		} else {
			$("#mailForm").slideUp();
			$("#mailStatus").slideDown();
		}
	}
	// ---------------------------------------------------------------------------
	// get some new links
	//---------------------------------------------------------------------------
	
	function refreshLinks(){
		linkArray = document.getElementById("linkIDs").value;
		$("#thelinks").load("/_lib/_funcs/links.php",
			{linkIDs: linkArray});
	}
	
	function showContact(){
		$("#thecontact").ScrollTo(800);
	}






//////////////////// ------ ////////////////////////////////////////////////////////////////////////////
//////////////////// scroll ////////////////////////////////////////////////////////////////////////////
jQuery.getPos = function (e)
{
	var l = 0;
	var t  = 0;
	var w = jQuery.intval(jQuery.css(e,'width'));
	var h = jQuery.intval(jQuery.css(e,'height'));
	var wb = e.offsetWidth;
	var hb = e.offsetHeight;
	while (e.offsetParent){
		l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
		t += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
		e = e.offsetParent;
	}
	l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
	t  += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
	return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
};
jQuery.getClient = function(e)
{
	if (e) {
		w = e.clientWidth;
		h = e.clientHeight;
	} else {
		w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;
		h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
	}
	return {w:w,h:h};
};
jQuery.getScroll = function (e) 
{
	if (e) {
		t = e.scrollTop;
		l = e.scrollLeft;
		w = e.scrollWidth;
		h = e.scrollHeight;
	} else  {
		if (document.documentElement && document.documentElement.scrollTop) {
			t = document.documentElement.scrollTop;
			l = document.documentElement.scrollLeft;
			w = document.documentElement.scrollWidth;
			h = document.documentElement.scrollHeight;
		} else if (document.body) {
			t = document.body.scrollTop;
			l = document.body.scrollLeft;
			w = document.body.scrollWidth;
			h = document.body.scrollHeight;
		}
	}
	return { t: t, l: l, w: w, h: h };
};

jQuery.intval = function (v)
{
	v = parseInt(v);
	return isNaN(v) ? 0 : v;
};

jQuery.fn.ScrollTo = function(s) {
	o = jQuery.speed(s);
	return this.each(function(){
		new jQuery.fx.ScrollTo(this, o);
	});
};

jQuery.fx.ScrollTo = function (e, o)
{
	var z = this;
	z.o = o;
	z.e = e;
	z.p = jQuery.getPos(e);
	z.s = jQuery.getScroll();
	z.clear = function(){clearInterval(z.timer);z.timer=null};
	z.t=(new Date).getTime();
	z.step = function(){
		var t = (new Date).getTime();
		var p = (t - z.t) / z.o.duration;
		if (t >= z.o.duration+z.t) {
			z.clear();
			setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
		} else {
			st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
			sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
			z.scroll(st, sl);
		}
	};
	z.scroll = function (t, l){window.scrollTo(l, t)};
	z.timer=setInterval(function(){z.step();},13);
};
