$(function(){ initHTML(); dropDown(); boxy(); sideBarGallery(); });
function initHTML(){
	neatUpHtml(); idfyCurrentPage();
}
function neatUpHtml(){
	$('input[type=text],input[type=password], textarea').addClass('text');
	addFirstLast('ol','li');
	addFirstLast('ul','li');
	addFirstLast('table','tr');
	addFirstLast('tr','td');
	addFirstLast('.sidebar','.widget');
}
function addFirstLast(parent,child){
	$.each($(parent),function(){$(this).find(child+':first').addClass('first');$(this).find(child+':last').addClass('last');});
}
function idfyCurrentPage(){
	var location = window.location+'';
	var locationArr = location.split('/');
	$('a.button ,input[type=button], input[type=submit], input[type=reset]').addClass('button').hover(function(){ $(this).addClass('hover'); },function(){ $(this).removeClass('hover'); }).focus(function(){ $(this).addClass('focus'); }).blur(function(){ $(this).removeClass('focus'); });

	$('a[href='+location+']').addClass('current');
	$('a[href='+locationArr[locationArr.length-1]+']').addClass('current');
}
function getUrlVars(url){
    var vars = [], hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++){
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
function boxy(){
	$('.box,td.centerColumn').each(function(){
		$(this).append('<div class="clear"></div>').wrapInner('<div class="w"></div>');
		$(this).append('<div class="clear"></div><div class="shadow"></div>');
		var topc = $(this).offset().top + $(this).height();
		var leftc = $(this).offset().left;
		$(this).find('.shadow').css({ width:$(this).width(), top:topc ,left:leftc});
	});
	$('.boxGroup .box').each(function(){ ( ($(this).index()+1)%3 == 0 ) ? $(this).addClass('last') : true; });
}
function dropDown(){
	$('ul.nav').superfish();
}
function sideBarGallery(){
	var slideShow = new Array();
	var captions = new Array();
	$.each($('.ngg_images .entry-content a'), function(){
		slideShow.push($(this).find('img').attr('src'));
		captions.push($(this).find('img').attr('title'));
	});
	$('.ngg_images').append('<div class="slideshow"><img src="'+$('base').attr('href')+'images/dot.gif" alt="Slideshow" style="background-image:url("'+slideShow[0]+' ");opacity: 1; "/></div><h4></h4>');
	processSlideshow('.ngg_images .slideshow',slideShow,captions);
}
function processSlideshow(elem, imageList,captions, imageDuration, fadeSpeed, current){
	var listSize = imageList.length;
    if (!current || current >= listSize) current = 0;
    if (!imageDuration) imageDuration = 4000;
    if (!fadeSpeed) fadeSpeed = 1000;
    $(elem + " img").css("background-image", "url(" + imageList[current] + ")");
	$(elem + '~h4:first').html(captions[current]);
    if (current == (listSize - 1)) { $(elem).css("background-image", "url(" + imageList[0] + ")");
    } else {
        $(elem).css("background-image", "url(" + imageList[current + 1] + ")");
    }
    $(elem + " img").animate({ opacity: "1" }, imageDuration).animate({ opacity: "0.01" }, fadeSpeed, function(){ 
		     $(this).css("opacity", "1");
			 processSlideshow(elem, imageList,captions, imageDuration, fadeSpeed, current + 1) 
	});

}
