package fishrungames.bashgid;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import java.util.Locale;

import fishrungames.bashgid.core.AlbumManager.AlbumFullData;
import fishrungames.bashgid.core.AlbumManager.AlbumShortData;
import fishrungames.bashgid.core.ArticleManager.ArticleFullData;
import fishrungames.bashgid.core.ArticleManager.ArticleShortData;
import fishrungames.bashgid.core.ChannelManager.ChannelFullData;
import fishrungames.bashgid.core.DatabaseDownloadTask;

import fishrungames.bashgid.core.JournalManager.JournalRecordData;
import fishrungames.bashgid.core.JournalManager.JournalSemiFullData;
import fishrungames.bashgid.core.NewsDownloadTask;

import fishrungames.bashgid.core.VideoManager.VideoRecordData;
import fishrungames.bashgid.core.db.AlbumDataSource;
import fishrungames.bashgid.core.db.ArticleDataSource;
import fishrungames.bashgid.core.db.BashgidSqliteHelper;
import fishrungames.bashgid.core.db.ChannelDataSource;
import fishrungames.bashgid.core.db.JournalDataSource;
import fishrungames.bashgid.core.db.PhotoDataSource;
import fishrungames.bashgid.core.db.TranslationDataSource;
import fishrungames.bashgid.core.db.VideoDataSource;

import fishrungames.networkutils.ContextHolder;
import fishrungames.networkutils.ImageDownloadTask;
import fishrungames.networkutils.ImageManager;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import android.view.View;
import android.view.inputmethod.InputMethodManager;

import android.support.v4.widget.DrawerLayout;

public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks
{

	public static final String TAG_BACK_TO_MAINFRAGMENT = "TAG_BACK_TO_MAINFRAGMENT";
	public static final String TAG_BACK_TO_CATALOGFRAGMENT = "TAG_BACK_TO_CATALOGFRAGMENT";
	public static final String TAG_BACK_TO_ALBUMLISTFRAGMENT = "TAG_BACK_TO_ALBUMLISTFRAGMENT";
	
	
	public static final String TAG_BACK_TO_JOURNALFRAGMENT = "TAG_BACK_TO_JOURNALFRAGMENT";
	public static final String TAG_BACK_TO_ARTICLEFRAGMENT = "TAG_BACK_TO_ARTICLEFRAGMENT";
	public static final String TAG_BACK_TO_ALBUMFRAGMENT = "TAG_BACK_TO_ALBUMFRAGMENT";
	public static final String TAG_BACK_TO_CHANNELFRAGMENT = "TAG_BACK_TO_CHANNELFRAGMENT";
	
	public static final String TAG_BACK_TO_SEARCHFRAGMENT = "TAG_BACK_TO_SEARCHFRAGMENT";
	public static final String TAG_BACK_TO_MAPFRAGMENT = "TAG_BACK_TO_MAPFRAGMENT";
	
	public static final String TAG_BACK_TO_NEWSLISTFRAGMENT = "TAG_BACK_TO_NEWSLISTFRAGMENT";
	
	
	
	
	

	public static final String PREFS_MAIN = "PREFS_MAIN";

	public static final int NEWS_DOWNLOADER_STATE_UPDATE = 10;
	public static final int NEWS_DOWNLOADER_STATE_FINISHED = 11;

	public static final int DATABASE_DOWNLOADER_STATE_UPDATE = 12;
	public static final int DATABASE_DOWNLOADER_STATE_FINISHED = 13;

	public static final int DATABASE_GET_ALBUM_STATE_UPDATE = 14;
	public static final int DATABASE_GET_ALBUM_STATE_FINISHED = 15;

	public static final int DATABASE_GET_ALBUM_LIST_STATE_UPDATE = 16;
	public static final int DATABASE_GET_ALBUM_LIST_STATE_FINISHED = 17;

	public static final int DATABASE_GET_ARTICLE_STATE_UPDATE = 18;
	public static final int DATABASE_GET_ARTICLE_STATE_FINISHED = 19;

	public static final int DATABASE_GET_JOURNAL_STATE_UPDATE = 20;
	public static final int DATABASE_GET_JOURNAL_STATE_FINISHED = 21;

	public static final int DATABASE_GET_CHANNEL_STATE_UPDATE = 22;
	public static final int DATABASE_GET_CHANNEL_STATE_FINISHED = 23;

	public static final int DATABASE_GET_VIDEO_STATE_UPDATE = 24;
	public static final int DATABASE_GET_VIDEO_STATE_FINISHED = 25;

	public static final int DATABASE_GET_JOURNAL_LIST_STATE_UPDATE = 26;
	public static final int DATABASE_GET_JOURNAL_LIST_STATE_FINISHED = 27;

	public static final int DATABASE_SEARCH_ARTICLE_STATE_UPDATE = 28;
	public static final int DATABASE_SEARCH_ARTICLE_STATE_FINISHED = 29;

	public static final int DATABASE_GET_GEO_JOURNAL_LIST_STATE_UPDATE = 30;
	public static final int DATABASE_GET_GEO_JOURNAL_LIST_STATE_FINISHED = 31;
	
	

	MainPageFragment mainPageFragment = null;
	NewsListFragment newsListFragment = null;

	AlbumListFragment albumListFragment = null;
	PhotoAlbumFragment photoAlbumFragment = null;
	PhotoFragment photoFragment = null;

	ArticleFragment articleFragment = null;
	JournalFragment journalFragment = null;

	VideoListFragment videoListFragment = null;
	VideoRecordFragment videoRecordFragment = null;
	OsmMapHolderFragment mapHolderFragment = null;

	SearchFragment searchFragment = null;

	CatalogFragment catalogFragment = null;

	static MainActivity instance = null;

	private NavigationDrawerFragment mNavigationDrawerFragment = null;

	public BashgidSqliteHelper dbHelper = null;

	public PhotoDataSource photoDataSource = null;
	public AlbumDataSource albumDataSource = null;
	public ArticleDataSource articleDataSource = null;

	public VideoDataSource videoDataSource = null;
	public ChannelDataSource channelDataSource = null;

	public JournalDataSource journalDataSource = null;

	public TranslationDataSource translationDataSource = null;
	
	public static final SimpleDateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);

	public Handler mHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		ImageManager.getInstance().PredefinedImageId.put("R.drawable.no_picture", R.drawable.no_picture);

		setContentView(R.layout.activity_main);

		SetupDrawer();

		ImageManager.getInstance().alwaysUpdateImagesWithoutHash = false;

		getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new WelcomeFragment(), null).commit();

		instance = this;
		ContextHolder.setContext(instance);

		dbHelper = new BashgidSqliteHelper(this);

		photoDataSource = new PhotoDataSource(this, dbHelper);

		albumDataSource = new AlbumDataSource(this, dbHelper);

		articleDataSource = new ArticleDataSource(this, dbHelper);

		videoDataSource = new VideoDataSource(this, dbHelper);

		channelDataSource = new ChannelDataSource(this, dbHelper);

		journalDataSource = new JournalDataSource(this, dbHelper);

		translationDataSource = new TranslationDataSource(this, dbHelper);

		mHandler = new Handler(Looper.getMainLooper())
		{
			@Override
			public void handleMessage(Message inputMessage)
			{

				switch (inputMessage.what)
				{
				case ImageDownloadTask.IMAGE_DOWNLOADER_STATE_UPDATE:
				case ImageDownloadTask.IMAGE_DOWNLOADER_STATE_FINISHED:
					UpdateOnImageDownloaded();
					break;
				case NEWS_DOWNLOADER_STATE_UPDATE:
				case NEWS_DOWNLOADER_STATE_FINISHED:
					UpdateOnNewsDownloaded();
					break;
				case DATABASE_DOWNLOADER_STATE_UPDATE:
				case DATABASE_DOWNLOADER_STATE_FINISHED:
					UpdateOnDatabaseDownloaded();
					break;
				case DATABASE_GET_ALBUM_STATE_UPDATE:
				case DATABASE_GET_ALBUM_STATE_FINISHED:
					UpdateOnAlbumLoaded((AlbumFullData) inputMessage.obj);
					break;
				case DATABASE_GET_ALBUM_LIST_STATE_UPDATE:
				case DATABASE_GET_ALBUM_LIST_STATE_FINISHED:

					ArrayList<AlbumShortData> albumShortList = (ArrayList<AlbumShortData>) inputMessage.obj;

					UpdateOnAlbumListLoaded(albumShortList);
					break;
				case DATABASE_GET_ARTICLE_STATE_UPDATE:
				case DATABASE_GET_ARTICLE_STATE_FINISHED:
					UpdateOnArticleLoaded((ArticleFullData) inputMessage.obj);
					break;
				case DATABASE_GET_JOURNAL_STATE_UPDATE:
				case DATABASE_GET_JOURNAL_STATE_FINISHED:
					UpdateOnJournalLoaded((JournalSemiFullData) inputMessage.obj);
					break;
				case DATABASE_GET_CHANNEL_STATE_UPDATE:
				case DATABASE_GET_CHANNEL_STATE_FINISHED:
					UpdateOnChannelLoaded((ChannelFullData) inputMessage.obj);
					break;

				case DATABASE_GET_VIDEO_STATE_UPDATE:
				case DATABASE_GET_VIDEO_STATE_FINISHED:
					UpdateOnVideoLoaded((VideoRecordData) inputMessage.obj);
					break;

				case DATABASE_GET_JOURNAL_LIST_STATE_UPDATE:
				case DATABASE_GET_JOURNAL_LIST_STATE_FINISHED:
					UpdateOnJournalListLoaded((ArrayList<JournalSemiFullData>) inputMessage.obj);
					break;

				case DATABASE_SEARCH_ARTICLE_STATE_UPDATE:
				case DATABASE_SEARCH_ARTICLE_STATE_FINISHED:
					UpdateOnArticleSearchPerformed((ArrayList<ArticleShortData>) inputMessage.obj);
					break;

				case DATABASE_GET_GEO_JOURNAL_LIST_STATE_UPDATE:
				case DATABASE_GET_GEO_JOURNAL_LIST_STATE_FINISHED:
					UpdateOnGeoJournalListLoaded((ArrayList<JournalRecordData>) inputMessage.obj);
					break;

				default:
					super.handleMessage(inputMessage);
				}
			}

		};

		DownloadDatabase();

	}

	public static MainActivity getInstance()
	{
		return instance;

	}

	public void DownloadImageArr(String[] imageUrlArr)
	{

		Bundle innerQuery = new Bundle();

		innerQuery.putStringArray("imageUrlArr", imageUrlArr);

		ImageDownloadTask task = new ImageDownloadTask();

		task.mHandler = mHandler;

		task.execute(innerQuery);
	}

	public void DownloadImageArr(String[] imageUrlArr, String[] imageHashArr)
	{

		Bundle innerQuery = new Bundle();

		innerQuery.putStringArray("imageUrlArr", imageUrlArr);
		innerQuery.putStringArray("imageHashArr", imageHashArr);

		ImageDownloadTask task = new ImageDownloadTask();

		task.mHandler = mHandler;

		task.execute(innerQuery);
	}

	public void DownloadImage(String imageUrl)
	{
		this.DownloadImageArr(new String[] { imageUrl });
	}

	public void DownloadImage(String imageUrl, String imageHash)
	{

		this.DownloadImageArr(new String[] { imageUrl }, new String[] { imageHash });
	}

	public void DownloadDatabase()
	{
		DatabaseDownloadTask task = new DatabaseDownloadTask();

		task.mHandler = mHandler;

		task.execute();
	}

	public void ClearAllFragmentLinks()
	{
		mainPageFragment = null;
		newsListFragment = null;

		albumListFragment = null;
		photoAlbumFragment = null;
		photoFragment = null;

		articleFragment = null;
		journalFragment = null;

		videoListFragment = null;
		videoRecordFragment = null;
		mapHolderFragment = null;

		searchFragment = null;
		catalogFragment = null;

		InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
	}

	public void StartDownloadImagesTask(ArrayList<String> imageUrlArr, ArrayList<String> imageHashArr)
	{
		ImageDownloadTask task = new ImageDownloadTask();

		task.mHandler = mHandler;

		Bundle query = new Bundle();

		query.putStringArray("imageUrlArr", (String[]) imageUrlArr.toArray());

		task.execute(query);
	}

	public void StartDownloadNewsTask()
	{
		NewsDownloadTask task = new NewsDownloadTask();

		task.mHandler = mHandler;

		task.execute();
	}

	public void UpdateOnImageDownloaded()
	{
		if (mainPageFragment != null)
		{
			mainPageFragment.RefreshAdapter();
		}

		if (newsListFragment != null)
		{
			newsListFragment.RefreshAdapter();
		}
	}

	public void UpdateOnNewsDownloaded()
	{
		if (mainPageFragment != null)
		{
			mainPageFragment.RefreshJournal();
		}

		if (newsListFragment != null)
		{
			newsListFragment.RefreshJournal();
		}
	}

	public void UpdateOnDatabaseDownloaded()
	{

	}

	public void UpdateOnAlbumLoaded(AlbumFullData album)
	{
		if (photoFragment != null)
		{
			photoFragment.SetAlbum(album);
		}

		if (photoAlbumFragment != null)
		{
			photoAlbumFragment.SetAlbum(album);
		}
	}

	public void UpdateOnArticleLoaded(ArticleFullData article)
	{
		if (articleFragment != null)
		{
			articleFragment.SetArticle(article);
		}

		if (mapHolderFragment != null)
		{
			mapHolderFragment.SetArticle(article);
		}
	}

	public void UpdateOnAlbumListLoaded(ArrayList<AlbumShortData> albumList)
	{
		if (albumListFragment != null)
		{
			albumListFragment.SetAlbumList(albumList);
		}
	}

	public void UpdateOnJournalLoaded(JournalSemiFullData journal)
	{
		if (journalFragment != null)
		{
			journalFragment.SetJournal(journal);
		}

		if (newsListFragment != null)
		{
			newsListFragment.SetJournal(journal);
		}

		if (mainPageFragment != null)
		{
			mainPageFragment.SetJournal(journal);
		}

		if (mapHolderFragment != null)
		{
			mapHolderFragment.SetJournal(journal);
		}

	}

	public void UpdateOnChannelLoaded(ChannelFullData channel)
	{

		if (videoListFragment != null)
		{
			videoListFragment.SetChannel(channel);
		}
	}

	public void UpdateOnVideoLoaded(VideoRecordData video)
	{

		if (videoRecordFragment != null)
		{
			videoRecordFragment.SetVideo(video);
		}
	}

	public void UpdateOnJournalListLoaded(ArrayList<JournalSemiFullData> journalList)
	{

		if (catalogFragment != null)
		{
			catalogFragment.SetJournalList(journalList);
		}

		if (mainPageFragment != null)
		{
			mainPageFragment.SetJournalList(journalList);
		}

	}

	public void UpdateOnGeoJournalListLoaded(ArrayList<JournalRecordData> journalList)
	{

		if (mapHolderFragment != null)
		{
			mapHolderFragment.SetGeoJournals(journalList);
		}

	}

	public void UpdateOnArticleSearchPerformed(ArrayList<ArticleShortData> articleArr)
	{

		if (searchFragment != null)
		{
			searchFragment.SetSearchResult(articleArr);
		}
	}

	public void OnSearchPressed()
	{
		if (searchFragment != null)
		{
			return;
		}

		if (journalFragment != null)
		{
			if (journalFragment.journal == null)
			{
				OpenSearchScreen(journalFragment.journalName, "");
			} else
			{
				OpenSearchScreen(journalFragment.journalName, journalFragment.journal.title);
			}
			return;
		}

		if (newsListFragment != null)
		{
			String newsTitle = getResources().getString(R.string.news_header);
			OpenSearchScreen("news", newsTitle);
			return;
		}

		OpenSearchScreen();
	}

	public void OnSelectEnglish(View view)
	{
		Locale.setDefault(Locale.US);
		Configuration config = new Configuration();
		config.locale = Locale.US;
		getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

		getSupportActionBar().setTitle(R.string.app_name);

		SetupDrawer();

		OpenMainScreen();
	}

	public void OnSelectRussian(View view)
	{
		Locale locale = new Locale("ru");
		Locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

		getSupportActionBar().setTitle(R.string.app_name);
		SetupDrawer();

		OpenMainScreen();
	}

	public void OnSelectChinese(View view)
	{
		Locale.setDefault(Locale.CHINA);
		Configuration config = new Configuration();
		config.locale = Locale.CHINA;
		getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

		getSupportActionBar().setTitle(R.string.app_name);
		SetupDrawer();

		OpenMainScreen();
	}

	public void OpenMainScreen()
	{
		ClearAllFragmentLinks();

		mainPageFragment = new MainPageFragment();

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mainPageFragment).commit();

		mNavigationDrawerFragment.EnableDrawer();

	}

	public void OpenNewsScreen()
	{

		ClearAllFragmentLinks();

		newsListFragment = new NewsListFragment();

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, newsListFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();

	}

	public void OpenCatalogScreen()
	{

		ClearAllFragmentLinks();

		catalogFragment = new CatalogFragment();

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, catalogFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();

	}

	public void OpenJournalScreen(String journalName, String tag)
	{

		ClearAllFragmentLinks();

		journalFragment = new JournalFragment(journalName);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, journalFragment, tag).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenSettingsScreen()
	{
		ClearAllFragmentLinks();

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SettingsFragment()).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenSearchScreen()
	{
		ClearAllFragmentLinks();

		searchFragment = new SearchFragment();

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, searchFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenSearchScreen(String query)
	{
		ClearAllFragmentLinks();

		searchFragment = new SearchFragment(query);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, searchFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenSearchScreen(String journalName, String journalTitle)
	{
		ClearAllFragmentLinks();

		searchFragment = new SearchFragment(journalName, journalTitle);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, searchFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}
	
	public void OpenSearchScreen(String query, String journalName, String journalTitle)
	{
		ClearAllFragmentLinks();

		searchFragment = new SearchFragment(query, journalName, journalTitle);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, searchFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenMapScreen(String filter, String tag)
	{
		ClearAllFragmentLinks();

		mapHolderFragment = new OsmMapHolderFragment(0, filter);
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mapHolderFragment, tag).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenMapScreen(ArticleFullData articleFullData, String tag)
	{
		ClearAllFragmentLinks();

		mapHolderFragment = new OsmMapHolderFragment(articleFullData);
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mapHolderFragment, tag).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenPhotoAlbumListScreen()
	{
		ClearAllFragmentLinks();

		albumListFragment = new AlbumListFragment();

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, albumListFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenPhotoAlbumScreen(String albumName)
	{
		ClearAllFragmentLinks();

		photoAlbumFragment = new PhotoAlbumFragment(albumName);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, photoAlbumFragment, TAG_BACK_TO_ALBUMLISTFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenPhotoScreen(String albumName, int position, String tag)
	{
		ClearAllFragmentLinks();

		photoFragment = new PhotoFragment(albumName, position);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, photoFragment, tag).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenPhotoScreen(AlbumFullData album, int position, String tag)
	{
		ClearAllFragmentLinks();

		photoFragment = new PhotoFragment(album, position);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, photoFragment, tag).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenArticleScreen(String articleName, String tag)
	{

		ClearAllFragmentLinks();

		articleFragment = new ArticleFragment(articleName);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, articleFragment, tag).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void OpenVideoListScreen(String channelName)
	{
		ClearAllFragmentLinks();

		videoListFragment = new VideoListFragment(channelName);
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, videoListFragment, TAG_BACK_TO_MAINFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();

	}

	public void OpenVideoRecordScreen(VideoRecordData video)
	{
		// Xperimental -- addToBackStack provoke error "Class not found". Need
		// to resolve somehow!
		// getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
		// new NewsRecordFragment(newsRecord)).addToBackStack(null).commit();

		// I added workaround:

		ClearAllFragmentLinks();

		videoRecordFragment = new VideoRecordFragment(video);

		getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, videoRecordFragment, TAG_BACK_TO_CHANNELFRAGMENT).commit();

		mNavigationDrawerFragment.EnableDrawer();
	}

	public void UpdateMainScreenIfPossible(Bundle result)
	{
		if (mainPageFragment != null)
		{
			mainPageFragment.RefreshJournal();
		}
	}

	@Override
	public void onAttachFragment(Fragment fragment)
	{
		super.onAttachFragment(fragment);

	}

	public void SetupDrawer()
	{

		mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

		if (mNavigationDrawerFragment != null)
		{

			// Set up the drawer.
			mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.global, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();

		if (id == R.id.action_search)
		{
			OnSearchPressed();
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onNavigationDrawerItemSelected(int position)
	{
		if (position == 1)
		{
			OpenMainScreen();
		} else if (position == 2)
		{
			OpenNewsScreen();

		} else if (position == 3)
		{
			OpenCatalogScreen();
		} else if (position == 4)
		{
			OpenMapScreen("map", MainActivity.TAG_BACK_TO_MAINFRAGMENT);
		} else if (position == 5)
		{
			OpenVideoListScreen("main");
		} else if (position == 6)
		{
			OpenPhotoAlbumListScreen();
		} else if (position == 7)
		{
			OpenJournalScreen("investor", MainActivity.TAG_BACK_TO_MAINFRAGMENT);
		} else if (position == 8)
		{
			OpenSettingsScreen();
		}
	}

	@Override
	public void onBackPressed()
	{

	
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_MAPFRAGMENT) != null)
		{
			OpenMapScreen(GetLastMapFilterName(), TAG_BACK_TO_MAINFRAGMENT);
			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_NEWSLISTFRAGMENT) != null)
		{
			OpenNewsScreen();
			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_SEARCHFRAGMENT) != null)
		{
			OpenSearchScreen(GetLastSearchQuery(), GetLastJournalName(), GetLastJournalTitle());
			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_JOURNALFRAGMENT) != null)
		{
			OpenJournalScreen(GetLastJournalName(), TAG_BACK_TO_MAINFRAGMENT);
			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_ARTICLEFRAGMENT) != null)
		{
			OpenArticleScreen(GetLastArticleName(), TAG_BACK_TO_MAINFRAGMENT);
			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_ALBUMFRAGMENT) != null)
		{
			OpenPhotoAlbumScreen(GetLastAlbumName());
			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_CHANNELFRAGMENT) != null)
		{
			OpenVideoListScreen(GetLastChannelName());
			return;
		}		
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_CATALOGFRAGMENT) != null)
		{
			OpenCatalogScreen();
			return;
		}
		
		if (getSupportFragmentManager().findFragmentByTag(TAG_BACK_TO_MAINFRAGMENT) != null)
		{
			OpenMainScreen();
			return;
		}
		
		
		super.onBackPressed();

	}

	public void SetLastAlbumName(String lastAlbumName)
	{
		SharedPreferences settings = MainActivity.getInstance().getSharedPreferences(PREFS_MAIN, 0);

		SharedPreferences.Editor editor = settings.edit();
		editor.putString("lastAlbumName", lastAlbumName);
		editor.commit();
	}

	public String GetLastAlbumName()
	{
		SharedPreferences settings = MainActivity.getInstance().getSharedPreferences(PREFS_MAIN, 0);
		return settings.getString("lastAlbumName", "");
	}
	
	public void SetLastChannelName(String lastChannelName)
	{
		SharedPreferences settings = MainActivity.getInstance().getSharedPreferences(PREFS_MAIN, 0);

		SharedPreferences.Editor editor = settings.edit();
		editor.putString("lastChannelName", lastChannelName);
		editor.commit();
	}

	public String GetLastChannelName()
	{
		SharedPreferences settings = MainActivity.getInstance().getSharedPreferences(PREFS_MAIN, 0);
		return settings.getString("lastChannelName", "");
	}

	public void SetLastArticleName(String lastArticleName)
	{
		SharedPreferences settings = MainActivity.getInstance().getSharedPreferences(PREFS_MAIN, 0);

		SharedPreferences.Editor editor = settings.edit();
		editor.putString("lastArticleName", lastArticleName);
		editor.commit();
	}

	public String GetLastArticleName()
	{
		SharedPreferences settings = MainActivity.getInstance().getSharedPreferences(PREFS_MAIN, 0);
		return settings.getString("lastArticleName", "");
	}
	
	
	
	public void SetLastJournalName(String lastJournalName)
	{
		SharedPreferences settings = MainActivity.getInstance().getSharedPreferences(PREFS_MAIN, 0);

		SharedPreferences.Editor editor = settings.edit();
		editor.putString("lastJournalName", lastJournalName);
		editor.commit();
	}

	
	public String GetLastJournalName()
	{
		SharedPreferences settings = MainActivity.getInstance().getSharedPreferences(PREFS_MAIN, 0);
		return settings.getString("lastJournalName", "");
	}
	
		
	public void SetLastJournalTitle(String lastJournalTitle)
	{
		SharedPreferences settings = MainActivity.getInstance().getSharedPreferences(PREFS_MAIN, 0);

		SharedPreferences.Editor editor = settings.edit();
		editor.putString("lastJournalTitle", lastJournalTitle);
		editor.commit();
	}
	
	public String GetLastJournalTitle()
	{
		SharedPreferences settings = MainActivity.getInstance().getSharedPreferences(PREFS_MAIN, 0);
		return settings.getString("lastJournalTitle", "");
	}
	
	public void SetLastSearchQuery(String searchQuery)
	{
		SharedPreferences settings = MainActivity.getInstance().getSharedPreferences(PREFS_MAIN, 0);

		SharedPreferences.Editor editor = settings.edit();
		editor.putString("searchQuery", searchQuery);
		editor.commit();
	}

	public String GetLastSearchQuery()
	{
		SharedPreferences settings = MainActivity.getInstance().getSharedPreferences(PREFS_MAIN, 0);
		return settings.getString("searchQuery", "");
	}

	public void SetLastMapFilterName(String lastMapFilterName)
	{
		SharedPreferences settings = MainActivity.getInstance().getSharedPreferences(PREFS_MAIN, 0);

		SharedPreferences.Editor editor = settings.edit();
		editor.putString("lastMapFilterName", lastMapFilterName);
		editor.commit();
	}

	public String GetLastMapFilterName()
	{
		SharedPreferences settings = MainActivity.getInstance().getSharedPreferences(PREFS_MAIN, 0);
		return settings.getString("lastMapFilterName", "");
	}

	@Override
	protected void onResume()
	{

		super.onResume();
	}

	@Override
	protected void onPause()
	{
		super.onPause();
	}

}
