com = {};
com.roqz = {};
com.roqz.net = {}
com.roqz.net.JSON = {
	decode : function( string )
	{
		if ( typeof string != 'string' || !string.length )
		{
			return null;
		}
		
//		if ( !(/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(string.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '') ) )
//		{
//			alert('danger');
//			return null;
//		}
		
		return eval('(' + string + ')');
	}
}

com.roqz.net.HTTPRequest = function ()
{
	this.url			= '';
	this.callback		= '';
	this.loader			= '';
	this.form;
	this.button;
	this.post			= {};
	this.mimeType		= 'text/html';		// 'text/xml'request.mimeType	= "text/xml";
	this.mode			= 'JSON';			// 'JSON', 'TEXT', 'XML'
	this.method			= 'POST';
	
	var httpRequest		= false;			// protected
	var parent			= this;				// Otherwise object is not available from onreadystatechange function call
	
	this.send = function()
	{
		var parameters	= '';
		
		// Mozilla, webkit,...
		if (window.XMLHttpRequest) 
		{
			this.httpRequest = new XMLHttpRequest();
			
			if ( this.mode != 'XML' )
			{
				if ( this.httpRequest.overrideMimeType )
				{
					this.httpRequest.overrideMimeType( this.mimeType );
				}
			}
		}
		// Internet Explorer
		else if ( window.ActiveXObject )
		{
			try
			{
				this.httpRequest = new ActiveXObject('Msxml2.XMLHTTP');
			}
			catch (e)
			{
				try
				{
					this.httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
				}
				catch (e)
				{
					
				}
			}
		}
		
		if ( !this.httpRequest )
		{
			alert('Please update your browser.');
			return false;
		}
		
		if ( this.loader )
		{
			var loaderElement = document.getElementById( this.loader );
			loaderElement.style.visibility = "visible";
		}
		
		if ( this.button )
		{
			document.getElementById( this.button ).disabled = true;
		}
		
		if ( this.form )
		{
			this.post = this.getForm( this.form );
		}
		
		for ( var i in this.post )
		{
			parameters += i + "=" + escape( this.post[i] ) + "&";
		}

		this.httpRequest.open( this.method, this.url, true );
		this.httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//this.httpRequest.setRequestHeader("Content-length", parameters.length );
		//this.httpRequest.setRequestHeader("Connection", "close");
		this.httpRequest.onreadystatechange = this.response;
		this.httpRequest.send( parameters );
	};
	
	this.response = function()
	{
		if ( parent.httpRequest.readyState == 4 )
		{
			if ( parent.httpRequest.status == 200 )
			{
				if ( dbg ) 
				{
					alert(parent.httpRequest.responseText);
				}
				
				switch ( parent.mode )
				{
					case 'XML':
					
						parent.callback( parent.httpRequest.responseXML.documentElement );
						break;
						
					case 'JSON':
						parent.callback( com.roqz.net.JSON.decode( parent.httpRequest.responseText ) );
						break;
				}
			}
			else
			{
				//alert( "Oops, cannot load the page: " + parent.httpRequest.status );
			}
			
			if ( parent.loader )
			{
				var loaderElement = document.getElementById( parent.loader );
				loaderElement.style.visibility = "hidden";
			}
			
			if ( parent.button )
			{
				document.getElementById( parent.button ).disabled = false;
			}
		}		
	};
	
	this.getForm = function( name )
	{
		var values = {};
		var elements = document.forms[name].elements;
		var total = elements.length;
		var counter = 0;
		
		for ( counter; counter < total; counter++)
		{
			var element = elements[counter];
	
			if ( element.type == "text" || element.type == "password" )
			{
				values[element.name] = element.value;
			}
		/*	
			if (obj.childNodes[i].type == "checkbox")
				{
					if (obj.childNodes[i].checked)
					{
						getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
					}
					else
					{
						getstr += obj.childNodes[i].name + "=&";
					}
				}
				if (obj.childNodes[i].type == "radio")
				{
					if (obj.childNodes[i].checked)
					{
						getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
					}
				}
			*/
		}
		
		return values;	
	}
}



/*****************************************************************************************************************************
 * Tween class to animate html blocks
 *****************************************************************************************************************************/
com.roqz.transitions = {};
com.roqz.transitions.Tween = function( id, property, easing, begin, finish, duration, callback )
{
	var _self			= this;													// Self reference for timer callback
	var stepCurrent		= 0;													// Current step number
	var begin			= begin;												// Start position/number
	var finish			= finish;												// End position/number
	var callback		= callback;												// Function to call when the tween is complete
	var property		= property;												// Which property to change ( x, y, alpha, width, height )
	var stepSize		= ( finish - begin ) / ( duration / 33 );				// Total change in one step
	var stepTotal		= Math.abs( ( finish - begin ) / stepSize ); 			// Total number of steps
	var stepValue		= begin;												// Current value...
	var stepTime		= setInterval( function() { _self.step(); }, 33 );		// Timer to perform next tween step
	
	if ( typeof id == "object" )
	{
		var element = id;
	}
	else
	{
		var element = document.getElementById( id );
	}
	
	if ( ( property == 'alpha' ) && ( begin == 0 ) )
	{
		element.style.opacity		= 0;
		element.style.MozOpacity	= 0;
		element.style.KhtmlOpacity	= 0;
		element.style.filter		= "alpha(opacity=0)";
		element.style.display = 'block';
	}
	
	this.step = function()
	{
		if ( stepCurrent > stepTotal )
		{
			clearInterval( stepTime );
			
			stepValue = finish;
			
			setTimeout( function() { _self.change(); }, 33) 
			
			if ( ( property == 'alpha' ) && ( finish == 0 ) )
			{
				element.style.display = 'none';
			}
			
			if ( callback )
			{
				callback();
			}
		}
		else
		{
			this.change();

			stepValue = easing( stepCurrent * 33, begin, ++stepCurrent * stepSize, duration );
		}
	}

	this.change = function()
	{
		switch ( property )
		{
			/**
			 * Move over x axis
			 */
			case 'x':
				element.style.left 		= Math.round( stepValue ) + "px";
				break;
	
			/**
			 * Move over y axis
			 */
			case 'y':
				element.style.top		= Math.round( stepValue ) + "px";
				break;
	
			/**
			 * Change width
			 */
			case 'width':
				element.style.width		= Math.round( stepValue ) + "px";
				break;
				
			/**
			 * Change height
			 */
			case 'height':
				element.style.height	= Math.round( stepValue ) + "px";
				break;
				
			/**
			 * Change alpha
			 *
			 * Needs browser check for compatibility 
			 */
			case 'alpha':
				element.style.opacity		= stepValue;
				element.style.MozOpacity	= stepValue;
				element.style.KhtmlOpacity	= stepValue;
				element.style.filter		= "alpha( opacity=" + stepValue * 100 + ")";
				break;
		}
	}
}

com.roqz.transitions.easing = {
	linear : function ( t, b, c, d )
	{
		t /= d;
		return b + c * ( t );
	},
	outBack : function ( t, b, c, d )
	{
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(4*tc + -9*ts + 6*t);
	},
	inElastic : function(t, b, c, d)
	{
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(33*tc*ts + -59*ts*ts + 32*tc + -5*ts);
	},
	backInCubic : function (t, b, c, d)
	{
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(4*tc + -3*ts);
	},
	outBackCubic : function(t, b, c, d)
	{
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(4*tc + -9*ts + 6*t);
	}
}

com.roqz.Util = {
	trace : function ( object )
	{
		var string = '';
		
		for ( key in object )
		{
			string += key + " = " + object[key] + "\n";
		}
		
		alert( string );
	},
	mousePosition : function(e)
	{
		var posx = 0;
		var posy = 0;
		
		if (!e)
		{
			var e = window.event;
		}
	
		if (e.pageX || e.pageY)
		{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY)
		{
			posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		
		return [posx,posy]
	}
}

com.roqz.cookie = {
	get : function ( name )
	{
		var cookies = document.cookie.split('; ');
		var total = cookies.length;
		var name = name + '=';
		var counter = 0;
		var cookie = '';
		
		for ( counter; counter < total, cookie = cookies[counter]; counter++ )
		{
			while ( cookie.charAt( 0 ) == ' ' )
			{
				cookie = cookie.substring( 1, cookie.length );
			}

			if ( cookie.indexOf( name ) === 0 )
			{
				return cookie.substring( name.length, cookie.length );
			}
		}
		
		return null;
	},
	set : function ( name, value )
	{
		var domain = document.domain.replace( 'www.', '' );
		var date = new Date();
		date.setTime( date.getTime() + ( 31536000000 ) );

		document.cookie = name + "=" + escape( value ) + "; expires=" + date.toGMTString() + "; path=/; domain=." + domain;
	}
}

com.roqz.Tooltip = {
	init : function( element )
	{
		if ( start = document.getElementById( element ) )
		{
			var elements = start.getElementsByTagName("*");
			var total = elements.length;
			var counter = 0;
	
			var tooltip = document.createElement( "div" );
			tooltip.style.position = "absolute";
			tooltip.className = "tooltip";
			
			document.body.appendChild( tooltip );
			
			for ( counter; counter < total; counter++ )
			{
				var element = elements[counter];
				
				if ( element.hasAttribute('tooltip') )
				{
					element.onmouseover = function(e)
					{
						tooltip.style.display = "block";
						tooltip.innerHTML = this.getAttribute('tooltip');
						//com.roqz.Tooltip.update( tooltip, e );
					}
					
					element.onmousemove = function( e ) {
						var position = com.roqz.Util.mousePosition( e );
							
						tooltip.style.left = position[0] + 12 + "px";
						tooltip.style.top = position[1] + 5 + "px";
					}
					
					element.onmouseout = function() {
						tooltip.style.display = "none";
					}
				}
			}
		}
	},
}

ROQZ = new function()
{
	var userAgent = navigator.userAgent.toLowerCase();
	
	this.browser = {
		version : ( userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
		webkit : /webkit/.test( userAgent ),
		opera : /opera/.test( userAgent ),
		msie : (/msie/.test( userAgent )) && (!/opera/.test( userAgent )),
		mozilla : (/mozilla/.test(userAgent)) && (!/(compatible|webkit)/.test( userAgent ))
	}
}();

ROQZ.Event = {};

ROQZ.Event.DomReady = new function()
{
	var initialised = false;
	var ready = false;
	var queue = [];
	
	this.add = function( fn )
	{
		if ( ready )
		{
			fn();	
		}
		else
		{
			queue[queue.length] = fn;
			
			if ( !initialised )
			{
				initialised = true;
				init();
			}
		}
	};
	
	function init()
	{
		// Mozilla, Opera (see further below for it) and webkit nightlies currently support this event
		if ( document.addEventListener && !ROQZ.browser.opera )
		{
			// Use the handy event callback
			document.addEventListener( "DOMContentLoaded", run, false );
		}
		
		// If IE is used and is not in a frame
		// Continually check to see if the document is ready
		if ( ROQZ.browser.msie && window == top )
		{
			(function()
			{
				if ( ready )
				{
					return;
				}
				
				try
				{
					// If IE is used, use the trick by Diego Perini
					// http://javascript.nwbox.com/IEContentLoaded/
					document.documentElement.doScroll("left");
				}
				catch( error )
				{
					setTimeout( arguments.callee, 0);
					return;
				}
				// and execute any waiting functions
				run();
			})();
		}
	};
	
	if( ROQZ.browser.opera )
	{
		document.addEventListener( "DOMContentLoaded", function ()
		{
			if ( ready ) return;
			
			for ( var i = 0; i < document.styleSheets.length; i++ )
			{
				if (document.styleSheets[i].disabled)
				{
					setTimeout( arguments.callee, 0 );
					return;
				}
			// and execute any waiting functions
				run();
			}
		}, false );
	}
	
	if( ROQZ.browser.webkit )
	{
		var numStyles;
		
		(function()
		{
			if ( ready ) return;
			
			if (document.readyState != "loaded" && document.readyState != "complete")
			{
				setTimeout( arguments.callee, 0 );
				return;
			}
			
			if (numStyles === undefined)
			{
				var links = document.getElementsByTagName("link");
				
				for (var i=0; i < links.length; i++)
				{
					if(links[i].getAttribute('rel') == 'stylesheet')
					{
						numStyles++;
					}
				}
				
				var styles = document.getElementsByTagName("style");
				numStyles += styles.length;
			}
			
			if (document.styleSheets.length != numStyles)
			{
				setTimeout( arguments.callee, 0 );
				return;
			}
			
				// and execute any waiting functions
			run();
		})();
	}
	
	
	function run()
	{
		ready = true;
		
		var counter = 0;
		var total = queue.length;
		
		for ( counter; counter < total; counter++ )
		{
			queue[counter]();
			queue[counter] = null;
		}
	};
};

ROQZ.Dom = {};
ROQZ.Dom.element = {
	position : function( element )
	{
		var element = document.getElementById( element );
		var left = 0;
		var top = 0;
/*		
		if ( element.offsetTop )
		{
		   top = element.offsetTop;
		}
		
		if ( element.offsetLeft )
		{
		   left = element.offsetLeft;
		}
		
		var parent = element.offsetParent;
		
		while ( parent )
		{
			if ( parent.offsetTop )
			{
				top += parent.offsetTop;
			}
			
			if ( element.offsetLeft )
			{
			   left += parent.offsetLeft;
			}
			
			parent = parent.offsetParent;
		}
		
*/		
	
		if ( element.offsetParent )
		{
			do {
				left += element.offsetLeft;
				top += element.offsetTop;
			}
			while ( element = element.offsetParent );
		}
		
		return [left, top];
	}
};
//alert( ROQZ.browser.version );
