GanjaGears.App = function()
{
	GanjaGears.Tools.debug( 'Initializing a new app' );
	GanjaGears.runningApp = this;
	this.settings = {};
	if( !GanjaGears.Tools.checkGears() )
	{
		$( 'gearsSetup' ).innerHTML = 'To use revision control, you need to <a href="http://gears.google.com/?action=install&return=' + location.href + '">install Google Gears</a>';
		$( 'tagAutocompleteIndicator' ).innerHTML = '<a href="http://gears.google.com/?action=install&return=' + location.href + '">Tag autocompletion is OFF</a>';
		return false;
	}
	this.init();
}	

GanjaGears.App.defaultSettings = {
	'postVersioning' 	: 1,
	'tagAutocomplete'	: 1,
	'imagePool'			: 1
};

GanjaGears.App.postFormId = 'post_form';

GanjaGears.App.prototype.init = function( force )
{
	if( google.gears.factory.hasPermission || force )
	{
		this.postId = this.obtainPostId();
		this.postCode = this.obtainCode();
		if( this.postCode && this.postId ) this.mergeCode( this.postId, this.postCode );
		for( var item in GanjaGears.App.defaultSettings )
		{
			if( typeof item == 'string' )
			{
				this.settings[item] = this.getSetting( item );
				GanjaGears.Tools.debug( item + ' is ' + this.settings[item] );
			}
		}
		if( this.settings.postVersioning == 1 )
		{
			GanjaGears.Tools.debug( 'post version saving is on' );
			GanjaGears.Versions.enable();
		}
		else
		{
			GanjaGears.Tools.debug( 'post version saving is off' );
			GanjaGears.Versions.disable();
		}
		if( this.settings.tagAutocomplete == 1 )
		{
			GanjaGears.Tools.debug( 'tag autocomplete is on' )
			GanjaGears.Tags.enable();
		}
		else
		{
			GanjaGears.Tools.debug( 'tag autocomplete is off' )
			GanjaGears.Tags.disable();
		}
		/*if( this.settings.imagePool == 1 )
		{
			GanjaGears.Tools.debug( 'image pool is on' )
			GanjaGears.Images.enable();
		}
		else
		{
			GanjaGears.Tools.debug( 'image pool is off' )
			GanjaGears.Images.disable();
		}*/
		$( 'gearsSetup' ).innerHTML = '';
	}
	else
	{
		$( 'gearsSetup' ).innerHTML = 'To use revision control, you need to <a href="#" onclick="GanjaGears.runningApp.init( true );return false;">give</a> permission to Gears to use local resources.';
		$( 'tagAutocompleteIndicator' ).innerHTML = '<a href="#" onclick="GanjaGears.runningApp.init( true );return false;">Tag autocompletion is OFF</a>';
	}
}

GanjaGears.App.prototype.readSetting = function( settingName )
{
	var db = GanjaGears.DB.getDB();
	var re = db.execute( 'SELECT value FROM settings WHERE name = ?', [settingName] );
	var ret = null;
	if( re.isValidRow() ) ret = re.fieldByName( 'value' );
	re.close();
	GanjaGears.Tools.debug( 'Reading ' + settingName + ', the value is ' + ret );
	return ret;
}

GanjaGears.App.prototype.getSetting = function( settingName )
{
	var ret = this.readSetting( settingName );
	if( ret == null) ret = GanjaGears.App.defaultSettings[settingName];
	return ret;
}

GanjaGears.App.prototype.setSetting = function( settingName, value )
{
	var db = GanjaGears.DB.getDB();
	if( this.readSetting( settingName ) == null ) db.execute( 'INSERT INTO settings ( name, value ) VALUES ( ?, ? )', [settingName, value] );
	else db.execute( 'UPDATE settings SET value = ? WHERE name = ?', [value, settingName] );
	GanjaGears.Tools.debug( 'Setting ' + settingName + ' to ' + value );
}


GanjaGears.App.prototype.obtainCode = function()
{
	GanjaGears.Tools.debug( 'Obtaining code for post' );
	if( !this.postCode )
	{
		if( this.postId )
		{
			var db = GanjaGears.DB.getDB();
			var re = db.execute( 'SELECT code FROM postCodes WHERE postId = ?', [this.postId] );
			if( re.isValidRow() )
			{
				this.postCode = re.fieldByName( 'code' );
				GanjaGears.Tools.debug( 'Already have a code for this post in the DB' );
			}
			re.close();
		}
		if( !this.postCode )
		{
			var re = /postCode=([0-9a-z\#\%]+)/;
			var match = re.exec( location.href );
			if( match && match[1] )
			{
				this.postCode = decodeURIComponent( match[1] );
				GanjaGears.Tools.debug( 'Found the code in the URL' );
			}
			else
			{
				var date = new Date();
				this.postCode = 'post#' + date.getTime() + '#' + Math.round( Math.random() * 100 );
				GanjaGears.Tools.debug( 'Generating new code' );
			}
		}
		
	}
	GanjaGears.Tools.debug( 'Code is ' + this.postCode );
	return this.postCode;
}

GanjaGears.App.prototype.obtainPostId = function()
{
	if( !this.postId )
	{
		if( $( 'postId' ).value ) this.postId = $( 'postId' ).value;
	}
	GanjaGears.Tools.debug( 'Obtaining code postId: ' + this.postId );
	return this.postId;
}

GanjaGears.App.prototype.mergeCode = function( postId, postCode )
{
	GanjaGears.Tools.debug( 'Pairing code (' + postCode + ') with postId (' + postId + ')' );
	var db = GanjaGears.DB.getDB();
	var re = db.execute( 'SELECT * FROM postCodes WHERE code = ?', [postCode] );
	if( !re.isValidRow() )
	{
		db.execute( 'INSERT INTO postCodes ( postId, code ) VALUES( ?, ? )', [ postId, postCode ] );
	}
	else GanjaGears.Tools.debug( 'Already paired' );
	re.close();
}
