(function($) {
  
   /**
	* initializes pwp_lightbox. It gives a specific index ['lightbox_'+ln+'_'+i] accounting
	* not just for the array index [i] in the jQuery object but also the static count [ln] of
	* pwp_lightbox usage. This prevents conflict of simultaneous usage of pwp_lightbox;
	* @param	options		options object
	*/  
	$.fn.pwpLightbox = function(options){

		var opts 	= $.extend({}, $.fn.pwpLightbox.defaults, options);
		var obj		= this;
		var json 	= [];
		var ln		= $.fn.pwpLightbox.i == undefined? 0 : $.fn.pwpLightbox.i;

		if($(this).length){
			
			$(this).each(function(i){
				$(this).attr("id", 'lightbox_'+ln+'_'+i);
				if(opts.group != false)	$(this).attr("rel", opts.group);
				json[i] = '{"id":"'+$(this).attr('id')+'","url":"'+$(this).attr('href')+'"}';
			});
			
			var params = {json:"["+json.join(',')+"]", dimensions:opts.size};

			$.getJSON(opts.dir, params, function(data){ 
				$.each(data, function(i, item){ $('#'+item.id).attr("href", item.url);});
				if(opts.group != false)	obj.colorbox();
				else 					$.each(data, function(i, item){ $('#'+item.id).colorbox(); });
			});
			
			$.fn.pwpLightbox.i = ++ln;
		}
	}
	
	$.fn.pwpLightbox.defaults = {size: 300, dir:pwp_template_dir+'_lib/php/pwp.process.lightbox.php', group:'lightbox-gallery'};
	
	
	
	//for son of suckerfish dropdwon menus slide up/down transitions.
	//ATTN: children() only return immediate children therefore this func should only target the ul itself
	//note an ancestor
	$.fn.pwpNavSlide	= function(options){
		var opts 	= $.extend({}, $.fn.pwpNavSlide.defaults, options);
		
		$(this).find('li ul').addClass('jqhide');
		$(this).find('li').hover(function(){$(this).children("ul").slideDown(opts.speed);}, function(){$(this).children("ul").slideUp(opts.speed);});
		$(this).find('.jqhide').css({'left':'auto','display':'none'});
	}
	
	$.fn.pwpNavSlide.defaults = {speed:'slow'}
	
})(jQuery);


$(document).ready(function(){
	
	//add ancestor class to all of li.current-cat class, because
	//wordpress only adds current-cat-parent to its parent
	$('li.current-cat').each(function(){
		var o = $(this).parent().parent();
		while(o.hasClass('cat-item')){
			o.addClass('current-cat-ancestor');
			o = o.parent().parent();
		}
	});
	
	
	//init slide automatically for nav with both .pwp_nav_ssf and .slide classes					   
	$('.pwp_nav_ssf.slide').pwpNavSlide();
});
