
/**
 * Digital Family Tree
 *
 * @author: Rahmin Pavlovic
 * @version: 18 March 2008
 *
 * copyright (c) 2008, Crain Communications
 *
 */

var treeDetails;
var digitalFamilyTree = {
	img:[],
	tree:[],
	property:{},
	currTab:null,
	currProp:null,
	resultCount:0,
	resultLimit:5,
	searchResults:null,
	imgPath:'/img/familytree/digital',
	imgs:[this.imgPath+'/arw-minus.gif', '/images/global/ajax-wheel-036.gif'],
	closeBtn:'<div class="close_btn"><a href="javascript:digitalFamilyTree.hideDetails(1);" title="Close">x<\/a><\/div>',
	loadingMsg:'<div class="loading">Loading...<\/div><br /><div align="center"><img src="/images/global/ajax-wheel-036.gif" width="32" height="32" alt="" /><\/div><br />',

	treeExists:function(id) {
		for(var n=0; n<this.tree.length; n++) {
			if(this.tree[n].id==id) {
				return true;
			}
		}
		return false;
	},

	getTree:function(id) {
		if(this.currtab!=id && this.treeExists(id)) {
			this.hideTree();
			DOM.getElementById(id).className='level1 selected';
			DOM.getElementById('d_'+id).style.display='block';
			DOM.getElementById('level2_'+id).style.display='block';
			this.currtab=id;
		}
	},

	hideTree:function() {
		if(this.currtab!=null) {
			this.hideDetails();
			DOM.getElementById(this.currtab).className='level1 unselected';
			DOM.getElementById('d_'+this.currtab).style.display='none';
			DOM.getElementById('level2_'+this.currtab).style.display='none';
		}
	},

	toggleCompany:function(id) {
		this.hideDetails();
		if(DOM.getElementById('parent_'+id).className == 'company_name collapsed') {
			DOM.getElementById('businesses_'+id).style.display='block';
			DOM.getElementById('parent_'+id).className='company_name expanded';
		}
		else {
			DOM.getElementById('businesses_'+id).style.display='none';
			DOM.getElementById('parent_'+id).className='company_name collapsed';
		}
	},

	getDetails:function(prop) {

		// hide any previous popovers
		this.hideDetails();

		if(prop!=this.currProp && this.property[prop]) {

			// remember which entry we're displaying
			treeDetails=this;
			this.currProp=prop;

			// position popover relative to entry
			popout=DOM.getElementById('popover_'+prop);
			popout.style.top=parseInt(DHTML.getPosY(DOM.getElementById(prop)) - 8) + 'px';
			popout.style.left=parseInt(DHTML.getPosX(DOM.getElementById(prop)) + 100) + 'px';

			// position indicator relative to popover
			ind=DOM.getElementById('indicator');
			ind.style.top=parseInt(DHTML.getPosY(popout) + 14) + 'px';
			ind.style.left=parseInt(DHTML.getPosX(popout) - 6) + 'px';

			// display popover
			popout.style.visibility="visible";
			ind.style.visibility="visible";

			// fetch feed if not already set 
			if(popout.innerHTML=='' && (this.property[prop].title || this.property[prop].searchterms)) {
				popout.innerHTML=[this.closeBtn, this.loadingMsg].join('');
				search_phrase=(this.property[prop].searchterms && trim(this.property[prop].searchterms) != '') ? this.property[prop].searchterms : this.property[prop].title;

				new AJAX.FileRequest(
					'/functions/ajax/get_search_results.php',
					this.setDetails,
					null,
					'get',
					'search_phrase='+escape(search_phrase)
				);
			}
		}
		else {

			// forget last entry
			this.currProp=null;
		}
	},

	setDetails:function() {

		// process AJAX request
		if(this.request && this.response) {

			// reset HTML w/close button
			treeDetails.results=[treeDetails.closeBtn];

			// add data
			if(treeDetails.property[treeDetails.currProp].title && trim(treeDetails.property[treeDetails.currProp].title)!='') {
				treeDetails.results.push('<div class="popover-title">', treeDetails.property[treeDetails.currProp].title,'</div>');
			}
			if(treeDetails.property[treeDetails.currProp].description && trim(treeDetails.property[treeDetails.currProp].description)!='') {
				treeDetails.results.push('<div class="popover-description">', treeDetails.property[treeDetails.currProp].description,'</div>');
			}
			if(treeDetails.property[treeDetails.currProp].url && trim(treeDetails.property[treeDetails.currProp].url)!='') {
				treeDetails.results.push(
					'<div class="popover-url">', '<a href="', treeDetails.property[treeDetails.currProp].url, '" target="new_window">',
					treeDetails.property[treeDetails.currProp].url,
					'</a></div>'
				);
			}

			if(treeDetails.property[treeDetails.currProp].searchAdAge) {

				// parse search results as JSON
				treeDetails.searchResults=JSON.parse(this.response);

				if(treeDetails.searchResults && treeDetails.searchResults.records && treeDetails.searchResults.results) {

					// reset result count
					treeDetails.resultCount=0;

					// build HTML for search results
					treeDetails.results.push('<div class="popover-results">Related AdAge.com content:</div>');
					treeDetails.results.push('<ul>');
					for(var j=0; j<treeDetails.searchResults.results.length; j++) {
						treeDetails.results.push('<li><a href="http://adage.com'+treeDetails.searchResults.results[j].url+'" target="new_window" title="Click here to read this story">'+treeDetails.searchResults.results[j].headline+'</a><\/li>');
						treeDetails.resultCount++;
	
						// stop if exceed limit
						if(treeDetails.resultCount >= treeDetails.resultLimit) {
							break;
						}
					}
					treeDetails.results.push('<\/ul>');
				}
			}
			treeDetails.showDetails();
		}
	},

	showDetails:function() {
		if(this.currProp!=null) {

			// display results
			DOM.getElementById('popover_'+this.currProp).innerHTML=this.results.join('');

			// free browser memory of results
			this.results.clear();
		}
	},

	hideDetails:function() {
		if(this.currProp!=null) {
			DOM.getElementById('indicator').style.visibility="hidden";
			DOM.getElementById('popover_'+this.currProp).style.visibility="hidden";
		}
	},

	preload:function() {
		for(var i=0; i<this.imgs.length; i++) {
			this.img[i]=new Image();
			this.img[i].src=this.imgs[i];
		}
	}
}

digitalFamilyTree.preload();
