/*
 * AC Fry - JavaScript Framework v1.0
 *
 * UI core support extension
 *
 * (c)2006 Petr Krontorad, April-Child.com
 * Portions of code based on WHOA Bender Framework, (c)2002-2005 Petr Krontorad, WHOA Group.
 * http://www.april-child.com. All rights reserved.
 * See the license/license.txt for additional details regarding the license.
 * Special thanks to Matt Groening and David X. Cohen for all the robots.
 */

/*--------*/

if ( 'undefined' != typeof ACNode )
{
	/*  ---------------------------------------------------------------- 
		fry.ui namespace
	*/
	fry.ui = 
	{
		info:
		{
			// returns page scroll position
			page:
			{
				scroll:
				{
					left:0,
					top:0
				},
				width:0,
				height:0
			},
			isIE:$__tune.isIE,
			isSafari:$__tune.isSafari,
			isGecko:$__tune.isGecko
		},	
		util:
		{
			getMillis:function()
			{
				var d = new Date();
				return 60000*d.getMinutes()+1000*d.getSeconds()+d.getMilliseconds();
			}
		},
		format:
		{
			formatDateTime:function( timestamp, langCode )
			{
				var ret = '--';
				try
				{
					var s = parseInt( timestamp );
					var d = new Date( s*1000 );
					var m = d.getMinutes();
					var time_p = ' '+d.getHours()+':'+(10>m?'0':'')+m;
					switch ( langCode )
					{
						case 'cs':
						{
							ret = d.getDate()+'.'+(d.getMonth()+1)+' '+d.getFullYear()+time_p
						}; break;
						default:
						{
							ret = (d.getMonth()+1)+'/'+d.getDate()+'/'+d.getFullYear()+time_p
						}
					}
				}
				catch (e)
				{
				}
				return ret;
			}			
		},
		drag:
		{
			MODE_STANDARD:0,
			MODE_HORIZONTAL:1,
			MODE_VERTICAL:2,
			
			state:{started:false, x:0, y:0, ix:0, iy:0, clickInterval:300, mdownMillis:0, usingCursor:false}
		},
		dnd:
		{
			MODE_DRAG:1,
			MODE_DROP:2,
			MODE_BOTH:3,
			MODE_DRAG_POINTER:17,	// pointer mode - will select target under mouse cursor, not using cursor node vs target node intersection
			MODE_BOTH_POINTER:19,

			state:{started:false, x:0, y:0, ix:0, iy:0, offsetX:0, offsetY:0, lastCheck:0, lastCheckInterval:100, mdownMillis:0, clickInterval:300},
			targets: []
		}
	}

}

fry.ui.init = function()
{
	fry.ui.info.page.width = document.documentElement.offsetWidth || document.body.offsetWidth;
	fry.ui.info.page.height = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	var inf = $__tune.node.getPageScrollPosition();
	fry.ui.info.page.scroll = {left:inf[0], top:inf[1]};
	inf = null;
	$__tune.event.addListener(document.documentElement, 'mousemove', function(evt)
	{
		if ( !fry.ui.drag.state.started )
		{
			if ( fry.ui.dnd.state.started )
			{
				fry.ui.dnd.move(evt);
			}
		}
		else
		{
			fry.ui.drag.move(evt);				
		}
		evt = null;	
	});

	$__tune.event.addListener(document.documentElement, 'mouseup', function(evt)
	{
		if ( !fry.ui.drag.state.started )
		{
			if ( fry.ui.dnd.state.started )
			{
				fry.ui.dnd.stop();
			}
		}
		else
		{
			fry.ui.drag.stop();
		}
		evt = null;
	});	
}



$__tune.event.addListener(self, 'load', function(evt)
{
	if ( evt.removeListener )
	{
		evt.removeListener();		
	}
	fry.ui.init();
});



/*--------*/

if ( 'undefined' != typeof fry && 'undefined' != typeof fry.script )
{
	fry.script.register();
}