﻿var slideSpeed = 20;
var slideInterval = 0;

var currentPage = null;
var currentDialog = null;
var currentWidth = 0;
var currentHash = location.hash;
var hashPrefix = "#_";
var pageHistory = [];
var newPageCount = 0;
var checkTimer;

function CheckOrientAndLocation()
{
    if (window.innerWidth != gCurrentWidth)
    {
        gCurrentWidth = window.innerWidth;

        var orient = gCurrentWidth == 320 ? "profile" : "landscape";
        document.body.setAttribute("orient", orient);
    }
}

function initStatLyr()
{
	// args: id, left, top, w, h, duration of glide to location onscroll, acceleration factor
	// acceleration factor should be -1 to 1. -1 is full deceleration

var x = document.getElementById('edit-box').offsetLeft;
var y = document.getElementById('edit-box').offsetTop;
var statLyr = new Glider("edit-box",x,y,null,null,1000,-1,GliderLimiter);

	statLyr.show();
}

function GliderLimiter(destY)
{
var y1 = document.getElementById('outliner').offsetTop;
var h1 = document.getElementById('outliner').offsetHeight;
var h2 = document.getElementById('edit-box').offsetHeight;
var y2 = y1 + h1 - h2;

	if (y2 < y1)
		y2 = y1;

	if (destY < y1)
		return y1;

	if (destY > y2)
		return y2;

	return destY;
}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function ScrollToAndFlash(div)
{
	new Effect.ScrollTo(div); Effect.Pulsate(div,{from:0.3, pulses:3});
}

function HandleClick(e)
{
	var target = (e && e.target) || (event && event.srcElement);
	var outliner = document.getElementById('outliner-root');
	var edit_box = document.getElementById('edit-box');
	var actions = document.getElementById('actions');
	var import_box = document.getElementById('import-box');
	var import_link = document.getElementById('import-link');

	try
	{
		// if import-box is visible, handle clicks for that first
		if (gIsEditable && import_box.style.display == 'block' && !isDecendant(target, import_box) && !isDecendant(target, import_link))
		{
			HideImportBox();
		}
	
		if (!isDecendant(target, outliner) && !isDecendant(target, edit_box) && !isDecendant(target, actions))
			DeselectRow();
	}
	catch(err)
	{
	
	}
}

function isDecendant(decendant,ancestor)
{
	return ((decendant.parentNode==ancestor) || (decendant.parentNode!=document) &&	isDecendant(decendant.parentNode,ancestor));
}

// Keyboard short cuts
function handleKeyPress(event)
{
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

	if (keyCode == 9 && gSelectedRow)			// tab
		TabRow();
	else if (keyCode == 38 && gSelectedRow)		// up arrow
		SelectPreviousRow(gSelectedRow);
	else if (keyCode == 40 && gSelectedRow)		// down arrow
		SelectNextRow(gSelectedRow);
	else if (keyCode == 46 && gSelectedRow)		// delete (not backspace))
		DeleteRow();

	event.returnValue=false;
	event.cancel = true;
}

function ToggleActionLink(action, status)
{
var act = document.getElementById(action+'_span');
var mini_act = document.getElementById(action+'_mini');

	switch (action)
	{
	case 'add':			if (status == 'active')
						{
							act.innerHTML = '<a href="javascript:AddRow();"><img class="action-icn" src="/images/actions/icn_new.gif" /> Add Row</a>';
							mini_act.innerHTML = '<a href="javascript:AddRow();" title="Add Row"><img class="action-icn" src="/images/actions/icn_new.gif" /></a>';
						}
						else
						{
							act.innerHTML = '<img class="action-icn" src="/images/actions/icn_new_off.gif" /><span class="disabled"> Add Row</span>';
							mini_act.innerHTML = '<img class="action-icn" src="/images/actions/icn_new_off.gif" /><span class="disabled"></span>';
						}
						break;

	case 'delete':		if (status == 'active')
						{
							act.innerHTML = '<a href="javascript:DeleteRow();"><img class="action-icn" src="/images/actions/icn_delete.gif" /> Delete Row</a>';
							mini_act.innerHTML = '<a href="javascript:DeleteRow();" title="Delete Row"><img class="action-icn" src="/images/actions/icn_delete.gif" /></a>';
						}
						else
						{
							act.innerHTML = '<img class="action-icn" src="/images/actions/icn_delete_off.gif" /><span class="disabled"> Delete Row</span>';
							mini_act.innerHTML = '<img class="action-icn" src="/images/actions/icn_delete_off.gif" /><span class="disabled"></span>';
						}
						break;

	case 'tab':			if (status == 'active')
						{
							act.innerHTML = '<a href="javascript:TabRow();"><img class="action-icn" src="/images/actions/icn_indent.gif" /> Indent</a>';
							mini_act.innerHTML = '<a href="javascript:TabRow();" title="Indent"><img class="action-icn" src="/images/actions/icn_indent.gif" /></a>';
						}
						else
						{
							act.innerHTML = '<img class="action-icn" src="/images/actions/icn_indent_off.gif" /><span class="disabled"> Indent</span>';
							mini_act.innerHTML = '<img class="action-icn" src="/images/actions/icn_indent_off.gif" /><span class="disabled"></span>';
						}
						break;

	case 'shifttab':	if (status == 'active')
						{
							act.innerHTML = '<a href=\"javascript:ShiftTabRow();\"><img class="action-icn" src="/images/actions/icn_outdent.gif" /> Outdent</a>';
							mini_act.innerHTML = '<a href="javascript:ShiftTabRow();" title="Outdent"><img class="action-icn" src="/images/actions/icn_outdent.gif" /></a>';
						}
						else
						{
							act.innerHTML = '<img class="action-icn" src="/images/actions/icn_outdent_off.gif" /><span class="disabled"> Outdent</span>';
							mini_act.innerHTML = '<img class="action-icn" src="/images/actions/icn_outdent_off.gif" /><span class="disabled"></span>';
						}
						break;

	case 'up':			if (status == 'active')
						{
							act.innerHTML = '<a href="javascript:MoveUp();"><img class="action-icn" src="/images/actions/icn_moveup.gif" /> Move Up</a>';
							mini_act.innerHTML = '<a href="javascript:MoveUp();" title="Move Up"><img class="action-icn" src="/images/actions/icn_moveup.gif" /></a>';
						}
						else
						{
							act.innerHTML = '<img class="action-icn" src="/images/actions/icn_moveup_off.gif" /><span class="disabled"> Move Up</span>';
							mini_act.innerHTML = '<img class="action-icn" src="/images/actions/icn_moveup_off.gif" /><span class="disabled"></span>';
						}
						break;

	case 'down':		if (status == 'active')
						{
							act.innerHTML = '<a href="javascript:MoveDown();"><img class="action-icn" src="/images/actions/icn_movedown.gif" /> Move Down</a>';
							mini_act.innerHTML = '<a href="javascript:MoveDown();" title="Move Down"><img class="action-icn" src="/images/actions/icn_movedown.gif" /></a>';
						}
						else
						{
							act.innerHTML = '<img class="action-icn" src="/images/actions/icn_movedown_off.gif" /><span class="disabled"> Move Down</span>';
							mini_act.innerHTML = '<img class="action-icn" src="/images/actions/icn_movedown_off.gif" /><span class="disabled"></span>';
						}
						break;
	case 'collapse':		if (status == 'active')
						{
							act.innerHTML = '<a href="javascript:HideAll();"><img class="action-icn" src="/images/actions/icn_collapse.gif" /> Collapse All</a>';
						}
						else
						{
							act.innerHTML = '<img class="action-icn" src="/images/actions/icn_collapse_off.gif" /><span class="disabled"> Collapse All</span>';
						}
						break;
	case 'expand':		if (status == 'active')
						{
							act.innerHTML = '<a href="javascript:ShowAll();"><img class="action-icn" src="/images/actions/icn_expand.gif" /> Expand All</a>';
						}
						else
						{
							act.innerHTML = '<img class="action-icn" src="/images/actions/icn_expand_off.gif" /><span class="disabled"> Expand All</span>';
						}
						break;

	case 'import':		if (status == 'active')
						{
							act.innerHTML = '<a href="javascript:ShowImportBox();">Import</a>';
						}
						else
						{
							act.innerHTML = '<span class="disabled"> Import</span>';
						}
						break;
						
	case 'edit':		if (status == 'active')
						{
							act.innerHTML = '<a href="#" onclick="ShowEditBox();return false;"><img class="action-icn" src="/images/actions/icn_edit.gif" /></a>';
						}
						else
						{
							act.innerHTML = '<img class="action-icn" src="/images/actions/icn_edit_off.gif" /><span class="disabled"></span>';
						}
						break;
	}
}

function SetActionButtonStates()
{
	ToggleActionLink('delete', 'active');
	ToggleActionLink('tab', 'active');
	ToggleActionLink('shifttab', 'active');
	ToggleActionLink('up', 'active');
	ToggleActionLink('down', 'active');
	ToggleActionLink('add', 'active');
	ToggleActionLink('expand', 'active');
	ToggleActionLink('collapse', 'active');
	ToggleActionLink('edit', 'active');
}

function DisableActionButtons()
{
	ToggleActionLink('delete', 'inactive');
	ToggleActionLink('tab', 'inactive');
	ToggleActionLink('shifttab', 'inactive');
	ToggleActionLink('up', 'inactive');
	ToggleActionLink('down', 'inactive');
	ToggleActionLink('edit', 'inactive');
}

function ShowEditBox()
{
	alert('hi');
	document.getElementById('edit-box').style.display = "block";
//	FocusTextBox();
}

function HideEditBox()
{
	document.getElementById('edit-box').style.display = "none";
}

function FocusTextBox()
{
	document.getElementById('edit_textarea').focus();
}

function ShowImportBox()
{
	document.getElementById('edit-box').style.display = "none";
	document.getElementById('import-box').style.display = "block";
}

function HideImportBox()
{
	document.getElementById('import-box').style.display = "none";
	document.getElementById('edit-box').style.display = "block";
}

// text is always loaded into attributes array FIRST
// this function therefore always converts either to or from this array to whatever else we require
// direction specifies where the text is going
function NormaliseText(txt, direction)
{
//	alert(txt+' dir: '+direction);
	if (typeof txt === "undefined")
		return "";

	switch (direction)
	{
	case 'html': 	// text = txt.escapeHTML();							// dumping text from attributes obj to innerHTML of row
					// text = text.replace(/\"/g, "&quot;");
					// text = text.replace(/\n/g, "<br />");

					text = txt.replace(/&#10;/g, "<br />");
					text = autoHTML(text, 'target="_blank" onclick="gHandleClicked=true;"');
					break;

	case 'opml':	text = txt.replace(/&/g, "&amp;");					// dumping text from edit box to attributes obj
					text = text.replace(/\"/g, "&quot;");
					text = text.replace(/\n/g, "&#10;");
					text = text.replace(/</g, "&lt;");
					text = text.replace(/>/g, "&gt;");
					break;

	case 'opml2attr':
					text = txt.replace(/&/g, "&amp;");					// dumping text from opml to attributes obj
					text = text.replace(/&#38;/g, "&amp;");				// these would have got expanded by the XML parser,
					text = text.replace(/\"/g, "&quot;");				// so re-encode them
					text = text.replace(/\n/g, "&#10;");				
					text = text.replace(/</g, "&lt;");
					text = text.replace(/>/g, "&gt;");
					break;

	case 'edit':	text = txt.replace(/&#10;/g, "\n");					// loading text from atributes to edit box
					text = text.unescapeHTML();
					break;
	}

	return text;
}

function nl2br(txt)
{
	txt = txt.replace(/\r\n/g, "&#10;");
	txt = txt.replace(/\r/g, "&#10;");
	txt = txt.replace(/\n/g, "&#10;");

	return txt;
}

function autoHTML(strSrc, optAtt)
{
	strSrc = strSrc.replace(/&quot;/gi, '        ');			// replace " with 8 spaces, definitely not allowed in email addresses and urls :)

	strSrc = strSrc.replace(/(https:\/\/|http:\/\/|ftp:\/\/|www.)([a-zA-Z0-9_?.#-\/&%+=;]*)/gi, '<a href="$1//$2" ' + optAtt + '>$1$2</a>');
	strSrc = strSrc.replace(/www.\/\//gi, 'http://www.');
	
	strSrc = strSrc.replace(/[\s]*([^\s]*@[^\s]*)/gi,' <a href="mailto:$1">$1</a>');

	strSrc = strSrc.replace(/        /gi, '&quot;');
	return strSrc;
}