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

var thisAgency;
var treeDetails;
var lnaFamilyTree = {
	img:[],
	tree:[],
	agency:[],
	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:lnaFamilyTree.hideDetails(1);" title="Close">x<\/a><\/div>',
	loadingMsg:'<div class="loading">Loading Data...<\/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;
	},

	agencyExists:function(id) {
		for(var n=0; n<this.agency.length; n++) {
			if(this.agency[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';
		}
	},

	toggleAgency:function(id) {
		this.hideDetails();
		if(DOM.getElementById('parent_'+id).className == 'company_name collapsed') {

			// display entry
			DOM.getElementById('businesses_'+id).style.display='block';
			DOM.getElementById('parent_'+id).className='company_name expanded';

			// snap to position
			//location='#'+id;
			window.scrollTo(0, DHTML.getPosY(DOM.getElementById('parent_'+id)));

			// load data (if not already set)
			if(DOM.getElementById('company_description_'+id).innerHTML=='') {
				DOM.getElementById('company_description_'+id).innerHTML=this.loadingMsg;
				new AJAX.FileRequest(
					'/functions/ajax/get_tree_data.php',
					function() {
						if(this.request && this.response) {
							company=JSON.parse(this.response);
							if(company.description) {
								DOM.getElementById('company_description_'+id).innerHTML=company.description;
							}
						}
					},
					null,
					'get',
					'tree=media08&level=2&descrip=1&id=' + escape(id)
				);
			}
		}
		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)
				);
*/

//prompt('','tree=media08&level=' + escape(this.property[prop].level) + '&id=' + escape(this.property[prop].id))

				new AJAX.FileRequest(
					'/functions/ajax/get_tree_data.php',
					this.setDetails,
					null,
					'get',
					'tree=media08&level=' + escape(this.property[prop].level) + '&id=' + escape(this.property[prop].id)
				);
			}
		}
		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.response=JSON.parse(this.response);

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

					if(treeDetails.response.searchresults && treeDetails.response.searchresults.results) {
						// reset result count
						treeDetails.resultCount=0;
	//alert(treeDetails.response.title)
						// 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.response.searchresults.results.length; j++) {
							treeDetails.results.push('<li><a href="http://adage.com'+treeDetails.response.searchresults.results[j].url+'" target="new_window" title="Click here to read this story">'+treeDetails.response.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];
		}
	}
}

lnaFamilyTree.preload();
