/**
 * Creativity Awards Tree
 *
 * @author: Rahmin Pavlovic
 * @date: 18 March 2008
 * @version: 25 August 2008
 *
 * copyright (c) 2008, Crain Communications
 *
 */

// 
var awards;
var creativityAwards  =  {
	img : [],
	tree : [],
	agency : [],
	property : {},
	currTab : null,
	currProp : null,
	json : null,
	level3 : -1,
	level4 : -1,
	results : null,
	resultCount : 0,
	resultLimit : 5,

	tree_name : 'creativity_awards',
	container : 'creativity_awards',

	imgs : [
		'/images/widgets/arw-minus.gif',
		'/images/widgets/ajax-arrows-036.gif'
	],
	closeBtn : '<div class = "close_btn"><a href = "javascript:creativityAwards.hideDetails(1);" title = "Close">x<\/a><\/div>',
	loadingAppMsg : '<div class = "loading">Loading Creativity Awards...<\/div><br /><div align = "center"><img src = "/images/widgets/ajax-arrows-036.gif" width = "16" height = "16" alt = "" \/><\/div><br \/>',
	loadingWinMsg : '<div class = "loading">Loading...<\/div><br /><div align = "center"><img src = "/images/widgets/ajax-arrows-036.gif" width = "16" height = "16" alt = "" \/><\/div><br \/>',

	output:function(html, id) {
		id = (id) ? id : this.container; 
		DOM.getElementById(id).innerHTML = html;
	},

	getLevel3Entry:function(prop) {

		for(var n=0; n<=this.json.level1.length; n++) {

			for(var j=0; j<=this.json.level1[n].level2.length; j++) {

				for(var i=0; i<=this.json.level1[n].level2[j].level3.length; i++) {

					if(this.json.level1[n].level2[j].level3[i] && this.json.level1[n].level2[j].level3[i].tree.name == prop) {
						return this.json.level1[n].level2[j].level3[i].tree;
					}
				}
			}
		}
		return -1;
	},

	getLevel4Index:function(prop) {
		for(var j=0; j<=this.json.level4.length; j++) {
			if(this.json.level4[j].tree.name == prop) {
				return j;
			}
		}
		return -1;
	},

	treeExists:function(id) {
		for(var n = 0; n<this.json.level1.length; n++) {
			if(this.json.level1[n].tree.name == 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;
	},

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

	load:function() {
		this.output(this.loadingAppMsg);
		
		this.initiate();
	},

	initiate:function() {
		awards = this;

		new AJAX.FileRequest(
			'/ajax/get_tree_json.php',
			function() {
				if(this.request && this.response) {
					awards.json = JSON.parse(this.response);
					if(awards.json) {
						//alert(awards.json.level1[0].tree.name)
						awards.getHTML();
					}
					else {
						awards.output('<h4 style="color:red">We\'re sorry, but there was an unexpected error loading the Creativity Awards. Please reload this page and try again.</h4>');
					}
				}
			},
			null,
			'get',
			'tree=' + this.tree_name
		);
	},

	getHTML:function() {
		awards = this;

		new AJAX.FileRequest(
			'/ajax/get_tree_html.php',
			this.buildTree,
			null,
			'get',
			'tree=' + this.tree_name
		);
	},

	buildTree:function() {
		if(this.request && this.response) {

			awards.results = this.response;

			if(awards.results) {
				awards.output(awards.results);

				// expand first tree by default
				awards.getTree(awards.json.level1[0].tree.name);
			}
		}
	},

	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') {
			DOM.getElementById('businesses_'+id).style.display = 'block';
			DOM.getElementById('parent_'+id).className = 'company_name expanded';
			//location = '#'+id;
			//window.scrollTo(0, DHTML.getPosY(DOM.getElementById('parent_'+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]
			
			this.level3 = this.getLevel3Entry(prop);

			if(this.level3 != -1) {

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

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

				// position indicator relative to popover
				ind = DOM.getElementById('indicator');
				ind.style.top = parseInt(DHTML.getPosY(popout) + 37) + '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.level3.title || this.level3.searchterms)) {
					popout.innerHTML = [this.closeBtn, this.loadingWinMsg].join('');

					search_phrase = (this.level3.searchterms && trim(this.level3.searchterms) != '') ? this.level3.searchterms : this.level3.title;

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

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

	setDetails:function() {

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

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

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

			if(awards.level3.searchAdAge) {

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

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

					// reset result count
					awards.resultCount = 0;

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

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

			// display results
			this.output(this.results.join(''), 'popover_'+this.currProp);

			// 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";
		}
	}
}

creativityAwards.preload();