var cookies = {
	'set': function( name, value, expire, path, domain )
	{
		var expires = expire instanceof Date ? ("; expires=" + date.toGMTString()) : "",
			path = typeof( path ) == 'string' ? path : "/",
			hostname = location.hostname.substr( location.hostname.lastIndexOf('.', location.hostname.lastIndexOf('.') - 1) +1 ),
			domain = typeof( domain ) == 'string' ? domain : hostname;
		
		document.cookie = name + "=" + value 
			+ expires 
			+ "; path=" + path 
			+ '; domain=' + domain;
	},

	'get': function( name )
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for( var i=0, c; c = ca[i]; i++)
		{
			// strip leading spaces
			while( c.charAt(0) == ' ' ) 
				c = c.substring( 1, c.length );
			
			if( c.indexOf( nameEQ ) == 0 ) 
				return unescape( c.substring( nameEQ.length, c.length ) );
		}
		
		return null;
	},

	'del': function( name )
	{
		this.set( name, "", new Date( (new Date()).getMilliseconds() - 3600000 ) );
	}
};

var storage = new function()
{
	var data = {};

	function read()
	{
		if( window.name == '' )
		{
			data = {};
			return;
		}
			
		try
		{
			data = JSON.parse( window.name );
		}
		catch( e )
		{
			data = {};
			if( typeof console != 'undefined') {
				if( console && console.log )
					console.log( e );
			}
		}
	}

	function write()
	{
		try
		{
			window.name = JSON.stringify( data );
		}
		catch( e )
		{
			window.name = '';
			if( typeof console != 'undefined') {
				if( console && console.log )
					console.log( e );
			}
		}
	}


	this.set = function( key, value )
	{
		data[ key ] = value;
		write();
	};

	this.get = function( key )
	{
		var v = data[ key ];
		return v;
	};

	this.getAll = function()
	{
		return data;
	};

	this.del = function( key )
	{
		if( typeof( data[ key ] ) != 'undefined' )
			delete data[ key ];
			
		write();
	};

	this.flush = function()
	{
		data = {};
		write();
	};

	read();
};

/*
 *	If RS.user is not the one registered in storage, session must've been killed
 */
(	
	function()
	{
		if( typeof( RS ) == 'undefined' )
			RS = {};
		
		var c = RS && RS.user && RS.user.name ? RS.user.name : null,
			t = storage.get( 'user' );
			
		if( t != c )
		{
			storage.flush();
		}
		
		if( c )
			storage.set( 'user', c );
			
		if( !storage.get( 'ident' ) )
			storage.set( 'ident', ""+ (new Date()).getTime() );
		
		if( !RS.ident )
			RS.ident = storage.get( 'ident' );
		
		RS.primary = true; //RS.ident == storage.get( 'ident' );
			
		
// 		console.log( 'window ident', storage.get( 'ident' ), RS.primary ? "being primary window" : "being secondary window" );
	}
)();

/**
 * Enable in-string variables in a certain notation.<br />
 * Note: When passing delimiters, make sure to escape the escaped sequence,
 * since this is a String. Good: "<\\\%\\\(" Bad: "<\%\("
 * @class StringTemplate
 * @param {String} [leftDelim] left delimiter of replace tags (default: "%(")
 * @param {String} [rightDelim] right delimiter of replacable tags (default: ")")
 **/
function StringTemplate( leftDelim, rightDelim )
{
	// set default values
	if( !leftDelim )
		leftDelim = '\\\%\\\(';

	if( !rightDelim )
		rightDelim = '\\\)';

	/**
	 * Find String-variables and replace them by the values mapped in Object d
	 * @method eval
	 * @param {String} s The String to replace String-variables in
	 * @param {Object} d The Object containing the attributes which make up the dictionary
	 * @param {boolean} [nullifiy] If set to true all String-variables not replaced by values must be replaced by an empty string (defaults to false)
	 **/
	this.eval = function( s, d, nullify )
	{
		//if( typeof s != 'string' )
			//throw 's needs to be a string';

		for( a in d )
		{
			s = s.replace( new RegExp( leftDelim + a + rightDelim, 'g' ), d[a] );
		}

		if( !nullify )
			return s;

		// nullify remaining String-vars
		return s.replace( new RegExp( leftDelim + '\\w+' + rightDelim, 'g'  ), "" );
	}
}

function numberic( v )
{
	return parseInt( v, 10 ) || 0;
}


/*
 * find external links to open in new window...
 */
jQuery( function()
{
	jQuery( 'a.ext' ).each( function( i, o )
	{  
		var $o = jQuery( o );
		$o.click( function( e )
		{ 
			// abort on non-left-clicks
			if( e.which != 1 ) 
				return; 
				
			window.open( this.href ); 
			e.stopPropagation(); 
			e.preventDefault(); 
			return false; 
		} );
	} );
});