
function test(v) { try { console.log(v); } catch(e) { }; }

// load folder icons
window._effects = new Array();
window._folder_images = new Array();
window._folder_images['folder_open'] = new Image();
window._folder_images['folder_open'].src = '/assets/images/folder_open.png';
window._folder_images['folder_closed'] = new Image();
window._folder_images['folder_closed'].src = '/assets/images/folder_closed_plain.png';
window._animation_in_progress = 0;

window._document_categories = new Array();
window._document_category_documents = new Array();
window._document_category_effect_duration = 23;

function init_trade_docs() {
	for(var n in window._document_categories) {
		if(window._document_categories[n].parentid == 0) window._document_categories[n].init();
	}

}

function document_category(id, title, parentid, nestlevel) {
	this.id = id * 1;
	this.title = title;
	this.parentid = (parentid) ? parentid : 0;
	this.nestlevel = (nestlevel) ? nestlevel : 0;

	this.categories = new Array();
	this.documents = new Array();

	this.addCategory = function(id, title) {
		window._document_categories[id] = new document_category(id, title, this.id, this.nestlevel + 1);
		this.categories[this.categories.length] = window._document_categories[id];
	}

	this.addDocument = function(id, title, description, href, icon, fsize, preview_href, preview_width, preview_height) {
		window._document_category_documents[id] = new document_category_document(this.id, id, title, description, href, icon, fsize, preview_href, preview_width, preview_height);
		this.documents[this.documents.length] = window._document_category_documents[id];
	}



	this.click = function() {
		if(window._animation_in_progress) return;

		var start = this.curheight;
		var end = (this.expanded) ? 1 : this.divheight;

		if(!this.expanded) {
			// expanding
			var duration = this.getDuration(end - start);
			this.expand(start, end, duration);

			if(this.parentid) {
				var par = window._document_categories[this.parentid];
				var start2 = par.curheight;
				var end2 = par.curheight + end;
				var duration2 = this.getDuration(end2 - start2);
				par.expand(start2, end2, duration2);
			}
			this.folderimg.src = window._folder_images['folder_open'].src;
		} else {
			// contracting
			var duration = this.getDuration(start - end);
			this.expand(start, end, duration);

			if(this.parentid) {
				var par = window._document_categories[this.parentid];
				var start2 = par.curheight;
				var end2 = par.curheight - start;
				var duration2 = this.getDuration(start2 - end2);
				par.expand(start2, end2, duration2);
			}

			for(var i=0; i<this.categories.length; ++i) {
				if(this.categories[i].expanded) this.categories[i].click();
			}
			this.folderimg.src = window._folder_images['folder_closed'].src;
		}
		window._animation_in_progress = 1;
	}

	this.getDuration = function(amt) {
		var min = 400;
		var duration = (amt / 10) * (window._document_category_effect_duration);
		if(duration < min) duration = min;
		return duration;
	}

	this.expand = function(start, end, duration) {
//		this.fxheight.options.duration = duration;
//		this.fxheight.custom(start, end);

		if(this.div.getStyle('display') == 'none') {
			
			this.div.setStyles({
				'height':	1,
				'overflow':	'hidden',
				'display':	'block'
			});
			new Fx.Tween(this.div, {
				duration: 1000,
				onComplete: function() {
					window._document_categories[this.element.getAttribute('categoryid')].curheight = this.now;
					window._document_categories[this.element.getAttribute('categoryid')].expanded = (this.now > 1) ? 1 : 0;
					window._animation_in_progress = 0;
				}
			}).start('height', this.div.getScrollHeight());
		}
		else {
			new Fx.Tween(this.div, {
				duration: 1000,
				onComplete: function() {
					this.element.setStyle('display', 'none');
					window._document_categories[this.element.getAttribute('categoryid')].curheight = this.now;
					window._document_categories[this.element.getAttribute('categoryid')].expanded = (this.now > 1) ? 1 : 0;
					window._animation_in_progress = 0;
				}
			}).start('height', 0);		
		}

	}

	this.init = function() {
		this.div = $('catspan_'+this.id);
		this.div.setAttribute('categoryid', this.id);
		this.div.setStyle('display', 'none');
		
		this.folderimg = $('catfolder_'+this.id);

		for(var i=0; i<this.categories.length; ++i) {
			this.categories[i].init();
		}
		this.divheight = this.div.offsetHeight;

//		this.fxheight = new fx.Height (
//			this.div, {
//				duration: 1,
//				onComplete: function() {
//					window._document_categories[this.el.getAttribute('categoryid')].curheight = this.now;
//					window._document_categories[this.el.getAttribute('categoryid')].expanded = (this.now > 1) ? 1 : 0;
//					window._animation_in_progress = 0;
//				}
//			}
//		);

		this.curheight = 1;
		this.div.style.visibility = 'visible';
		//this.fxheight.hide();
	}
}


function document_category_document(categoryid, id, title, description, href, icon, fsize, preview_href, preview_width, preview_height) {
	this.categoryid = categoryid;
	this.id = id;
	this.title = title;
	this.description = description;
	this.href = href;
	this.icon = icon;
	this.fsize = fsize;
	this.preview_href = preview_href;
	this.preview_width = preview_width;
	this.preview_height = preview_height;
	this.doc_icon;
	this.doc_link;

	this.show_doc_preview = function() {

		if(!this.doc_icon) {
			this.doc_icon = document.getElementById('doc_icon_' + this.categoryid + '_' + this.id);
		}
		if(!this.doc_link) {
			this.doc_link = document.getElementById('doc_link_' + this.categoryid + '_' + this.id);
		}

		var preview_content = '';
		if(this.preview_href && this.preview_width && this.preview_height) {
			var t = this.title.replace(/'|"/, '');
			preview_content = '<img src="'+this.preview_href+'" width="'+this.preview_width+'" height="'+this.preview_height+'" alt="'+this.title+'" title="'+this.title+'" style="border:solid #666666 1px;">';
		}

		if(preview_content) {
			if(!window._preview_div) {
				window._preview_div = document.createElement('div');
				window._preview_div.style.position = 'absolute';
				window._preview_div.top = '0px';
				//window._preview_div.style.background = '#3F3F3F';
				window._preview_div.style.border = '';
				window._preview_div.style.padding = '0px';
				window._preview_div.style.visibility = 'hidden';
				window._preview_div.style.opacity = '0.9';
				window._preview_div.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=90)';
				document.body.appendChild(window._preview_div);
			}

			var buf = '';
			//buf += '';
			buf += '<table cellpadding="0" cellspacing="0">';
			buf += '<tr>';
			buf += '<td><img src="/assets/images/tradedocs-arrow-left.png" width="27" height="20"></td>';
			buf += '<td style="padding:10px; background:#555555;">'+preview_content+'</td>';
			buf += '</tr>';
			buf += '</table>';
				


			window._preview_div.style.width = (this.preview_width) ? this.preview_width : 250 + 'px';
			window._preview_div.innerHTML = buf;
			window._preview_div.style.visibility = 'visible';

			//window._preview_div.style.top = (window.curPos.y - (window._preview_div.offsetHeight / 2)) + 'px';
			//window._preview_div.style.left = (window.curPos.x + 50) + 'px';

			//var x = getLeft(this.doc_link) + this.doc_link.offsetWidth + 40;
			var x = $(this.doc_link).getLeft() + $(this.doc_link).getWidth() + 40;

			//var y = getTop(this.doc_icon) - (window._preview_div.offsetHeight / 2) + 10;
			var y = $(this.doc_icon).getTop() - ($(window._preview_div).getHeight() / 2) + 10;
			
			window._preview_div.style.top = y + 'px';
			window._preview_div.style.left = x + 'px';

		}

	}
	this.hide_doc_preview = function() {
		window._preview_div.style.visibility = 'hidden';
	}
}










/*
function getCurPos(e) {
	window.curPos.x = (window.Event) ? e.pageX : event.clientX;
	window.curPos.y = (window.Event) ? e.pageY : event.clientY;
}


function trackCoords() {
	window.curPos = new Object(); 
	if (document.layers) document.captureEvents(Event.MOUSEMOVE);
	if (document.layers || document.all) document.onmousemove = getCurPos;
	if (document.addEventListener) document.addEventListener('mousemove', getCurPos, true);
}
*/

function trackCoords() { }

