// to get this script to function, you must have the same dom tree for the tabbed navigation.
// <ul class="ajax_tabs">
//    <li class="activeTab">
// 		 <a class="ajax_tab">

/* scanLinks: function() {
		var anchor, imgNum, groupName, keyNum;
		var anchors = document.getElementsByTagName('a');

		for(var x = 0; x < anchors.length; x++) {
			anchor = anchors[x];

			// Find thinbox links, assign onclick event
			if (
				anchor.getAttribute('href') &&
				anchor.rel && 
				(
					anchor.rel.toLowerCase().match(/^lightbox\b/) ||
					anchor.rel.toLowerCase() == 'thinbox' ||
					anchor.rel.toLowerCase() == 'thinconfirm'
				)
			) {
				Event.observe($(anchor), 'click', function(e) {

					var clicked = Event.element(e);
					if(clicked.tagName.toLowerCase() != 'a') {
						clicked = $(clicked).up('a');
					}
					this.start(clicked);
					Event.stop(e);
					return false;
				}.bindAsEventListener(this), true)
				//alert("found");
				imgNum = thinLinks.length;
				thinLinks[imgNum] = anchor;

				// Group images by className
				if(anchor.className && anchor.href.match(/\.(jpg|jpeg|png|gif)$/i)) {
					// this.preloadImg(anchor.href);
					groupName = anchor.className;
				} else if(anchor.href.match(/\.(jpg|jpeg|png|gif)$/i)) {
					// this.preloadImg(anchor.href);
					groupName = 'noclass';
					anchor.className = groupName;
				} else {
					groupName = 'nogroup';
				}

				if(!thinGroups[groupName]) thinGroups[groupName] = new Array();
				keyNum = thinGroups[groupName].length;
				thinGroups[groupName][keyNum] = anchor;

			}
		}
	}
*/



var umbrella_class_name = '.ajax_tabs';
var tab_class_name = '.ajax_tab';
var active_tab_name = 'activeTab';
var content_frame_class = 'tabbed_content';
var loading_content = '<div width="100%" height="200px" style="text-align:center;"><img style="margin-top: 100px;" src="/assets/ajax_loader.gif"/><br/><span class="loading_text">loading...</span></div>';

function add_tab_handlers() {
	var li = $$(tab_class_name);
	
	var item; 
	
	for(var i = 0; item = li[i]; i++) {
		Event.observe(item.down('a'), 'click', function(e) {
			e.stop();
			var a = e.element();
			var li = a.up('li');
			li.up(umbrella_class_name).select('.activeTab').invoke('removeClassName', 'activeTab');
			li.addClassName(active_tab_name);
			
			ajax_request_tab(a);
			// custom for thinbox tab
			
		})
	}
	
	
}

function ajax_request_tab(page) {
	new Ajax.Request('auxiliary/serve_tabs.php?p=' + page, {
		onSuccess: function(response) {
			fill_content(response);
	  	}, 
		onFailure: function(response) {
	  		alert("There was a problem displaying the page: " + response);
		},
		onCreate: function(response) {
			$(content_frame_class).innerHTML = loading_content;
		}
	});
}

function fill_content(data) {
// this rescans everything, accumulating more of the same images over and over. so to fix...

	// this rescans everything, accumulating more of the same images over and over. so to fix..
	var thinLinks = Array();
	$(content_frame_class).innerHTML = data.responseText;
	
	myThinbox.scanLinks();
	// this will try to activate thinbox stuff for the ajax page
	

}

document.observe("dom:loaded", function() {
	add_tab_handlers();
});

