# Expects properties:
#   title
#   focus
#   header_stuff
#   section

# url used as referer

append url [ad_url] [ad_return_url]

if { ![info exists section] } {
    set section {}
}

if { ![info exists header_stuff] } {
    set header_stuff {}
}

if { [template::util::is_nil subnavbar_link] } {
    set subnavbar_link ""
}

# DRB: Hack to ensure that subgroups keep the same color as their ultimate club or
# class parent.  A top-level community that's not a class or club will keep the
# top-level Selva colors.

set url_image "/resources/theme-ges/theme-ges/images/picwhere.gif"
set path "comun"
set css_region ""

#We most get the correct place to load the correc CSS
set user_id [ad_conn untrusted_user_id]

#Getting the style css and layour for the render
if { [string match /dotlrn/clubs/* [ad_conn url]] } {
    set community_id [dotlrn_community::get_community_id]
    if {![empty_string_p $community_id]} {
	set comm_type [dotlrn_community::get_community_type_from_community_id $community_id]
	if {[string equal $comm_type "dotlrn_club"]} {
	    set css_url [parameter::get_from_package_key -package_key "theme-ges" -parameter "ComunidadCss"]
	    set path "comunidad"
	} else {
	    set css_url [parameter::get_from_package_key -package_key "theme-ges" -parameter "GrupoCss"]
	    set path "grupo"
	}
	set page_num [ns_queryget page_num]
	if {![empty_string_p $page_num]} {
	    set portal_id [dotlrn_community::get_portal_id -community_id $community_id]
	    set css_region [portal::get_page_header_stuff -portal_id $portal_id -page_num $page_num]
	} else {
	    # Getting the layour for the admin of clubs
	    if { [string match *one-community-admin [ad_conn url]] } {
		set portal_id [dotlrn_community::get_admin_portal_id -community_id $community_id]
		set css_region [portal::get_page_header_stuff -portal_id $portal_id -page_num 0]
	    }
	}
    } else {
	set page_num [ns_queryget page_num]
	if {![empty_string_p $page_num]} {
	    set portal_id [dotlrn::get_portal_id -user_id $user_id]
	    set css_region [portal::get_page_header_stuff -portal_id $portal_id -page_num $page_num]
	} 
	set css_url [parameter::get_from_package_key -package_key "theme-ges" -parameter "ComunCss"]
	set path "comun"
    }
} elseif { [string match /dotlrn/classes/* [ad_conn url]] } {
    set community_id [dotlrn_community::get_community_id]
    set comm_type [dotlrn_community::get_community_type_from_community_id $community_id]
    if { $comm_type eq "dotlrn_community" || $comm_type eq "dotlrn_pers_community" } {
	set css_url [parameter::get_from_package_key -package_key "theme-ges" -parameter "GrupoCss"]
	set path "grupo"
    } else {
	set css_url [parameter::get_from_package_key -package_key "theme-ges" -parameter "ClaseCss"]
	set path "clase"
    }
    set page_num [ns_queryget page_num]
    if {![empty_string_p $page_num]} {
	set portal_id [dotlrn_community::get_portal_id -community_id $community_id]
	set css_region [portal::get_page_header_stuff -portal_id $portal_id -page_num $page_num]
    } else {
        # Getting the layour for the admin of class
	if { [string match *one-community-admin [ad_conn url]] } {
	    set portal_id [dotlrn_community::get_admin_portal_id -community_id $community_id]
	    set css_region [portal::get_page_header_stuff -portal_id $portal_id -page_num 0]
	}
    }
} else {
    set page_num [ns_queryget page_num]
    if {![empty_string_p $page_num]} {
	set portal_id [dotlrn::get_portal_id -user_id $user_id]
	set css_region [portal::get_page_header_stuff -portal_id $portal_id -page_num $page_num]
    } 
    set css_url [parameter::get_from_package_key -package_key "theme-ges" -parameter "ComunCss"]
    set path "comun"
}

# Get system name
set system_name [ad_system_name]
set system_url [ad_url]
if { [string equal [ad_conn url] "/"] } {
    set system_url ""
}

# Get user information
set sw_admin_p 0
set untrusted_user_id [ad_conn untrusted_user_id]
if { $untrusted_user_id != 0 } {
    set user_name [person::name -person_id $untrusted_user_id]
    set pvt_home_url [ad_pvt_home]
    set pvt_home_name [ad_pvt_home_name]
    if [empty_string_p $pvt_home_name] {
	set pvt_home_name [_ acs-subsite.Your_Account]
    }
    set logout_url [ad_get_logout_url]

    # Site-wide admin link
    set admin_url {}

    set sw_admin_p [acs_user::site_wide_admin_p -user_id $untrusted_user_id]

    if { $sw_admin_p } {
        set admin_url "/acs-admin/"
        set devhome_url "/acs-admin/developer"
        set locale_admin_url "/acs-lang/admin"
    } else {
        set subsite_admin_p [permission::permission_p \
                                 -object_id [subsite::get_element -element object_id] \
                                 -privilege admin \
                                 -party_id $untrusted_user_id]

        if { $subsite_admin_p  } {
            set admin_url "[subsite::get_element -element url]admin/"
        }
    }
} 

if { $untrusted_user_id == 0 } {
    set login_url [ad_get_login_url -return]
}

# Context bar
if { [info exists context] } {
    set context_tmp $context
    unset context
} else {
    set context_tmp {}
}
ad_context_bar_multirow -- $context_tmp
set community_id [dotlrn_community::get_community_id]

if {[exists_and_not_null community_id]} {
    #Check if this community has a parent then the first node it is
    # the parent_community_id otherwise is the community_id
    set parent_community_id [theme_ges::get_parent_id -community_id $community_id]
    if {$parent_community_id > 0} {
	ad_context_bar_multirow -from_node [dotlrn_community::get_community_node_id $parent_community_id] -multirow multirow_aux -- $context_tmp
    } else {
	ad_context_bar_multirow -from_node [dotlrn_community::get_community_node_id $community_id] -multirow multirow_aux -- $context_tmp
    }
    multirow create context url label
    multirow append context /dotlrn [_ dotlrn.user_portal_page_home_title]
    multirow foreach multirow_aux {
	multirow append context $url $label
    }
 }


# change locale
set num_of_locales [llength [lang::system::get_locales]]
if { $num_of_locales > 1 } {
    set change_locale_url \
        "/acs-lang/?[export_vars { { package_id "[ad_conn package_id]" } }]"
}

#----------------------------------------------------------------------
# Display user messages
#----------------------------------------------------------------------

set community_id [dotlrn_community::get_community_id]

if {[empty_string_p $community_id]} {
	set send_comment 1
} else {
	set send_comment 0
}

util_get_user_messages -multirow "user_messages"

