//
// The Disney Driven Life - Zwally Design
// Author: Todd Perlmutter
// Copyright (c) 2011, all rights reserved
//

jQuery.noConflict();

// Store the last elements
var ddl_LastActiveTopic = null;
var ddl_LastActiveTopicTitle = null;
var ddl_LastActiveItem = null;
var ddl_LastActiveItemTitle = null;
var ddl_LastHolder = null;
var ddl_LastSibling = null;
var ddl_LastSlug = '';
var ddl_LastOver = null;

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

function ddl_DoEllipses()
{
	jQuery('.dsq-widget-comment').ellipsis();
	//jQuery('.bar-topic-title').ellipsis();
}

function ddl_MouseOverZero(id, hasChildren)
{
	// Hide the active child
	var active = document.getElementById(id + '-active-child');
	active.style.display = 'none';
	
	// Show the other children
	var children = document.getElementById(id + '-children');
	children.style.display = 'block';
	
	if (id !== ddl_LastOver)
		jQuery('.bar-topic-title').ellipsis();

	ddl_LastOver = id;
	
	return false;
}

function ddl_MouseOutZero(id, hasChildren)
{
	// Show the active child as needed
	var active = document.getElementById(id + '-active-child');
	var active_title = document.getElementById(id + '-active-child-title');
	if (active_title.innerHTML !== '')
		active.style.display = 'block';

	// Hide the other children
	var children = document.getElementById(id + '-children');
	children.style.display = 'none';
	
	return false;
}

function ddl_LoadDataDisplay()
{
	var data_display = document.getElementById('bar-group-sidebar-data-display');
	var hframe = document.getElementById('data-display-iframe');
	
	if ((data_display != null) && (hframe != null))
	{
		var doc = hframe.document;
		if (hframe.contentDocument)
			doc = hframe.contentDocument;
		else if (hframe.contentWindow)
			doc = hframe.contentWindow.document;
		//alert(doc.body.innerHTML);
		
		data_to_show = doc.body.innerHTML.replace(/\\/g, '');
		start = data_to_show.toLowerCase().indexOf('<li');
		end = data_to_show.toLowerCase().lastIndexOf('</li>');
		//alert("start = " + start + " / end = " + end);
		if ((start != -1) && (end != -1))
		{
			doc.body.innerHTML = data_to_show.substr(start, end - start + 5);
			items = doc.getElementsByTagName('li');
			//alert(items.length);
			
			data_to_show = '';
			count = 0;
			for (i = 0; i < items.length; i++)
			{
				//alert(items[i].innerHTML);
				if (items[i].innerHTML.length > 0)
				{
					if (count++ > 0)
						data_to_show += '<div class="ddl-sidebar-post-separator"></div>';
					data_to_show += '<li class="' + items[i].className + '">' + items[i].innerHTML + '</li>';
				}
			}
			
			//alert(data_to_show);
			//data_display.innerHTML = '<!-- ' + data_to_show + ' -->';
			data_display.innerHTML = data_to_show;
			ddl_DoEllipses();
			
			//data_display.style.height = '250px'
			
			/*
			//data_to_show = data_to_show.substr(start, end - start + 5);
			items = data_to_show.split('<li');
			if (items.length == 1)
				items = data_to_show.split('<LI');
			//alert(items.length);
			*/
		}
	}
}

function ddl_MoveDataDisplay()
{
	var data_display = document.getElementById('bar-group-sidebar-data-display');
	
	// Move the data display item
	if ((data_display != null) && (ddl_LastHolder != null))
	{
		// Hide and empty it
		data_display.style.display = 'none';
		data_display.innerHTML = '';
		
		//alert(ddl_LastHolder.offsetTop + ' / ' + ddl_LastHolder.id);
		//alert(data_display.offsetTop + ' / ' + data_display.id);
		//ddl_LastHolder.appendChild(data_display);
		ddl_LastHolder.className += ' ddl-sidebar-current';
		//alert(ddl_LastHolder.className);
	
		//data_display.innerHTML = ddl_LastSlug;
		
		// Find the data
		data_to_show = ddl_topics[ddl_LastSlug];
		if (data_to_show.substr(0, 7) == 'http://')
		{
			data_display.innerHTML = '<br/>Loading...';
			
			var hframe = document.getElementById('data-display-iframe');
			if (hframe != null)
				hframe.src = data_to_show;
		}
		else
		{
			data_display.innerHTML = data_to_show;
			ddl_DoEllipses();
		}
		
		// Show and move it
		data_display.style.display = 'block';
		//data_display.style.top = '0';
		
		//alert(data_display.offsetHeight);
		//alert(data_display.parentNode.style.height);
		
		// Muck with the height
		//if (data_display.style.height < data_display.parentNode.style.height)
		//	data_display.style.height = data_display.parentNode.style.height;
		
		// Fill in data here!!!
		//data_display.style.height = '250px';
		//data_display.parentNode.height = '100px';
		//dd_height = parseInt(data_display.style.height); if (isNaN(dd_height)) dd_height = 0;
		//ls_height = parseInt(ddl_LastSibling.style.height); if (isNaN(ls_height)) ls_height = 0;
		
		/*
		alert(data_display.style.height + ' / ' + ddl_LastSibling.style.height);
		alert(dd_height + ' / ' + ls_height + ' / ' + ddl_LastSibling.id + ' / ' + data_display.parentNode.id);
		data_display.parentNode.height = Math.max(dd_height, ls_height) + 'px';
		*/
		//alert(data_display.parentNode.height + ' / ' + data_display.parentNode.id);
		//lh_height = parseInt(ddl_LastHolder.style.height);
		//ddl_LastHolder
		
		//data_display.style.cssFloat = 'right';
		//data_display.style.position = 'absolute';
		//dd_new_height = ls_height * -1;
		//data_display.style.top = dd_new_height.toString() + 'px';
		//data_display.style.top = (-1 * (parseInt(ddl_LastSibling.style.height) - parseInt(data_display.style.height))) + 'px';
		//data_display.style.top = (-1 * ;//-1 * (data_display.offsetTop - ddl_lastHolder.offsetTop) + 'px';
	}
}

function ddl_SetActiveTopic(slug, id, parent_id, depth)
{
	//msg = slug + " / " + id + " / " + parent_id + " / " + depth;
	//alert(msg);

	// Hide the last one
	if (ddl_LastActiveTopic != null)
		ddl_LastActiveTopic.style.display = 'none';
	if (ddl_LastActiveTopicTitle != null)
		ddl_LastActiveTopicTitle.innerHTML = '';
	
	// Clear the old item
	//if (ddl_LastActiveItem != null) { }
	
	// Get the item
	var sel_item  = document.getElementById(id);
	var sel_title = document.getElementById(id + '-title');
	
	// Uncolor the current holder if there is one
	if (ddl_LastHolder != null)
	{
		ddl_LastHolder.className = ddl_LastHolder.className.replace(' ddl-sidebar-current', '').trim();
	}
	
	// Find the proper holder
	ddl_LastHolder = null;
	var holder = sel_item;
	while (holder != null)
	{
		if ((holder.className != null) && (holder.className.indexOf('bar-group-sidebar') != -1))
			ddl_LastHolder = holder;
		holder = holder.parentNode;
	}
	//alert(ddl_LastHolder.id);
	
	// Find the proper sibling
	ddl_LastSibling = null;
	pn = sel_item;
	while (pn != null)
	{
		//alert(pn.className);
		if ((pn.className != null) && (pn.className.indexOf('bar-group-left') != -1))
			ddl_LastSibling = pn;
		pn = pn.parentNode;
	}
	//alert(ddl_LastSibling.id);
	
	// Store the slug
	ddl_LastSlug = slug;

	// Only need to hide at top level
	if (depth > 0)
	{
		active       = document.getElementById(parent_id + '-active-child');
		active_title = document.getElementById(parent_id + '-active-child-title');
		
		// Show the element
		active_title.innerHTML = sel_title.innerHTML;
		
		// Store it so next time through we can hide it
		ddl_LastActiveTopic = active;
		ddl_LastActiveTopicTitle = active_title;
	}
	
	// Remove higlight from last active item
	if (ddl_LastActiveItem != null)
	{
		ddl_LastActiveItem.className = ddl_LastActiveItem.className.replace(' bar-topic-active', '').trim();
		ddl_LastActiveItem.className = ddl_LastActiveItem.className.replace(' bar-topic-parent-active', '').trim();
	}
	
	// Add the highlights to the new item
	sel_item.className += ' bar-topic-active';
	if (sel_item.className.indexOf('bar-topic-parent') != -1)
		sel_item.className += ' bar-topic-parent-active';

	// Store the selected item
	ddl_LastActiveItem = sel_item;
	
	// Remove highlight from last active title
	if (ddl_LastActiveItemTitle != null)
	{
		ddl_LastActiveItemTitle.className = ddl_LastActiveItemTitle.className.replace(' bar-topic-active-title', '').trim();
		ddl_LastActiveItemTitle.className = ddl_LastActiveItemTitle.className.replace(' bar-topic-active-parent-title', '').trim();
	}
	
	// Store the selected title
	ddl_LastActiveItemTitle = sel_title;
	
	ddl_MoveDataDisplay();
	
	return false;
}

function ddl_ShowComments()
{ 
	//alert("function");
	//alert("ddl_PageType = " + ddl_PageType);
	//alert("testing 777");
	
	if (ddl_PageType !== 'page')
		ddl_SetActiveTopic('recent-comments', 'bar-group-recent-comments', 'bar-group-recent-comments', '0');
}

jQuery(document).ready(ddl_ShowComments);

