var a_openTimelines = new Array();
var i_currentTimeline = -1;
var i_currentTimelineItem = -1;
var b_flashLoaded = false;

function reposition(nHeight)
{
	if(nHeight <= 1) nHeight = 349;
	nHeight += "px";
	document.getElementById("flashcontent").style.height = nHeight;
	document.getElementById("timerimeSWF").style.height = nHeight;
}

function initTimerimeFlash(i_timelineId, b_loaded)
{
	if(isIE7 && !b_loaded)
	{
		window.addEvent('load', function()
		{
			initTimerimeFlash(i_timelineId, true);
		});
		return;
	}

	if(i_timelineId != -2 && i_timelineId != 0)
	{
		var s_url ="/flash/timerimeSWF.swf";
		if(1)
		{
			s_url += '?Qxml=' + i_timelineId;

		}
		var o_swfObject = swfobject.createSWF({data: s_url, width:"912", height:"384" }, {bgcolor: '#ffffff',quality: "high",allowScriptAccess: "always", wmode: "transparent"}, "flashcontent");
	}
}

function compareTimeline(i_timelineId)
{
	showTimeline(i_timelineId, true, undefined, true);
}

function showTimeline(i_timelineId, b_switchTab, i_timelineElementId, b_compare)
{
	if(pageTracker)
	{
		pageTracker._trackPageview('/timeline/' + i_timelineId + '/');
	}

	if(i_timelineId > 0)
	{
		var s_tagName = document.getElementById('flashcontent').tagName.toLowerCase();
		if(s_tagName == 'div')
		{
			initTimerimeFlash(i_timelineId, true);
		}
		else
		{	
			//document.getElementById('flashcontent').goHome(i_timelineId);
			//if(false)
			{	
				if(b_compare)
				{
					if(!a_openTimelines.contains(i_timelineId.toString()))
					{
						document.getElementById('flashcontent').addTimeLine(i_timelineId);		
					}					
				}
				else
				{
					a_openTimelines = new Array();
					document.getElementById('flashcontent').replaceTimeLine(i_timelineId);
				}
				
				a_openTimelines.include(i_timelineId.toString());
			}
		}
	}
	else
	{
		i_timelineId = -1;
	}
	if(i_currentTimeline != i_timelineId)
	{
		i_currentTimelineItem = -1;
		i_currentTimeline = i_timelineId;
	}

	if(i_timelineElementId == undefined || i_timelineElementId == false)
	{
		i_timelineElementId = 806;
	}
	if(b_switchTab != false)
	{
		//alert(i_timelineElementId);
		handleTab(i_timelineElementId,b_switchTab);
	}

}

function closeTimeline(i_timelineId)
{

	if(a_openTimelines.length > 0)
	{
		a_openTimelines.remove(i_timelineId.toString());

		if(i_currentTimeline == i_timelineId && a_openTimelines.length > 0)
		{

			var i_newTimelineId = a_openTimelines[a_openTimelines.length-1];
			showTimeline(i_newTimelineId);
		}
	}
}

function showTimelineItem(i_itemId,b_switchTab,i_timelineId)
{	
	if(pageTracker)
	{
		pageTracker._trackPageview('/timelineitem/' + i_itemId + '/');
	}
	if(typeof(document.getElementById('flashcontent').selectItem) == 'function')
	{
		document.getElementById('flashcontent').selectItem(i_timelineId,i_itemId);
	}
	i_currentTimelineItem = i_itemId;
	if(b_switchTab != false)
	{
		i_timelineItemElementId = 807;
		handleTab(i_timelineItemElementId,b_switchTab);
	}

}

function loadDefaultTimeline()
{
	b_flashLoaded = true;
	if(i_currentTimeline != '')
	{
		(function(){showTimeline(i_currentTimeline,false);}).delay(1);
	}
	else
	{
		var s_url = '/?views=getDefaultTimelineJson()&elementId=90';
		new Json.Remote(s_url, {onComplete: function (o_content)
						{
							showTimeline(o_content.timeline_id,false);
						}}).send();
	}

	if(o_initEdit)
	{
		o_initEdit();
		delete(o_initEdit);
	}
}

function handleTabClick(o_href, s_ajax)
{
	if(!s_ajax)
	{
		s_ajax = 'tabPage';
	}

	s_url = o_href.href;
		
	if(pageTracker)
	{
		pageTracker._trackPageview(s_url);
	}
	
	document.title = s_windowTitleBase + '' + o_href.title;
	
	b = handleTabHref(s_url, 'contentContainer', s_ajax);
	
	setActiveTab(o_href);
	
	return b;
}

function handleTab(i_elementId,b_switchTab)
{
	var s_url = '/pagina/-/' +  i_elementId + '/-1/' + i_currentTimeline + '/' + i_currentTimelineItem + '/1/';
	
	b = handleTabHref(s_url, 'contentContainer');
	
	setActiveTab(false);
	
	return b;
}

function setActiveTab(o_href)
{
	if(o_href)
	{
		o_href.addClass('menu_selected');
		if(o_href.getParent())
		{
			if(o_href.getParent().getParent())
			{
				o_menuUl = o_href.getParent().getParent();
			}
		}
	}
	else
	{
		o_menuUl = $('subMenuContainer');
	}
	
	if(o_menuUl)
	{
		o_menuUl.getElements('a').each(function(o_otherHref)
		{
			if(!o_href || (o_otherHref != o_href))
			{
				o_otherHref.removeClass('menu_selected');
			}
		});
	}
}

function handleTabHref(s_url, s_update, s_addAjax)
{
	if(s_addAjax)
	{
		s_url += '?'+s_addAjax+'=1';
	}

	document.body.setStyle('cursor','wait');
	new Ajax(s_url, {update: $(s_update),evalScripts: true, onComplete: function()
	{
		document.body.setStyle('cursor','default');
		resizeImages();
		if(a_openTimelines.length > 1)
		{
			scrollToItemInfo();
		}
	}}).request();

	return false;
}


function showGalleryPhoto(i_photoId,s_content,i_width, i_height)
{

	MOOdalBox.open('/?views=showGalleryPhoto(' + i_photoId + ')&elementId=90',s_content,i_width + ' ' + i_height);
}

function updateReaction(s_formId)
{
	updateDiv = 'content';

	if($('reactionForm'))
	{
		$('reactionForm').setStyle('display','none');
	}
	
	$(s_formId).action += '&a=1';
	$(s_formId).send({
		update: $(updateDiv),
		onComplete: function() {}
	});		
}

function insertReaction(s_formId,i_elementId, b_first)
{
	if(trim($('reactionContent').value) != '')
	{ 
		if($('reactionForm'))
		{
			$('reactionForm').setStyle('display','none');
		}
		
		if(b_first)
		{
			updateDiv = 'content';
		}
		else
		{
			if($('reactionOverview'))
			{
				var i_height = $('reactionOverview').getStyle('height').toInt();;

				$('reactionOverview').setStyle('height',i_height);
				$('reactionOverview').empty();
				$('reactionOverview').className = 'waiting';
			}
			
			updateDiv = 'reactions';
		}
	
		$(s_formId).action += '&a=1';
		$(s_formId).send({
			update: $(updateDiv),
			onComplete: function() {}
		});		
	}
}

function deleteReaction(elementId,reactionId)
{
	updateDiv = 'content';
	s_url = '/?views=showReactions('+elementId+')&functions=deleteReaction('+reactionId+')&elementId=90&a=1';
	
	document.body.style.cursor = 'wait';
	new Ajax(s_url, 
	{
		method: 'get', 
		update: $(updateDiv),
		evalScripts:true,
		onComplete: function()
		{
			document.body.style.cursor = 'default';
			setOverlay('none');
		}
	}).request();
}

function editReaction(elementId,reactionId)
{
	updateDiv = 'content';
	s_url = '/?views=showReactions('+elementId+', '+reactionId+')&elementId=90&a=1';
	
	document.body.style.cursor = 'wait';
	new Ajax(s_url, 
	{
		method: 'get', 
		update: $(updateDiv),
		evalScripts:true,
		onComplete: function()
		{
			document.body.style.cursor = 'default';
			setOverlay('none');
		}
	}).request();
}
