local p = {}


-- display a gallery of cladograms using floating div elements

function p.gallery(frame)
	
	local params = mw.getCurrentFrame():getParent().args

    local outerBorders = false
    if params['caption'] or params['footer'] then outerBorders = true end
    
	-- the gallery container
	local gallery = mw.html.create('div'):addClass('clade-gallery'):css('float',params['align'])

	-- main caption for gallery if there is one
	
	if params['main-caption'] then
		gallery:tag('div'):wikitext(params['main-caption']):addClass('main-caption')
	end

	-- a gallery of floating divs with content

    local shading = ''
	if params['shading'] then shading = 'wikitable'	end -- class for shading
	
	local vertical = '' -- defaults to horizontal
	if params['vertical'] then vertical = 'vertical'	end -- class for vertical display
	
	local styleAll = params['style'] or '' -- style for all elements
	 
    local mainBorders = ''
    local elementBorders = ''
    if 	not params['main-borders'] or params['main-borders'] == "no" then mainBorders = 'no-border' end
    if 	params['borders'] == "no" then elementBorders = 'no-border' end
    	
    -- the gallery	contents
    local content = gallery:tag('div'):addClass('main-content')
                                      :addClass(mainBorders)
                                      --:css('height',params['height'])  
   	local i=0
	while i<15 do
		i=i+1 
		local caption = params['caption'..i] --or ''
		local header = params['header'..i] --or ''
		local footer = params['footer'..i] --or ''
		local style = params['style'..i] or styleAll
		local width = params['width'..i] or params['width']

		-- for each gallery element containing  a cladogram
        if params['cladogram'..i] then
		    local cladogramTable = content:tag('table')
		                  :addClass(elementBorders)
		                  :addClass(shading):addClass('gallery-element')
		                  :addClass(vertical)
		                  :css('width',width)    
		                  :css('height',params['height']) 
		                  :cssText(style)    
            if caption then 
	        	cladogramTable:tag('caption'):wikitext(caption):addClass('caption')
	        end
	        if header then 
	        	local row = cladogramTable:tag('tr'):addClass('header')
	        	row:tag('th'):wikitext(header)
	        end
	        -- if params['cladogram'..i] already checked
	        local row = cladogramTable:tag('tr')
	        row:tag('td') :wikitext("\n" .. params['cladogram'..i])
	                      :addClass('cladogram')
	                      --:css("padding", "20px")
		                  -- :css('width',params['cladogram-width'])    
		                  --:css('height',params['cladogram-height'])    
		                  :css('height',params['height']) 
	        
	        if footer then
	        	local row = cladogramTable:tag('tr')
	        	row:tag('td'):wikitext(footer):addClass('footer'):addClass(shading)
	        end
		    
		    --[[DIV VERSION - different browsers behave differently
		    -- the div containing each element in the gallery
		    local div = content:tag('div'):addClass('gallery-element'):addClass(direction)
		                  :css('width',params['width'])    
		                  :css('height',params['height'])    
		    
		    -- the caption, header, content and footer for each element
		    if caption then div:tag('div'):wikitext(caption):addClass('caption'):addClass(shading) end
		    
		    if header then div:tag('div'):wikitext(header):addClass('header'):addClass(shading) end
		    
		    div:tag('div'):wikitext(params['cladogram'..i]):addClass('cladogram'):addClass(shading)
		                  :css('width',params['cladogram-width'])    
		                  :css('height',params['cladogram-height'])    
		    
		    if footer then div:tag('div'):wikitext(footer):addClass('footer'):addClass(shading) end
		    END DIV VERSION--]] 
	    else
	       --break	-- either break or comment out to allow gaps in sequence
	    end
	end
	
	-- main footer
	if params['main-footer'] then
		gallery:tag('div'):wikitext(params['main-footer']):addClass('main-footer'):addClass(noborder)
	end
	-- TemplateStyles file
	local src = "Template:Clade gallery/styles.css"
    	
	return p.templateStyle( frame, src ) .. tostring(gallery)
end
function p.templateStyle( frame, src )
   return frame:extensionTag( 'templatestyles', '', { src = src } );
end

-- this must be at end
return p