=begin rdoc
	ApplicationController class:: The filters added to this controller will be run for all controllers in the application. Likewise will all the methods added be available for all controllers.
	Author:: Ana Kovatcheva. 2005
	© Copyright:: iCookWare Team 2005  
=end

require_dependency "login_system"

	class ApplicationController < ActionController::Base
		include LoginSystem
		include DebugHelper
		
		model :user
		model :setting
		model :query
		#layout  'icw'
		
		before_filter :configure_charsets

		  def configure_charsets
		    @response.headers["Content-Type"] = "text/html; charset=utf-8"
		    # Set connection charset. MySQL 4.0 doesn't support this so it
		    # will throw an error, MySQL 4.1 needs this
		        suppress(ActiveRecord::StatementInvalid) do
		          ActiveRecord::Base.connection.execute 'SET NAMES UTF8'
		        end
		   end

		after_filter	:fix_unicode_for_safari
  
	   # automatically and transparently fixes utf-8 bug 
		 # with Safari when using xmlhttp
		def fix_unicode_for_safari 
			if @headers["Content-Type"] == "text/html; charset=utf-8" and
				@request.env['HTTP_USER_AGENT'].to_s.include? 'AppleWebKit' then @response.body =
				@response.body.gsub(/([^\x00-\xa0])/u) { |s| "&#x%x;" % $1.unpack('U')[0] } 
				end 		
		end

		end

						
