

function isAChildOf(p, c) {
	child_test_results = false;
	p.find(".expand").each(function() {
		if ($(this).attr('id') == c) child_test_results = true;
	});
	return child_test_results;
}


var last_opened = new Array();
var child_test_results = false;


$(document).ready(function(){
	$('.expandable a.expand').click(function() {
		
		for (e in last_opened) {
			if (last_opened[e] && !isAChildOf(last_opened[e].parent().next(), $(this).attr('id')) ) {
				last_opened[e].parent().next().toggle('slow');
				if ( last_opened[e].children().attr('src') == '/inc/tree/blank.gif') {
				}
				else if ( last_opened[e].children().attr('src') == '/inc/tree/retracted.gif') {
					last_opened[e].children().attr('src', '/inc/tree/expanded.gif');
				}
				else {
					last_opened[e].children().attr('src', '/inc/tree/retracted.gif');
				}
				delete last_opened[e];
			}
		}
		
		$(".selected_category_link").removeClass('selected_category_link');
		
		var nextElem = $(this).parent().next();
		if (!nextElem.hasClass("expandable_content")) {
			return false;
		}	
		$(this).parent().next().toggle('slow');
		if ( $(this).children().attr('src') == '/inc/tree/blank.gif') {
		}
		else if ( $(this).children().attr('src') == '/inc/tree/retracted.gif') {
			$(this).children().attr('src', '/inc/tree/expanded.gif');
			last_opened[ $(this).attr('id') ] = $(this).next().prev() ;
			$(this).next().addClass('selected_category_link');
		}
		else {
			$(this).children().attr('src', '/inc/tree/retracted.gif');
		}
		
		refreshProductData( $(this).next().attr('href') );
		
		
		
		return false;
	}).each(function() {
		var nextElem = $(this).parent().next();

		if (!nextElem.hasClass("expandable_content")) {
			$(this).children().attr('src', '/inc/tree/blank.gif');
			$(this).removeClass("expand");
			$(this).addClass("no_expand");
			return;
		}
		if ( jQuery.trim(nextElem.html()) == "" ) {
			$(this).children().attr('src', '/inc/tree/blank.gif');
			$(this).removeClass("expand");
			$(this).addClass("no_expand");
			return;
		}
		
		if ( $(this).parent().next().find('.selected_category_link').length == 0 ) {
			$(this).children().attr('src', '/inc/tree/retracted.gif');
			nextElem.hide();
		}
		else {
			$(this).children().attr('src', '/inc/tree/expanded.gif');
			last_opened[ $(this).attr('id') ] = $(this).next().prev() ;
		}
		
		$(this).next().click(function() {
			$(this).prev().click();
			return false;
		});
		
	});
});


function refreshProductData(url) {
	if (Spry.$("product_list_div") == null) {
		document.location = url;
		return;
	}
	//try {
		dsPhotos = new Spry.Data.HTMLDataSet(url, "product_list", { firstRowAsHeaders:true, columnNames:["thumbnail", "name", "model", "weight", "price", "manufacturer", "buy", "quantity", "desc", "similar_text", "other_cat", "room_image", "breadcrumb", "leftnav", "rightnav", "allnav", "about_artist", "view_products", "title", "imageurl", "thumburl"],  filterDataFunc: PhotosFilter });

var gThumbnailsContainerID = "thumbnails";
var gMainImageContainerID = "mainImageOutline";
var gSlideShowControlsContainerID = "transport";
	
		
		dsPhotos.addObserver({ onPostLoad: function(notifier, data) {
			if (dsPhotos.getData().length > 0) {
				var d = new Date();
				Spry.Utils.updateContent("product_list_div", getStartAddress() + "/SpryGallery/static_hd_gallery_template.html?t=" + d.getTime(), function() {
					Spry.Data.initRegions();
					Spry.$('product_list_div').style.display = "block";
				});
				
				var rows = dsPhotos.getData(); 
				for (k = 0; k < rows.length; k++) {
					var heavyImage = new Image(); 
					heavyImage.src = dsPhotos.getRowByID(rows[0]['ds_RowID']).path;
				}
				
				if (ie) window.setTimeout ( 
					"var rows = dsPhotos.getData(); gImageViewer.setImage( dsPhotos.getRowByID(rows[0]['ds_RowID']).path ); ", 2000 
				);

				
			}
			else {
				Spry.Utils.updateContent("product_list_div", getStartAddress() +"/SpryGallery/none.html?");
			}
			
/*
		//if (gThumbViewer == null) 
		gThumbViewer = new Spry.Widget.ThumbViewer(gThumbnailsContainerID);
		//else gThumbViewer.removeObserver(gThumbViewer_Observer);
		//if (gImageViewer == null) 
		gImageViewer = new Spry.Widget.ImageViewer(gMainImageContainerID);
		//else gImageViewer.removeObserver(gImageViewer_Observer);
		//if (gSlideShowControl == null) 
		gSlideShowControl = new Spry.Widget.SlideShowControl(gSlideShowControlsContainerID);
		//else gSlideShowControl.removeObserver(gSlideShowControl_Observer);

	gThumbViewer.addObserver(function(notificationType, notifier, data)
	{
		if (notificationType == "onSelect")
			gImageViewer.setImage(data);
	});
	gImageViewer.addObserver(function(notificationType, notifier, data)
	{
		if (notificationType == "onPreUpdate")
			gSlideShowControl.killTimer();
		else if (notificationType == "onPostUpdate")
		{
			if (gSlideShowControl.isActive())
				gSlideShowControl.startTimer();
		}
	});
	gSlideShowControl.addObserver(function(notificationType, notifier, data)
	{
		if (notificationType == "onNextSlide")
			gThumbViewer.next();
		else if (notificationType == "onPreviousSlide")
			gThumbViewer.previous(true);
		else if (notificationType == "onFirstSlide")
			gThumbViewer.first(true);
		else if (notificationType == "onLastSlide")
			gThumbViewer.last(true);
	});
		*/
			
		}});
		


	//Spry.$$("#thumbnails a, #transport .previousBtn, #transport .nextBtn").addEventListener("click", function(e) { gSlideShowControl.stop(); }, "false");

	// Now that all of our widgets are setup, tell the image viewer
	// to load whatever is currently selected in the thumbnail viewer.


		//window.alert(gThumbnailsContainerID);
		//InitializeGallery();
		dsPhotos.loadData();
	
	
	//	var currentLink = gThumbViewer.getCurrentThumbLink();
	//	if (currentLink)
	//		gImageViewer.setImage(currentLink.href);


	//}
	//catch (ex) {
		//document.location = url;		
	//}
}

