var mk_init_done = false;
var accFacets;
function init()
{
	// quit if this function has already been called
	if (mk_init_done) return;
    // flag this function so we don't do the same thing twice
    mk_init_done = true;
	

	//leftmenu
	var myBodies = document.getElementsByClassName('gadget-body');
	var myHeads = document.getElementsByClassName('gadget-head');
	//var accFacets = new fx.Accordion(myHeads, myBodies, {duration: 300, opacity: false, height:false, width:false});
	accFacets = new fx.Accordion(myHeads, myBodies);
	
	var nSelect = 0;
	var wl=window.location.toString();
	if (wl.indexOf('/category/')>0) nSelect=0;
	if (wl.indexOf('/tag/')>0) nSelect=1;
	if (wl.indexOf('/archive/')>0) nSelect=2;
	
	accFacets.showThisHideOpen(myBodies[nSelect]);
	
	
	//category and tags
	$S('#menu a','.otherlinks a', '.meta a', '.post-title a').action({
		initialize: function(){
			this.fxFade= new fx.Color(this, {duration: 400, property: "color"});
		},
		onmouseover: function(){
			if(this.fxFade){
				this.fxFade.clearTimer();
				this.fxFade.duration=100;
				this.fxFade.customColor("#404040","#0000FF");
			}			
			
		},
		onmouseout: function(){
			if(this.fxFade){
				this.fxFade.clearTimer();
				this.fxFade.duration=500;
				this.fxFade.customColor("#0000FF","#404040");
			}
		}
	});	

		
	//category and tags
	/**
	$S('#menu a','.otherlinks a', '.meta a', '.post-title a').action({
		initialize: function(){
			//this.style.color = 'black';
		},
		onmouseover: function(){
			var ColorFader = new fx.Color(this, {duration: 100, fromColor: "#404040", toColor: "#0000FF", property: "color"});
			ColorFader.toggle();
		},
		onmouseout: function(){
			var ColorFader = new fx.Color(this, {duration: 500, fromColor: "#0000FF", toColor: "#404040", property: "color"});
			ColorFader.toggle();
		}
	});	
	**/
	
	//this must happen before we create the accordeon
	$S('.gadget-head').action({
		initialize: function(){
			
		},
		onmouseover: function(){
			Element.addClassName(this,'GH-Hover');
		},
		onmouseout: function(){
			Element.removeClassName(this,'GH-Hover');
		}
	});
	

	//assign karma counters to story links
	$S('#content .storycontent a').action({
		initialize: function(){
		},
		onclick: function(){
			return countKarma(this);
		}
	});

	
	//submit comment
	$S('#cmdComment').action({
		initialize: function(){
			//this.style.color = 'black';
		},
		
		onclick: function(){
			this.enabled=false;
			var postid=$('comment_postid').value;
			var user=$('comment_user').value;
			var email=$('comment_email').value;
			var comment = $('comment_body').value;
			
			//alert(myPostbody);
			var doSubmit = true;
			if(user.length==0 || user.length>200)
			{
				doSubmit = false;
			}
			if(email.length==0 || email.length>100)
			{
				doSubmit = false;
			}
			if(comment.length==0 || comment.length>2000)
			{
				doSubmit = false;
			}
			if(doSubmit)
			{
				var myPostbody = 'user='+encodeURIComponent(user)+'&email='+encodeURIComponent(email)+'&comment='+encodeURIComponent(comment)+'&postid='+encodeURIComponent(postid);
				new ajax ('/blog/comment/', {postBody: myPostbody, onComplete: function()
				  {
					alert('comment posted');
					$('comment_body').value='';
				    window.location='/post/'+postid;
				  }
				  });
				return false;						
			}else{
				alert('All fields are required');
				return false;
			}
			
		}
	});	
	
	
	
	//this must happen before we create the accordeon
	$S('.TabControl').action({
		initialize: function(){
			
		},
		onmouseover: function(){
			Element.addClassName(this,'TC-Hover');
		},
		onmouseout: function(){
			Element.removeClassName(this,'TC-Hover');
		}
	});
	$S('#tabAbout').action({
		initialize: function(){
			$('tabAboutContent').getElementsByTagName('div')[0].style.height='230px';
		},
		onclick: function(){
			if($('txtAbout'))
			{
			}else{
				new ajax ('/blog/minipage/about', {update: $('tabAboutContent')});
				return false;
			}
			//myTabs.showThisHideOpen('#tabAbout');
		}
	});	
	$S('#tabAttract').action({
		initialize: function(){
			$('tabAttractContent').getElementsByTagName('div')[0].style.height='230px';
		},
		onclick: function(){
			if($('txtAttractors'))
			{
			}else{
				new ajax ('/blog/minipage/attractors', {update: $('tabAttractContent'), onComplete: function()
			  {
				
				//now let's wait a little and assign behaviors to the loaded thumbs
				setTimeout(
				function(){
				//let's assign some behavior to the thumbs
				$S('.attrthumb').action({
					initialize: function(){
					},
					onclick: function(){
						var newSrc='http://static.flickr.com/' + this.getAttribute('src') + '.jpg';
						$('lightboxImage').src=newSrc;
						$('overlay').style.display='block';
						//alert('i am clicked dude!:' + this.getAttribute('src'));
					}
				});
				
				$S('.lightboxClose','#overlay').action({
					initialize: function(){
					},
					onclick: function(){
						$('overlay').style.display='none';
						$('lightboxImage').src='/assets/img/blank.gif';
					}
				});
				
				//ok, that's all that happens whenwe load attractor thumbs	
				}
				,500);

				
				
			  }
			  });
				return false;
			}
		}
	});
	
	$S('#tabSiteMap').action({
		initialize: function(){
		},
		onclick: function(){
			if($('txtSiteMap'))
			{
			}else{
				new ajax ('/blog/minipage/sitemap', {update: $('tabSitemapContent')});
				return false;
			}
		}
	});
	
	myTabBodies = document.getElementsByClassName('TabContent');
	myTabHeads = document.getElementsByClassName('TabControl');
	myTabs = new fx.Accordion(myTabHeads, myTabBodies);
	myTabs.showThisHideOpen = function(toShow)
	{
		this.elements.each(function(el,j)
		{
			if (el.offsetHeight > 0 && el != toShow) this.clearAndToggle(el,j);
			if (el == toShow) setTimeout(function(){this.clearAndToggle(toShow,j)}.bind(this), this.options.delay);
		}.bind(this));	
	};
			

}

var myTabBodies;
var myTabHeads;
var myTabs;

function countKarma(e)
{
	if(e.href){
		 new ajax ('/blog/karma/', {postBody: 'url='+e.href, onComplete: doRedirect});
		 return false;
	}else{
		return true;
	}
}

function doRedirect(request)
{
		window.location=request.responseText;
		return false;
}


function include_once(sURL,sID)
{
    if($(sID)) return true; //load only once
    new ajax (sURL, {onComplete: function(request)
    {
        var hd = document.getElementsByTagName('head')[0];
        scr = document.createElement('script'); 
        scr.setAttribute('type', 'text/javascript');
        scr.setAttribute('id', sID);
        hd.appendChild(scr); 
        scr.text = request.responseText;
    }
    });
   
}

function getChildrenByTagName(element, sTag)
{
	aChildren	= new Array();
	sTag		= sTag.toLowerCase();
	aChildNodes = element.childNodes;
	
	for (i = 0; i < aChildNodes.length; i++)
	{		
		if (aChildNodes[i].tagName != undefined) 
		{	
			if (aChildNodes[i].tagName.toLowerCase() == sTag) 
			{	
				aChildren.push(aChildNodes[i]);
			}
		}
	}
	
	return aChildren;
}
